Skip to content

Commit

Permalink
feat: Adding support for all namespaces flags
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <[email protected]>
  • Loading branch information
ViBiOh committed Aug 11, 2022
1 parent 2db2493 commit fe72adf
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ chmod +x "/usr/local/bin/kmux"
go install "github.com/ViBiOh/kmux@latest"
```

### {ba,z}sh-completion
### Shell completions

Shell completions are available by running the following command (example is for `bash`, but it's available for `zsh`, `fish` and `powershell`).

Expand All @@ -54,6 +54,7 @@ kmux --context central1,europe1,asia1 image

```
Global Flags:
-A, --all-namespaces Find resources in all namespaces
--context string Kubernetes context, comma separated for mutiplexing commands
-h, --help help for kmux
--kubeconfig string Kubernetes configuration file (default "${HOME}/.kube/config")
Expand Down
2 changes: 1 addition & 1 deletion cmd/choose.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func getCommonObjects(namespace string, lister resource.Lister) []string {
defer close(successChan)

clients.Execute(func(kube client.Kube) error {
if len(namespace) == 0 {
if len(namespace) == 0 && !allNamespace {
namespace = kube.Namespace
}

Expand Down
11 changes: 10 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ import (
"k8s.io/client-go/util/homedir"
)

var clients client.Array
var (
clients client.Array
allNamespace bool
)

var rootCmd = &cobra.Command{
Use: "kmux",
Expand Down Expand Up @@ -70,6 +73,10 @@ func getKubernetesClient(contexts []string) (client.Array, error) {
return nil, fmt.Errorf("read configured namespace: %w", err)
}

if allNamespace {
namespace = ""
}

clientset, err := kubernetes.NewForConfig(k8sConfig)
if err != nil {
return nil, fmt.Errorf("create kubernetes client: %w", err)
Expand Down Expand Up @@ -104,6 +111,8 @@ func init() {
output.Fatal("bind env `KUBECONTEXT`: %s", err)
}

flags.BoolVarP(&allNamespace, "all-namespaces", "A", false, "Find resources in all namespaces")

flags.StringP("namespace", "n", "", "Override kubernetes namespace in context")
if err := viper.BindPFlag("namespace", flags.Lookup("namespace")); err != nil {
output.Fatal("bind `namespace` flag: %s", err)
Expand Down
28 changes: 28 additions & 0 deletions pkg/resource/lister.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,34 @@ func ListerFor(resourceType string) (Lister, error) {
output[i] = item.ObjectMeta
}

return output, nil
}, nil
case "no", "node", "nodes":
return func(ctx context.Context, kube client.Kube, _ string, options metav1.ListOptions) ([]metav1.ObjectMeta, error) {
items, err := kube.CoreV1().Nodes().List(ctx, options)
if err != nil {
return nil, err
}

output := make([]metav1.ObjectMeta, len(items.Items))
for i, item := range items.Items {
output[i] = item.ObjectMeta
}

return output, nil
}, nil
case "svc", "service", "services":
return func(ctx context.Context, kube client.Kube, namespace string, options metav1.ListOptions) ([]metav1.ObjectMeta, error) {
items, err := kube.CoreV1().Services(namespace).List(ctx, options)
if err != nil {
return nil, err
}

output := make([]metav1.ObjectMeta, len(items.Items))
for i, item := range items.Items {
output[i] = item.ObjectMeta
}

return output, nil
}, nil
case "sts", "statefulset", "statefulsets":
Expand Down

0 comments on commit fe72adf

Please sign in to comment.