mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-04 09:49:50 +00:00
Let the initializer admission plugin set the metadata.intializers to nil
if an update makes the pendings and the result both nil
This commit is contained in:
parent
f2993b810c
commit
fcd646d80e
@ -208,6 +208,9 @@ func (i *initializer) Admit(a admission.Attributes) (err error) {
|
|||||||
|
|
||||||
glog.V(5).Infof("Modifying uninitialized resource %s", a.GetResource())
|
glog.V(5).Infof("Modifying uninitialized resource %s", a.GetResource())
|
||||||
|
|
||||||
|
if updated != nil && len(updated.Pending) == 0 && updated.Result == nil {
|
||||||
|
accessor.SetInitializers(nil)
|
||||||
|
}
|
||||||
// because we are called before validation, we need to ensure the update transition is valid.
|
// because we are called before validation, we need to ensure the update transition is valid.
|
||||||
if errs := validation.ValidateInitializersUpdate(updated, existing, initializerFieldPath); len(errs) > 0 {
|
if errs := validation.ValidateInitializersUpdate(updated, existing, initializerFieldPath); len(errs) > 0 {
|
||||||
return errors.NewInvalid(a.GetKind().GroupKind(), a.GetName(), errs)
|
return errors.NewInvalid(a.GetKind().GroupKind(), a.GetName(), errs)
|
||||||
|
@ -200,9 +200,6 @@ func ValidateInitializers(initializers *metav1.Initializers, fldPath *field.Path
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
allErrs = append(allErrs, validateInitializersResult(initializers.Result, fldPath.Child("result"))...)
|
allErrs = append(allErrs, validateInitializersResult(initializers.Result, fldPath.Child("result"))...)
|
||||||
if len(initializers.Pending) == 0 && initializers.Result == nil {
|
|
||||||
allErrs = append(allErrs, field.Invalid(fldPath.Child("pending"), nil, "must be non-empty when result is not set"))
|
|
||||||
}
|
|
||||||
return allErrs
|
return allErrs
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -262,6 +262,29 @@ var _ = SIGDescribe("Initializers", func() {
|
|||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
Expect(len(pods.Items)).Should(Equal(1))
|
Expect(len(pods.Items)).Should(Equal(1))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
It("will be set to nil if a patch removes the last pending initializer", func() {
|
||||||
|
ns := f.Namespace.Name
|
||||||
|
c := f.ClientSet
|
||||||
|
|
||||||
|
podName := "to-be-patch-initialized-pod"
|
||||||
|
framework.Logf("Creating pod %s", podName)
|
||||||
|
|
||||||
|
// TODO: lower the timeout so that the server responds faster.
|
||||||
|
_, err := c.CoreV1().Pods(ns).Create(newUninitializedPod(podName))
|
||||||
|
if err != nil && !errors.IsTimeout(err) {
|
||||||
|
framework.Failf("expect err to be timeout error, got %v", err)
|
||||||
|
}
|
||||||
|
uninitializedPod, err := c.CoreV1().Pods(ns).Get(podName, metav1.GetOptions{})
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
Expect(uninitializedPod.Initializers).NotTo(BeNil())
|
||||||
|
Expect(len(uninitializedPod.Initializers.Pending)).Should(Equal(1))
|
||||||
|
|
||||||
|
patch := fmt.Sprintf(`{"metadata":{"initializers":{"pending":[{"$patch":"delete","name":"%s"}]}}}`, uninitializedPod.Initializers.Pending[0].Name)
|
||||||
|
patchedPod, err := c.CoreV1().Pods(ns).Patch(uninitializedPod.Name, types.StrategicMergePatchType, []byte(patch))
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
Expect(patchedPod.Initializers).To(BeNil())
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
func newUninitializedPod(podName string) *v1.Pod {
|
func newUninitializedPod(podName string) *v1.Pod {
|
||||||
|
Loading…
Reference in New Issue
Block a user