client-go连接kubernetes集群-delete相关操作

网友投稿 264 2022-09-11

client-go连接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

其他的:

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

接下来要做的:把客户端单独提取出来,跟gin整合做一些简单的crud操作......

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:client-go gin的简单整合一(list列表相关操作)
下一篇:全媒派:“笋丝”的快乐,利路修下班后也想象不到!
相关文章

 发表评论

暂时没有评论,来抢沙发吧~