import ( "k8s.io/kubernetes/pkg/client" "k8s.io/client-go/rest" ) func main() { config, err := rest.InClusterConfig() if err != nil { panic(err.Error()) } kubernetesClient, err := client.New(config) if err != nil { panic(err.Error()) } }
import ( "k8s.io/api/core/v1" ) func createNamespace(client *kubernetes.Clientset, namespaceName string) error { namespace := &v1.Namespace{ ObjectMeta: metav1.ObjectMeta{ Name: namespaceName, }, } _, err := client.CoreV1().Namespaces().Create(namespace) if err != nil { return err } return nil }
func deleteNamespace(client *kubernetes.Clientset, namespaceName string) error { deletePolicy := metav1.DeletePropagationForeground err := client.CoreV1().Namespaces().Delete(namespaceName, &metav1.DeleteOptions{ PropagationPolicy: &deletePolicy, }) return err }This code deletes a Kubernetes namespace using the Kubernetes client. The `deleteNamespace()` function takes a Kubernetes client and the name of the namespace to delete, and returns an error if the namespace deletion fails. In summary, the k8s.io/kubernetes/pkg/client package library provides a wide range of functions for interacting with Kubernetes cluster resources. It allows developers to perform basic CRUD operations on Kubernetes resources like namespaces, pods, and services.