乐趣区

关于kubernetes:clientgo连接kubernetes集群delete相关操作

背景

紧跟 client-go 连贯 kubernetes 集群 -connect and list,client-go 连贯 kubernetes 集群 -create 相干操作与 client-go 连贯 kubernetes 集群 -update 相干操作。当初操作一下删除 deployment 与 namespace。当然了也想看一下操作集群 crud 的操作都有哪些动作!

client-go 连贯 kubernetes 集群 -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
    }
}

kubectl get deploymnt -n zhangpeng 

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

kubectl get ns


zhangpeng namespace 删除胜利!

其余的:

  1. 当然了除了罕用的 List delete update and create 还有很多操作方法,比方 namespace 与 deployment 的一些其余操作:


  1. 接下来要做的: 把客户端独自提取进去,跟 gin 整合做一些简略的 crud 操作 ……
退出移动版