关于运维:clientgo-gin的简单整合十一Delete

41次阅读

共计 2421 个字符,预计需要花费 7 分钟才能阅读完成。

背景:

实现了 client-go gin 的简略整合十 -Update,上面演示一下 namespace deployment pod 的删除,删除实现了也能够总结一下进行更深刻的操作了!删除就从 pod deployment namespace 的程序开始了!

delete Pod

以 nginx Pod 为例

留神: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

编写对于 pod delete 的代码

持续模拟 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 减少路由,并运行 main.go

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

postman 操作

http://127.0.0.1:8080/pod/delete

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

delete deployment

以 zhangpeng deployment 为例

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

编写 delete deployment 代码

/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")
}

增加路由并运行 main.go

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

postman 测试

http://127.0.0.1:8080/deploym…

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

delete namespace

以 zhangpeng namespace 为例

[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

编写 delete namespace 代码

/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")
}

增加路由并运行 main.go

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

postman 测试

http://127.0.0.1:8080/namespa…

{"name":"zhangpeng"}

总结

  1. client-go 基于 gin 的 curd 算是根本实现
  2. 接下来要实现的
  3. 更优雅的输入
  4. 扩大一下 statefulset svc configmap 等等其余 k8s 资源
  5. 数据的类型也的整顿一下
  6. 办法的提取,公共类的整顿?构造整顿一下

正文完
 0