Home > Software design >  List all namespaces in k8s in Go
List all namespaces in k8s in Go

Time:05-28

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.

CodePudding user response:

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.

CodePudding user response:

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)
    }
}
  • Related