当前位置:网站首页>Simple integration of client go gin 11 delete

Simple integration of client go gin 11 delete

2022-06-22 20:19:00 51CTO

background :

It's done  client-go gin Simple integration of ten -Update, Let's demonstrate namespace deployment pod The deletion of , After the deletion is completed, you can also summarize and conduct more in-depth operations ! Delete from pod deployment namespace The sequence begins !

delete Pod

With nginx Pod For example

Be careful :zhangpeng namespace Next nginx pod For example

[[email protected] k8s]$ kubectl get pods -n zhangpeng
NAME                        READY   STATUS    RESTARTS   AGE
nginx                       1/1     Running   0          113s
zhangpeng-5546976d9-mkslb   1/1     Running   0          102m
zhangpeng-5546976d9-tcsb5   1/1     Running   0          101m

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

Write about pod delete Code for

Continue to imitate create update Write delete Method , Actually delete It only needs namespace and pod Of name That's all right. , To simplify the :
/src/service/Pod.go

func DeletePod(g *gin.Context) {
	var NewPod Pod
	if err := g.ShouldBind(&NewPod); err != nil {
		g.JSON(500, err)
	}
	err := K8sClient.CoreV1().Pods(NewPod.Namespace).Delete(context.TODO(), NewPod.Name, metav1.DeleteOptions{})
	if err != nil {
		fmt.Println(err)
	}
	g.JSON(200, "ok")
}

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

Add route run main.go

main.go Add route , And run main.go

	r.POST("/pod/delete", service.DeletePod)

     
  • 1.

postman operation

 http://127.0.0.1:8080/pod/delete

{"name":"nginx",
"namespace":"zhangpeng"}

     
  • 1.
  • 2.

client-go gin Simple integration of 11 -Delete_client-go

delete deployment

With zhangpeng deployment For example

With namespace zhangpeng Under the namespace zhangpeng** deployment** For example

[[email protected] k8s]$ kubectl get pods -n zhangpeng
NAME                        READY   STATUS    RESTARTS   AGE
zhangpeng-5546976d9-mkslb   1/1     Running   0          124m
zhangpeng-5546976d9-tcsb5   1/1     Running   0          123m
[[email protected] k8s]$ kubectl get deployment -n zhangpeng
NAME        READY   UP-TO-DATE   AVAILABLE   AGE
zhangpeng   2/2     2            2           141m

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

To write delete deployment Code

/src/service/Deployment.go

func DeleteDep(g *gin.Context) {
	var newDep Deployment
	if err := g.ShouldBind(&newDep); err != nil {
		g.JSON(500, err)
	}
	err := K8sClient.AppsV1().Deployments(newDep.Namespace).Delete(context.Background(), newDep.Name, metav1.DeleteOptions{})
	if err != nil {
		g.JSON(500, err)
	}
	g.JSON(200, "Deployment has delete")
}

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

Add a route and run main.go

	r.POST("/deployment/delete", service.DeleteDep)
 

     
  • 1.
  • 2.

client-go gin Simple integration of 11 -Delete_client-go_02

postman test

 http://127.0.0.1:8080/deployment/delete

{"name":"zhangpeng",
"namespace":"zhangpeng"}

     
  • 1.
  • 2.

client-go gin Simple integration of 11 -Delete_ Cloud native _03

delete namespace

With zhangpeng namespace For example

[[email protected] k8s]$ kubectl get ns
NAME              STATUS   AGE
default           Active   54d
kube-node-lease   Active   54d
kube-public       Active   54d
kube-system       Active   54d
zhangpeng         Active   21h
zhangpeng1        Active   24h

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

To write delete namespace Code

/src/service/Deployment.go

func DeleteDep(g *gin.Context) {
	var newDep Deployment
	if err := g.ShouldBind(&newDep); err != nil {
		g.JSON(500, err)
	}
	err := K8sClient.AppsV1().Deployments(newDep.Namespace).Delete(context.Background(), newDep.Name, metav1.DeleteOptions{})
	if err != nil {
		g.JSON(500, err)
	}
	g.JSON(200, "Deployment has delete")
}

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

Add a route and run main.go

	r.POST("/namespace/update", service.DeleteNameSpace)

     
  • 1.

client-go gin Simple integration of 11 -Delete_ Cloud native _04

postman test

 http://127.0.0.1:8080/namespace/delete

{"name":"zhangpeng"}

     
  • 1.

client-go gin Simple integration of 11 -Delete_client-go_05

summary

  1. client-go be based on gin Of curd It is basically realized
  2. What to achieve next
  3. More elegant output
  4. Extend the statefulset svc configmap And so on other k8s resources
  5. Sort out the types of data
  6. Extraction of methods , Sorting of public classes ? Organize the structure
原网站

版权声明
本文为[51CTO]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221832392079.html