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 <ericyang@birentech.com>
This commit is contained in:
Windfarer 2022-12-19 17:43:25 +08:00 committed by GitHub
parent 740e31dffc
commit 5acbcf6419
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 4 deletions

View file

@ -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

View file

@ -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 {

View file

@ -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()
},