Skip to content

Commit

Permalink
limit target namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
petar-cvit committed Nov 4, 2024
1 parent 33827f4 commit 90b69e9
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
11 changes: 4 additions & 7 deletions cyclops-ctrl/internal/controller/cluster.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package controller

import (
"fmt"
"net/http"

"github.com/gin-gonic/gin"
Expand All @@ -27,7 +26,7 @@ func (c *Cluster) ListNodes(ctx *gin.Context) {

nodes, err := c.kubernetesClient.ListNodes()
if err != nil {
ctx.Status(http.StatusInternalServerError)
ctx.JSON(http.StatusInternalServerError, dto.NewError("Error fetching nodes", err.Error()))
return
}

Expand All @@ -48,15 +47,13 @@ func (c *Cluster) GetNode(ctx *gin.Context) {
return
}
if err != nil {
ctx.Status(http.StatusInternalServerError)
ctx.JSON(http.StatusInternalServerError, dto.NewError("Error fetching node", err.Error()))
return
}

pods, err := c.kubernetesClient.GetPodsForNode(nodeName)
if err != nil {
ctx.JSON(http.StatusInternalServerError, dto.Error{
Message: fmt.Sprintf("Error listing pods for node: %v", nodeName),
})
ctx.JSON(http.StatusInternalServerError, dto.NewError("Error fetching pod nodes", err.Error()))
return
}

Expand All @@ -70,7 +67,7 @@ func (c *Cluster) ListNamespaces(ctx *gin.Context) {

namespaces, err := c.kubernetesClient.ListNamespaces()
if err != nil {
ctx.Status(http.StatusInternalServerError)
ctx.JSON(http.StatusInternalServerError, dto.NewError("Error fetching namespaces", err.Error()))
return
}

Expand Down
2 changes: 1 addition & 1 deletion cyclops-ctrl/internal/controller/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (m *Modules) GetModule(ctx *gin.Context) {
module, err := m.kubernetesClient.GetModule(ctx.Param("name"))
if err != nil {
fmt.Println(err)
ctx.Status(http.StatusInternalServerError)
ctx.JSON(http.StatusInternalServerError, dto.NewError("Error fetching module", err.Error()))
return
}

Expand Down
23 changes: 16 additions & 7 deletions cyclops-ctrl/pkg/cluster/k8sclient/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,28 @@ func (k *KubernetesClient) GetResourcesForRelease(releaseName string) ([]dto.Res
}

other := make([]unstructured.Unstructured, 0)
listOptions := metav1.ListOptions{
LabelSelector: labels.Set(map[string]string{
"app.kubernetes.io/instance": releaseName,
"app.kubernetes.io/managed-by": "Helm",
}).String(),
}

for _, gvr := range managedGVRs {
rs, err := k.Dynamic.Resource(gvr).Namespace(k.helmReleaseNamespace).List(context.Background(), metav1.ListOptions{
LabelSelector: labels.Set(map[string]string{
"app.kubernetes.io/instance": releaseName,
"app.kubernetes.io/managed-by": "Helm",
}).String(),
})
var rs *unstructured.UnstructuredList
var err error
if len(k.helmReleaseNamespace) > 0 {
rs, err = k.Dynamic.Resource(gvr).Namespace(k.helmReleaseNamespace).List(context.Background(), listOptions)
} else {
rs, err = k.Dynamic.Resource(gvr).List(context.Background(), listOptions)
}

if err != nil {
if apierrors.IsNotFound(err) {
continue
}

k.logger.Error(err, "failed to list resources", "gvr", gvr, "namespace", k.helmReleaseNamespace)
k.logger.Info("failed to list resources", "gvr", gvr, "namespace", k.helmReleaseNamespace)
continue
}

Expand Down
7 changes: 6 additions & 1 deletion cyclops-ctrl/pkg/cluster/k8sclient/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
batchv1 "k8s.io/api/batch/v1"
apiv1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/labels"
Expand Down Expand Up @@ -79,7 +80,11 @@ func (k *KubernetesClient) GetResourcesForModule(name string) ([]dto.Resource, e
}

if err != nil {
k.logger.Error(err, "failed to list resources", "gvr", gvr, "namespace", k.helmReleaseNamespace)
if apierrors.IsNotFound(err) {
continue
}

k.logger.Info("error fetching resource", "gvr", gvr, "namespace", k.helmReleaseNamespace)
continue
}

Expand Down

0 comments on commit 90b69e9

Please sign in to comment.