提交 2d6b7fef 编写于 作者: R Roland.Ma

cascading deletion of children groups

Signed-off-by: NRoland.Ma <rolandma@yunify.com>
上级 078dead7
...@@ -42,6 +42,7 @@ import ( ...@@ -42,6 +42,7 @@ import (
fedv1beta1lister "kubesphere.io/kubesphere/pkg/client/listers/types/v1beta1" fedv1beta1lister "kubesphere.io/kubesphere/pkg/client/listers/types/v1beta1"
"kubesphere.io/kubesphere/pkg/constants" "kubesphere.io/kubesphere/pkg/constants"
"kubesphere.io/kubesphere/pkg/controller/utils/controller" "kubesphere.io/kubesphere/pkg/controller/utils/controller"
"kubesphere.io/kubesphere/pkg/utils/k8sutil"
"kubesphere.io/kubesphere/pkg/utils/sliceutil" "kubesphere.io/kubesphere/pkg/utils/sliceutil"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
) )
...@@ -142,6 +143,30 @@ func (c *Controller) reconcile(key string) error { ...@@ -142,6 +143,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 g != nil {
if _, err = c.ksClient.IamV1alpha2().Groups().Update(g); err != nil { if _, err = c.ksClient.IamV1alpha2().Groups().Update(g); err != nil {
return err return err
......
...@@ -288,6 +288,20 @@ func (f *fixture) expectUpdateGroupsFinalizerAction(group *v1alpha2.Group) { ...@@ -288,6 +288,20 @@ func (f *fixture) expectUpdateGroupsFinalizerAction(group *v1alpha2.Group) {
f.actions = append(f.actions, action) 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) { func (f *fixture) expectCreateFederatedGroupsAction(group *v1alpha2.Group) {
federatedGroup := newFederatedGroup(group) federatedGroup := newFederatedGroup(group)
...@@ -357,6 +371,19 @@ func TestDoNothing(t *testing.T) { ...@@ -357,6 +371,19 @@ func TestDoNothing(t *testing.T) {
f.run(getKey(group, 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) { func TestFederetedGroupCreate(t *testing.T) {
f := newFixture(t) f := newFixture(t)
......
...@@ -576,7 +576,7 @@ func AddToContainer(container *restful.Container, im im.IdentityManagementInterf ...@@ -576,7 +576,7 @@ func AddToContainer(container *restful.Container, im im.IdentityManagementInterf
Returns(http.StatusOK, api.StatusOK, []v1.RoleBinding{}). Returns(http.StatusOK, api.StatusOK, []v1.RoleBinding{}).
Metadata(restfulspec.KeyOpenAPITags, []string{constants.NamespaceRoleTag})) 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). To(handler.DeleteRoleBinding).
Param(ws.PathParameter("workspace", "workspace name")). Param(ws.PathParameter("workspace", "workspace name")).
Param(ws.PathParameter("namespace", "groupbinding name")). Param(ws.PathParameter("namespace", "groupbinding name")).
......
...@@ -1050,9 +1050,9 @@ func (am *amOperator) CreateWorkspaceRoleBinding(workspace string, roleBinding * ...@@ -1050,9 +1050,9 @@ func (am *amOperator) CreateWorkspaceRoleBinding(workspace string, roleBinding *
} }
if roleBinding.Subjects[0].Kind == rbacv1.GroupKind { 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 { } 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 roleBinding.Labels[tenantv1alpha1.WorkspaceLabel] = workspace
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册