Skip to content

Commit

Permalink
auto authorize system:admin to proxy member clusters in operation ins…
Browse files Browse the repository at this point in the history
…tall method

Signed-off-by: chaosi-zju <[email protected]>
  • Loading branch information
chaosi-zju committed Sep 20, 2024
1 parent b8edec1 commit 364800b
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
27 changes: 27 additions & 0 deletions operator/pkg/karmadaresource/rbac/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,5 +166,32 @@ rules:
- deletecollection
- patch
- update
`
// ClusterProxyAdminClusterRole authorize system:admin to proxy member clusters
ClusterProxyAdminClusterRole = `
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: cluster-proxy-admin
rules:
- apiGroups:
- 'cluster.karmada.io'
resources:
- clusters/proxy
verbs:
- '*'
`
ClusterProxyAdminClusterRoleBinding = `

Check failure on line 184 in operator/pkg/karmadaresource/rbac/manifest.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported const ClusterProxyAdminClusterRoleBinding should have comment (or a comment on this block) or be unexported (revive)
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: cluster-proxy-admin
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-proxy-admin
subjects:
- kind: User
name: "system:admin"
`
)
21 changes: 21 additions & 0 deletions operator/pkg/karmadaresource/rbac/rbac.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,33 @@ import (

// EnsureKarmadaRBAC create karmada resource view and edit clusterrole
func EnsureKarmadaRBAC(client clientset.Interface) error {
if err := grantClusterProxyAdminRBAC(client); err != nil {
return err
}
if err := grantKarmadaResourceViewClusterrole(client); err != nil {
return err
}
return grantKarmadaResourceEditClusterrole(client)
}

func grantClusterProxyAdminRBAC(client clientset.Interface) error {
role := &rbacv1.ClusterRole{}
if err := kuberuntime.DecodeInto(clientsetscheme.Codecs.UniversalDecoder(), []byte(ClusterProxyAdminClusterRole), role); err != nil {
return fmt.Errorf("err when decoding ClusterProxyAdmin Clusterrole: %w", err)
}
util.MergeLabel(role, util.KarmadaSystemLabel, util.KarmadaSystemLabelValue)
if err := apiclient.CreateOrUpdateClusterRole(client, role); err != nil {
return fmt.Errorf("failed to create or update clusterrole: %w", err)
}

roleBinding := &rbacv1.ClusterRoleBinding{}
if err := kuberuntime.DecodeInto(clientsetscheme.Codecs.UniversalDecoder(), []byte(ClusterProxyAdminClusterRoleBinding), roleBinding); err != nil {
return fmt.Errorf("err when decoding ClusterProxyAdmin Clusterrolebinding: %w", err)
}
util.MergeLabel(role, util.KarmadaSystemLabel, util.KarmadaSystemLabelValue)
return apiclient.CreateOrUpdateClusterRoleBinding(client, roleBinding)
}

func grantKarmadaResourceViewClusterrole(client clientset.Interface) error {
role := &rbacv1.ClusterRole{}
if err := kuberuntime.DecodeInto(clientsetscheme.Codecs.UniversalDecoder(), []byte(KarmadaResourceViewClusterRole), role); err != nil {
Expand Down
25 changes: 25 additions & 0 deletions operator/pkg/util/apiclient/idempotency.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,31 @@ func CreateOrUpdateClusterRole(client clientset.Interface, clusterrole *rbacv1.C
return nil
}

// CreateOrUpdateClusterRoleBinding creates a Clusterrolebinding if the target resource doesn't exist. If the resource exists already, this function will update the resource instead.
func CreateOrUpdateClusterRoleBinding(client clientset.Interface, clusterrolebinding *rbacv1.ClusterRoleBinding) error {
_, err := client.RbacV1().ClusterRoleBindings().Create(context.TODO(), clusterrolebinding, metav1.CreateOptions{})

if err != nil {
if !apierrors.IsAlreadyExists(err) {
return err
}

older, err := client.RbacV1().ClusterRoleBindings().Get(context.TODO(), clusterrolebinding.GetName(), metav1.GetOptions{})
if err != nil {
return err
}

clusterrolebinding.ResourceVersion = older.ResourceVersion
_, err = client.RbacV1().ClusterRoleBindings().Update(context.TODO(), clusterrolebinding, metav1.UpdateOptions{})
if err != nil {
return err
}
}

klog.V(4).InfoS("Successfully created or updated clusterrolebinding", "clusterrolebinding", clusterrolebinding.GetName())
return nil
}

// DeleteDeploymentIfHasLabels deletes a Deployment that exists the given labels.
func DeleteDeploymentIfHasLabels(client clientset.Interface, name, namespace string, ls labels.Set) error {
deployment, err := client.AppsV1().Deployments(namespace).Get(context.TODO(), name, metav1.GetOptions{})
Expand Down

0 comments on commit 364800b

Please sign in to comment.