From 948e6debee02d11f1f6124d40819f7e1c157f402 Mon Sep 17 00:00:00 2001 From: petar-cvit Date: Mon, 4 Nov 2024 17:15:18 +0100 Subject: [PATCH 1/6] limit target namespaces --- cyclops-ctrl/.env | 1 + cyclops-ctrl/cmd/main/main.go | 18 +++++++++- cyclops-ctrl/pkg/cluster/k8sclient/client.go | 36 +++++++++++-------- .../pkg/cluster/k8sclient/resources.go | 4 +++ .../components/pages/NewModule/NewModule.tsx | 3 -- 5 files changed, 44 insertions(+), 18 deletions(-) diff --git a/cyclops-ctrl/.env b/cyclops-ctrl/.env index 011a7a1d..4291c074 100644 --- a/cyclops-ctrl/.env +++ b/cyclops-ctrl/.env @@ -3,3 +3,4 @@ PORT=8888 WATCH_NAMESPACE=cyclops WATCH_NAMESPACE_HELM= CYCLOPS_VERSION=v0.0.0 +MODULE_TARGET_NAMESPACES= diff --git a/cyclops-ctrl/cmd/main/main.go b/cyclops-ctrl/cmd/main/main.go index 1f905812..e6dae479 100644 --- a/cyclops-ctrl/cmd/main/main.go +++ b/cyclops-ctrl/cmd/main/main.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "strconv" + "strings" "time" _ "github.com/joho/godotenv/autoload" @@ -74,7 +75,12 @@ func main() { watchNamespace := getWatchNamespace() helmWatchNamespace := getHelmWatchNamespace() - k8sClient, err := k8sclient.New(watchNamespace, helmWatchNamespace, zap.New(zap.UseFlagOptions(&opts))) + k8sClient, err := k8sclient.New( + watchNamespace, + helmWatchNamespace, + getModuleTargetNamespaces(), + zap.New(zap.UseFlagOptions(&opts)), + ) if err != nil { fmt.Println("error bootstrapping Kubernetes client", err) panic(err) @@ -182,3 +188,13 @@ func getHelmWatchNamespace() string { } return value } + +func getModuleTargetNamespaces() []string { + envVar := os.Getenv("MODULE_TARGET_NAMESPACES") + + if envVar == "" { + return []string{} + } + + return strings.Split(envVar, ",") +} diff --git a/cyclops-ctrl/pkg/cluster/k8sclient/client.go b/cyclops-ctrl/pkg/cluster/k8sclient/client.go index 8d39751e..7f5f9f18 100644 --- a/cyclops-ctrl/pkg/cluster/k8sclient/client.go +++ b/cyclops-ctrl/pkg/cluster/k8sclient/client.go @@ -20,17 +20,24 @@ import ( ) type KubernetesClient struct { - Dynamic dynamic.Interface - clientset *kubernetes.Clientset - discovery *discovery.DiscoveryClient - moduleset *client.CyclopsV1Alpha1Client - moduleNamespace string - helmReleaseNamespace string + Dynamic dynamic.Interface + clientset *kubernetes.Clientset + discovery *discovery.DiscoveryClient + moduleset *client.CyclopsV1Alpha1Client + + moduleNamespace string + helmReleaseNamespace string + moduleTargetNamespaces []string logger logr.Logger } -func New(moduleNamespace, helmReleaseNamespace string, logger logr.Logger) (*KubernetesClient, error) { +func New( + moduleNamespace string, + helmReleaseNamespace string, + moduleTargetNamespaces []string, + logger logr.Logger, +) (*KubernetesClient, error) { config := ctrl.GetConfigOrDie() clientset, err := kubernetes.NewForConfig(config) if err != nil { @@ -50,13 +57,14 @@ func New(moduleNamespace, helmReleaseNamespace string, logger logr.Logger) (*Kub } return &KubernetesClient{ - Dynamic: dynamic, - discovery: discovery, - clientset: clientset, - moduleset: moduleSet, - moduleNamespace: moduleNamespace, - helmReleaseNamespace: helmReleaseNamespace, - logger: logger, + Dynamic: dynamic, + discovery: discovery, + clientset: clientset, + moduleset: moduleSet, + moduleNamespace: moduleNamespace, + helmReleaseNamespace: helmReleaseNamespace, + moduleTargetNamespaces: moduleTargetNamespaces, + logger: logger, }, nil } diff --git a/cyclops-ctrl/pkg/cluster/k8sclient/resources.go b/cyclops-ctrl/pkg/cluster/k8sclient/resources.go index 78f21895..11427cf8 100644 --- a/cyclops-ctrl/pkg/cluster/k8sclient/resources.go +++ b/cyclops-ctrl/pkg/cluster/k8sclient/resources.go @@ -370,6 +370,10 @@ func (k *KubernetesClient) GetPodsForNode(nodeName string) ([]apiv1.Pod, error) } func (k *KubernetesClient) ListNamespaces() ([]string, error) { + if len(k.moduleTargetNamespaces) > 0 { + return k.moduleTargetNamespaces, nil + } + namespaceList, err := k.clientset.CoreV1().Namespaces().List(context.TODO(), metav1.ListOptions{}) if err != nil { return nil, err diff --git a/cyclops-ui/src/components/pages/NewModule/NewModule.tsx b/cyclops-ui/src/components/pages/NewModule/NewModule.tsx index cd65a9f5..ae34a628 100644 --- a/cyclops-ui/src/components/pages/NewModule/NewModule.tsx +++ b/cyclops-ui/src/components/pages/NewModule/NewModule.tsx @@ -565,9 +565,6 @@ const NewModule = () => { showSearch={true} onChange={onTemplateStoreSelected} style={{ width: "100%" }} - placeholder="default" - value="default" - defaultValue="default" > {namespaces.map((namespace: string) => (