mirror of
				https://github.com/optim-enterprises-bv/kubernetes.git
				synced 2025-11-04 04:08:16 +00:00 
			
		
		
		
	Introduce validatePullPolicyWithDefault to validation.
This commit is contained in:
		@@ -266,6 +266,29 @@ func validateLifecycle(lifecycle *api.Lifecycle) errs.ValidationErrorList {
 | 
				
			|||||||
	return allErrs
 | 
						return allErrs
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// TODO(dchen1107): Move this along with other defaulting values
 | 
				
			||||||
 | 
					func validatePullPolicyWithDefault(ctr *api.Container) errs.ValidationErrorList {
 | 
				
			||||||
 | 
						allErrors := errs.ValidationErrorList{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// TODO(dchen1107): Move ParseImageName code to pkg/util
 | 
				
			||||||
 | 
						if len(ctr.ImagePullPolicy) == 0 {
 | 
				
			||||||
 | 
							parts := strings.Split(ctr.Image, ":")
 | 
				
			||||||
 | 
							// Check image tag
 | 
				
			||||||
 | 
							if parts[len(parts)-1] == "latest" {
 | 
				
			||||||
 | 
								ctr.ImagePullPolicy = api.PullAlways
 | 
				
			||||||
 | 
							} else {
 | 
				
			||||||
 | 
								ctr.ImagePullPolicy = api.PullIfNotPresent
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if ctr.ImagePullPolicy != api.PullAlways &&
 | 
				
			||||||
 | 
							ctr.ImagePullPolicy != api.PullIfNotPresent &&
 | 
				
			||||||
 | 
							ctr.ImagePullPolicy != api.PullNever {
 | 
				
			||||||
 | 
							allErrors = append(allErrors, errs.NewFieldNotSupported("", ctr.ImagePullPolicy))
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return allErrors
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func validateContainers(containers []api.Container, volumes util.StringSet) errs.ValidationErrorList {
 | 
					func validateContainers(containers []api.Container, volumes util.StringSet) errs.ValidationErrorList {
 | 
				
			||||||
	allErrs := errs.ValidationErrorList{}
 | 
						allErrs := errs.ValidationErrorList{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -294,6 +317,7 @@ func validateContainers(containers []api.Container, volumes util.StringSet) errs
 | 
				
			|||||||
		cErrs = append(cErrs, validatePorts(ctr.Ports).Prefix("ports")...)
 | 
							cErrs = append(cErrs, validatePorts(ctr.Ports).Prefix("ports")...)
 | 
				
			||||||
		cErrs = append(cErrs, validateEnv(ctr.Env).Prefix("env")...)
 | 
							cErrs = append(cErrs, validateEnv(ctr.Env).Prefix("env")...)
 | 
				
			||||||
		cErrs = append(cErrs, validateVolumeMounts(ctr.VolumeMounts, volumes).Prefix("volumeMounts")...)
 | 
							cErrs = append(cErrs, validateVolumeMounts(ctr.VolumeMounts, volumes).Prefix("volumeMounts")...)
 | 
				
			||||||
 | 
							cErrs = append(cErrs, validatePullPolicyWithDefault(ctr).Prefix("pullPolicy")...)
 | 
				
			||||||
		allErrs = append(allErrs, cErrs.PrefixIndex(i)...)
 | 
							allErrs = append(allErrs, cErrs.PrefixIndex(i)...)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	// Check for colliding ports across all containers.
 | 
						// Check for colliding ports across all containers.
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -220,6 +220,54 @@ func TestValidateVolumeMounts(t *testing.T) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestValidatePullPolicy(t *testing.T) {
 | 
				
			||||||
 | 
						type T struct {
 | 
				
			||||||
 | 
							Container      api.Container
 | 
				
			||||||
 | 
							ExpectedPolicy api.PullPolicy
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						testCases := map[string]T{
 | 
				
			||||||
 | 
							"NotPresent1": {
 | 
				
			||||||
 | 
								api.Container{Name: "abc", Image: "image:latest", ImagePullPolicy: "PullIfNotPresent"},
 | 
				
			||||||
 | 
								api.PullIfNotPresent,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							"NotPresent2": {
 | 
				
			||||||
 | 
								api.Container{Name: "abc1", Image: "image", ImagePullPolicy: "PullIfNotPresent"},
 | 
				
			||||||
 | 
								api.PullIfNotPresent,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							"Always1": {
 | 
				
			||||||
 | 
								api.Container{Name: "123", Image: "image:latest", ImagePullPolicy: "PullAlways"},
 | 
				
			||||||
 | 
								api.PullAlways,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							"Always2": {
 | 
				
			||||||
 | 
								api.Container{Name: "1234", Image: "image", ImagePullPolicy: "PullAlways"},
 | 
				
			||||||
 | 
								api.PullAlways,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							"Never1": {
 | 
				
			||||||
 | 
								api.Container{Name: "abc-123", Image: "image:latest", ImagePullPolicy: "PullNever"},
 | 
				
			||||||
 | 
								api.PullNever,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							"Never2": {
 | 
				
			||||||
 | 
								api.Container{Name: "abc-1234", Image: "image", ImagePullPolicy: "PullNever"},
 | 
				
			||||||
 | 
								api.PullNever,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							"DefaultToNotPresent":  {api.Container{Name: "notPresent", Image: "image"}, api.PullIfNotPresent},
 | 
				
			||||||
 | 
							"DefaultToNotPresent2": {api.Container{Name: "notPresent1", Image: "image:sometag"}, api.PullIfNotPresent},
 | 
				
			||||||
 | 
							"DefaultToAlways1":     {api.Container{Name: "always", Image: "image:latest"}, api.PullAlways},
 | 
				
			||||||
 | 
							"DefaultToAlways2":     {api.Container{Name: "always", Image: "foo.bar.com:5000/my/image:latest"}, api.PullAlways},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						for k, v := range testCases {
 | 
				
			||||||
 | 
							ctr := &v.Container
 | 
				
			||||||
 | 
							errs := validatePullPolicyWithDefault(ctr)
 | 
				
			||||||
 | 
							if len(errs) != 0 {
 | 
				
			||||||
 | 
								t.Errorf("case[%s] expected success, got %#v", k, errs)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							if ctr.ImagePullPolicy != v.ExpectedPolicy {
 | 
				
			||||||
 | 
								t.Errorf("case[%s] expected policy %v, got %v", k, v.ExpectedPolicy, ctr.ImagePullPolicy)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestValidateContainers(t *testing.T) {
 | 
					func TestValidateContainers(t *testing.T) {
 | 
				
			||||||
	volumes := util.StringSet{}
 | 
						volumes := util.StringSet{}
 | 
				
			||||||
	capabilities.SetForTests(capabilities.Capabilities{
 | 
						capabilities.SetForTests(capabilities.Capabilities{
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -156,7 +156,8 @@ func TestExtractFromHTTP(t *testing.T) {
 | 
				
			|||||||
						Containers: []api.Container{{
 | 
											Containers: []api.Container{{
 | 
				
			||||||
							Name:  "1",
 | 
												Name:  "1",
 | 
				
			||||||
							Image: "foo",
 | 
												Image: "foo",
 | 
				
			||||||
							TerminationMessagePath: "/dev/termination-log"}},
 | 
												TerminationMessagePath: "/dev/termination-log",
 | 
				
			||||||
 | 
												ImagePullPolicy:        "PullIfNotPresent"}},
 | 
				
			||||||
					},
 | 
										},
 | 
				
			||||||
				},
 | 
									},
 | 
				
			||||||
				api.BoundPod{
 | 
									api.BoundPod{
 | 
				
			||||||
@@ -169,7 +170,8 @@ func TestExtractFromHTTP(t *testing.T) {
 | 
				
			|||||||
						Containers: []api.Container{{
 | 
											Containers: []api.Container{{
 | 
				
			||||||
							Name:  "1",
 | 
												Name:  "1",
 | 
				
			||||||
							Image: "foo",
 | 
												Image: "foo",
 | 
				
			||||||
							TerminationMessagePath: "/dev/termination-log"}},
 | 
												TerminationMessagePath: "/dev/termination-log",
 | 
				
			||||||
 | 
												ImagePullPolicy:        "PullIfNotPresent"}},
 | 
				
			||||||
					},
 | 
										},
 | 
				
			||||||
				}),
 | 
									}),
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user