From f2653786d92d777e3a511243dac5cf05639feafc Mon Sep 17 00:00:00 2001 From: tanbirali Date: Wed, 3 Jul 2024 13:23:38 +0530 Subject: [PATCH 01/14] fix:(#368) Removed the third unnecessary status from the code --- cyclops-ui/src/components/pages/ModuleDetails.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/cyclops-ui/src/components/pages/ModuleDetails.tsx b/cyclops-ui/src/components/pages/ModuleDetails.tsx index f95a0b32..6afbe2b9 100644 --- a/cyclops-ui/src/components/pages/ModuleDetails.tsx +++ b/cyclops-ui/src/components/pages/ModuleDetails.tsx @@ -510,7 +510,6 @@ const ModuleDetails = () => { {resource.name} - {statusIcon} From 7a9369a3af5c88e4d2dab385f97e0400023ea68a Mon Sep 17 00:00:00 2001 From: tanbirali Date: Wed, 24 Jul 2024 18:26:54 +0530 Subject: [PATCH 02/14] The new types of the role is added here --- .../internal/cluster/k8sclient/client.go | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/cyclops-ctrl/internal/cluster/k8sclient/client.go b/cyclops-ctrl/internal/cluster/k8sclient/client.go index 1a9e1253..2e804841 100644 --- a/cyclops-ctrl/internal/cluster/k8sclient/client.go +++ b/cyclops-ctrl/internal/cluster/k8sclient/client.go @@ -355,6 +355,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 @@ -877,6 +879,29 @@ 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){ + roleList, err := k.clientset.CoreV1().Role(namespace).Get(context.Background(), name, metav1.GetOptions{}) + if err != nil { + return nil, err + } + + storage := "" + + if roleList.Spec.Resources.Requests != nil && roleList.Spec.Resources.Requests.Storage() != nil { + storage = roleList.Spec.Resources.Requests.Storage().String() + } + + return &dto.Role{ + Group: group, + Version: version, + Kind: kind, + Name: name, + Namespace: namespace, + AccessModes: roleList.Spec.AccessModes, + Size: storage, + }, nil +} + func isDeployment(group, version, kind string) bool { return group == "apps" && version == "v1" && kind == "Deployment" } @@ -916,3 +941,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 == "" && version == "v1" && kind == "Role" +} From d345e1407f1918537bfe9ebc29a342da8658f103 Mon Sep 17 00:00:00 2001 From: tanbirali Date: Wed, 24 Jul 2024 18:28:02 +0530 Subject: [PATCH 03/14] The handler is implemented here --- cyclops-ctrl/internal/controller/modules.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/cyclops-ctrl/internal/controller/modules.go b/cyclops-ctrl/internal/controller/modules.go index 762c1818..ede7fcec 100644 --- a/cyclops-ctrl/internal/controller/modules.go +++ b/cyclops-ctrl/internal/controller/modules.go @@ -653,3 +653,24 @@ func getTargetGeneration(generation string, module *v1alpha1.Module) (*v1alpha1. Status: module.Status, }, true } + +func (m *Modules) GetRole(ctx *gin.Context) { + ctx.Header("Access-Control-Allow-Origin", "*") + + group := ctx.Query("group") + version := ctx.Query("version") + kind := ctx.Query("kind") + name := ctx.Query("name") + namespace := ctx.Query("namespace") + + resource, err := m.kubernetesClient.GetResource(group, version, kind, name, namespace) + if err != nil { + ctx.JSON(http.StatusInternalServerError, gin.H{ + "error": "Failed to fetch resource", + "reason": err.Error(), + }) + return + } + + ctx.JSON(http.StatusOK, resource) +} \ No newline at end of file From 752bc227b7ea5e1ef4ed1de2bb48f2f6bc81a734 Mon Sep 17 00:00:00 2001 From: tanbirali Date: Wed, 24 Jul 2024 20:39:52 +0530 Subject: [PATCH 04/14] The models for Role added --- cyclops-ctrl/internal/models/dto/k8s.go | 42 +++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/cyclops-ctrl/internal/models/dto/k8s.go b/cyclops-ctrl/internal/models/dto/k8s.go index 04850cb4..b9c9ac2a 100644 --- a/cyclops-ctrl/internal/models/dto/k8s.go +++ b/cyclops-ctrl/internal/models/dto/k8s.go @@ -533,3 +533,45 @@ 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"` +} + +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 +} From 6be19ff444c7d33cb44fef4f52dda0c40ba6a60c Mon Sep 17 00:00:00 2001 From: tanbirali Date: Wed, 24 Jul 2024 20:42:11 +0530 Subject: [PATCH 05/14] The mapRole func updated --- cyclops-ctrl/internal/cluster/k8sclient/client.go | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/cyclops-ctrl/internal/cluster/k8sclient/client.go b/cyclops-ctrl/internal/cluster/k8sclient/client.go index 2e804841..98048b6b 100644 --- a/cyclops-ctrl/internal/cluster/k8sclient/client.go +++ b/cyclops-ctrl/internal/cluster/k8sclient/client.go @@ -880,25 +880,19 @@ func (k *KubernetesClient) isResourceNamespaced(gvk schema.GroupVersionKind) (bo } func (k *KubernetesClient) mapRole(group, version, kind, name, namespace string ) (*dto.Role, error){ - roleList, err := k.clientset.CoreV1().Role(namespace).Get(context.Background(), name, metav1.GetOptions{}) + roleList, err := k.clientset.RbacV1().Roles(namespace).Get(context.Background(), name, metav1.GetOptions{}) if err != nil { return nil, err } - storage := "" - - if roleList.Spec.Resources.Requests != nil && roleList.Spec.Resources.Requests.Storage() != nil { - storage = roleList.Spec.Resources.Requests.Storage().String() - } - + return &dto.Role{ Group: group, Version: version, Kind: kind, - Name: name, + Name: roleList.Name, Namespace: namespace, - AccessModes: roleList.Spec.AccessModes, - Size: storage, + }, nil } From 25d1b5dbb0431744b52161559a32c3a35208d3d6 Mon Sep 17 00:00:00 2001 From: tanbirali Date: Thu, 1 Aug 2024 17:58:04 +0530 Subject: [PATCH 06/14] Added the requested changes --- cyclops-ctrl/internal/cluster/k8sclient/client.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cyclops-ctrl/internal/cluster/k8sclient/client.go b/cyclops-ctrl/internal/cluster/k8sclient/client.go index 98048b6b..040c9722 100644 --- a/cyclops-ctrl/internal/cluster/k8sclient/client.go +++ b/cyclops-ctrl/internal/cluster/k8sclient/client.go @@ -356,7 +356,7 @@ func (k *KubernetesClient) GetResource(group, version, kind, name, namespace str 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 k.role(group, version, kind, name, namespace) } return nil, nil @@ -879,8 +879,9 @@ 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){ +func (k *KubernetesClient) role(group, version, kind, name, namespace string ) (*dto.Role, error){ roleList, err := k.clientset.RbacV1().Roles(namespace).Get(context.Background(), name, metav1.GetOptions{}) + if err != nil { return nil, err } From d6c36fa6d96b88c0fd2eaf76c63c702d9f4cf5a5 Mon Sep 17 00:00:00 2001 From: tanbirali Date: Thu, 1 Aug 2024 17:59:16 +0530 Subject: [PATCH 07/14] Removed the GetResource handler --- cyclops-ctrl/internal/controller/modules.go | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/cyclops-ctrl/internal/controller/modules.go b/cyclops-ctrl/internal/controller/modules.go index ede7fcec..1c063def 100644 --- a/cyclops-ctrl/internal/controller/modules.go +++ b/cyclops-ctrl/internal/controller/modules.go @@ -654,23 +654,3 @@ func getTargetGeneration(generation string, module *v1alpha1.Module) (*v1alpha1. }, true } -func (m *Modules) GetRole(ctx *gin.Context) { - ctx.Header("Access-Control-Allow-Origin", "*") - - group := ctx.Query("group") - version := ctx.Query("version") - kind := ctx.Query("kind") - name := ctx.Query("name") - namespace := ctx.Query("namespace") - - resource, err := m.kubernetesClient.GetResource(group, version, kind, name, namespace) - if err != nil { - ctx.JSON(http.StatusInternalServerError, gin.H{ - "error": "Failed to fetch resource", - "reason": err.Error(), - }) - return - } - - ctx.JSON(http.StatusOK, resource) -} \ No newline at end of file From 254bdb96594f3daba329ec631e24705dde7b100b Mon Sep 17 00:00:00 2001 From: tanbirali Date: Thu, 1 Aug 2024 19:19:36 +0530 Subject: [PATCH 08/14] Renamed the roleList variable to role --- cyclops-ctrl/internal/cluster/k8sclient/client.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cyclops-ctrl/internal/cluster/k8sclient/client.go b/cyclops-ctrl/internal/cluster/k8sclient/client.go index 040c9722..d79463a4 100644 --- a/cyclops-ctrl/internal/cluster/k8sclient/client.go +++ b/cyclops-ctrl/internal/cluster/k8sclient/client.go @@ -356,7 +356,7 @@ func (k *KubernetesClient) GetResource(group, version, kind, name, namespace str case isJob(group, version, kind): return k.mapJob(group, version, kind, name, namespace) case isRole(group, version, kind): - return k.role(group, version, kind, name, namespace) + return k.mapRole(group, version, kind, name, namespace) } return nil, nil @@ -879,8 +879,8 @@ 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) role(group, version, kind, name, namespace string ) (*dto.Role, error){ - roleList, err := k.clientset.RbacV1().Roles(namespace).Get(context.Background(), name, metav1.GetOptions{}) +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 @@ -891,7 +891,7 @@ func (k *KubernetesClient) role(group, version, kind, name, namespace string ) ( Group: group, Version: version, Kind: kind, - Name: roleList.Name, + Name: role.Name, Namespace: namespace, }, nil From 4c5d1ae234130d2835c25772a05ee0ac5e292acd Mon Sep 17 00:00:00 2001 From: tanbirali Date: Thu, 1 Aug 2024 22:24:39 +0530 Subject: [PATCH 09/14] Formated the code --- .../internal/cluster/k8sclient/client.go | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/cyclops-ctrl/internal/cluster/k8sclient/client.go b/cyclops-ctrl/internal/cluster/k8sclient/client.go index d79463a4..2b5f1af8 100644 --- a/cyclops-ctrl/internal/cluster/k8sclient/client.go +++ b/cyclops-ctrl/internal/cluster/k8sclient/client.go @@ -879,21 +879,19 @@ 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 { +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, - + Group: group, + Version: version, + Kind: kind, + Name: role.Name, + Namespace: namespace, }, nil } From 80ad3a1492d121f04e81c4e77cb7e15a32f507bd Mon Sep 17 00:00:00 2001 From: tanbirali Date: Sat, 3 Aug 2024 20:21:14 +0530 Subject: [PATCH 10/14] Rules added in the dto.Role{...} --- cyclops-ctrl/internal/cluster/k8sclient/client.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cyclops-ctrl/internal/cluster/k8sclient/client.go b/cyclops-ctrl/internal/cluster/k8sclient/client.go index 2b5f1af8..c5dfafcf 100644 --- a/cyclops-ctrl/internal/cluster/k8sclient/client.go +++ b/cyclops-ctrl/internal/cluster/k8sclient/client.go @@ -892,6 +892,7 @@ func (k *KubernetesClient) mapRole(group, version, kind, name, namespace string) Kind: kind, Name: role.Name, Namespace: namespace, + Rules: role.Rules, }, nil } From 9d8b6b843c3648ebe0f0ee07d19611326ff4015b Mon Sep 17 00:00:00 2001 From: tanbirali Date: Sat, 3 Aug 2024 20:22:20 +0530 Subject: [PATCH 11/14] dto Rule added to the object --- cyclops-ctrl/internal/models/dto/k8s.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cyclops-ctrl/internal/models/dto/k8s.go b/cyclops-ctrl/internal/models/dto/k8s.go index b9c9ac2a..9c168b1b 100644 --- a/cyclops-ctrl/internal/models/dto/k8s.go +++ b/cyclops-ctrl/internal/models/dto/k8s.go @@ -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" ) @@ -542,6 +543,7 @@ type Role struct { Namespace string `json:"namespace"` Status string `json:"status"` Deleted bool `json:"deleted"` + Rules []rbacv1.PolicyRule `json:"rules"` } func (s *Role) GetGroupVersionKind() string { From 43202d1acb14b6793b752c1f5d885890c4d5087e Mon Sep 17 00:00:00 2001 From: tanbirali Date: Sat, 3 Aug 2024 20:23:59 +0530 Subject: [PATCH 12/14] formated the code --- cyclops-ctrl/internal/cluster/k8sclient/client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cyclops-ctrl/internal/cluster/k8sclient/client.go b/cyclops-ctrl/internal/cluster/k8sclient/client.go index c5dfafcf..7503f654 100644 --- a/cyclops-ctrl/internal/cluster/k8sclient/client.go +++ b/cyclops-ctrl/internal/cluster/k8sclient/client.go @@ -892,7 +892,7 @@ func (k *KubernetesClient) mapRole(group, version, kind, name, namespace string) Kind: kind, Name: role.Name, Namespace: namespace, - Rules: role.Rules, + Rules: role.Rules, }, nil } From e00db3ab01e72ae0356e3704adc09e24b1eb272d Mon Sep 17 00:00:00 2001 From: tanbirali Date: Sun, 4 Aug 2024 15:33:07 +0530 Subject: [PATCH 13/14] Ran the cmd go fmt ./... from cyclops-ctrl --- cyclops-ctrl/internal/controller/modules.go | 2 -- cyclops-ctrl/internal/models/dto/k8s.go | 16 ++++++++-------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/cyclops-ctrl/internal/controller/modules.go b/cyclops-ctrl/internal/controller/modules.go index 1c063def..30506ed6 100644 --- a/cyclops-ctrl/internal/controller/modules.go +++ b/cyclops-ctrl/internal/controller/modules.go @@ -137,7 +137,6 @@ func (m *Modules) Manifest(ctx *gin.Context) { return } - manifest, err := m.renderer.HelmTemplate(v1alpha1.Module{ ObjectMeta: metav1.ObjectMeta{ Name: ctx.Param("name"), @@ -653,4 +652,3 @@ func getTargetGeneration(generation string, module *v1alpha1.Module) (*v1alpha1. Status: module.Status, }, true } - diff --git a/cyclops-ctrl/internal/models/dto/k8s.go b/cyclops-ctrl/internal/models/dto/k8s.go index 9c168b1b..3364c96c 100644 --- a/cyclops-ctrl/internal/models/dto/k8s.go +++ b/cyclops-ctrl/internal/models/dto/k8s.go @@ -536,14 +536,14 @@ func (s *Other) SetDeleted(deleted bool) { } 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"` + 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 { From 1b4ff80623255413da579e50e63644d22def31de Mon Sep 17 00:00:00 2001 From: tanbirali Date: Sun, 4 Aug 2024 16:17:13 +0530 Subject: [PATCH 14/14] Updated the group --- cyclops-ctrl/internal/cluster/k8sclient/client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cyclops-ctrl/internal/cluster/k8sclient/client.go b/cyclops-ctrl/internal/cluster/k8sclient/client.go index 7503f654..82be69a2 100644 --- a/cyclops-ctrl/internal/cluster/k8sclient/client.go +++ b/cyclops-ctrl/internal/cluster/k8sclient/client.go @@ -937,5 +937,5 @@ func isCronJob(group, version, kind string) bool { } func isRole(group, version, kind string) bool { - return group == "" && version == "v1" && kind == "Role" + return group == "rbac.authorization.k8s.io" && version == "v1" && kind == "Role" }