简体   繁体   中英

List all namespaces in k8s in Go

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM