Client go connect kubernetes cluster delete related operations

Time:2022-7-12

background

Follow closelyClient go connect kubernetes cluster -connect and listClient go connect kubernetes cluster create related operationsAndClient go connection kubernetes cluster update related operations。 Now, delete the deployment and namespace. Of course, I also want to see what actions are involved in the operation of cluster crud!

Client go connect kubernetes cluster delete related operations

Delete deployment

main.go

package main

import (
    "context"
    "flag"
    "fmt"
    metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    "k8s.io/client-go/kubernetes"
    "k8s.io/client-go/tools/clientcmd"
    "k8s.io/client-go/util/homedir"
    "path/filepath"
)

func main() {
    var kubeconfig *string
    if home := homedir.HomeDir(); home != "" {
        kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
    } else {
        kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
    }
    flag.Parse()
    config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
    if err != nil {
        panic(err.Error())
    }

    // create the clientset
    clientset, err := kubernetes.NewForConfig(config)
    if err != nil {
        panic(err.Error())
    }
    DeploymentName := "nginx"
    if err = clientset.AppsV1().Deployments("zhangpeng").Delete(context.TODO(), DeploymentName, metav1.DeleteOptions{}); err != nil {
        fmt.Println(err)
        return
    }
}

Client go connect kubernetes cluster delete related operations

kubectl get deploymnt -n zhangpeng 

Client go connect kubernetes cluster delete related operations

Delete namespace

main.go

package main

import (
    "context"
    "flag"
    "fmt"
    metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    "k8s.io/client-go/kubernetes"
    "k8s.io/client-go/tools/clientcmd"
    "k8s.io/client-go/util/homedir"
    "path/filepath"
)

func main() {
    var kubeconfig *string
    if home := homedir.HomeDir(); home != "" {
        kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
    } else {
        kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
    }
    flag.Parse()
    config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
    if err != nil {
        panic(err.Error())
    }

    // create the clientset
    clientset, err := kubernetes.NewForConfig(config)
    if err != nil {
        panic(err.Error())
    }
    NamespaceName := "zhangpeng"
    if err = clientset.CoreV1().Namespaces().Delete(context.TODO(), NamespaceName, metav1.DeleteOptions{}); err != nil {
        fmt.Println(err.Error())
        return
    } else {
        fmt.Printf("Deleted Namespaces %s\n", NamespaceName)
    }
}

Client go connect kubernetes cluster delete related operations

kubectl get ns

Client go connect kubernetes cluster delete related operations
Zhang namespace deleted successfully!

other:

  1. Of course, in addition to the commonly used list delete update and create, there are many operation methods, such as some other operations of namespace and deployment:

Client go connect kubernetes cluster delete related operations
Client go connect kubernetes cluster delete related operations

  1. What to do next: extract the client separately and integrate it with gin to do some simple CRUD operations