diff --git a/plugin/pkg/admission/gc/gc_admission.go b/plugin/pkg/admission/gc/gc_admission.go index 93834cb040a..5c4287925b6 100644 --- a/plugin/pkg/admission/gc/gc_admission.go +++ b/plugin/pkg/admission/gc/gc_admission.go @@ -122,7 +122,7 @@ func (a *gcPermissionsEnforcement) Admit(attributes admission.Attributes) (err e for _, record := range records { allowed, reason, err := a.authorizer.Authorize(record) if !allowed { - return admission.NewForbidden(attributes, fmt.Errorf("cannot set blockOwnerDeletion if an ownerReference refers to a resource you can't delete: %v, %v", reason, err)) + return admission.NewForbidden(attributes, fmt.Errorf("cannot set blockOwnerDeletion if an ownerReference refers to a resource you can't set finalizers on: %v, %v", reason, err)) } } } @@ -178,12 +178,13 @@ func (a *gcPermissionsEnforcement) ownerRefToDeleteAttributeRecords(ref metav1.O for _, mapping := range mappings { ret = append(ret, authorizer.AttributesRecord{ User: attributes.GetUserInfo(), - Verb: "delete", + Verb: "update", // ownerReference can only refer to an object in the same namespace, so attributes.GetNamespace() equals to the owner's namespace Namespace: attributes.GetNamespace(), APIGroup: groupVersion.Group, APIVersion: groupVersion.Version, Resource: mapping.Resource, + Subresource: "finalizers", Name: ref.Name, ResourceRequest: true, Path: "", diff --git a/plugin/pkg/admission/gc/gc_admission_test.go b/plugin/pkg/admission/gc/gc_admission_test.go index 4d7a6aac6a4..ac750c29f36 100644 --- a/plugin/pkg/admission/gc/gc_admission_test.go +++ b/plugin/pkg/admission/gc/gc_admission_test.go @@ -39,6 +39,9 @@ func (fakeAuthorizer) Authorize(a authorizer.Attributes) (bool, string, error) { if a.GetVerb() == "delete" { return false, "", nil } + if a.GetVerb() == "update" && a.GetSubresource() == "/finalizers" { + return false, "", nil + } return true, "", nil } @@ -46,6 +49,9 @@ func (fakeAuthorizer) Authorize(a authorizer.Attributes) (bool, string, error) { if a.GetVerb() == "delete" && a.GetResource() == "pods" { return false, "", nil } + if a.GetVerb() == "update" && a.GetResource() == "pods" && a.GetSubresource() == "finalizers" { + return false, "", nil + } return true, "", nil } @@ -53,6 +59,9 @@ func (fakeAuthorizer) Authorize(a authorizer.Attributes) (bool, string, error) { if a.GetVerb() == "delete" && a.GetResource() == "replicationcontrollers" { return false, "", nil } + if a.GetVerb() == "update" && a.GetResource() == "replicationcontrollers" && a.GetSubresource() == "finalizers" { + return false, "", nil + } return true, "", nil } @@ -326,7 +335,10 @@ func TestBlockOwnerDeletionAdmission(t *testing.T) { return err == nil } expectCantSetBlockOwnerDeletionError := func(err error) bool { - return strings.Contains(err.Error(), "cannot set blockOwnerDeletion if an ownerReference refers to a resource you can't delete") + if err == nil { + return false + } + return strings.Contains(err.Error(), "cannot set blockOwnerDeletion if an ownerReference refers to a resource you can't set finalizers on") } tests := []struct { name string