• client-go gin的简单整合十一-Delete


    背景:

    完成了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
    
    • 1
    • 2
    • 3
    • 4
    • 5

    编写关于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")
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    添加路由运行main.go

    main.go增加路由,并运行main.go

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

    postman操作

    http://127.0.0.1:8080/pod/delete

    {"name":"nginx",
    "namespace":"zhangpeng"}
    
    • 1
    • 2

    XlkVV48G5d.png

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    编写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")
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    添加路由并运行main.go

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

    image.png

    postman测试

    http://127.0.0.1:8080/deployment/delete

    {"name":"zhangpeng",
    "namespace":"zhangpeng"}
    
    • 1
    • 2

    KxFwPTjqoA.png

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    编写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")
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    添加路由并运行main.go

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

    image.png

    postman测试

    http://127.0.0.1:8080/namespace/delete

    {"name":"zhangpeng"}
    
    • 1

    image.png

    总结

    1. client-go 基于gin 的curd算是基本实现
    2. 接下来要实现的
    3. 更优雅的输出
    4. 扩展一下statefulset svc configmap 等等其他k8s资源
    5. 数据的类型也的整理一下
    6. 方法的提取,公共类的整理?结构整理一下
  • 相关阅读:
    Spring Bean 的作用域(Bean Scope)
    python使用泛型
    英语单词: entropy;熵
    安防监控/视频汇聚/云存储/AI智能视频分析平台EasyCVR下级海康设备无法级联是什么原因?
    单代号搭接网络计划:时间参数的计算
    【算法|双指针系列No.4】leetcode11. 盛最多水的容器
    怎么用postman连接websocket
    KMP算法详解以及Java代码实现
    行深智能亮相乌镇互联网大会,荣获直通乌镇全球互联网大赛一等奖
    C++ 关键字
  • 原文地址:https://blog.csdn.net/saynaihe/article/details/125414512