Merge pull request #3247 from RolandMa1986/group-parent

Cascading deletion of children groups
This commit is contained in:
KubeSphere CI Bot 2021-01-06 09:50:33 +08:00 committed by GitHub
commit ee373b8e55
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 55 additions and 3 deletions

View File

@ -43,6 +43,7 @@ import (
fedv1beta1lister "kubesphere.io/kubesphere/pkg/client/listers/types/v1beta1"
"kubesphere.io/kubesphere/pkg/constants"
"kubesphere.io/kubesphere/pkg/controller/utils/controller"
"kubesphere.io/kubesphere/pkg/utils/k8sutil"
"kubesphere.io/kubesphere/pkg/utils/sliceutil"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
@ -143,6 +144,30 @@ func (c *Controller) reconcile(key string) error {
}
}
if group.Labels != nil {
// Set OwnerReferences when the group has a parent.
if parent, ok := group.Labels[iam1alpha2.GroupParent]; ok && !k8sutil.IsControlledBy(group.OwnerReferences, "Group", parent) {
if g == nil {
g = group.DeepCopy()
}
groupParent, err := c.groupLister.Get(parent)
if err != nil {
if errors.IsNotFound(err) {
utilruntime.HandleError(fmt.Errorf("Parent group '%s' no longer exists", key))
delete(group.Labels, iam1alpha2.GroupParent)
} else {
klog.Error(err)
return err
}
} else {
if err := controllerutil.SetControllerReference(groupParent, g, scheme.Scheme); err != nil {
klog.Error(err)
return err
}
}
}
}
if g != nil {
if _, err = c.ksClient.IamV1alpha2().Groups().Update(context.Background(), g, metav1.UpdateOptions{}); err != nil {
return err

View File

@ -288,6 +288,20 @@ func (f *fixture) expectUpdateGroupsFinalizerAction(group *v1alpha2.Group) {
f.actions = append(f.actions, action)
}
func (f *fixture) expectUpdateParentsRefAction(parent, child *v1alpha2.Group) {
expect := child.DeepCopy()
if expect.Labels == nil {
expect.Labels = make(map[string]string, 0)
}
controllerutil.SetControllerReference(parent, expect, scheme.Scheme)
expect.Finalizers = []string{"finalizers.kubesphere.io/groups"}
expect.Labels[constants.KubefedManagedLabel] = "false"
action := core.NewUpdateAction(schema.GroupVersionResource{Resource: "groups"}, "", expect)
f.actions = append(f.actions, action)
}
func (f *fixture) expectCreateFederatedGroupsAction(group *v1alpha2.Group) {
federatedGroup := newFederatedGroup(group)
@ -357,6 +371,19 @@ func TestDoNothing(t *testing.T) {
f.run(getKey(group, t))
}
func TestGroupCreateWithParent(t *testing.T) {
f := newFixture(t)
parent := newGroup("parent")
child := newGroup("child")
child.Labels = map[string]string{v1alpha2.GroupParent: "parent"}
f.groupLister = append(f.groupLister, parent, child)
f.objects = append(f.objects, parent, child)
f.expectUpdateParentsRefAction(parent, child)
f.run(getKey(child, t))
}
func TestFederetedGroupCreate(t *testing.T) {
f := newFixture(t)

View File

@ -576,7 +576,7 @@ func AddToContainer(container *restful.Container, im im.IdentityManagementInterf
Returns(http.StatusOK, api.StatusOK, []v1.RoleBinding{}).
Metadata(restfulspec.KeyOpenAPITags, []string{constants.NamespaceRoleTag}))
ws.Route(ws.DELETE("/namespace/{namespace}/rolebindings/{rolebinding}").
ws.Route(ws.DELETE("/namespaces/{namespace}/rolebindings/{rolebinding}").
To(handler.DeleteRoleBinding).
Param(ws.PathParameter("workspace", "workspace name")).
Param(ws.PathParameter("namespace", "groupbinding name")).

View File

@ -1051,9 +1051,9 @@ func (am *amOperator) CreateWorkspaceRoleBinding(workspace string, roleBinding *
}
if roleBinding.Subjects[0].Kind == rbacv1.GroupKind {
roleBinding.Labels[iamv1alpha2.GroupReferenceLabel] = roleBinding.RoleRef.Name
roleBinding.Labels[iamv1alpha2.GroupReferenceLabel] = roleBinding.Subjects[0].Name
} else if roleBinding.Subjects[0].Kind == rbacv1.UserKind {
roleBinding.Labels[iamv1alpha2.UserReferenceLabel] = roleBinding.RoleRef.Name
roleBinding.Labels[iamv1alpha2.UserReferenceLabel] = roleBinding.Subjects[0].Name
}
roleBinding.Labels[tenantv1alpha1.WorkspaceLabel] = workspace