From 5acbcf6419d7088dad6128d039c7acc56dc6296d Mon Sep 17 00:00:00 2001 From: Windfarer Date: Mon, 19 Dec 2022 17:43:25 +0800 Subject: [PATCH] fix: kubernetes watch and list in all namespaces (#112) * watch and list in all namespaces * add flag * update readme * cleaning * fmt Co-authored-by: Eric Yang --- plugin/kubernetes/README.md | 1 + plugin/kubernetes/kubernetes.go | 5 ++++- plugin/kubernetes/main.go | 12 +++++++++--- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/plugin/kubernetes/README.md b/plugin/kubernetes/README.md index 4a8bfd90..cf10605a 100644 --- a/plugin/kubernetes/README.md +++ b/plugin/kubernetes/README.md @@ -6,6 +6,7 @@ this plugin is inpsired by the [first version kubernetes plugin](https://github. ## Usage +Start plugin with flag `--all-namespaces` or environment variable `SSHPIPERD_KUBERNETES_ALL_NAMESPACES=true` for cluster-wide usage, or it will listen to the namespace where it is in by default. ### Apply CRD definition diff --git a/plugin/kubernetes/kubernetes.go b/plugin/kubernetes/kubernetes.go index 7b66ca69..6e5ab42b 100644 --- a/plugin/kubernetes/kubernetes.go +++ b/plugin/kubernetes/kubernetes.go @@ -32,7 +32,7 @@ type plugin struct { cache *gocache.Cache } -func newKubernetesPlugin() (*plugin, error) { +func newKubernetesPlugin(allNamespaces bool) (*plugin, error) { kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig( clientcmd.NewDefaultClientConfigLoadingRules(), &clientcmd.ConfigOverrides{}, @@ -47,6 +47,9 @@ func newKubernetesPlugin() (*plugin, error) { if err != nil { return nil, err } + if allNamespaces { + ns = metav1.NamespaceAll + } k8sclient, err := kubernetes.NewForConfig(config) if err != nil { diff --git a/plugin/kubernetes/main.go b/plugin/kubernetes/main.go index 1c79db56..63ee44db 100644 --- a/plugin/kubernetes/main.go +++ b/plugin/kubernetes/main.go @@ -9,14 +9,20 @@ func main() { libplugin.CreateAndRunPluginTemplate(&libplugin.PluginTemplate{ Name: "kubernetes", Usage: "sshpiperd kubernetes plugin", + Flags: []cli.Flag{ + &cli.BoolFlag{ + Name: "all-namespaces", + Usage: "To watch all namespaces in the cluster", + EnvVars: []string{"SSHPIPERD_KUBERNETES_ALL_NAMESPACES"}, + Required: false, + }, + }, CreateConfig: func(c *cli.Context) (*libplugin.SshPiperPluginConfig, error) { - plugin, err := newKubernetesPlugin() + plugin, err := newKubernetesPlugin(c.Bool("all-namespaces")) if err != nil { return nil, err } - return &libplugin.SshPiperPluginConfig{ - NextAuthMethodsCallback: func(_ libplugin.ConnMetadata) ([]string, error) { return plugin.supportedMethods() },