Skip to content

Commit

Permalink
🎭 add ClusterRole resource (cyclops-ui#665)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikonhub authored Oct 31, 2024
1 parent 842b901 commit 4f42543
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
42 changes: 42 additions & 0 deletions cyclops-ctrl/internal/models/dto/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -716,3 +716,45 @@ func (s *Role) GetDeleted() bool {
func (s *Role) SetDeleted(deleted bool) {
s.Deleted = deleted
}

type ClusterRole struct {
Group string `json:"group"`
Version string `json:"version"`
Kind string `json:"kind"`
Name string `json:"name"`
Namespace string `json:"namespace"`
Deleted bool `json:"deleted"`
Rules []rbacv1.PolicyRule `json:"rules"`
}

func (s *ClusterRole) GetGroupVersionKind() string {
return s.Group + "/" + s.Version + ", Kind=" + s.Kind
}

func (s *ClusterRole) GetGroup() string {
return s.Group
}

func (s *ClusterRole) GetVersion() string {
return s.Version
}

func (s *ClusterRole) GetKind() string {
return s.Kind
}

func (s *ClusterRole) GetName() string {
return s.Name
}

func (s *ClusterRole) GetNamespace() string {
return s.Namespace
}

func (s *ClusterRole) GetDeleted() bool {
return s.Deleted
}

func (s *ClusterRole) SetDeleted(deleted bool) {
s.Deleted = deleted
}
15 changes: 15 additions & 0 deletions cyclops-ctrl/pkg/cluster/k8sclient/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,21 @@ func (k *KubernetesClient) mapRole(group, version, kind, name, namespace string)
}, nil
}

func (k *KubernetesClient) mapClusterRole(group, version, kind, name string) (*dto.ClusterRole, error) {
clusterRole, err := k.clientset.RbacV1().ClusterRoles().Get(context.Background(), name, metav1.GetOptions{})
if err != nil {
return nil, err
}

return &dto.ClusterRole{
Group: group,
Version: version,
Kind: kind,
Name: clusterRole.Name,
Rules: clusterRole.Rules,
}, nil
}

func mapNetworkPolicyIngressRules(rules []networkingv1.NetworkPolicyIngressRule) []dto.NetworkPolicyIngressRule {
mapped := make([]dto.NetworkPolicyIngressRule, len(rules))
for i, rule := range rules {
Expand Down
6 changes: 6 additions & 0 deletions cyclops-ctrl/pkg/cluster/k8sclient/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ func (k *KubernetesClient) GetResource(group, version, kind, name, namespace str
return k.mapRole(group, version, kind, name, namespace)
case isNetworkPolicy(group, version, kind):
return k.mapNetworkPolicy(group, version, kind, name, namespace)
case isClusterRole(group, version, kind):
return k.mapClusterRole(group, version, kind, name)
}

return nil, nil
Expand Down Expand Up @@ -466,6 +468,10 @@ func isNetworkPolicy(group, version, kind string) bool {
return group == "networking.k8s.io" && version == "v1" && kind == "NetworkPolicy"
}

func isClusterRole(group, version, kind string) bool {
return group == "rbac.authorization.k8s.io" && version == "v1" && kind == "ClusterRole"
}

func IsWorkload(group, version, kind string) bool {
return isDeployment(group, version, kind) ||
isStatefulSet(group, version, kind) ||
Expand Down

0 comments on commit 4f42543

Please sign in to comment.