Skip to content

Commit

Permalink
🎭 role backend implementation (#467)
Browse files Browse the repository at this point in the history
* fix:(#368) Removed the third unnecessary status from the code

* The new types of the role is added here

* The handler is implemented here

* The models for Role added

* The mapRole func updated

* Added the requested changes

* Removed the GetResource handler

* Renamed the roleList variable to role

* Formated the code

* Rules added in the dto.Role{...}

* dto Rule added to the object

* formated the code

* Ran the cmd go fmt ./... from cyclops-ctrl

* Updated the group
  • Loading branch information
tanbirali authored Aug 4, 2024
1 parent 8f08952 commit 45fc534
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
23 changes: 23 additions & 0 deletions cyclops-ctrl/internal/cluster/k8sclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,8 @@ func (k *KubernetesClient) GetResource(group, version, kind, name, namespace str
return k.mapCronJob(group, version, kind, name, namespace)
case isJob(group, version, kind):
return k.mapJob(group, version, kind, name, namespace)
case isRole(group, version, kind):
return k.mapRole(group, version, kind, name, namespace)
}

return nil, nil
Expand Down Expand Up @@ -910,6 +912,23 @@ func (k *KubernetesClient) isResourceNamespaced(gvk schema.GroupVersionKind) (bo
return false, errors.New(fmt.Sprintf("group version kind not found: %v", gvk.String()))
}

func (k *KubernetesClient) mapRole(group, version, kind, name, namespace string) (*dto.Role, error) {
role, err := k.clientset.RbacV1().Roles(namespace).Get(context.Background(), name, metav1.GetOptions{})

if err != nil {
return nil, err
}

return &dto.Role{
Group: group,
Version: version,
Kind: kind,
Name: role.Name,
Namespace: namespace,
Rules: role.Rules,
}, nil
}

func isDeployment(group, version, kind string) bool {
return group == "apps" && version == "v1" && kind == "Deployment"
}
Expand Down Expand Up @@ -953,3 +972,7 @@ func isSecret(group, version, kind string) bool {
func isCronJob(group, version, kind string) bool {
return group == "batch" && version == "v1" && kind == "CronJob"
}

func isRole(group, version, kind string) bool {
return group == "rbac.authorization.k8s.io" && version == "v1" && kind == "Role"
}
44 changes: 44 additions & 0 deletions cyclops-ctrl/internal/models/dto/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dto

import (
v1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -596,3 +597,46 @@ func (s *Other) GetDeleted() bool {
func (s *Other) SetDeleted(deleted bool) {
s.Deleted = deleted
}

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

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

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

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

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

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

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

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

func (s *Role) SetDeleted(deleted bool) {
s.Deleted = deleted
}

0 comments on commit 45fc534

Please sign in to comment.