Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

show missing resources #470

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 46 additions & 17 deletions cyclops-ctrl/internal/cluster/k8sclient/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,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)
Expand All @@ -168,38 +176,59 @@ 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
namespace := resource.GetNamespace()
if len(namespace) == 0 {
namespace = "default"
}

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: namespace,
}

_, 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
}

Expand Down
11 changes: 11 additions & 0 deletions cyclops-ctrl/internal/models/dto/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type Resource interface {
GetNamespace() string
GetDeleted() bool
SetDeleted(bool)
IsMissing() bool
SetMissing(bool)
}

type Deployment struct {
Expand Down Expand Up @@ -633,6 +635,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 {
Expand Down Expand Up @@ -667,6 +670,14 @@ 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
}

type Role struct {
Group string `json:"group"`
Version string `json:"version"`
Expand Down
6 changes: 6 additions & 0 deletions cyclops-ctrl/internal/models/dto/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
41 changes: 41 additions & 0 deletions cyclops-ui/src/components/pages/ModuleDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,47 @@ const ModuleDetails = () => {
);
}

if (resource.missing) {
resourceCollapses.push(
<Collapse.Panel
collapsible={"disabled"}
header={
<Popover
content={
"Try reconciling the module or applying the missing resource"
}
placement="topRight"
title={
"Missing resource " +
resource.kind +
" " +
resource.namespace +
"/" +
resource.name
}
trigger="hover"
>
{genExtra(resource, resource.status)}
</Popover>
}
key={collapseKey}
style={{
display: getResourceDisplay(
resource.group,
resource.version,
resource.kind,
),
width: getCollapseWidth(collapseKey),
backgroundColor: "#fff",
marginBottom: "12px",
borderRadius: "10px",
border: "1px dashed #777",
}}
></Collapse.Panel>,
);
return;
}

resourceCollapses.push(
<Collapse.Panel
header={genExtra(resource, resource.status)}
Expand Down
Loading