From e45072b825d7f3cf55f865e2c0b0e2eb2076878b Mon Sep 17 00:00:00 2001 From: Erwin de Haan Date: Mon, 26 Jun 2023 11:34:55 +0200 Subject: [PATCH] Make sure the namespaces are correctly set so the informers are namespaced. --- .vscode/launch.json | 7 +++++-- cmd/wait.go | 24 +++++++++++++++++++++++- pkg/utils.go | 9 --------- pkg/waitables.go | 7 ++++--- utils/strings.go | 10 ++++++++++ 5 files changed, 42 insertions(+), 15 deletions(-) create mode 100644 utils/strings.go diff --git a/.vscode/launch.json b/.vscode/launch.json index 42d17bc..f016032 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -10,7 +10,10 @@ "request": "launch", "mode": "debug", "program": "./main.go", - "args": ["wait-multi", "service,model", "service,model2", "model-6fd69c9d97-hrxt5"] + "args": [ + "--kubeconfig=kubeconfig.txt", + "default,service,model", + ] } - } + ] } \ No newline at end of file diff --git a/cmd/wait.go b/cmd/wait.go index 6d0bcca..351ae14 100644 --- a/cmd/wait.go +++ b/cmd/wait.go @@ -24,6 +24,7 @@ import ( "sync" "github.com/erayan/k8s-wait-for-multi/pkg" + "github.com/erayan/k8s-wait-for-multi/utils" "github.com/spf13/cobra" "sigs.k8s.io/controller-runtime/pkg/cache" @@ -84,8 +85,29 @@ func wait(cmd *cobra.Command, args []string) error { timeoutCtx, cancelFn = context.WithTimeout(context.Background(), timeout) defer cancelFn() + namespaces := []string{} + + for _, arg := range args { + arg_items := strings.Split(arg, ",") + len := len(arg_items) + err = nil + if len == 1 || len == 2 { + if !utils.StringInSlice(*KubernetesConfigFlags.Namespace, namespaces) { + namespaces = append(namespaces, *KubernetesConfigFlags.Namespace) + } + } else if len == 3 { + if !utils.StringInSlice(arg_items[0], namespaces) { + namespaces = append(namespaces, arg_items[0]) + } + } else { + log.Printf("illegal argument '%s'", arg) + } + } + + log.Printf("Starting with namespaces: %v", namespaces) + opts := cache.Options{ - Namespaces: waits.GetAllNamespaces(), + Namespaces: namespaces, } conf, err := KubernetesConfigFlags.ToRESTConfig() diff --git a/pkg/utils.go b/pkg/utils.go index ab8a02b..c1d2704 100644 --- a/pkg/utils.go +++ b/pkg/utils.go @@ -26,15 +26,6 @@ const ( type TreeStatus string -func stringInSlice(a string, list []string) bool { - for _, b := range list { - if b == a { - return true - } - } - return false -} - func metaInSlice(a treeprint.MetaValue, list []treeprint.MetaValue) bool { for _, b := range list { if b == a { diff --git a/pkg/waitables.go b/pkg/waitables.go index 680b83f..f04bdb4 100644 --- a/pkg/waitables.go +++ b/pkg/waitables.go @@ -21,6 +21,7 @@ import ( "strings" "github.com/erayan/k8s-wait-for-multi/pkg/items" + "github.com/erayan/k8s-wait-for-multi/utils" "github.com/xlab/treeprint" batchv1 "k8s.io/api/batch/v1" @@ -261,18 +262,18 @@ func (w *Waitables) GetAllNamespaces() []string { namespaces := []string{} for ns := range w.Services { - if !stringInSlice(ns, namespaces) { + if !utils.StringInSlice(ns, namespaces) { namespaces = append(namespaces, ns) } } for ns := range w.Jobs { - if !stringInSlice(ns, namespaces) { + if !utils.StringInSlice(ns, namespaces) { namespaces = append(namespaces, ns) } } for ns := range w.Pods { - if !stringInSlice(ns, namespaces) { + if !utils.StringInSlice(ns, namespaces) { namespaces = append(namespaces, ns) } } diff --git a/utils/strings.go b/utils/strings.go new file mode 100644 index 0000000..bcde9f5 --- /dev/null +++ b/utils/strings.go @@ -0,0 +1,10 @@ +package utils + +func StringInSlice(a string, list []string) bool { + for _, b := range list { + if b == a { + return true + } + } + return false +}