diff --git a/cmd/kubeadm/app/preflight/checks.go b/cmd/kubeadm/app/preflight/checks.go index 5de0e646fa8..af3d40786bf 100644 --- a/cmd/kubeadm/app/preflight/checks.go +++ b/cmd/kubeadm/app/preflight/checks.go @@ -832,9 +832,15 @@ func (ImagePullCheck) Name() string { // Check pulls images required by kubeadm. This is a mutating check func (ipc ImagePullCheck) Check() (warnings, errorList []error) { + policy := ipc.imagePullPolicy + if len(policy) == 0 { + policy = v1.PullIfNotPresent // Default behavior if the policy is unset + } + klog.V(1).Infof("using image pull policy: %s", policy) for _, image := range ipc.imageList { - switch ipc.imagePullPolicy { + switch policy { case v1.PullNever: + klog.V(1).Infof("skipping pull of image: %s", image) continue case v1.PullIfNotPresent: ret, err := ipc.runtime.ImageExists(image) @@ -845,11 +851,16 @@ func (ipc ImagePullCheck) Check() (warnings, errorList []error) { if err != nil { errorList = append(errorList, errors.Wrapf(err, "failed to check if image %s exists", image)) } - } - - klog.V(1).Infof("pulling %s", image) - if err := ipc.runtime.PullImage(image); err != nil { - errorList = append(errorList, errors.Wrapf(err, "failed to pull image %s", image)) + fallthrough // Proceed with pulling the image if it does not exist + case v1.PullAlways: + klog.V(1).Infof("pulling: %s", image) + if err := ipc.runtime.PullImage(image); err != nil { + errorList = append(errorList, errors.Wrapf(err, "failed to pull image %s", image)) + } + default: + // If the policy is unknown return early with an error + errorList = append(errorList, errors.Errorf("unsupported pull policy %q", policy)) + return warnings, errorList } } return warnings, errorList diff --git a/cmd/kubeadm/app/preflight/checks_test.go b/cmd/kubeadm/app/preflight/checks_test.go index a7f923f6c72..e19ccd157c5 100644 --- a/cmd/kubeadm/app/preflight/checks_test.go +++ b/cmd/kubeadm/app/preflight/checks_test.go @@ -31,7 +31,6 @@ import ( "net/http" "os" - v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/util/sets" kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm" "k8s.io/kubernetes/cmd/kubeadm/app/constants" @@ -924,7 +923,7 @@ func TestImagePullCheck(t *testing.T) { check := ImagePullCheck{ runtime: containerRuntime, imageList: []string{"img1", "img2", "img3"}, - imagePullPolicy: v1.PullIfNotPresent, + imagePullPolicy: "", // should be defaulted to v1.PullIfNotPresent } warnings, errors := check.Check() if len(warnings) != 0 {