-1

how to get the list of the empty namespaces in kubernetes through go?

I want to write a code in golng by which I can find empty namespaces in kubernetes

1 Answers1

0

You can use a Kubernetes golang client to connect to your namespace and get all resources you want e.g. Pods and Deployments. So you can check if there is any resources or not. For example, this code checks if there is a Deployment or not:

package main

import (
    "context"
    "fmt"

    metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    "k8s.io/client-go/kubernetes"
    ctrl "sigs.k8s.io/controller-runtime"
)

func main() {
    ctx := context.Background()
    config := ctrl.GetConfigOrDie()
    clientset := kubernetes.NewForConfigOrDie(config)

    namespace := "namespace"
    list, err := clientset.AppsV1().Deployments(namespace).List(ctx, metav1.ListOptions{})
    if err != nil {
        return
    }
    fmt.Printf("Namespace has %d Deployments", len(list.Items))
}

You can check the count of other resources as well.

Amin Rashidbeigi
  • 670
  • 6
  • 11