Skip to content

Commit

Permalink
Make sure the namespaces are correctly set so the informers are names…
Browse files Browse the repository at this point in the history
…paced.
  • Loading branch information
EraYaN committed Jun 26, 2023
1 parent 2bab7c3 commit e45072b
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 15 deletions.
7 changes: 5 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]
}
}
]
}
24 changes: 23 additions & 1 deletion cmd/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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()
Expand Down
9 changes: 0 additions & 9 deletions pkg/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 4 additions & 3 deletions pkg/waitables.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
}
Expand Down
10 changes: 10 additions & 0 deletions utils/strings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package utils

func StringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}

0 comments on commit e45072b

Please sign in to comment.