kubeadm: fix image pull policy integration

If the user has not specified a pull policy we must assume a default of
v1.PullIfNotPresent.

Add some extra verbose output to help users monitor what policy is
used and what images are skipped / pulled.

Use "fallthrough" and case handle "v1.PullAlways".

Update unit test.
This commit is contained in:
Lubomir I. Ivanov 2021-06-23 00:52:35 +03:00
parent 2453f07e93
commit 3b36e6bcea
2 changed files with 18 additions and 8 deletions

View File

@ -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

View File

@ -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 {