完成了client-go gin的简单整合十-Update,下面演示一下namespace deployment pod的删除,删除完成了也可以总结一下进行更深入的操作了!删除就从pod deployment namespace的顺序开始了!
注意:zhangpeng namespace 下nginx pod为例
[zhangpeng@zhangpeng 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
继续模仿create update 写delete方法,其实delete只需要namespace 和pod的name就可以了,简化一下:
/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")
}
main.go增加路由,并运行main.go
r.POST("/pod/delete", service.DeletePod)
http://127.0.0.1:8080/pod/delete
{"name":"nginx",
"namespace":"zhangpeng"}
以namespace zhangpeng命名空间下zhangpeng** deployment**为例
[zhangpeng@zhangpeng 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
[zhangpeng@zhangpeng k8s]$ kubectl get deployment -n zhangpeng
NAME READY UP-TO-DATE AVAILABLE AGE
zhangpeng 2/2 2 2 141m
/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")
}
r.POST("/deployment/delete", service.DeleteDep)
http://127.0.0.1:8080/deployment/delete
{"name":"zhangpeng",
"namespace":"zhangpeng"}
[zhangpeng@zhangpeng 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
/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")
}
r.POST("/namespace/update", service.DeleteNameSpace)
http://127.0.0.1:8080/namespace/delete
{"name":"zhangpeng"}