import ( "fmt" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" ) func main() { // create a Kubernetes client using the clientset interface config, _ := rest.InClusterConfig() clientset, _ := kubernetes.NewForConfig(config) // list all the namespaces in the cluster namespaces, _ := clientset.CoreV1().Namespaces().List(metav1.ListOptions{}) for _, namespace := range namespaces.Items { fmt.Printf("Namespace: %s\n", namespace.ObjectMeta.Name) } }
import ( "fmt" "k8s.io/api/core/v1" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" ) func main() { // create a Kubernetes client using the clientset interface config, _ := rest.InClusterConfig() clientset, _ := kubernetes.NewForConfig(config) // create a new Pod in the "default" namespace pod := &v1.Pod{ ObjectMeta: metav1.ObjectMeta{ Name: "example-pod", }, Spec: v1.PodSpec{ Containers: []v1.Container{ { Name: "example-container", Image: "nginx", ImagePullPolicy: v1.PullAlways, }, }, }, } createdPod, _ := clientset.CoreV1().Pods("default").Create(pod) fmt.Printf("Created Pod: %s\n", createdPod.ObjectMeta.Name) }In both examples, the package library being used is the Kubernetes client-go library.