From fffbe26fcfcee9f10991381f9940b324e9a29716 Mon Sep 17 00:00:00 2001 From: petar-cvit Date: Thu, 25 Jul 2024 10:18:29 +0200 Subject: [PATCH 1/2] show missing resources --- .../internal/cluster/k8sclient/modules.go | 62 +++++++++++++------ cyclops-ctrl/internal/models/dto/k8s.go | 11 ++++ cyclops-ctrl/internal/models/dto/modules.go | 6 ++ .../src/components/pages/ModuleDetails.tsx | 41 ++++++++++++ 4 files changed, 101 insertions(+), 19 deletions(-) diff --git a/cyclops-ctrl/internal/cluster/k8sclient/modules.go b/cyclops-ctrl/internal/cluster/k8sclient/modules.go index 8e83f5d4..6db3e9da 100644 --- a/cyclops-ctrl/internal/cluster/k8sclient/modules.go +++ b/cyclops-ctrl/internal/cluster/k8sclient/modules.go @@ -152,7 +152,15 @@ func (k *KubernetesClient) GetDeletedResources( resources []dto.Resource, manifest string, ) ([]dto.Resource, error) { - resourcesFromTemplate := make(map[string][]dto.Resource, 0) + type resKey struct { + Group string + Version string + Kind string + Name string + Namespace string + } + + resourcesFromTemplate := make(map[resKey]struct{}, 0) for _, s := range strings.Split(manifest, "\n---\n") { s := strings.TrimSpace(s) @@ -167,38 +175,54 @@ func (k *KubernetesClient) GetDeletedResources( panic(err) } - objGVK := obj.GetObjectKind().GroupVersionKind().String() - resourcesFromTemplate[objGVK] = append(resourcesFromTemplate[objGVK], &dto.Service{ + namespace := obj.GetNamespace() + if len(namespace) == 0 { + namespace = "default" + } + + key := resKey{ + Group: obj.GroupVersionKind().Group, + Version: obj.GroupVersionKind().Version, + Kind: obj.GroupVersionKind().Kind, Name: obj.GetName(), - Namespace: obj.GetNamespace(), - }) + Namespace: namespace, + } + + resourcesFromTemplate[key] = struct{}{} } out := make([]dto.Resource, 0, len(resources)) for _, resource := range resources { - gvk := resource.GetGroupVersionKind() - - if _, ok := resourcesFromTemplate[gvk]; !ok { - resource.SetDeleted(true) - out = append(out, resource) - continue - } - - found := false - for _, rs := range resourcesFromTemplate[gvk] { - if resource.GetName() == rs.GetName() && (resource.GetNamespace() == rs.GetNamespace() || rs.GetNamespace() == "") { - found = true - break - } + key := resKey{ + Group: resource.GetGroup(), + Version: resource.GetVersion(), + Kind: resource.GetKind(), + Name: resource.GetName(), + Namespace: resource.GetNamespace(), } + _, found := resourcesFromTemplate[key] if found == false { resource.SetDeleted(true) } + delete(resourcesFromTemplate, key) + out = append(out, resource) } + // add missing resources + for missingResource, _ := range resourcesFromTemplate { + out = append([]dto.Resource{&dto.Other{ + Group: missingResource.Group, + Version: missingResource.Version, + Kind: missingResource.Kind, + Name: missingResource.Name, + Namespace: missingResource.Namespace, + Missing: true, + }}, out...) + } + return out, nil } diff --git a/cyclops-ctrl/internal/models/dto/k8s.go b/cyclops-ctrl/internal/models/dto/k8s.go index 04850cb4..68272d40 100644 --- a/cyclops-ctrl/internal/models/dto/k8s.go +++ b/cyclops-ctrl/internal/models/dto/k8s.go @@ -14,6 +14,8 @@ type Resource interface { GetNamespace() string GetDeleted() bool SetDeleted(bool) + IsMissing() bool + SetMissing(bool) } type Deployment struct { @@ -500,6 +502,7 @@ type Other struct { Namespace string `json:"namespace"` Status string `json:"status"` Deleted bool `json:"deleted"` + Missing bool `json:"missing"` } func (s *Other) GetGroupVersionKind() string { @@ -533,3 +536,11 @@ func (s *Other) GetDeleted() bool { func (s *Other) SetDeleted(deleted bool) { s.Deleted = deleted } + +func (s *Other) IsMissing() bool { + return s.Missing +} + +func (s *Other) SetMissing(missing bool) { + s.Missing = missing +} diff --git a/cyclops-ctrl/internal/models/dto/modules.go b/cyclops-ctrl/internal/models/dto/modules.go index 992f5bc9..5d5de74c 100644 --- a/cyclops-ctrl/internal/models/dto/modules.go +++ b/cyclops-ctrl/internal/models/dto/modules.go @@ -60,6 +60,12 @@ func (d *DeleteResource) GetDeleted() bool { func (d *DeleteResource) SetDeleted(_ bool) {} +func (d *DeleteResource) IsMissing() bool { + return false +} + +func (d *DeleteResource) SetMissing(_ bool) {} + type KeyValue struct { Key string `json:"key"` Value string `json:"value"` diff --git a/cyclops-ui/src/components/pages/ModuleDetails.tsx b/cyclops-ui/src/components/pages/ModuleDetails.tsx index 7e1ad079..c14d716c 100644 --- a/cyclops-ui/src/components/pages/ModuleDetails.tsx +++ b/cyclops-ui/src/components/pages/ModuleDetails.tsx @@ -524,6 +524,47 @@ const ModuleDetails = () => { ); } + if (resource.missing) { + resourceCollapses.push( + + {genExtra(resource, resource.status)} + + } + key={collapseKey} + style={{ + display: getResourceDisplay( + resource.group, + resource.version, + resource.kind, + ), + width: getCollapseWidth(collapseKey), + backgroundColor: "#fff", + marginBottom: "12px", + borderRadius: "10px", + border: "1px dashed #777", + }} + >, + ); + return; + } + resourceCollapses.push( Date: Fri, 16 Aug 2024 14:34:11 +0200 Subject: [PATCH 2/2] default key namespace --- cyclops-ctrl/internal/cluster/k8sclient/modules.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/cyclops-ctrl/internal/cluster/k8sclient/modules.go b/cyclops-ctrl/internal/cluster/k8sclient/modules.go index 27bd59ca..09ef7002 100644 --- a/cyclops-ctrl/internal/cluster/k8sclient/modules.go +++ b/cyclops-ctrl/internal/cluster/k8sclient/modules.go @@ -194,12 +194,17 @@ func (k *KubernetesClient) GetDeletedResources( out := make([]dto.Resource, 0, len(resources)) for _, resource := range resources { + namespace := resource.GetNamespace() + if len(namespace) == 0 { + namespace = "default" + } + key := resKey{ Group: resource.GetGroup(), Version: resource.GetVersion(), Kind: resource.GetKind(), Name: resource.GetName(), - Namespace: resource.GetNamespace(), + Namespace: namespace, } _, found := resourcesFromTemplate[key] @@ -213,7 +218,7 @@ func (k *KubernetesClient) GetDeletedResources( } // add missing resources - for missingResource, _ := range resourcesFromTemplate { + for missingResource := range resourcesFromTemplate { out = append([]dto.Resource{&dto.Other{ Group: missingResource.Group, Version: missingResource.Version,