generated from replicatedhq/krew-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.go
32 lines (25 loc) · 780 Bytes
/
plugin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package plugin
import (
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/kubernetes"
)
func RunPlugin(configFlags *genericclioptions.ConfigFlags, outputCh chan string) error {
config, err := configFlags.ToRESTConfig()
if err != nil {
return fmt.Errorf("failed to read kubeconfig: %w", err)
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return fmt.Errorf("failed to create clientset: %w", err)
}
namespaces, err := clientset.CoreV1().Namespaces().List(metav1.ListOptions{})
if err != nil {
return fmt.Errorf("failed to list namespaces: %w", err)
}
for _, namespace := range namespaces.Items {
outputCh <- fmt.Sprintf("Namespace %s", namespace.Name)
}
return nil
}