4

Can anyone tell me how to list all namespaces in k8s using Go? I have been referencing this link but couldn't find anything that can list all namespaces.

Link: https://pkg.go.dev/github.com/gruntwork-io/terratest/modules/k8s

I don't see any ListNamespaces functions for the k8s package in Go.

blackgreen
  • 34,072
  • 23
  • 111
  • 129
TechGirl
  • 123
  • 1
  • 7

2 Answers2

5

Try kubernetes/client-go, you can do like clientset.CoreV1().Namespaces("").List(context.TODO(), metav1.ListOptions{}). Your clientset maybe instantiate within the cluster or outside.

gohm'c
  • 13,492
  • 1
  • 9
  • 16
4

To list namespaces you can use something like this:

func ListNameSpaces(coreClient kubernetes.Interface) {

    nsList, err := coreClient.CoreV1().
        Namespaces().
        List(context.Background(), metav1.ListOptions{})
    //checkErr(err)
    fmt.Println(err)

    for _, n := range nsList.Items {
        fmt.Println(n.Name)
    }
}
Sam
  • 345
  • 3
  • 14