Skip to content

Commit

Permalink
add karmada.io/system label to created clusterrole+bindings
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Grosser <[email protected]>
  • Loading branch information
grosser committed Jul 31, 2024
1 parent 145a67e commit 4fd67ed
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
15 changes: 9 additions & 6 deletions operator/pkg/karmadaresource/rbac/rbac.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
clientsetscheme "k8s.io/client-go/kubernetes/scheme"

"github.com/karmada-io/karmada/operator/pkg/util/apiclient"
"github.com/karmada-io/karmada/pkg/util"
)

// EnsureKarmadaRBAC create karmada resource view and edit clusterrole
Expand All @@ -36,17 +37,19 @@ func EnsureKarmadaRBAC(client clientset.Interface) error {
}

func grantKarmadaResourceViewClusterrole(client clientset.Interface) error {
viewClusterrole := &rbacv1.ClusterRole{}
if err := kuberuntime.DecodeInto(clientsetscheme.Codecs.UniversalDecoder(), []byte(KarmadaResourceViewClusterRole), viewClusterrole); err != nil {
role := &rbacv1.ClusterRole{}
if err := kuberuntime.DecodeInto(clientsetscheme.Codecs.UniversalDecoder(), []byte(KarmadaResourceViewClusterRole), role); err != nil {
return fmt.Errorf("err when decoding Karmada view Clusterrole: %w", err)
}
return apiclient.CreateOrUpdateClusterRole(client, viewClusterrole)
util.MergeLabel(role, util.KarmadaSystemLabel, util.KarmadaSystemLabelValue)
return apiclient.CreateOrUpdateClusterRole(client, role)
}

func grantKarmadaResourceEditClusterrole(client clientset.Interface) error {
editClusterrole := &rbacv1.ClusterRole{}
if err := kuberuntime.DecodeInto(clientsetscheme.Codecs.UniversalDecoder(), []byte(KarmadaResourceEditClusterRole), editClusterrole); err != nil {
role := &rbacv1.ClusterRole{}
if err := kuberuntime.DecodeInto(clientsetscheme.Codecs.UniversalDecoder(), []byte(KarmadaResourceEditClusterRole), role); err != nil {
return fmt.Errorf("err when decoding Karmada edit Clusterrole: %w", err)
}
return apiclient.CreateOrUpdateClusterRole(client, editClusterrole)
util.MergeLabel(role, util.KarmadaSystemLabel, util.KarmadaSystemLabelValue)
return apiclient.CreateOrUpdateClusterRole(client, role)
}
6 changes: 6 additions & 0 deletions pkg/controllers/unifiedauth/unified_auth_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ func (c *Controller) buildImpersonationClusterRole(cluster *clusterv1alpha1.Clus
},
ObjectMeta: metav1.ObjectMeta{
Name: karmadaImpersonatorName,
Labels: map[string]string{
util.KarmadaSystemLabel: util.KarmadaSystemLabelValue,
},
},
Rules: rules,
}
Expand All @@ -197,6 +200,9 @@ func (c *Controller) buildImpersonationClusterRoleBinding(cluster *clusterv1alph
},
ObjectMeta: metav1.ObjectMeta{
Name: karmadaImpersonatorName,
Labels: map[string]string{
util.KarmadaSystemLabel: util.KarmadaSystemLabelValue,
},
},
Subjects: []rbacv1.Subject{
{
Expand Down
10 changes: 8 additions & 2 deletions pkg/karmadactl/cmdinit/utils/rbac.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ package utils
import (
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/karmada-io/karmada/pkg/util"
)

// ClusterRoleFromRules ClusterRole Rules
func ClusterRoleFromRules(name string, rules []rbacv1.PolicyRule, annotations map[string]string, labels map[string]string) *rbacv1.ClusterRole {
return &rbacv1.ClusterRole{
cr := &rbacv1.ClusterRole{
TypeMeta: metav1.TypeMeta{
APIVersion: "rbac.authorization.k8s.io/v1",
Kind: "ClusterRole",
Expand All @@ -35,11 +37,13 @@ func ClusterRoleFromRules(name string, rules []rbacv1.PolicyRule, annotations ma
},
Rules: rules,
}
util.MergeLabel(cr, util.KarmadaSystemLabel, util.KarmadaSystemLabelValue)
return cr
}

// ClusterRoleBindingFromSubjects ClusterRoleBinding Subjects
func ClusterRoleBindingFromSubjects(clusterRoleBindingName, clusterRoleName string, sub []rbacv1.Subject, labels map[string]string) *rbacv1.ClusterRoleBinding {
return &rbacv1.ClusterRoleBinding{
crb := &rbacv1.ClusterRoleBinding{
TypeMeta: metav1.TypeMeta{
APIVersion: "rbac.authorization.k8s.io/v1",
Kind: "ClusterRoleBinding",
Expand All @@ -55,4 +59,6 @@ func ClusterRoleBindingFromSubjects(clusterRoleBindingName, clusterRoleName stri
},
Subjects: sub,
}
util.MergeLabel(crb, util.KarmadaSystemLabel, util.KarmadaSystemLabelValue)
return crb
}

0 comments on commit 4fd67ed

Please sign in to comment.