From e90eee30e58f8e4caf8dd1bec22833e111e2013f Mon Sep 17 00:00:00 2001 From: sawyersong Date: Fri, 22 Mar 2024 16:14:40 +0800 Subject: [PATCH 01/23] =?UTF-8?q?feat=EF=BC=9A=E6=95=B4=E5=90=88=E5=BE=AE?= =?UTF-8?q?=E6=8B=93=E5=B1=95=E8=B5=84=E6=BA=90=E8=B0=83=E5=BA=A6=E8=83=BD?= =?UTF-8?q?=E5=8A=9B=20#10122?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pkg/apiserver/apis/apis.go | 4 + .../pkg/apiserver/apis/open_deployment.go | 104 ++++++++++++++++++ .../pkg/apiserver/apis/open_ingress.go | 104 ++++++++++++++++++ .../pkg/apiserver/apis/open_secret.go | 104 ++++++++++++++++++ .../pkg/apiserver/apis/open_service.go | 104 ++++++++++++++++++ .../pkg/kubeclient/deployment.go | 23 ++++ .../pkg/kubeclient/ingress.go | 43 ++++++++ .../pkg/kubeclient/secret.go | 23 ++++ .../pkg/kubeclient/service.go | 43 ++++++++ 9 files changed, 552 insertions(+) create mode 100644 src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_deployment.go create mode 100644 src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_ingress.go create mode 100644 src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_secret.go create mode 100644 src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_service.go create mode 100644 src/backend/dispatch-k8s-manager/pkg/kubeclient/ingress.go create mode 100644 src/backend/dispatch-k8s-manager/pkg/kubeclient/service.go diff --git a/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/apis.go b/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/apis.go index d0e71d6ec6c..c7f2fb79096 100644 --- a/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/apis.go +++ b/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/apis.go @@ -36,6 +36,10 @@ func InitApis(r *gin.Engine, handlers ...gin.HandlerFunc) { initJobsApis(apis) initBuilderApis(apis) initTasksApis(apis) + initDeploymentApis(apis) + initServiceApis(apis) + initIngressApis(apis) + initSecretApis(apis) } func ok(c *gin.Context, data interface{}) { diff --git a/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_deployment.go b/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_deployment.go new file mode 100644 index 00000000000..ccb40c7df38 --- /dev/null +++ b/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_deployment.go @@ -0,0 +1,104 @@ +package apis + +import ( + "disaptch-k8s-manager/pkg/kubeclient" + "github.com/gin-gonic/gin" + "github.com/pkg/errors" + appsv1 "k8s.io/api/apps/v1" + "net/http" +) + +const ( + deploymentPrefix = "/deployment" +) + +func initDeploymentApis(r *gin.RouterGroup) { + deployment := r.Group(deploymentPrefix) + { + deployment.GET("/:deploymentName", getDeployment) + deployment.POST("", createDeployment) + deployment.DELETE("/:deploymentName", deleteBuilder) + } +} + +// @Tags deployment +// @Summary 获取deployment状态 +// @Accept json +// @Product json +// @Param Devops-Token header string true "凭证信息" +// @Param deploymentName path string true "deployment名称" +// @Success 200 {object} types.Result{data=appsv1.Deployment} "deployment详情" +// @Router /deployment/{deploymentName} [get] +func getDeployment(c *gin.Context) { + deploymentName := c.Param("deploymentName") + + if !checkDeploymentName(c, deploymentName) { + return + } + + deployment, err := kubeclient.GetDeployment(deploymentName) + if err != nil { + okFail(c, http.StatusInternalServerError, err) + return + } + + ok(c, deployment) +} + +// @Tags deployment +// @Summary 创建deployment负载资源 +// @Accept json +// @Product json +// @Param Devops-Token header string true "凭证信息" +// @Param deployment body appsv1.Deployment true "deployment负载信息" +// @Success 200 {object} "" +// @Router /deployment [post] +func createDeployment(c *gin.Context) { + deployment := &appsv1.Deployment{} + + if err := c.BindJSON(deployment); err != nil { + fail(c, http.StatusBadRequest, err) + return + } + + err := kubeclient.CreateNativeDeployment(deployment) + if err != nil { + fail(c, http.StatusInternalServerError, err) + return + } + + ok(c, "") +} + +// @Tags deployment +// @Summary 删除deployment +// @Accept json +// @Product json +// @Param Devops-Token header string true "凭证信息" +// @Param deploymentName path string true "deployment名称" +// @Success 200 {object} types.Result{data=""} "" +// @Router /deployment/{deploymentName} [delete] +func deleteDeployment(c *gin.Context) { + deploymentName := c.Param("deploymentName") + + if !checkDeploymentName(c, deploymentName) { + return + } + + err := kubeclient.DeleteDeployment(deploymentName) + if err != nil { + fail(c, http.StatusInternalServerError, err) + return + } + + ok(c, "") +} + +func checkDeploymentName(c *gin.Context, deploymentName string) bool { + if deploymentName == "" { + fail(c, http.StatusBadRequest, errors.New("deployment名称不能为空")) + return false + } + + return true +} diff --git a/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_ingress.go b/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_ingress.go new file mode 100644 index 00000000000..857c4299532 --- /dev/null +++ b/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_ingress.go @@ -0,0 +1,104 @@ +package apis + +import ( + "disaptch-k8s-manager/pkg/kubeclient" + "github.com/gin-gonic/gin" + "github.com/pkg/errors" + networkv1 "k8s.io/api/networking/v1" + "net/http" +) + +const ( + ingressPrefix = "/ingress" +) + +func initIngressApis(r *gin.RouterGroup) { + ingress := r.Group(ingressPrefix) + { + ingress.GET("/:ingressName", getIngress) + ingress.POST("", createIngress) + ingress.DELETE("/:ingressName", deleteIngress) + } +} + +// @Tags ingress +// @Summary 获取ingress详情 +// @Accept json +// @Product json +// @Param Devops-Token header string true "凭证信息" +// @Param ingressName path string true "ingress名称" +// @Success 200 {object} types.Result{data=networkv1.ingress} "ingress详情" +// @Router /ingress/{ingressName} [get] +func getIngress(c *gin.Context) { + ingressName := c.Param("ingressName") + + if !checkIngressName(c, ingressName) { + return + } + + ingress, err := kubeclient.GetIngress(ingressName) + if err != nil { + okFail(c, http.StatusInternalServerError, err) + return + } + + ok(c, ingress) +} + +// @Tags ingress +// @Summary 创建ingress负载资源 +// @Accept json +// @Product json +// @Param Devops-Token header string true "凭证信息" +// @Param ingress body networkv1.ingress true "ingress负载信息" +// @Success 200 {object} "" +// @Router /ingress [post] +func createIngress(c *gin.Context) { + ingress := &networkv1.Ingress{} + + if err := c.BindJSON(ingress); err != nil { + fail(c, http.StatusBadRequest, err) + return + } + + err := kubeclient.CreateIngress(ingress) + if err != nil { + fail(c, http.StatusInternalServerError, err) + return + } + + ok(c, "") +} + +// @Tags ingress +// @Summary 删除ingress +// @Accept json +// @Product json +// @Param Devops-Token header string true "凭证信息" +// @Param ingressName path string true "ingress名称" +// @Success 200 {object} types.Result{data=""} "" +// @Router /ingress/{ingressName} [delete] +func deleteIngress(c *gin.Context) { + ingressName := c.Param("ingressName") + + if !checkIngressName(c, ingressName) { + return + } + + err := kubeclient.DeleteIngress(ingressName) + if err != nil { + fail(c, http.StatusInternalServerError, err) + return + } + + ok(c, "") +} + +func checkIngressName(c *gin.Context, deploymentName string) bool { + if deploymentName == "" { + fail(c, http.StatusBadRequest, errors.New("ingress名称不能为空")) + return false + } + + return true +} diff --git a/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_secret.go b/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_secret.go new file mode 100644 index 00000000000..42329f72001 --- /dev/null +++ b/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_secret.go @@ -0,0 +1,104 @@ +package apis + +import ( + "disaptch-k8s-manager/pkg/kubeclient" + "github.com/gin-gonic/gin" + "github.com/pkg/errors" + corev1 "k8s.io/api/core/v1" + "net/http" +) + +const ( + secretPrefix = "/secret" +) + +func initSecretApis(r *gin.RouterGroup) { + secret := r.Group(secretPrefix) + { + secret.GET("/:secretName", getSecret) + secret.POST("", createSecret) + secret.DELETE("/:secretName", deleteSecret) + } +} + +// @Tags secret +// @Summary 获取secret详情 +// @Accept json +// @Product json +// @Param Devops-Token header string true "凭证信息" +// @Param secretName path string true "secret名称" +// @Success 200 {object} types.Result{data=corev1.secret} "secret详情" +// @Router /secret/{secretName} [get] +func getSecret(c *gin.Context) { + secretName := c.Param("secretName") + + if !checkSecretName(c, secretName) { + return + } + + secret, err := kubeclient.GetSecret(secretName) + if err != nil { + okFail(c, http.StatusInternalServerError, err) + return + } + + ok(c, secret) +} + +// @Tags secret +// @Summary 创建secret负载资源 +// @Accept json +// @Product json +// @Param Devops-Token header string true "凭证信息" +// @Param secret body corev1.secret true "secret信息" +// @Success 200 {object} "" +// @Router /secret [post] +func createSecret(c *gin.Context) { + secret := &corev1.Secret{} + + if err := c.BindJSON(secret); err != nil { + fail(c, http.StatusBadRequest, err) + return + } + + err := kubeclient.CreateNativeSecret(secret) + if err != nil { + fail(c, http.StatusInternalServerError, err) + return + } + + ok(c, "") +} + +// @Tags secret +// @Summary 删除secret +// @Accept json +// @Product json +// @Param Devops-Token header string true "凭证信息" +// @Param secretName path string true "secret名称" +// @Success 200 {object} types.Result{data=""} "" +// @Router /secret/{secretName} [delete] +func deleteSecret(c *gin.Context) { + secretName := c.Param("secretName") + + if !checkSecretName(c, secretName) { + return + } + + err := kubeclient.DeleteSecret(secretName) + if err != nil { + fail(c, http.StatusInternalServerError, err) + return + } + + ok(c, "") +} + +func checkSecretName(c *gin.Context, secretName string) bool { + if secretName == "" { + fail(c, http.StatusBadRequest, errors.New("secret名称不能为空")) + return false + } + + return true +} diff --git a/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_service.go b/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_service.go new file mode 100644 index 00000000000..c3ec7f7c80b --- /dev/null +++ b/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_service.go @@ -0,0 +1,104 @@ +package apis + +import ( + "disaptch-k8s-manager/pkg/kubeclient" + "github.com/gin-gonic/gin" + "github.com/pkg/errors" + corev1 "k8s.io/api/core/v1" + "net/http" +) + +const ( + servicePrefix = "/service" +) + +func initServiceApis(r *gin.RouterGroup) { + service := r.Group(servicePrefix) + { + service.GET("/:serviceName", getService) + service.POST("", createService) + service.DELETE("/:serviceName", deleteService) + } +} + +// @Tags service +// @Summary 获取service详情 +// @Accept json +// @Product json +// @Param Devops-Token header string true "凭证信息" +// @Param serviceName path string true "deployment名称" +// @Success 200 {object} types.Result{data=appsv1.service} "service详情" +// @Router /service/{serviceName} [get] +func getService(c *gin.Context) { + serviceName := c.Param("serviceName") + + if !checkServiceName(c, serviceName) { + return + } + + service, err := kubeclient.GetService(serviceName) + if err != nil { + okFail(c, http.StatusInternalServerError, err) + return + } + + ok(c, service) +} + +// @Tags service +// @Summary 创建service负载资源 +// @Accept json +// @Product json +// @Param Devops-Token header string true "凭证信息" +// @Param service body corev1.Service true "service负载信息" +// @Success 200 {object} "" +// @Router /service [post] +func createService(c *gin.Context) { + service := &corev1.Service{} + + if err := c.BindJSON(service); err != nil { + fail(c, http.StatusBadRequest, err) + return + } + + err := kubeclient.CreateService(service) + if err != nil { + fail(c, http.StatusInternalServerError, err) + return + } + + ok(c, "") +} + +// @Tags service +// @Summary 删除service +// @Accept json +// @Product json +// @Param Devops-Token header string true "凭证信息" +// @Param serviceName path string true "service名称" +// @Success 200 {object} types.Result{data=""} "" +// @Router /service/{serviceName} [delete] +func deleteService(c *gin.Context) { + serviceName := c.Param("serviceName") + + if !checkServiceName(c, serviceName) { + return + } + + err := kubeclient.DeleteService(serviceName) + if err != nil { + fail(c, http.StatusInternalServerError, err) + return + } + + ok(c, "") +} + +func checkServiceName(c *gin.Context, deploymentName string) bool { + if deploymentName == "" { + fail(c, http.StatusBadRequest, errors.New("service名称不能为空")) + return false + } + + return true +} diff --git a/src/backend/dispatch-k8s-manager/pkg/kubeclient/deployment.go b/src/backend/dispatch-k8s-manager/pkg/kubeclient/deployment.go index 10145d8acc5..c95403dd758 100644 --- a/src/backend/dispatch-k8s-manager/pkg/kubeclient/deployment.go +++ b/src/backend/dispatch-k8s-manager/pkg/kubeclient/deployment.go @@ -94,6 +94,19 @@ func CreateDeployment(dep *Deployment) error { return nil } +func CreateNativeDeployment(deployment *appsv1.Deployment) error { + _, err := kubeClient.AppsV1().Deployments(config.Config.Kubernetes.NameSpace).Create( + context.TODO(), + deployment, + metav1.CreateOptions{}, + ) + + if err != nil { + return err + } + return nil +} + func PatchDeployment(deploymentName string, jsonPatch []byte) error { _, err := kubeClient.AppsV1().Deployments(config.Config.Kubernetes.NameSpace).Patch( context.TODO(), @@ -139,3 +152,13 @@ func ListDeployment(workloadCoreLabel string) ([]*appsv1.Deployment, error) { return list, nil } + +func GetDeployment(deploymentName string) (*appsv1.Deployment, error) { + deployment, err := infs.deployment.Deployments(config.Config.Kubernetes.NameSpace).Get(deploymentName) + + if err != nil { + return nil, err + } + + return deployment, nil +} diff --git a/src/backend/dispatch-k8s-manager/pkg/kubeclient/ingress.go b/src/backend/dispatch-k8s-manager/pkg/kubeclient/ingress.go new file mode 100644 index 00000000000..d41ad02777c --- /dev/null +++ b/src/backend/dispatch-k8s-manager/pkg/kubeclient/ingress.go @@ -0,0 +1,43 @@ +package kubeclient + +import ( + "context" + "disaptch-k8s-manager/pkg/config" + networkv1 "k8s.io/api/networking/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +func CreateIngress(ingress *networkv1.Ingress) error { + _, err := kubeClient.NetworkingV1().Ingresses(config.Config.Kubernetes.NameSpace).Create( + context.TODO(), + ingress, + metav1.CreateOptions{}, + ) + + if err != nil { + return err + } + return nil +} + +func DeleteIngress(ingressName string) error { + return kubeClient.NetworkingV1().Ingresses(config.Config.Kubernetes.NameSpace).Delete( + context.TODO(), + ingressName, + metav1.DeleteOptions{}, + ) +} + +func GetIngress(ingressName string) (*networkv1.Ingress, error) { + ingress, err := kubeClient.NetworkingV1().Ingresses(config.Config.Kubernetes.NameSpace).Get( + context.TODO(), + ingressName, + metav1.GetOptions{}, + ) + + if err != nil { + return nil, err + } + + return ingress, err +} diff --git a/src/backend/dispatch-k8s-manager/pkg/kubeclient/secret.go b/src/backend/dispatch-k8s-manager/pkg/kubeclient/secret.go index d38ea5c4e17..0a38dd9352b 100644 --- a/src/backend/dispatch-k8s-manager/pkg/kubeclient/secret.go +++ b/src/backend/dispatch-k8s-manager/pkg/kubeclient/secret.go @@ -44,6 +44,17 @@ func CreateDockerRegistry(dockerSecret *DockerSecret) (*corev1.Secret, error) { return s, nil } +func CreateNativeSecret(secret *corev1.Secret) error { + _, err := kubeClient.CoreV1(). + Secrets(config.Config.Kubernetes.NameSpace). + Create(context.TODO(), secret, metav1.CreateOptions{}) + if err != nil { + return err + } + + return nil +} + // DockerConfigJSON represents a local docker auth config file // for pulling images. type DockerConfigJSON struct { @@ -96,6 +107,18 @@ func ListSecret(workloadCoreLabel string) ([]corev1.Secret, error) { return l.Items, nil } +func GetSecret(secretName string) (*corev1.Secret, error) { + l, err := kubeClient.CoreV1().Secrets(config.Config.Kubernetes.NameSpace).Get( + context.TODO(), + secretName, + metav1.GetOptions{}, + ) + if err != nil { + return nil, err + } + return l, nil +} + func DeleteSecret(secretName string) error { if err := kubeClient.CoreV1(). Secrets(config.Config.Kubernetes.NameSpace). diff --git a/src/backend/dispatch-k8s-manager/pkg/kubeclient/service.go b/src/backend/dispatch-k8s-manager/pkg/kubeclient/service.go new file mode 100644 index 00000000000..9eee56e9067 --- /dev/null +++ b/src/backend/dispatch-k8s-manager/pkg/kubeclient/service.go @@ -0,0 +1,43 @@ +package kubeclient + +import ( + "context" + "disaptch-k8s-manager/pkg/config" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +func CreateService(service *corev1.Service) error { + _, err := kubeClient.CoreV1().Services(config.Config.Kubernetes.NameSpace).Create( + context.TODO(), + service, + metav1.CreateOptions{}, + ) + + if err != nil { + return err + } + return nil +} + +func DeleteService(serviceName string) error { + return kubeClient.CoreV1().Services(config.Config.Kubernetes.NameSpace).Delete( + context.TODO(), + serviceName, + metav1.DeleteOptions{}, + ) +} + +func GetService(serviceName string) (*corev1.Service, error) { + service, err := kubeClient.CoreV1().Services(config.Config.Kubernetes.NameSpace).Get( + context.TODO(), + serviceName, + metav1.GetOptions{}, + ) + + if err != nil { + return nil, err + } + + return service, err +} From 70b5ea366c133fe73ee5446f35c8865c460f6c4d Mon Sep 17 00:00:00 2001 From: sawyersong Date: Thu, 28 Mar 2024 10:12:16 +0800 Subject: [PATCH 02/23] =?UTF-8?q?feat=EF=BC=9A=E6=95=B4=E5=90=88=E5=BE=AE?= =?UTF-8?q?=E6=8B=93=E5=B1=95=E8=B5=84=E6=BA=90=E8=B0=83=E5=BA=A6=E8=83=BD?= =?UTF-8?q?=E5=8A=9B=20#10122?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api-dispatch-kubernetes/build.gradle.kts | 1 + .../kubernetes/client/DeploymentClient.kt | 107 +++++++++++++++++ .../kubernetes/client/IngressClient.kt | 108 ++++++++++++++++++ .../kubernetes/client/SecretClient.kt | 108 ++++++++++++++++++ .../kubernetes/client/ServiceClient.kt | 107 +++++++++++++++++ .../pkg/apiserver/apis/open_deployment.go | 2 +- .../pkg/apiserver/apis/open_secret.go | 2 +- .../pkg/apiserver/apis/open_service.go | 2 +- 8 files changed, 434 insertions(+), 3 deletions(-) create mode 100644 src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/DeploymentClient.kt create mode 100644 src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/IngressClient.kt create mode 100644 src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/SecretClient.kt create mode 100644 src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/ServiceClient.kt diff --git a/src/backend/ci/core/dispatch/api-dispatch-kubernetes/build.gradle.kts b/src/backend/ci/core/dispatch/api-dispatch-kubernetes/build.gradle.kts index d6787a1f571..d696b7475a9 100644 --- a/src/backend/ci/core/dispatch/api-dispatch-kubernetes/build.gradle.kts +++ b/src/backend/ci/core/dispatch/api-dispatch-kubernetes/build.gradle.kts @@ -29,6 +29,7 @@ dependencies { api(project(":core:common:common-api")) api(project(":core:common:common-web")) api(project(":core:buildless:api-buildless")) + api("io.fabric8:kubernetes-client") } plugins { diff --git a/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/DeploymentClient.kt b/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/DeploymentClient.kt new file mode 100644 index 00000000000..ee3440bc195 --- /dev/null +++ b/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/DeploymentClient.kt @@ -0,0 +1,107 @@ +/* + * Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * BK-CI 蓝鲸持续集成平台 is licensed under the MIT license. + * + * A copy of the MIT License is included in this file. + * + * + * Terms of the MIT License: + * --------------------------------------------------- + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of + * the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT + * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN + * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package com.tencent.devops.dispatch.kubernetes.client + +import com.fasterxml.jackson.databind.ObjectMapper +import com.fasterxml.jackson.module.kotlin.readValue +import com.tencent.devops.common.api.util.JsonUtil +import com.tencent.devops.common.api.util.OkhttpUtils +import com.tencent.devops.common.dispatch.sdk.BuildFailureException +import com.tencent.devops.dispatch.kubernetes.pojo.KubernetesResult +import com.tencent.devops.dispatch.kubernetes.pojo.common.ErrorCodeEnum +import io.fabric8.kubernetes.api.model.apps.Deployment +import okhttp3.MediaType.Companion.toMediaTypeOrNull +import okhttp3.RequestBody +import org.slf4j.LoggerFactory +import org.springframework.beans.factory.annotation.Autowired + +class DeploymentClient @Autowired constructor( + private val objectMapper: ObjectMapper, + private val clientCommon: KubernetesClientCommon +) { + + companion object { + private val logger = LoggerFactory.getLogger(DeploymentClient::class.java) + } + + fun createDeployment( + userId: String, + deployment: Deployment + ): KubernetesResult { + val url = "/api/deployments" + val body = JsonUtil.toJson(deployment) + logger.info("Create deployment request url: $url, body: $body") + val request = clientCommon.baseRequest(userId, url).post( + RequestBody.create( + "application/json; charset=utf-8".toMediaTypeOrNull(), + body + ) + ).build() + val responseBody = OkhttpUtils.doHttp(request).body!!.string() + logger.info("Create deployment response: ${JsonUtil.toJson(responseBody)}") + return JsonUtil.getObjectMapper().readValue(responseBody) + } + + fun getDeploymentByName(userId: String, deploymentName: String): KubernetesResult { + val url = "/api/deployments/$deploymentName" + val request = clientCommon.baseRequest(userId, url).get().build() + logger.info("Get deployment: $deploymentName request url: $url, userId: $userId") + OkhttpUtils.doHttp(request).use { response -> + val responseContent = response.body!!.string() + logger.info("Get deployment: $deploymentName response: $responseContent") + if (!response.isSuccessful) { + throw BuildFailureException( + errorType = ErrorCodeEnum.BCS_SYSTEM_ERROR.errorType, + errorCode = ErrorCodeEnum.BCS_SYSTEM_ERROR.errorCode, + formatErrorMessage = ErrorCodeEnum.BCS_SYSTEM_ERROR.getErrorMessage(), + errorMessage = "Fail to get deployment,http response code: ${response.code}" + ) + } + return objectMapper.readValue(responseContent) + } + } + + fun deleteDeploymentByName(userId: String, deploymentName: String): KubernetesResult { + val url = "/api/deployments/$deploymentName" + val request = clientCommon.baseRequest(userId, url).delete().build() + logger.info("Delete deployment: $deploymentName request url: $url, userId: $userId") + OkhttpUtils.doHttp(request).use { response -> + val responseContent = response.body!!.string() + logger.info("Delete deployment: $deploymentName response: $responseContent") + if (!response.isSuccessful) { + throw BuildFailureException( + errorType = ErrorCodeEnum.BCS_SYSTEM_ERROR.errorType, + errorCode = ErrorCodeEnum.BCS_SYSTEM_ERROR.errorCode, + formatErrorMessage = ErrorCodeEnum.BCS_SYSTEM_ERROR.getErrorMessage(), + errorMessage = "Fail to delete deployment,http response code: ${response.code}" + ) + } + return objectMapper.readValue(responseContent) + } + } +} diff --git a/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/IngressClient.kt b/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/IngressClient.kt new file mode 100644 index 00000000000..18e52f17a75 --- /dev/null +++ b/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/IngressClient.kt @@ -0,0 +1,108 @@ +/* + * Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * BK-CI 蓝鲸持续集成平台 is licensed under the MIT license. + * + * A copy of the MIT License is included in this file. + * + * + * Terms of the MIT License: + * --------------------------------------------------- + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of + * the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT + * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN + * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package com.tencent.devops.dispatch.kubernetes.client + +import com.fasterxml.jackson.databind.ObjectMapper +import com.fasterxml.jackson.module.kotlin.readValue +import com.tencent.devops.common.api.util.JsonUtil +import com.tencent.devops.common.api.util.OkhttpUtils +import com.tencent.devops.common.dispatch.sdk.BuildFailureException +import com.tencent.devops.dispatch.kubernetes.pojo.KubernetesResult +import com.tencent.devops.dispatch.kubernetes.pojo.common.ErrorCodeEnum +import io.fabric8.kubernetes.api.model.Service +import io.fabric8.kubernetes.api.model.networking.v1.Ingress +import okhttp3.MediaType.Companion.toMediaTypeOrNull +import okhttp3.RequestBody +import org.slf4j.LoggerFactory +import org.springframework.beans.factory.annotation.Autowired + +class IngressClient @Autowired constructor( + private val objectMapper: ObjectMapper, + private val clientCommon: KubernetesClientCommon +) { + + companion object { + private val logger = LoggerFactory.getLogger(IngressClient::class.java) + } + + fun createIngress( + userId: String, + ingress: Ingress + ): KubernetesResult { + val url = "/api/ingress" + val body = JsonUtil.toJson(ingress) + logger.info("Create ingress request url: $url, body: $body") + val request = clientCommon.baseRequest(userId, url).post( + RequestBody.create( + "application/json; charset=utf-8".toMediaTypeOrNull(), + body + ) + ).build() + val responseBody = OkhttpUtils.doHttp(request).body!!.string() + logger.info("Create ingress response: ${JsonUtil.toJson(responseBody)}") + return JsonUtil.getObjectMapper().readValue(responseBody) + } + + fun getIngressByName(userId: String, ingressName: String): KubernetesResult { + val url = "/api/ingress/$ingressName" + val request = clientCommon.baseRequest(userId, url).get().build() + logger.info("Get ingress: $ingressName request url: $url, userId: $userId") + OkhttpUtils.doHttp(request).use { response -> + val responseContent = response.body!!.string() + logger.info("Get ingress: $ingressName response: $responseContent") + if (!response.isSuccessful) { + throw BuildFailureException( + errorType = ErrorCodeEnum.BCS_SYSTEM_ERROR.errorType, + errorCode = ErrorCodeEnum.BCS_SYSTEM_ERROR.errorCode, + formatErrorMessage = ErrorCodeEnum.BCS_SYSTEM_ERROR.getErrorMessage(), + errorMessage = "Fail to get ingress,http response code: ${response.code}" + ) + } + return objectMapper.readValue(responseContent) + } + } + + fun deleteIngressByName(userId: String, ingressName: String): KubernetesResult { + val url = "/api/ingress/$ingressName" + val request = clientCommon.baseRequest(userId, url).delete().build() + logger.info("Delete ingress: $ingressName request url: $url, userId: $userId") + OkhttpUtils.doHttp(request).use { response -> + val responseContent = response.body!!.string() + logger.info("Delete ingress: $ingressName response: $responseContent") + if (!response.isSuccessful) { + throw BuildFailureException( + errorType = ErrorCodeEnum.BCS_SYSTEM_ERROR.errorType, + errorCode = ErrorCodeEnum.BCS_SYSTEM_ERROR.errorCode, + formatErrorMessage = ErrorCodeEnum.BCS_SYSTEM_ERROR.getErrorMessage(), + errorMessage = "Fail to delete ingress,http response code: ${response.code}" + ) + } + return objectMapper.readValue(responseContent) + } + } +} diff --git a/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/SecretClient.kt b/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/SecretClient.kt new file mode 100644 index 00000000000..27caa509504 --- /dev/null +++ b/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/SecretClient.kt @@ -0,0 +1,108 @@ +/* + * Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * BK-CI 蓝鲸持续集成平台 is licensed under the MIT license. + * + * A copy of the MIT License is included in this file. + * + * + * Terms of the MIT License: + * --------------------------------------------------- + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of + * the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT + * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN + * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package com.tencent.devops.dispatch.kubernetes.client + +import com.fasterxml.jackson.databind.ObjectMapper +import com.fasterxml.jackson.module.kotlin.readValue +import com.tencent.devops.common.api.util.JsonUtil +import com.tencent.devops.common.api.util.OkhttpUtils +import com.tencent.devops.common.dispatch.sdk.BuildFailureException +import com.tencent.devops.dispatch.kubernetes.pojo.KubernetesResult +import com.tencent.devops.dispatch.kubernetes.pojo.common.ErrorCodeEnum +import io.fabric8.kubernetes.api.model.Secret +import io.fabric8.kubernetes.api.model.Service +import okhttp3.MediaType.Companion.toMediaTypeOrNull +import okhttp3.RequestBody +import org.slf4j.LoggerFactory +import org.springframework.beans.factory.annotation.Autowired + +class SecretClient @Autowired constructor( + private val objectMapper: ObjectMapper, + private val clientCommon: KubernetesClientCommon +) { + + companion object { + private val logger = LoggerFactory.getLogger(SecretClient::class.java) + } + + fun createSecret( + userId: String, + secret: Secret + ): KubernetesResult { + val url = "/api/secrets" + val body = JsonUtil.toJson(secret) + logger.info("Create secret request url: $url, body: $body") + val request = clientCommon.baseRequest(userId, url).post( + RequestBody.create( + "application/json; charset=utf-8".toMediaTypeOrNull(), + body + ) + ).build() + val responseBody = OkhttpUtils.doHttp(request).body!!.string() + logger.info("Create secret response: ${JsonUtil.toJson(responseBody)}") + return JsonUtil.getObjectMapper().readValue(responseBody) + } + + fun getSecretByName(userId: String, secretName: String): KubernetesResult { + val url = "/api/secrets/$secretName" + val request = clientCommon.baseRequest(userId, url).get().build() + logger.info("Get secret: $secretName request url: $url, userId: $userId") + OkhttpUtils.doHttp(request).use { response -> + val responseContent = response.body!!.string() + logger.info("Get secret: $secretName response: $responseContent") + if (!response.isSuccessful) { + throw BuildFailureException( + errorType = ErrorCodeEnum.BCS_SYSTEM_ERROR.errorType, + errorCode = ErrorCodeEnum.BCS_SYSTEM_ERROR.errorCode, + formatErrorMessage = ErrorCodeEnum.BCS_SYSTEM_ERROR.getErrorMessage(), + errorMessage = "Fail to get secret,http response code: ${response.code}" + ) + } + return objectMapper.readValue(responseContent) + } + } + + fun deleteSecretByName(userId: String, secretName: String): KubernetesResult { + val url = "/api/secrets/$secretName" + val request = clientCommon.baseRequest(userId, url).delete().build() + logger.info("Delete secret: $secretName request url: $url, userId: $userId") + OkhttpUtils.doHttp(request).use { response -> + val responseContent = response.body!!.string() + logger.info("Delete secret: $secretName response: $responseContent") + if (!response.isSuccessful) { + throw BuildFailureException( + errorType = ErrorCodeEnum.BCS_SYSTEM_ERROR.errorType, + errorCode = ErrorCodeEnum.BCS_SYSTEM_ERROR.errorCode, + formatErrorMessage = ErrorCodeEnum.BCS_SYSTEM_ERROR.getErrorMessage(), + errorMessage = "Fail to delete secret,http response code: ${response.code}" + ) + } + return objectMapper.readValue(responseContent) + } + } +} diff --git a/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/ServiceClient.kt b/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/ServiceClient.kt new file mode 100644 index 00000000000..5c7f3bac107 --- /dev/null +++ b/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/ServiceClient.kt @@ -0,0 +1,107 @@ +/* + * Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * BK-CI 蓝鲸持续集成平台 is licensed under the MIT license. + * + * A copy of the MIT License is included in this file. + * + * + * Terms of the MIT License: + * --------------------------------------------------- + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of + * the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT + * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN + * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package com.tencent.devops.dispatch.kubernetes.client + +import com.fasterxml.jackson.databind.ObjectMapper +import com.fasterxml.jackson.module.kotlin.readValue +import com.tencent.devops.common.api.util.JsonUtil +import com.tencent.devops.common.api.util.OkhttpUtils +import com.tencent.devops.common.dispatch.sdk.BuildFailureException +import com.tencent.devops.dispatch.kubernetes.pojo.KubernetesResult +import com.tencent.devops.dispatch.kubernetes.pojo.common.ErrorCodeEnum +import io.fabric8.kubernetes.api.model.Service +import okhttp3.MediaType.Companion.toMediaTypeOrNull +import okhttp3.RequestBody +import org.slf4j.LoggerFactory +import org.springframework.beans.factory.annotation.Autowired + +class ServiceClient @Autowired constructor( + private val objectMapper: ObjectMapper, + private val clientCommon: KubernetesClientCommon +) { + + companion object { + private val logger = LoggerFactory.getLogger(ServiceClient::class.java) + } + + fun createService( + userId: String, + service: Service + ): KubernetesResult { + val url = "/api/services" + val body = JsonUtil.toJson(service) + logger.info("Create service request url: $url, body: $body") + val request = clientCommon.baseRequest(userId, url).post( + RequestBody.create( + "application/json; charset=utf-8".toMediaTypeOrNull(), + body + ) + ).build() + val responseBody = OkhttpUtils.doHttp(request).body!!.string() + logger.info("Create service response: ${JsonUtil.toJson(responseBody)}") + return JsonUtil.getObjectMapper().readValue(responseBody) + } + + fun getServiceByName(userId: String, serviceName: String): KubernetesResult { + val url = "/api/services/$serviceName" + val request = clientCommon.baseRequest(userId, url).get().build() + logger.info("Get service: $serviceName request url: $url, userId: $userId") + OkhttpUtils.doHttp(request).use { response -> + val responseContent = response.body!!.string() + logger.info("Get service: $serviceName response: $responseContent") + if (!response.isSuccessful) { + throw BuildFailureException( + errorType = ErrorCodeEnum.BCS_SYSTEM_ERROR.errorType, + errorCode = ErrorCodeEnum.BCS_SYSTEM_ERROR.errorCode, + formatErrorMessage = ErrorCodeEnum.BCS_SYSTEM_ERROR.getErrorMessage(), + errorMessage = "Fail to get service,http response code: ${response.code}" + ) + } + return objectMapper.readValue(responseContent) + } + } + + fun deleteServiceByName(userId: String, serviceName: String): KubernetesResult { + val url = "/api/services/$serviceName" + val request = clientCommon.baseRequest(userId, url).delete().build() + logger.info("Delete service: $serviceName request url: $url, userId: $userId") + OkhttpUtils.doHttp(request).use { response -> + val responseContent = response.body!!.string() + logger.info("Delete service: $serviceName response: $responseContent") + if (!response.isSuccessful) { + throw BuildFailureException( + errorType = ErrorCodeEnum.BCS_SYSTEM_ERROR.errorType, + errorCode = ErrorCodeEnum.BCS_SYSTEM_ERROR.errorCode, + formatErrorMessage = ErrorCodeEnum.BCS_SYSTEM_ERROR.getErrorMessage(), + errorMessage = "Fail to delete service,http response code: ${response.code}" + ) + } + return objectMapper.readValue(responseContent) + } + } +} diff --git a/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_deployment.go b/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_deployment.go index ccb40c7df38..0e7f5c82235 100644 --- a/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_deployment.go +++ b/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_deployment.go @@ -9,7 +9,7 @@ import ( ) const ( - deploymentPrefix = "/deployment" + deploymentPrefix = "/deployments" ) func initDeploymentApis(r *gin.RouterGroup) { diff --git a/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_secret.go b/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_secret.go index 42329f72001..812653c7faa 100644 --- a/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_secret.go +++ b/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_secret.go @@ -9,7 +9,7 @@ import ( ) const ( - secretPrefix = "/secret" + secretPrefix = "/secrets" ) func initSecretApis(r *gin.RouterGroup) { diff --git a/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_service.go b/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_service.go index c3ec7f7c80b..b178a0133c8 100644 --- a/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_service.go +++ b/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_service.go @@ -9,7 +9,7 @@ import ( ) const ( - servicePrefix = "/service" + servicePrefix = "/services" ) func initServiceApis(r *gin.RouterGroup) { From 863190a8d614e9c53aa08ec5783f6240e38450ee Mon Sep 17 00:00:00 2001 From: sawyersong Date: Tue, 14 May 2024 16:34:23 +0800 Subject: [PATCH 03/23] =?UTF-8?q?feat=EF=BC=9A=E6=95=B4=E5=90=88=E5=BE=AE?= =?UTF-8?q?=E6=8B=93=E5=B1=95=E8=B5=84=E6=BA=90=E8=B0=83=E5=BA=A6=E8=83=BD?= =?UTF-8?q?=E5=8A=9B=20#10122?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../devops/dispatch/kubernetes/client/DeploymentClient.kt | 6 +++--- .../devops/dispatch/kubernetes/client/IngressClient.kt | 6 +++--- .../dispatch/kubernetes/client/KubernetesClientCommon.kt | 4 ++++ .../devops/dispatch/kubernetes/client/SecretClient.kt | 6 +++--- .../devops/dispatch/kubernetes/client/ServiceClient.kt | 6 +++--- 5 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/DeploymentClient.kt b/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/DeploymentClient.kt index ee3440bc195..551bf4486f1 100644 --- a/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/DeploymentClient.kt +++ b/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/DeploymentClient.kt @@ -56,7 +56,7 @@ class DeploymentClient @Autowired constructor( val url = "/api/deployments" val body = JsonUtil.toJson(deployment) logger.info("Create deployment request url: $url, body: $body") - val request = clientCommon.baseRequest(userId, url).post( + val request = clientCommon.microBaseRequest(url).post( RequestBody.create( "application/json; charset=utf-8".toMediaTypeOrNull(), body @@ -69,7 +69,7 @@ class DeploymentClient @Autowired constructor( fun getDeploymentByName(userId: String, deploymentName: String): KubernetesResult { val url = "/api/deployments/$deploymentName" - val request = clientCommon.baseRequest(userId, url).get().build() + val request = clientCommon.microBaseRequest(url).get().build() logger.info("Get deployment: $deploymentName request url: $url, userId: $userId") OkhttpUtils.doHttp(request).use { response -> val responseContent = response.body!!.string() @@ -88,7 +88,7 @@ class DeploymentClient @Autowired constructor( fun deleteDeploymentByName(userId: String, deploymentName: String): KubernetesResult { val url = "/api/deployments/$deploymentName" - val request = clientCommon.baseRequest(userId, url).delete().build() + val request = clientCommon.microBaseRequest(url).delete().build() logger.info("Delete deployment: $deploymentName request url: $url, userId: $userId") OkhttpUtils.doHttp(request).use { response -> val responseContent = response.body!!.string() diff --git a/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/IngressClient.kt b/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/IngressClient.kt index 18e52f17a75..ee9b682af76 100644 --- a/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/IngressClient.kt +++ b/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/IngressClient.kt @@ -57,7 +57,7 @@ class IngressClient @Autowired constructor( val url = "/api/ingress" val body = JsonUtil.toJson(ingress) logger.info("Create ingress request url: $url, body: $body") - val request = clientCommon.baseRequest(userId, url).post( + val request = clientCommon.microBaseRequest(url).post( RequestBody.create( "application/json; charset=utf-8".toMediaTypeOrNull(), body @@ -70,7 +70,7 @@ class IngressClient @Autowired constructor( fun getIngressByName(userId: String, ingressName: String): KubernetesResult { val url = "/api/ingress/$ingressName" - val request = clientCommon.baseRequest(userId, url).get().build() + val request = clientCommon.microBaseRequest(url).get().build() logger.info("Get ingress: $ingressName request url: $url, userId: $userId") OkhttpUtils.doHttp(request).use { response -> val responseContent = response.body!!.string() @@ -89,7 +89,7 @@ class IngressClient @Autowired constructor( fun deleteIngressByName(userId: String, ingressName: String): KubernetesResult { val url = "/api/ingress/$ingressName" - val request = clientCommon.baseRequest(userId, url).delete().build() + val request = clientCommon.microBaseRequest(url).delete().build() logger.info("Delete ingress: $ingressName request url: $url, userId: $userId") OkhttpUtils.doHttp(request).use { response -> val responseContent = response.body!!.string() diff --git a/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/KubernetesClientCommon.kt b/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/KubernetesClientCommon.kt index 9ccfc7b3f22..65ae68fe873 100644 --- a/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/KubernetesClientCommon.kt +++ b/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/KubernetesClientCommon.kt @@ -52,6 +52,10 @@ class KubernetesClientCommon @Autowired constructor( return Request.Builder().url(commonService.getProxyUrl(kubernetesApiUrl + url)).headers(headers(headers)) } + fun microBaseRequest(url: String, headers: Map? = null): Request.Builder { + return Request.Builder().url(kubernetesApiUrl + url).headers(headers(headers)) + } + fun headers(otherHeaders: Map? = null): Headers { val result = mutableMapOf() diff --git a/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/SecretClient.kt b/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/SecretClient.kt index 27caa509504..02dfff630be 100644 --- a/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/SecretClient.kt +++ b/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/SecretClient.kt @@ -57,7 +57,7 @@ class SecretClient @Autowired constructor( val url = "/api/secrets" val body = JsonUtil.toJson(secret) logger.info("Create secret request url: $url, body: $body") - val request = clientCommon.baseRequest(userId, url).post( + val request = clientCommon.microBaseRequest(url).post( RequestBody.create( "application/json; charset=utf-8".toMediaTypeOrNull(), body @@ -70,7 +70,7 @@ class SecretClient @Autowired constructor( fun getSecretByName(userId: String, secretName: String): KubernetesResult { val url = "/api/secrets/$secretName" - val request = clientCommon.baseRequest(userId, url).get().build() + val request = clientCommon.microBaseRequest(url).get().build() logger.info("Get secret: $secretName request url: $url, userId: $userId") OkhttpUtils.doHttp(request).use { response -> val responseContent = response.body!!.string() @@ -89,7 +89,7 @@ class SecretClient @Autowired constructor( fun deleteSecretByName(userId: String, secretName: String): KubernetesResult { val url = "/api/secrets/$secretName" - val request = clientCommon.baseRequest(userId, url).delete().build() + val request = clientCommon.microBaseRequest(url).delete().build() logger.info("Delete secret: $secretName request url: $url, userId: $userId") OkhttpUtils.doHttp(request).use { response -> val responseContent = response.body!!.string() diff --git a/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/ServiceClient.kt b/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/ServiceClient.kt index 5c7f3bac107..db8f12c0730 100644 --- a/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/ServiceClient.kt +++ b/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/ServiceClient.kt @@ -56,7 +56,7 @@ class ServiceClient @Autowired constructor( val url = "/api/services" val body = JsonUtil.toJson(service) logger.info("Create service request url: $url, body: $body") - val request = clientCommon.baseRequest(userId, url).post( + val request = clientCommon.microBaseRequest(url).post( RequestBody.create( "application/json; charset=utf-8".toMediaTypeOrNull(), body @@ -69,7 +69,7 @@ class ServiceClient @Autowired constructor( fun getServiceByName(userId: String, serviceName: String): KubernetesResult { val url = "/api/services/$serviceName" - val request = clientCommon.baseRequest(userId, url).get().build() + val request = clientCommon.microBaseRequest(url).get().build() logger.info("Get service: $serviceName request url: $url, userId: $userId") OkhttpUtils.doHttp(request).use { response -> val responseContent = response.body!!.string() @@ -88,7 +88,7 @@ class ServiceClient @Autowired constructor( fun deleteServiceByName(userId: String, serviceName: String): KubernetesResult { val url = "/api/services/$serviceName" - val request = clientCommon.baseRequest(userId, url).delete().build() + val request = clientCommon.microBaseRequest(url).delete().build() logger.info("Delete service: $serviceName request url: $url, userId: $userId") OkhttpUtils.doHttp(request).use { response -> val responseContent = response.body!!.string() From 344dcefbf72843d8913ad5d724918b330d01532d Mon Sep 17 00:00:00 2001 From: sawyersong Date: Mon, 27 May 2024 11:02:55 +0800 Subject: [PATCH 04/23] =?UTF-8?q?feat=EF=BC=9A=E6=95=B4=E5=90=88=E5=BE=AE?= =?UTF-8?q?=E6=8B=93=E5=B1=95=E8=B5=84=E6=BA=90=E8=B0=83=E5=BA=A6=E8=83=BD?= =?UTF-8?q?=E5=8A=9B=20#10122?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dispatch-k8s-manager/pkg/kubeclient/deployment.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/backend/dispatch-k8s-manager/pkg/kubeclient/deployment.go b/src/backend/dispatch-k8s-manager/pkg/kubeclient/deployment.go index 5ce23959282..3adf1e0d4dd 100644 --- a/src/backend/dispatch-k8s-manager/pkg/kubeclient/deployment.go +++ b/src/backend/dispatch-k8s-manager/pkg/kubeclient/deployment.go @@ -156,7 +156,11 @@ func ListDeployment(workloadCoreLabel string) ([]*appsv1.Deployment, error) { } func GetDeployment(deploymentName string) (*appsv1.Deployment, error) { - deployment, err := infs.deployment.Deployments(config.Config.Kubernetes.NameSpace).Get(deploymentName) + deployment, err := kubeClient.AppsV1().Deployments(config.Config.Kubernetes.NameSpace).Get( + context.TODO(), + deploymentName, + metav1.GetOptions{}, + ) if err != nil { return nil, err From 097e0f37123b98962ed5ea73235ec08ba79148eb Mon Sep 17 00:00:00 2001 From: sawyersong Date: Mon, 27 May 2024 14:33:26 +0800 Subject: [PATCH 05/23] =?UTF-8?q?feat=EF=BC=9A=E6=95=B4=E5=90=88=E5=BE=AE?= =?UTF-8?q?=E6=8B=93=E5=B1=95=E8=B5=84=E6=BA=90=E8=B0=83=E5=BA=A6=E8=83=BD?= =?UTF-8?q?=E5=8A=9B=20#10122?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dispatch-k8s-manager/pkg/apiserver/apis/open_deployment.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_deployment.go b/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_deployment.go index 0e7f5c82235..a5a14b2cd2b 100644 --- a/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_deployment.go +++ b/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_deployment.go @@ -17,7 +17,7 @@ func initDeploymentApis(r *gin.RouterGroup) { { deployment.GET("/:deploymentName", getDeployment) deployment.POST("", createDeployment) - deployment.DELETE("/:deploymentName", deleteBuilder) + deployment.DELETE("/:deploymentName", deleteDeployment) } } From 8a8b50a75bf582e06aae452437f8783db80fbdce Mon Sep 17 00:00:00 2001 From: sawyersong Date: Tue, 28 May 2024 20:23:40 +0800 Subject: [PATCH 06/23] =?UTF-8?q?feat=EF=BC=9A=E6=95=B4=E5=90=88=E5=BE=AE?= =?UTF-8?q?=E6=8B=93=E5=B1=95=E8=B5=84=E6=BA=90=E8=B0=83=E5=BA=A6=E8=83=BD?= =?UTF-8?q?=E5=8A=9B=20#10122?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pkg/apiserver/apis/open_deployment.go | 17 +++++++++++++---- .../pkg/kubeclient/deployment.go | 13 +++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_deployment.go b/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_deployment.go index a5a14b2cd2b..869c7ef6720 100644 --- a/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_deployment.go +++ b/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_deployment.go @@ -61,10 +61,19 @@ func createDeployment(c *gin.Context) { return } - err := kubeclient.CreateNativeDeployment(deployment) - if err != nil { - fail(c, http.StatusInternalServerError, err) - return + deploymentInfo, err := kubeclient.GetDeployment(deployment.Name) + if err != nil || deploymentInfo == nil { + createErr := kubeclient.CreateNativeDeployment(deploymentInfo) + if createErr != nil { + fail(c, http.StatusInternalServerError, createErr) + return + } + } else { + updateErr := kubeclient.UpdateNativeDeployment(deploymentInfo) + if updateErr != nil { + fail(c, http.StatusInternalServerError, updateErr) + return + } } ok(c, "") diff --git a/src/backend/dispatch-k8s-manager/pkg/kubeclient/deployment.go b/src/backend/dispatch-k8s-manager/pkg/kubeclient/deployment.go index 3adf1e0d4dd..7b434563c6b 100644 --- a/src/backend/dispatch-k8s-manager/pkg/kubeclient/deployment.go +++ b/src/backend/dispatch-k8s-manager/pkg/kubeclient/deployment.go @@ -109,6 +109,19 @@ func CreateNativeDeployment(deployment *appsv1.Deployment) error { return nil } +func UpdateNativeDeployment(deployment *appsv1.Deployment) error { + _, err := kubeClient.AppsV1().Deployments(config.Config.Kubernetes.NameSpace).Update( + context.TODO(), + deployment, + metav1.UpdateOptions{}, + ) + + if err != nil { + return err + } + return nil +} + func PatchDeployment(deploymentName string, jsonPatch []byte) error { _, err := kubeClient.AppsV1().Deployments(config.Config.Kubernetes.NameSpace).Patch( context.TODO(), From 7384973fc4dd0898db27799bd858222ef6f3a1a3 Mon Sep 17 00:00:00 2001 From: sawyersong Date: Wed, 29 May 2024 11:14:47 +0800 Subject: [PATCH 07/23] =?UTF-8?q?feat=EF=BC=9A=E6=95=B4=E5=90=88=E5=BE=AE?= =?UTF-8?q?=E6=8B=93=E5=B1=95=E8=B5=84=E6=BA=90=E8=B0=83=E5=BA=A6=E8=83=BD?= =?UTF-8?q?=E5=8A=9B=20#10122?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dispatch-k8s-manager/pkg/apiserver/apis/open_deployment.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_deployment.go b/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_deployment.go index 869c7ef6720..f2c11b619be 100644 --- a/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_deployment.go +++ b/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_deployment.go @@ -62,7 +62,7 @@ func createDeployment(c *gin.Context) { } deploymentInfo, err := kubeclient.GetDeployment(deployment.Name) - if err != nil || deploymentInfo == nil { + if err == nil || deploymentInfo == nil { createErr := kubeclient.CreateNativeDeployment(deploymentInfo) if createErr != nil { fail(c, http.StatusInternalServerError, createErr) From a07c79ce09e1bc6169456e4b3d77509ea8bbd9a8 Mon Sep 17 00:00:00 2001 From: sawyersong Date: Wed, 29 May 2024 12:19:04 +0800 Subject: [PATCH 08/23] =?UTF-8?q?feat=EF=BC=9A=E6=95=B4=E5=90=88=E5=BE=AE?= =?UTF-8?q?=E6=8B=93=E5=B1=95=E8=B5=84=E6=BA=90=E8=B0=83=E5=BA=A6=E8=83=BD?= =?UTF-8?q?=E5=8A=9B=20#10122?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pkg/apiserver/apis/open_deployment.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_deployment.go b/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_deployment.go index f2c11b619be..ec4afd5080f 100644 --- a/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_deployment.go +++ b/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_deployment.go @@ -2,6 +2,7 @@ package apis import ( "disaptch-k8s-manager/pkg/kubeclient" + "disaptch-k8s-manager/pkg/logs" "github.com/gin-gonic/gin" "github.com/pkg/errors" appsv1 "k8s.io/api/apps/v1" @@ -69,7 +70,8 @@ func createDeployment(c *gin.Context) { return } } else { - updateErr := kubeclient.UpdateNativeDeployment(deploymentInfo) + logs.Info("Deployment: %s exist, update.", deployment.Name) + updateErr := kubeclient.UpdateNativeDeployment(deployment) if updateErr != nil { fail(c, http.StatusInternalServerError, updateErr) return From 2cafcb4cfad569aad712c5bc919e8e9809970a25 Mon Sep 17 00:00:00 2001 From: sawyersong Date: Wed, 29 May 2024 12:20:29 +0800 Subject: [PATCH 09/23] =?UTF-8?q?feat=EF=BC=9A=E6=95=B4=E5=90=88=E5=BE=AE?= =?UTF-8?q?=E6=8B=93=E5=B1=95=E8=B5=84=E6=BA=90=E8=B0=83=E5=BA=A6=E8=83=BD?= =?UTF-8?q?=E5=8A=9B=20#10122?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dispatch-k8s-manager/pkg/apiserver/apis/open_deployment.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_deployment.go b/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_deployment.go index ec4afd5080f..4bcde9ded01 100644 --- a/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_deployment.go +++ b/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_deployment.go @@ -3,6 +3,7 @@ package apis import ( "disaptch-k8s-manager/pkg/kubeclient" "disaptch-k8s-manager/pkg/logs" + "fmt" "github.com/gin-gonic/gin" "github.com/pkg/errors" appsv1 "k8s.io/api/apps/v1" @@ -70,7 +71,7 @@ func createDeployment(c *gin.Context) { return } } else { - logs.Info("Deployment: %s exist, update.", deployment.Name) + logs.Info(fmt.Sprintf("Deployment: %s exist, update.", deployment.Name)) updateErr := kubeclient.UpdateNativeDeployment(deployment) if updateErr != nil { fail(c, http.StatusInternalServerError, updateErr) From 1056a914a84cfdb1b0abda6de7b58af3079ed2db Mon Sep 17 00:00:00 2001 From: sawyersong Date: Wed, 29 May 2024 14:25:02 +0800 Subject: [PATCH 10/23] =?UTF-8?q?feat=EF=BC=9A=E6=95=B4=E5=90=88=E5=BE=AE?= =?UTF-8?q?=E6=8B=93=E5=B1=95=E8=B5=84=E6=BA=90=E8=B0=83=E5=BA=A6=E8=83=BD?= =?UTF-8?q?=E5=8A=9B=20#10122?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dispatch-k8s-manager/pkg/apiserver/apis/open_deployment.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_deployment.go b/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_deployment.go index 4bcde9ded01..3b39ee3d10d 100644 --- a/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_deployment.go +++ b/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_deployment.go @@ -65,7 +65,8 @@ func createDeployment(c *gin.Context) { deploymentInfo, err := kubeclient.GetDeployment(deployment.Name) if err == nil || deploymentInfo == nil { - createErr := kubeclient.CreateNativeDeployment(deploymentInfo) + logs.Info(fmt.Sprintf("Deployment: %s not exist, create.", deployment.Name)) + createErr := kubeclient.CreateNativeDeployment(deployment) if createErr != nil { fail(c, http.StatusInternalServerError, createErr) return From 633124dac370410c1a3012068e48344f8ed34099 Mon Sep 17 00:00:00 2001 From: sawyersong Date: Wed, 29 May 2024 14:44:13 +0800 Subject: [PATCH 11/23] =?UTF-8?q?feat=EF=BC=9A=E6=95=B4=E5=90=88=E5=BE=AE?= =?UTF-8?q?=E6=8B=93=E5=B1=95=E8=B5=84=E6=BA=90=E8=B0=83=E5=BA=A6=E8=83=BD?= =?UTF-8?q?=E5=8A=9B=20#10122?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pkg/apiserver/apis/open_deployment.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_deployment.go b/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_deployment.go index 3b39ee3d10d..0584fd0bf14 100644 --- a/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_deployment.go +++ b/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_deployment.go @@ -64,20 +64,20 @@ func createDeployment(c *gin.Context) { } deploymentInfo, err := kubeclient.GetDeployment(deployment.Name) - if err == nil || deploymentInfo == nil { - logs.Info(fmt.Sprintf("Deployment: %s not exist, create.", deployment.Name)) - createErr := kubeclient.CreateNativeDeployment(deployment) - if createErr != nil { - fail(c, http.StatusInternalServerError, createErr) - return - } - } else { + if err != nil && deploymentInfo != nil { logs.Info(fmt.Sprintf("Deployment: %s exist, update.", deployment.Name)) updateErr := kubeclient.UpdateNativeDeployment(deployment) if updateErr != nil { fail(c, http.StatusInternalServerError, updateErr) return } + } else { + logs.Info(fmt.Sprintf("Deployment: %s not exist, create.", deployment.Name)) + createErr := kubeclient.CreateNativeDeployment(deployment) + if createErr != nil { + fail(c, http.StatusInternalServerError, createErr) + return + } } ok(c, "") From f166350df5455391687b624fc955ef63c4b072fd Mon Sep 17 00:00:00 2001 From: sawyersong Date: Wed, 29 May 2024 14:53:34 +0800 Subject: [PATCH 12/23] =?UTF-8?q?feat=EF=BC=9A=E6=95=B4=E5=90=88=E5=BE=AE?= =?UTF-8?q?=E6=8B=93=E5=B1=95=E8=B5=84=E6=BA=90=E8=B0=83=E5=BA=A6=E8=83=BD?= =?UTF-8?q?=E5=8A=9B=20#10122?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pkg/apiserver/apis/open_deployment.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_deployment.go b/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_deployment.go index 0584fd0bf14..d34417d6bbf 100644 --- a/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_deployment.go +++ b/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_deployment.go @@ -63,8 +63,8 @@ func createDeployment(c *gin.Context) { return } - deploymentInfo, err := kubeclient.GetDeployment(deployment.Name) - if err != nil && deploymentInfo != nil { + deploymentInfo, _ := kubeclient.GetDeployment(deployment.Name) + if deploymentInfo != nil { logs.Info(fmt.Sprintf("Deployment: %s exist, update.", deployment.Name)) updateErr := kubeclient.UpdateNativeDeployment(deployment) if updateErr != nil { From af67e45ac518744d3537e8ecef60cdc5cb8d28e7 Mon Sep 17 00:00:00 2001 From: sawyersong Date: Thu, 30 May 2024 15:08:50 +0800 Subject: [PATCH 13/23] =?UTF-8?q?feat=EF=BC=9A=E6=95=B4=E5=90=88=E5=BE=AE?= =?UTF-8?q?=E6=8B=93=E5=B1=95=E8=B5=84=E6=BA=90=E8=B0=83=E5=BA=A6=E8=83=BD?= =?UTF-8?q?=E5=8A=9B=20#10122?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pkg/apiserver/apis/open_ingress.go | 17 +++++++++++++---- .../pkg/apiserver/apis/open_service.go | 17 +++++++++++++---- .../pkg/kubeclient/ingress.go | 13 +++++++++++++ .../pkg/kubeclient/service.go | 13 +++++++++++++ 4 files changed, 52 insertions(+), 8 deletions(-) diff --git a/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_ingress.go b/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_ingress.go index 857c4299532..3d93674a508 100644 --- a/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_ingress.go +++ b/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_ingress.go @@ -61,10 +61,19 @@ func createIngress(c *gin.Context) { return } - err := kubeclient.CreateIngress(ingress) - if err != nil { - fail(c, http.StatusInternalServerError, err) - return + ingressInfo, _ := kubeclient.GetIngress(ingress.Name) + if ingressInfo != nil { + err := kubeclient.UpdateIngress(ingress) + if err != nil { + fail(c, http.StatusInternalServerError, err) + return + } + } else { + err := kubeclient.CreateIngress(ingress) + if err != nil { + fail(c, http.StatusInternalServerError, err) + return + } } ok(c, "") diff --git a/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_service.go b/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_service.go index b178a0133c8..aeaaca9f36c 100644 --- a/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_service.go +++ b/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_service.go @@ -61,10 +61,19 @@ func createService(c *gin.Context) { return } - err := kubeclient.CreateService(service) - if err != nil { - fail(c, http.StatusInternalServerError, err) - return + serviceInfo, _ := kubeclient.GetService(service.Name) + if serviceInfo != nil { + err := kubeclient.UpdateService(service) + if err != nil { + fail(c, http.StatusInternalServerError, err) + return + } + } else { + err := kubeclient.CreateService(service) + if err != nil { + fail(c, http.StatusInternalServerError, err) + return + } } ok(c, "") diff --git a/src/backend/dispatch-k8s-manager/pkg/kubeclient/ingress.go b/src/backend/dispatch-k8s-manager/pkg/kubeclient/ingress.go index d41ad02777c..20241fdb9f7 100644 --- a/src/backend/dispatch-k8s-manager/pkg/kubeclient/ingress.go +++ b/src/backend/dispatch-k8s-manager/pkg/kubeclient/ingress.go @@ -20,6 +20,19 @@ func CreateIngress(ingress *networkv1.Ingress) error { return nil } +func UpdateIngress(ingress *networkv1.Ingress) error { + _, err := kubeClient.NetworkingV1().Ingresses(config.Config.Kubernetes.NameSpace).Update( + context.TODO(), + ingress, + metav1.UpdateOptions{}, + ) + + if err != nil { + return err + } + return nil +} + func DeleteIngress(ingressName string) error { return kubeClient.NetworkingV1().Ingresses(config.Config.Kubernetes.NameSpace).Delete( context.TODO(), diff --git a/src/backend/dispatch-k8s-manager/pkg/kubeclient/service.go b/src/backend/dispatch-k8s-manager/pkg/kubeclient/service.go index 9eee56e9067..dcfa9808292 100644 --- a/src/backend/dispatch-k8s-manager/pkg/kubeclient/service.go +++ b/src/backend/dispatch-k8s-manager/pkg/kubeclient/service.go @@ -20,6 +20,19 @@ func CreateService(service *corev1.Service) error { return nil } +func UpdateService(service *corev1.Service) error { + _, err := kubeClient.CoreV1().Services(config.Config.Kubernetes.NameSpace).Update( + context.TODO(), + service, + metav1.UpdateOptions{}, + ) + + if err != nil { + return err + } + return nil +} + func DeleteService(serviceName string) error { return kubeClient.CoreV1().Services(config.Config.Kubernetes.NameSpace).Delete( context.TODO(), From 4796d24a783c9aa6ff522e019dc0d59d6e1be1ac Mon Sep 17 00:00:00 2001 From: sawyersong Date: Fri, 31 May 2024 16:12:33 +0800 Subject: [PATCH 14/23] =?UTF-8?q?feat=EF=BC=9A=E6=95=B4=E5=90=88=E5=BE=AE?= =?UTF-8?q?=E6=8B=93=E5=B1=95=E8=B5=84=E6=BA=90=E8=B0=83=E5=BA=A6=E8=83=BD?= =?UTF-8?q?=E5=8A=9B=20#10122?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pkg/apiserver/apis/open_deployment.go | 22 +++++++++++-------- .../pkg/apiserver/apis/open_ingress.go | 15 ++++++++----- .../pkg/apiserver/apis/open_secret.go | 11 ++++++---- .../pkg/apiserver/apis/open_service.go | 15 ++++++++----- .../pkg/kubeclient/deployment.go | 20 ++++++++++++----- .../pkg/kubeclient/ingress.go | 17 +++++++------- .../pkg/kubeclient/secret.go | 18 +++++++++++---- .../pkg/kubeclient/service.go | 17 +++++++------- 8 files changed, 82 insertions(+), 53 deletions(-) diff --git a/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_deployment.go b/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_deployment.go index d34417d6bbf..f52b38fa388 100644 --- a/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_deployment.go +++ b/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_deployment.go @@ -11,7 +11,7 @@ import ( ) const ( - deploymentPrefix = "/deployments" + deploymentPrefix = "/namespace/:namespace/deployments" ) func initDeploymentApis(r *gin.RouterGroup) { @@ -32,13 +32,14 @@ func initDeploymentApis(r *gin.RouterGroup) { // @Success 200 {object} types.Result{data=appsv1.Deployment} "deployment详情" // @Router /deployment/{deploymentName} [get] func getDeployment(c *gin.Context) { + namespace := c.Param("namespace") deploymentName := c.Param("deploymentName") - if !checkDeploymentName(c, deploymentName) { + if !checkDeploymentParams(c, deploymentName) { return } - deployment, err := kubeclient.GetDeployment(deploymentName) + deployment, err := kubeclient.GetNativeDeployment(namespace, deploymentName) if err != nil { okFail(c, http.StatusInternalServerError, err) return @@ -56,6 +57,8 @@ func getDeployment(c *gin.Context) { // @Success 200 {object} "" // @Router /deployment [post] func createDeployment(c *gin.Context) { + namespace := c.Param("namespace") + deployment := &appsv1.Deployment{} if err := c.BindJSON(deployment); err != nil { @@ -63,17 +66,17 @@ func createDeployment(c *gin.Context) { return } - deploymentInfo, _ := kubeclient.GetDeployment(deployment.Name) + deploymentInfo, _ := kubeclient.GetNativeDeployment(namespace, deployment.Name) if deploymentInfo != nil { logs.Info(fmt.Sprintf("Deployment: %s exist, update.", deployment.Name)) - updateErr := kubeclient.UpdateNativeDeployment(deployment) + updateErr := kubeclient.UpdateNativeDeployment(namespace, deployment) if updateErr != nil { fail(c, http.StatusInternalServerError, updateErr) return } } else { logs.Info(fmt.Sprintf("Deployment: %s not exist, create.", deployment.Name)) - createErr := kubeclient.CreateNativeDeployment(deployment) + createErr := kubeclient.CreateNativeDeployment(namespace, deployment) if createErr != nil { fail(c, http.StatusInternalServerError, createErr) return @@ -92,13 +95,14 @@ func createDeployment(c *gin.Context) { // @Success 200 {object} types.Result{data=""} "" // @Router /deployment/{deploymentName} [delete] func deleteDeployment(c *gin.Context) { + namespace := c.Param("namespace") deploymentName := c.Param("deploymentName") - if !checkDeploymentName(c, deploymentName) { + if !checkDeploymentParams(c, deploymentName) { return } - err := kubeclient.DeleteDeployment(deploymentName) + err := kubeclient.DeleteNativeDeployment(namespace, deploymentName) if err != nil { fail(c, http.StatusInternalServerError, err) return @@ -107,7 +111,7 @@ func deleteDeployment(c *gin.Context) { ok(c, "") } -func checkDeploymentName(c *gin.Context, deploymentName string) bool { +func checkDeploymentParams(c *gin.Context, deploymentName string) bool { if deploymentName == "" { fail(c, http.StatusBadRequest, errors.New("deployment名称不能为空")) return false diff --git a/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_ingress.go b/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_ingress.go index 3d93674a508..055e5bf9343 100644 --- a/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_ingress.go +++ b/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_ingress.go @@ -9,7 +9,7 @@ import ( ) const ( - ingressPrefix = "/ingress" + ingressPrefix = "/namespace/:namespace/ingress" ) func initIngressApis(r *gin.RouterGroup) { @@ -30,13 +30,14 @@ func initIngressApis(r *gin.RouterGroup) { // @Success 200 {object} types.Result{data=networkv1.ingress} "ingress详情" // @Router /ingress/{ingressName} [get] func getIngress(c *gin.Context) { + namespace := c.Param("namespace") ingressName := c.Param("ingressName") if !checkIngressName(c, ingressName) { return } - ingress, err := kubeclient.GetIngress(ingressName) + ingress, err := kubeclient.GetIngress(namespace, ingressName) if err != nil { okFail(c, http.StatusInternalServerError, err) return @@ -54,6 +55,7 @@ func getIngress(c *gin.Context) { // @Success 200 {object} "" // @Router /ingress [post] func createIngress(c *gin.Context) { + namespace := c.Param("namespace") ingress := &networkv1.Ingress{} if err := c.BindJSON(ingress); err != nil { @@ -61,15 +63,15 @@ func createIngress(c *gin.Context) { return } - ingressInfo, _ := kubeclient.GetIngress(ingress.Name) + ingressInfo, _ := kubeclient.GetIngress(namespace, ingress.Name) if ingressInfo != nil { - err := kubeclient.UpdateIngress(ingress) + err := kubeclient.UpdateIngress(namespace, ingress) if err != nil { fail(c, http.StatusInternalServerError, err) return } } else { - err := kubeclient.CreateIngress(ingress) + err := kubeclient.CreateIngress(namespace, ingress) if err != nil { fail(c, http.StatusInternalServerError, err) return @@ -88,13 +90,14 @@ func createIngress(c *gin.Context) { // @Success 200 {object} types.Result{data=""} "" // @Router /ingress/{ingressName} [delete] func deleteIngress(c *gin.Context) { + namespace := c.Param("namespace") ingressName := c.Param("ingressName") if !checkIngressName(c, ingressName) { return } - err := kubeclient.DeleteIngress(ingressName) + err := kubeclient.DeleteIngress(namespace, ingressName) if err != nil { fail(c, http.StatusInternalServerError, err) return diff --git a/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_secret.go b/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_secret.go index 812653c7faa..9a3369d1106 100644 --- a/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_secret.go +++ b/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_secret.go @@ -9,7 +9,7 @@ import ( ) const ( - secretPrefix = "/secrets" + secretPrefix = "/namespace/:namespace/secrets" ) func initSecretApis(r *gin.RouterGroup) { @@ -30,13 +30,14 @@ func initSecretApis(r *gin.RouterGroup) { // @Success 200 {object} types.Result{data=corev1.secret} "secret详情" // @Router /secret/{secretName} [get] func getSecret(c *gin.Context) { + namespace := c.Param("namespace") secretName := c.Param("secretName") if !checkSecretName(c, secretName) { return } - secret, err := kubeclient.GetSecret(secretName) + secret, err := kubeclient.GetNativeSecret(namespace, secretName) if err != nil { okFail(c, http.StatusInternalServerError, err) return @@ -54,6 +55,7 @@ func getSecret(c *gin.Context) { // @Success 200 {object} "" // @Router /secret [post] func createSecret(c *gin.Context) { + namespace := c.Param("namespace") secret := &corev1.Secret{} if err := c.BindJSON(secret); err != nil { @@ -61,7 +63,7 @@ func createSecret(c *gin.Context) { return } - err := kubeclient.CreateNativeSecret(secret) + err := kubeclient.CreateNativeSecret(namespace, secret) if err != nil { fail(c, http.StatusInternalServerError, err) return @@ -79,13 +81,14 @@ func createSecret(c *gin.Context) { // @Success 200 {object} types.Result{data=""} "" // @Router /secret/{secretName} [delete] func deleteSecret(c *gin.Context) { + namespace := c.Param("namespace") secretName := c.Param("secretName") if !checkSecretName(c, secretName) { return } - err := kubeclient.DeleteSecret(secretName) + err := kubeclient.DeleteNativeSecret(namespace, secretName) if err != nil { fail(c, http.StatusInternalServerError, err) return diff --git a/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_service.go b/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_service.go index aeaaca9f36c..289df57b985 100644 --- a/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_service.go +++ b/src/backend/dispatch-k8s-manager/pkg/apiserver/apis/open_service.go @@ -9,7 +9,7 @@ import ( ) const ( - servicePrefix = "/services" + servicePrefix = "/namespace/:namespace/services" ) func initServiceApis(r *gin.RouterGroup) { @@ -30,13 +30,14 @@ func initServiceApis(r *gin.RouterGroup) { // @Success 200 {object} types.Result{data=appsv1.service} "service详情" // @Router /service/{serviceName} [get] func getService(c *gin.Context) { + namespace := c.Param("namespace") serviceName := c.Param("serviceName") if !checkServiceName(c, serviceName) { return } - service, err := kubeclient.GetService(serviceName) + service, err := kubeclient.GetService(namespace, serviceName) if err != nil { okFail(c, http.StatusInternalServerError, err) return @@ -54,6 +55,7 @@ func getService(c *gin.Context) { // @Success 200 {object} "" // @Router /service [post] func createService(c *gin.Context) { + namespace := c.Param("namespace") service := &corev1.Service{} if err := c.BindJSON(service); err != nil { @@ -61,15 +63,15 @@ func createService(c *gin.Context) { return } - serviceInfo, _ := kubeclient.GetService(service.Name) + serviceInfo, _ := kubeclient.GetService(namespace, service.Name) if serviceInfo != nil { - err := kubeclient.UpdateService(service) + err := kubeclient.UpdateService(namespace, service) if err != nil { fail(c, http.StatusInternalServerError, err) return } } else { - err := kubeclient.CreateService(service) + err := kubeclient.CreateService(namespace, service) if err != nil { fail(c, http.StatusInternalServerError, err) return @@ -88,13 +90,14 @@ func createService(c *gin.Context) { // @Success 200 {object} types.Result{data=""} "" // @Router /service/{serviceName} [delete] func deleteService(c *gin.Context) { + namespace := c.Param("namespace") serviceName := c.Param("serviceName") if !checkServiceName(c, serviceName) { return } - err := kubeclient.DeleteService(serviceName) + err := kubeclient.DeleteService(namespace, serviceName) if err != nil { fail(c, http.StatusInternalServerError, err) return diff --git a/src/backend/dispatch-k8s-manager/pkg/kubeclient/deployment.go b/src/backend/dispatch-k8s-manager/pkg/kubeclient/deployment.go index 7b434563c6b..804c81dfcde 100644 --- a/src/backend/dispatch-k8s-manager/pkg/kubeclient/deployment.go +++ b/src/backend/dispatch-k8s-manager/pkg/kubeclient/deployment.go @@ -96,8 +96,8 @@ func CreateDeployment(dep *Deployment) error { return nil } -func CreateNativeDeployment(deployment *appsv1.Deployment) error { - _, err := kubeClient.AppsV1().Deployments(config.Config.Kubernetes.NameSpace).Create( +func CreateNativeDeployment(namespace string, deployment *appsv1.Deployment) error { + _, err := kubeClient.AppsV1().Deployments(namespace).Create( context.TODO(), deployment, metav1.CreateOptions{}, @@ -109,8 +109,8 @@ func CreateNativeDeployment(deployment *appsv1.Deployment) error { return nil } -func UpdateNativeDeployment(deployment *appsv1.Deployment) error { - _, err := kubeClient.AppsV1().Deployments(config.Config.Kubernetes.NameSpace).Update( +func UpdateNativeDeployment(namespace string, deployment *appsv1.Deployment) error { + _, err := kubeClient.AppsV1().Deployments(namespace).Update( context.TODO(), deployment, metav1.UpdateOptions{}, @@ -168,8 +168,8 @@ func ListDeployment(workloadCoreLabel string) ([]*appsv1.Deployment, error) { return list, nil } -func GetDeployment(deploymentName string) (*appsv1.Deployment, error) { - deployment, err := kubeClient.AppsV1().Deployments(config.Config.Kubernetes.NameSpace).Get( +func GetNativeDeployment(namespace string, deploymentName string) (*appsv1.Deployment, error) { + deployment, err := kubeClient.AppsV1().Deployments(namespace).Get( context.TODO(), deploymentName, metav1.GetOptions{}, @@ -181,3 +181,11 @@ func GetDeployment(deploymentName string) (*appsv1.Deployment, error) { return deployment, nil } + +func DeleteNativeDeployment(namespace string, deploymentName string) error { + return kubeClient.AppsV1().Deployments(namespace).Delete( + context.TODO(), + deploymentName, + metav1.DeleteOptions{}, + ) +} diff --git a/src/backend/dispatch-k8s-manager/pkg/kubeclient/ingress.go b/src/backend/dispatch-k8s-manager/pkg/kubeclient/ingress.go index 20241fdb9f7..0668fc223a2 100644 --- a/src/backend/dispatch-k8s-manager/pkg/kubeclient/ingress.go +++ b/src/backend/dispatch-k8s-manager/pkg/kubeclient/ingress.go @@ -2,13 +2,12 @@ package kubeclient import ( "context" - "disaptch-k8s-manager/pkg/config" networkv1 "k8s.io/api/networking/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -func CreateIngress(ingress *networkv1.Ingress) error { - _, err := kubeClient.NetworkingV1().Ingresses(config.Config.Kubernetes.NameSpace).Create( +func CreateIngress(namespace string, ingress *networkv1.Ingress) error { + _, err := kubeClient.NetworkingV1().Ingresses(namespace).Create( context.TODO(), ingress, metav1.CreateOptions{}, @@ -20,8 +19,8 @@ func CreateIngress(ingress *networkv1.Ingress) error { return nil } -func UpdateIngress(ingress *networkv1.Ingress) error { - _, err := kubeClient.NetworkingV1().Ingresses(config.Config.Kubernetes.NameSpace).Update( +func UpdateIngress(namespace string, ingress *networkv1.Ingress) error { + _, err := kubeClient.NetworkingV1().Ingresses(namespace).Update( context.TODO(), ingress, metav1.UpdateOptions{}, @@ -33,16 +32,16 @@ func UpdateIngress(ingress *networkv1.Ingress) error { return nil } -func DeleteIngress(ingressName string) error { - return kubeClient.NetworkingV1().Ingresses(config.Config.Kubernetes.NameSpace).Delete( +func DeleteIngress(namespace string, ingressName string) error { + return kubeClient.NetworkingV1().Ingresses(namespace).Delete( context.TODO(), ingressName, metav1.DeleteOptions{}, ) } -func GetIngress(ingressName string) (*networkv1.Ingress, error) { - ingress, err := kubeClient.NetworkingV1().Ingresses(config.Config.Kubernetes.NameSpace).Get( +func GetIngress(namespace string, ingressName string) (*networkv1.Ingress, error) { + ingress, err := kubeClient.NetworkingV1().Ingresses(namespace).Get( context.TODO(), ingressName, metav1.GetOptions{}, diff --git a/src/backend/dispatch-k8s-manager/pkg/kubeclient/secret.go b/src/backend/dispatch-k8s-manager/pkg/kubeclient/secret.go index 0a38dd9352b..0ef18fe7ecc 100644 --- a/src/backend/dispatch-k8s-manager/pkg/kubeclient/secret.go +++ b/src/backend/dispatch-k8s-manager/pkg/kubeclient/secret.go @@ -44,9 +44,9 @@ func CreateDockerRegistry(dockerSecret *DockerSecret) (*corev1.Secret, error) { return s, nil } -func CreateNativeSecret(secret *corev1.Secret) error { +func CreateNativeSecret(namespace string, secret *corev1.Secret) error { _, err := kubeClient.CoreV1(). - Secrets(config.Config.Kubernetes.NameSpace). + Secrets(namespace). Create(context.TODO(), secret, metav1.CreateOptions{}) if err != nil { return err @@ -107,8 +107,8 @@ func ListSecret(workloadCoreLabel string) ([]corev1.Secret, error) { return l.Items, nil } -func GetSecret(secretName string) (*corev1.Secret, error) { - l, err := kubeClient.CoreV1().Secrets(config.Config.Kubernetes.NameSpace).Get( +func GetNativeSecret(namespace string, secretName string) (*corev1.Secret, error) { + l, err := kubeClient.CoreV1().Secrets(namespace).Get( context.TODO(), secretName, metav1.GetOptions{}, @@ -119,6 +119,16 @@ func GetSecret(secretName string) (*corev1.Secret, error) { return l, nil } +func DeleteNativeSecret(namespace string, secretName string) error { + if err := kubeClient.CoreV1(). + Secrets(namespace). + Delete(context.TODO(), secretName, metav1.DeleteOptions{}); err != nil { + return err + } + + return nil +} + func DeleteSecret(secretName string) error { if err := kubeClient.CoreV1(). Secrets(config.Config.Kubernetes.NameSpace). diff --git a/src/backend/dispatch-k8s-manager/pkg/kubeclient/service.go b/src/backend/dispatch-k8s-manager/pkg/kubeclient/service.go index dcfa9808292..37ab318d9e7 100644 --- a/src/backend/dispatch-k8s-manager/pkg/kubeclient/service.go +++ b/src/backend/dispatch-k8s-manager/pkg/kubeclient/service.go @@ -2,13 +2,12 @@ package kubeclient import ( "context" - "disaptch-k8s-manager/pkg/config" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -func CreateService(service *corev1.Service) error { - _, err := kubeClient.CoreV1().Services(config.Config.Kubernetes.NameSpace).Create( +func CreateService(namespace string, service *corev1.Service) error { + _, err := kubeClient.CoreV1().Services(namespace).Create( context.TODO(), service, metav1.CreateOptions{}, @@ -20,8 +19,8 @@ func CreateService(service *corev1.Service) error { return nil } -func UpdateService(service *corev1.Service) error { - _, err := kubeClient.CoreV1().Services(config.Config.Kubernetes.NameSpace).Update( +func UpdateService(namespace string, service *corev1.Service) error { + _, err := kubeClient.CoreV1().Services(namespace).Update( context.TODO(), service, metav1.UpdateOptions{}, @@ -33,16 +32,16 @@ func UpdateService(service *corev1.Service) error { return nil } -func DeleteService(serviceName string) error { - return kubeClient.CoreV1().Services(config.Config.Kubernetes.NameSpace).Delete( +func DeleteService(namespace string, serviceName string) error { + return kubeClient.CoreV1().Services(namespace).Delete( context.TODO(), serviceName, metav1.DeleteOptions{}, ) } -func GetService(serviceName string) (*corev1.Service, error) { - service, err := kubeClient.CoreV1().Services(config.Config.Kubernetes.NameSpace).Get( +func GetService(namespace string, serviceName string) (*corev1.Service, error) { + service, err := kubeClient.CoreV1().Services(namespace).Get( context.TODO(), serviceName, metav1.GetOptions{}, From 8d6b288b677d6640115e6b8d0381cccce7d9b18a Mon Sep 17 00:00:00 2001 From: sawyersong Date: Fri, 31 May 2024 16:58:17 +0800 Subject: [PATCH 15/23] =?UTF-8?q?feat=EF=BC=9A=E6=95=B4=E5=90=88=E5=BE=AE?= =?UTF-8?q?=E6=8B=93=E5=B1=95=E8=B5=84=E6=BA=90=E8=B0=83=E5=BA=A6=E8=83=BD?= =?UTF-8?q?=E5=8A=9B=20#10122?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kubernetes/client/DeploymentClient.kt | 19 ++++++++++++----- .../kubernetes/client/IngressClient.kt | 19 ++++++++++++----- .../kubernetes/client/SecretClient.kt | 19 ++++++++++++----- .../kubernetes/client/ServiceClient.kt | 21 +++++++++++++------ 4 files changed, 57 insertions(+), 21 deletions(-) diff --git a/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/DeploymentClient.kt b/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/DeploymentClient.kt index 551bf4486f1..6eb1cf4505c 100644 --- a/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/DeploymentClient.kt +++ b/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/DeploymentClient.kt @@ -51,9 +51,10 @@ class DeploymentClient @Autowired constructor( fun createDeployment( userId: String, + namespace: String, deployment: Deployment ): KubernetesResult { - val url = "/api/deployments" + val url = "/api/namespace/$namespace/deployments" val body = JsonUtil.toJson(deployment) logger.info("Create deployment request url: $url, body: $body") val request = clientCommon.microBaseRequest(url).post( @@ -67,8 +68,12 @@ class DeploymentClient @Autowired constructor( return JsonUtil.getObjectMapper().readValue(responseBody) } - fun getDeploymentByName(userId: String, deploymentName: String): KubernetesResult { - val url = "/api/deployments/$deploymentName" + fun getDeploymentByName( + userId: String, + namespace: String, + deploymentName: String + ): KubernetesResult { + val url = "/api/namespace/$namespace/deployments/$deploymentName" val request = clientCommon.microBaseRequest(url).get().build() logger.info("Get deployment: $deploymentName request url: $url, userId: $userId") OkhttpUtils.doHttp(request).use { response -> @@ -86,8 +91,12 @@ class DeploymentClient @Autowired constructor( } } - fun deleteDeploymentByName(userId: String, deploymentName: String): KubernetesResult { - val url = "/api/deployments/$deploymentName" + fun deleteDeploymentByName( + userId: String, + namespace: String, + deploymentName: String + ): KubernetesResult { + val url = "/api/namespace/$namespace/deployments/$deploymentName" val request = clientCommon.microBaseRequest(url).delete().build() logger.info("Delete deployment: $deploymentName request url: $url, userId: $userId") OkhttpUtils.doHttp(request).use { response -> diff --git a/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/IngressClient.kt b/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/IngressClient.kt index ee9b682af76..deb5e19169b 100644 --- a/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/IngressClient.kt +++ b/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/IngressClient.kt @@ -52,9 +52,10 @@ class IngressClient @Autowired constructor( fun createIngress( userId: String, + namespace: String, ingress: Ingress ): KubernetesResult { - val url = "/api/ingress" + val url = "/api/namespace/$namespace/ingress" val body = JsonUtil.toJson(ingress) logger.info("Create ingress request url: $url, body: $body") val request = clientCommon.microBaseRequest(url).post( @@ -68,8 +69,12 @@ class IngressClient @Autowired constructor( return JsonUtil.getObjectMapper().readValue(responseBody) } - fun getIngressByName(userId: String, ingressName: String): KubernetesResult { - val url = "/api/ingress/$ingressName" + fun getIngressByName( + userId: String, + namespace: String, + ingressName: String + ): KubernetesResult { + val url = "/api/namespace/$namespace/ingress/$ingressName" val request = clientCommon.microBaseRequest(url).get().build() logger.info("Get ingress: $ingressName request url: $url, userId: $userId") OkhttpUtils.doHttp(request).use { response -> @@ -87,8 +92,12 @@ class IngressClient @Autowired constructor( } } - fun deleteIngressByName(userId: String, ingressName: String): KubernetesResult { - val url = "/api/ingress/$ingressName" + fun deleteIngressByName( + userId: String, + namespace: String, + ingressName: String + ): KubernetesResult { + val url = "/api/namespace/$namespace/ingress/$ingressName" val request = clientCommon.microBaseRequest(url).delete().build() logger.info("Delete ingress: $ingressName request url: $url, userId: $userId") OkhttpUtils.doHttp(request).use { response -> diff --git a/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/SecretClient.kt b/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/SecretClient.kt index 02dfff630be..6e28e52e1a6 100644 --- a/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/SecretClient.kt +++ b/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/SecretClient.kt @@ -52,9 +52,10 @@ class SecretClient @Autowired constructor( fun createSecret( userId: String, + namespace: String, secret: Secret ): KubernetesResult { - val url = "/api/secrets" + val url = "/api/namespace/$namespace/secrets" val body = JsonUtil.toJson(secret) logger.info("Create secret request url: $url, body: $body") val request = clientCommon.microBaseRequest(url).post( @@ -68,8 +69,12 @@ class SecretClient @Autowired constructor( return JsonUtil.getObjectMapper().readValue(responseBody) } - fun getSecretByName(userId: String, secretName: String): KubernetesResult { - val url = "/api/secrets/$secretName" + fun getSecretByName( + userId: String, + namespace: String, + secretName: String + ): KubernetesResult { + val url = "/api/namespace/$namespace/secrets/$secretName" val request = clientCommon.microBaseRequest(url).get().build() logger.info("Get secret: $secretName request url: $url, userId: $userId") OkhttpUtils.doHttp(request).use { response -> @@ -87,8 +92,12 @@ class SecretClient @Autowired constructor( } } - fun deleteSecretByName(userId: String, secretName: String): KubernetesResult { - val url = "/api/secrets/$secretName" + fun deleteSecretByName( + userId: String, + namespace: String, + secretName: String + ): KubernetesResult { + val url = "/api/namespace/$namespace/secrets/$secretName" val request = clientCommon.microBaseRequest(url).delete().build() logger.info("Delete secret: $secretName request url: $url, userId: $userId") OkhttpUtils.doHttp(request).use { response -> diff --git a/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/ServiceClient.kt b/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/ServiceClient.kt index db8f12c0730..826da1da8a8 100644 --- a/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/ServiceClient.kt +++ b/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/ServiceClient.kt @@ -51,11 +51,12 @@ class ServiceClient @Autowired constructor( fun createService( userId: String, + namespace: String, service: Service ): KubernetesResult { - val url = "/api/services" + val url = "/api/namespace/$namespace/services" val body = JsonUtil.toJson(service) - logger.info("Create service request url: $url, body: $body") + logger.info("$userId Create service request url: $url, body: $body") val request = clientCommon.microBaseRequest(url).post( RequestBody.create( "application/json; charset=utf-8".toMediaTypeOrNull(), @@ -67,8 +68,12 @@ class ServiceClient @Autowired constructor( return JsonUtil.getObjectMapper().readValue(responseBody) } - fun getServiceByName(userId: String, serviceName: String): KubernetesResult { - val url = "/api/services/$serviceName" + fun getServiceByName( + userId: String, + namespace: String, + serviceName: String + ): KubernetesResult { + val url = "/api/namespace/$namespace/services/$serviceName" val request = clientCommon.microBaseRequest(url).get().build() logger.info("Get service: $serviceName request url: $url, userId: $userId") OkhttpUtils.doHttp(request).use { response -> @@ -86,8 +91,12 @@ class ServiceClient @Autowired constructor( } } - fun deleteServiceByName(userId: String, serviceName: String): KubernetesResult { - val url = "/api/services/$serviceName" + fun deleteServiceByName( + userId: String, + namespace: String, + serviceName: String + ): KubernetesResult { + val url = "/api/namespace/$namespace/services/$serviceName" val request = clientCommon.microBaseRequest(url).delete().build() logger.info("Delete service: $serviceName request url: $url, userId: $userId") OkhttpUtils.doHttp(request).use { response -> From 7a357a92c90694f512f4d0b694838f0cafd26e4e Mon Sep 17 00:00:00 2001 From: yjieliang Date: Tue, 4 Jun 2024 10:20:33 +0800 Subject: [PATCH 16/23] =?UTF-8?q?feat=EF=BC=9A=E6=95=B4=E5=90=88=E5=BE=AE?= =?UTF-8?q?=E6=8B=93=E5=B1=95=E8=B5=84=E6=BA=90=E8=B0=83=E5=BA=A6=E8=83=BD?= =?UTF-8?q?=E5=8A=9B=20#10122?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/com/tencent/devops/store/constant/StoreMessageCode.kt | 1 + support-files/i18n/store/message_en_US.properties | 1 + support-files/i18n/store/message_zh_CN.properties | 1 + 3 files changed, 3 insertions(+) diff --git a/src/backend/ci/core/store/api-store/src/main/kotlin/com/tencent/devops/store/constant/StoreMessageCode.kt b/src/backend/ci/core/store/api-store/src/main/kotlin/com/tencent/devops/store/constant/StoreMessageCode.kt index 878fe7caff4..377c9b2e4fa 100644 --- a/src/backend/ci/core/store/api-store/src/main/kotlin/com/tencent/devops/store/constant/StoreMessageCode.kt +++ b/src/backend/ci/core/store/api-store/src/main/kotlin/com/tencent/devops/store/constant/StoreMessageCode.kt @@ -125,6 +125,7 @@ object StoreMessageCode { const val USER_SERVICE_PROJECT_NOT_PERMISSION = "2120408" // 研发商店:选中调试项目无创建流水线权限 const val USER_SERVICE_NOT_EXIST = "2120409" // 研发商店:扩展服务不存在{0} const val USER_ITEM_SERVICE_USED_IS_NOT_ALLOW_DELETE = "2120410" // 研发商店:扩展点下还有可用的扩展服务,不能删除 + const val USER_SERVICE_NOT_DEPLOY = "2120411" // 研发商店:用户扩展服务未部署 const val USER_PRAISE_IS_INVALID = "2120901" // 研发商店:你已点赞过 const val USER_PROJECT_IS_NOT_ALLOW_INSTALL = "2120902" // 研发商店:你没有权限将组件安装到项目:{0} diff --git a/support-files/i18n/store/message_en_US.properties b/support-files/i18n/store/message_en_US.properties index 70afca4cd8e..5a97035c4d0 100644 --- a/support-files/i18n/store/message_en_US.properties +++ b/support-files/i18n/store/message_en_US.properties @@ -69,6 +69,7 @@ 2120408=store: selected debugging project does not have the permission to create assembly line 2120409=store: extension service does not exist {0} 2120410=store: extension services are available under the extension point and cannot be deleted +2120411=Store: User Extension service is not deployed 2120901=store: you have already liked it. 2120902=store: you do not have permission to install components to the project: {0} 2120903=Store: you have already commented, so you can no longer add comments. But you can modify the original comments. diff --git a/support-files/i18n/store/message_zh_CN.properties b/support-files/i18n/store/message_zh_CN.properties index b75bb4073ef..f8f984477a2 100644 --- a/support-files/i18n/store/message_zh_CN.properties +++ b/support-files/i18n/store/message_zh_CN.properties @@ -69,6 +69,7 @@ 2120408=研发商店: 选中调试项目无创建流水线权限 2120409=研发商店: 微扩展不存在{0} 2120410=研发商店: 扩展点下还有可用的微扩展,不能删除 +2120411=研发商店: 用户扩展服务未部署 2120901=研发商店: 你已点赞过 2120902=研发商店: 你没有权限将组件安装到项目: {0} 2120903=研发商店: 你已评论过,无法继续添加评论。但可以修改原有评论 From b3748e2cc5be6750f0b19794ef09c4130a22bc1f Mon Sep 17 00:00:00 2001 From: yjieliang Date: Mon, 15 Apr 2024 18:22:02 +0800 Subject: [PATCH 17/23] =?UTF-8?q?feat=EF=BC=9A=E6=95=B4=E5=90=88=E5=BE=AE?= =?UTF-8?q?=E6=8B=93=E5=B1=95=E8=B5=84=E6=BA=90=E8=B0=83=E5=BA=A6=E8=83=BD?= =?UTF-8?q?=E5=8A=9B=20#10122?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/KubernetesBeanConfiguration.kt | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/config/KubernetesBeanConfiguration.kt b/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/config/KubernetesBeanConfiguration.kt index 9d28886d8d8..fed7be9143e 100644 --- a/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/config/KubernetesBeanConfiguration.kt +++ b/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/config/KubernetesBeanConfiguration.kt @@ -35,10 +35,14 @@ import com.tencent.devops.dispatch.kubernetes.bcs.client.BcsJobClient import com.tencent.devops.dispatch.kubernetes.bcs.client.BcsTaskClient import com.tencent.devops.dispatch.kubernetes.bcs.service.BcsContainerService import com.tencent.devops.dispatch.kubernetes.bcs.service.BcsJobService +import com.tencent.devops.dispatch.kubernetes.client.DeploymentClient +import com.tencent.devops.dispatch.kubernetes.client.IngressClient import com.tencent.devops.dispatch.kubernetes.client.KubernetesBuilderClient import com.tencent.devops.dispatch.kubernetes.client.KubernetesClientCommon import com.tencent.devops.dispatch.kubernetes.client.KubernetesJobClient import com.tencent.devops.dispatch.kubernetes.client.KubernetesTaskClient +import com.tencent.devops.dispatch.kubernetes.client.SecretClient +import com.tencent.devops.dispatch.kubernetes.client.ServiceClient import com.tencent.devops.dispatch.kubernetes.components.LogsPrinter import com.tencent.devops.dispatch.kubernetes.interfaces.CommonService import com.tencent.devops.dispatch.kubernetes.service.CoreCommonService @@ -165,4 +169,36 @@ class KubernetesBeanConfiguration { fun bcsJobService( bcsJobClient: BcsJobClient ) = BcsJobService(bcsJobClient) + + @Bean + @Primary + @ConditionalOnProperty(prefix = "kubernetes", name = ["enable"], havingValue = "true") + fun deploymentClient( + objectMapper: ObjectMapper, + clientCommon: KubernetesClientCommon + ) = DeploymentClient(objectMapper, clientCommon) + + @Bean + @Primary + @ConditionalOnProperty(prefix = "kubernetes", name = ["enable"], havingValue = "true") + fun ingressClient( + objectMapper: ObjectMapper, + clientCommon: KubernetesClientCommon + ) = IngressClient(objectMapper, clientCommon) + + @Bean + @Primary + @ConditionalOnProperty(prefix = "kubernetes", name = ["enable"], havingValue = "true") + fun secretClient( + objectMapper: ObjectMapper, + clientCommon: KubernetesClientCommon + ) = SecretClient(objectMapper, clientCommon) + + @Bean + @Primary + @ConditionalOnProperty(prefix = "kubernetes", name = ["enable"], havingValue = "true") + fun serviceClient( + objectMapper: ObjectMapper, + clientCommon: KubernetesClientCommon + ) = ServiceClient(objectMapper, clientCommon) } From 1ce8c6d112eb68de907ee782583b81e782a76875 Mon Sep 17 00:00:00 2001 From: yjieliang Date: Mon, 15 Apr 2024 19:28:26 +0800 Subject: [PATCH 18/23] =?UTF-8?q?feat=EF=BC=9A=E6=95=B4=E5=90=88=E5=BE=AE?= =?UTF-8?q?=E6=8B=93=E5=B1=95=E8=B5=84=E6=BA=90=E8=B0=83=E5=BA=A6=E8=83=BD?= =?UTF-8?q?=E5=8A=9B=20#10122?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tencent/devops/dispatch/kubernetes/client/IngressClient.kt | 1 - .../tencent/devops/dispatch/kubernetes/client/SecretClient.kt | 1 - 2 files changed, 2 deletions(-) diff --git a/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/IngressClient.kt b/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/IngressClient.kt index deb5e19169b..48b86ab47af 100644 --- a/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/IngressClient.kt +++ b/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/IngressClient.kt @@ -34,7 +34,6 @@ import com.tencent.devops.common.api.util.OkhttpUtils import com.tencent.devops.common.dispatch.sdk.BuildFailureException import com.tencent.devops.dispatch.kubernetes.pojo.KubernetesResult import com.tencent.devops.dispatch.kubernetes.pojo.common.ErrorCodeEnum -import io.fabric8.kubernetes.api.model.Service import io.fabric8.kubernetes.api.model.networking.v1.Ingress import okhttp3.MediaType.Companion.toMediaTypeOrNull import okhttp3.RequestBody diff --git a/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/SecretClient.kt b/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/SecretClient.kt index 6e28e52e1a6..3590dafa062 100644 --- a/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/SecretClient.kt +++ b/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/SecretClient.kt @@ -35,7 +35,6 @@ import com.tencent.devops.common.dispatch.sdk.BuildFailureException import com.tencent.devops.dispatch.kubernetes.pojo.KubernetesResult import com.tencent.devops.dispatch.kubernetes.pojo.common.ErrorCodeEnum import io.fabric8.kubernetes.api.model.Secret -import io.fabric8.kubernetes.api.model.Service import okhttp3.MediaType.Companion.toMediaTypeOrNull import okhttp3.RequestBody import org.slf4j.LoggerFactory From 8219f7bc361a870db6cc18c3bf8b3c050bd3ffc0 Mon Sep 17 00:00:00 2001 From: yjieliang Date: Mon, 15 Apr 2024 20:28:35 +0800 Subject: [PATCH 19/23] =?UTF-8?q?feat=EF=BC=9A=E6=95=B4=E5=90=88=E5=BE=AE?= =?UTF-8?q?=E6=8B=93=E5=B1=95=E8=B5=84=E6=BA=90=E8=B0=83=E5=BA=A6=E8=83=BD?= =?UTF-8?q?=E5=8A=9B=20#10122?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../devops/dispatch/kubernetes/pojo/common/ErrorCodeEnum.kt | 5 +++++ support-files/i18n/dispatch/message_en_US.properties | 1 + support-files/i18n/dispatch/message_zh_CN.properties | 1 + 3 files changed, 7 insertions(+) diff --git a/src/backend/ci/core/dispatch/api-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/pojo/common/ErrorCodeEnum.kt b/src/backend/ci/core/dispatch/api-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/pojo/common/ErrorCodeEnum.kt index 5f7b282b3fd..ca84eb0f29f 100644 --- a/src/backend/ci/core/dispatch/api-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/pojo/common/ErrorCodeEnum.kt +++ b/src/backend/ci/core/dispatch/api-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/pojo/common/ErrorCodeEnum.kt @@ -303,6 +303,11 @@ enum class ErrorCodeEnum( ErrorType.USER, 2126054, "已超过DevCloud创建Job环境上限." + ), + KUBERNETES_CREATE_RESOURCE_FAIL( + ErrorType.SYSTEM, + 2126055, + "kubernetes创建{0}资源失败,原因:{1}" ); fun getErrorMessage(): String { diff --git a/support-files/i18n/dispatch/message_en_US.properties b/support-files/i18n/dispatch/message_en_US.properties index 1897caa230f..24caf3c1986 100644 --- a/support-files/i18n/dispatch/message_en_US.properties +++ b/support-files/i18n/dispatch/message_en_US.properties @@ -111,6 +111,7 @@ bkEnvWorkerErrorIgnore=The {0} node in the build machine environment failed to s 2126052=THIRD_PARTY_SERVICES_DEVCLOUD_EXCEPTIONS, EXCEPTION_INFORMATION: API request timed out 2126053=THIRD_PARTY_SERVICES_DEVCLOUD_EXCEPTIONS, EXCEPTION_INFORMATION: User operation exception 2126054=The upper limit of DevCloud creating Job environment has been exceeded. +2126055=failed to create {0} kubernetes resource,cause:{1} bcsBuilderContainerStatus.running=Container running bcsBuilderContainerStatus.terminated=The container has finished executing bcsBuilderContainerStatus.waiting=The container is pulling up diff --git a/support-files/i18n/dispatch/message_zh_CN.properties b/support-files/i18n/dispatch/message_zh_CN.properties index a2bdf9880f2..e395a005674 100644 --- a/support-files/i18n/dispatch/message_zh_CN.properties +++ b/support-files/i18n/dispatch/message_zh_CN.properties @@ -113,6 +113,7 @@ bkThirdJobNodeCurr=单节点上的并发限制,当前环境下每个节点运 2126052=第三方服务-DEVCLOUD 异常,异常信息 - 接口请求超时 2126053=第三方服务-DEVCLOUD 异常,异常信息 - 用户操作异常 2126054=已超过DevCloud创建Job环境上限. +2126055=kubernetes创建{0}资源失败,原因:{1} bcsBuilderContainerStatus.running=容器运行中 bcsBuilderContainerStatus.terminated=容器已经执行完 bcsBuilderContainerStatus.waiting=容器拉起中 From db95f677d0616b42cbd93901dffe68a1c19de97a Mon Sep 17 00:00:00 2001 From: yjieliang Date: Tue, 14 May 2024 10:23:28 +0800 Subject: [PATCH 20/23] =?UTF-8?q?feat=EF=BC=9A=E6=95=B4=E5=90=88=E5=BE=AE?= =?UTF-8?q?=E6=8B=93=E5=B1=95=E8=B5=84=E6=BA=90=E8=B0=83=E5=BA=A6=E8=83=BD?= =?UTF-8?q?=E5=8A=9B=20#10122?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kubernetes/pojo/common/KubernetesRepo.kt | 42 +++++++++++++++ .../kubernetes/client/SecretClient.kt | 54 +++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 src/backend/ci/core/dispatch/api-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/pojo/common/KubernetesRepo.kt diff --git a/src/backend/ci/core/dispatch/api-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/pojo/common/KubernetesRepo.kt b/src/backend/ci/core/dispatch/api-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/pojo/common/KubernetesRepo.kt new file mode 100644 index 00000000000..921f4dce26d --- /dev/null +++ b/src/backend/ci/core/dispatch/api-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/pojo/common/KubernetesRepo.kt @@ -0,0 +1,42 @@ +/* + * Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * BK-CI 蓝鲸持续集成平台 is licensed under the MIT license. + * + * A copy of the MIT License is included in this file. + * + * + * Terms of the MIT License: + * --------------------------------------------------- + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of + * the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT + * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN + * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package com.tencent.devops.dispatch.kubernetes.pojo.common + +import io.swagger.v3.oas.annotations.media.Schema + +@Schema(title = "k8s仓库信息") +data class KubernetesRepo( + @get:Schema(title = "仓库地址", required = true) + val registryUrl: String, + @get:Schema(title = "用户名", required = true) + val username: String, + @get:Schema(title = "密码", required = true) + val password: String, + @get:Schema(title = "邮箱", required = false) + val email: String? +) diff --git a/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/SecretClient.kt b/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/SecretClient.kt index 3590dafa062..86d3c4bf1c8 100644 --- a/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/SecretClient.kt +++ b/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/SecretClient.kt @@ -34,9 +34,12 @@ import com.tencent.devops.common.api.util.OkhttpUtils import com.tencent.devops.common.dispatch.sdk.BuildFailureException import com.tencent.devops.dispatch.kubernetes.pojo.KubernetesResult import com.tencent.devops.dispatch.kubernetes.pojo.common.ErrorCodeEnum +import com.tencent.devops.dispatch.kubernetes.pojo.common.KubernetesRepo import io.fabric8.kubernetes.api.model.Secret +import io.fabric8.kubernetes.api.model.SecretBuilder import okhttp3.MediaType.Companion.toMediaTypeOrNull import okhttp3.RequestBody +import org.apache.commons.codec.binary.Base64 import org.slf4j.LoggerFactory import org.springframework.beans.factory.annotation.Autowired @@ -113,4 +116,55 @@ class SecretClient @Autowired constructor( return objectMapper.readValue(responseContent) } } + + /** + * 创建k8s拉取镜像secret + * @param namespaceName 命名空间名称 + * @param secretName 秘钥名称 + * @param kubernetesRepoInfo k8s仓库信息 + */ + fun createImagePullSecret( + userId: String, + secretName: String, + namespaceName: String, + kubernetesRepoInfo: KubernetesRepo + ) { + var secret = getSecretByName(userId, secretName).data + logger.info("the secret is: $secret") + if (secret == null) { + val secretData: HashMap = HashMap(1) + val basicAuth = String( + Base64.encodeBase64("${kubernetesRepoInfo.username}:${kubernetesRepoInfo.password}".toByteArray()) + ) + var dockerCfg = String.format( + "{ " + + " \"auths\": { " + + " \"%s\": { " + + " \"username\": \"%s\", " + + " \"password\": \"%s\", " + + " \"email\": \"%s\", " + + " \"auth\": \"%s\" " + + " } " + + " } " + + "}", + kubernetesRepoInfo.registryUrl, + kubernetesRepoInfo.username, + kubernetesRepoInfo.password, + kubernetesRepoInfo.email, + basicAuth + ) + dockerCfg = String(Base64.encodeBase64(dockerCfg.toByteArray(Charsets.UTF_8)), Charsets.UTF_8) + secretData[".dockerconfigjson"] = dockerCfg + secret = SecretBuilder() + .withNewMetadata() + .withName(secretName) + .withNamespace(namespaceName) + .endMetadata() + .addToData(secretData) + .withType("kubernetes.io/dockerconfigjson") + .build() + createSecret(userId, secret) + logger.info("create new secret: $secret") + } + } } From 2a192f9b7d21fff30dcf3c11f58566d583389191 Mon Sep 17 00:00:00 2001 From: yjieliang Date: Tue, 14 May 2024 11:06:48 +0800 Subject: [PATCH 21/23] =?UTF-8?q?feat=EF=BC=9A=E6=95=B4=E5=90=88=E5=BE=AE?= =?UTF-8?q?=E6=8B=93=E5=B1=95=E8=B5=84=E6=BA=90=E8=B0=83=E5=BA=A6=E8=83=BD?= =?UTF-8?q?=E5=8A=9B=20#10122?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pojo/base/CreateImagePullSecretRequest.kt | 39 +++++++++++++++++++ .../pojo/{common => base}/KubernetesRepo.kt | 2 +- .../kubernetes/client/SecretClient.kt | 2 +- 3 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 src/backend/ci/core/dispatch/api-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/pojo/base/CreateImagePullSecretRequest.kt rename src/backend/ci/core/dispatch/api-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/pojo/{common => base}/KubernetesRepo.kt (97%) diff --git a/src/backend/ci/core/dispatch/api-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/pojo/base/CreateImagePullSecretRequest.kt b/src/backend/ci/core/dispatch/api-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/pojo/base/CreateImagePullSecretRequest.kt new file mode 100644 index 00000000000..769f45745f0 --- /dev/null +++ b/src/backend/ci/core/dispatch/api-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/pojo/base/CreateImagePullSecretRequest.kt @@ -0,0 +1,39 @@ +/* + * Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * BK-CI 蓝鲸持续集成平台 is licensed under the MIT license. + * + * A copy of the MIT License is included in this file. + * + * + * Terms of the MIT License: + * --------------------------------------------------- + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of + * the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT + * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN + * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package com.tencent.devops.dispatch.kubernetes.pojo.base + +import io.swagger.v3.oas.annotations.media.Schema + +data class CreateImagePullSecretRequest( + @get:Schema(title = "bcs请求路径", required = true) + val bcsUrl: String, + @get:Schema(title = "请求token", required = true) + val token: String, + @get:Schema(title = "k8s仓库信息", required = true) + val kubernetesRepo: KubernetesRepo +) diff --git a/src/backend/ci/core/dispatch/api-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/pojo/common/KubernetesRepo.kt b/src/backend/ci/core/dispatch/api-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/pojo/base/KubernetesRepo.kt similarity index 97% rename from src/backend/ci/core/dispatch/api-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/pojo/common/KubernetesRepo.kt rename to src/backend/ci/core/dispatch/api-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/pojo/base/KubernetesRepo.kt index 921f4dce26d..83c5a60f1ba 100644 --- a/src/backend/ci/core/dispatch/api-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/pojo/common/KubernetesRepo.kt +++ b/src/backend/ci/core/dispatch/api-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/pojo/base/KubernetesRepo.kt @@ -25,7 +25,7 @@ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -package com.tencent.devops.dispatch.kubernetes.pojo.common +package com.tencent.devops.dispatch.kubernetes.pojo.base import io.swagger.v3.oas.annotations.media.Schema diff --git a/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/SecretClient.kt b/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/SecretClient.kt index 86d3c4bf1c8..903710f31e2 100644 --- a/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/SecretClient.kt +++ b/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/SecretClient.kt @@ -33,8 +33,8 @@ import com.tencent.devops.common.api.util.JsonUtil import com.tencent.devops.common.api.util.OkhttpUtils import com.tencent.devops.common.dispatch.sdk.BuildFailureException import com.tencent.devops.dispatch.kubernetes.pojo.KubernetesResult +import com.tencent.devops.dispatch.kubernetes.pojo.base.KubernetesRepo import com.tencent.devops.dispatch.kubernetes.pojo.common.ErrorCodeEnum -import com.tencent.devops.dispatch.kubernetes.pojo.common.KubernetesRepo import io.fabric8.kubernetes.api.model.Secret import io.fabric8.kubernetes.api.model.SecretBuilder import okhttp3.MediaType.Companion.toMediaTypeOrNull From d0daf4a68986454f049a86740bbc9f2488eb714c Mon Sep 17 00:00:00 2001 From: yjieliang Date: Tue, 4 Jun 2024 10:41:07 +0800 Subject: [PATCH 22/23] =?UTF-8?q?feat=EF=BC=9A=E6=95=B4=E5=90=88=E5=BE=AE?= =?UTF-8?q?=E6=8B=93=E5=B1=95=E8=B5=84=E6=BA=90=E8=B0=83=E5=BA=A6=E8=83=BD?= =?UTF-8?q?=E5=8A=9B=20#10122?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../devops/dispatch/kubernetes/client/IngressClient.kt | 2 +- .../devops/dispatch/kubernetes/client/SecretClient.kt | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/IngressClient.kt b/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/IngressClient.kt index 48b86ab47af..9cf45037004 100644 --- a/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/IngressClient.kt +++ b/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/IngressClient.kt @@ -72,7 +72,7 @@ class IngressClient @Autowired constructor( userId: String, namespace: String, ingressName: String - ): KubernetesResult { + ): KubernetesResult { val url = "/api/namespace/$namespace/ingress/$ingressName" val request = clientCommon.microBaseRequest(url).get().build() logger.info("Get ingress: $ingressName request url: $url, userId: $userId") diff --git a/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/SecretClient.kt b/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/SecretClient.kt index 903710f31e2..17780a2c779 100644 --- a/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/SecretClient.kt +++ b/src/backend/ci/core/dispatch/biz-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/client/SecretClient.kt @@ -75,7 +75,7 @@ class SecretClient @Autowired constructor( userId: String, namespace: String, secretName: String - ): KubernetesResult { + ): KubernetesResult { val url = "/api/namespace/$namespace/secrets/$secretName" val request = clientCommon.microBaseRequest(url).get().build() logger.info("Get secret: $secretName request url: $url, userId: $userId") @@ -129,7 +129,7 @@ class SecretClient @Autowired constructor( namespaceName: String, kubernetesRepoInfo: KubernetesRepo ) { - var secret = getSecretByName(userId, secretName).data + var secret = getSecretByName(userId, namespaceName, secretName).data logger.info("the secret is: $secret") if (secret == null) { val secretData: HashMap = HashMap(1) @@ -163,7 +163,7 @@ class SecretClient @Autowired constructor( .addToData(secretData) .withType("kubernetes.io/dockerconfigjson") .build() - createSecret(userId, secret) + createSecret(userId, namespaceName, secret) logger.info("create new secret: $secret") } } From 3742686a0da12865b592fef1f554430ee7675c32 Mon Sep 17 00:00:00 2001 From: yjieliang Date: Tue, 4 Jun 2024 11:14:05 +0800 Subject: [PATCH 23/23] =?UTF-8?q?feat=EF=BC=9A=E6=95=B4=E5=90=88=E5=BE=AE?= =?UTF-8?q?=E6=8B=93=E5=B1=95=E8=B5=84=E6=BA=90=E8=B0=83=E5=BA=A6=E8=83=BD?= =?UTF-8?q?=E5=8A=9B=20#10122?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pojo/base/CreateImagePullSecretRequest.kt | 39 ------------------- 1 file changed, 39 deletions(-) delete mode 100644 src/backend/ci/core/dispatch/api-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/pojo/base/CreateImagePullSecretRequest.kt diff --git a/src/backend/ci/core/dispatch/api-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/pojo/base/CreateImagePullSecretRequest.kt b/src/backend/ci/core/dispatch/api-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/pojo/base/CreateImagePullSecretRequest.kt deleted file mode 100644 index 769f45745f0..00000000000 --- a/src/backend/ci/core/dispatch/api-dispatch-kubernetes/src/main/kotlin/com/tencent/devops/dispatch/kubernetes/pojo/base/CreateImagePullSecretRequest.kt +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available. - * - * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. - * - * BK-CI 蓝鲸持续集成平台 is licensed under the MIT license. - * - * A copy of the MIT License is included in this file. - * - * - * Terms of the MIT License: - * --------------------------------------------------- - * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated - * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all copies or substantial portions of - * the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT - * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN - * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, - * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -package com.tencent.devops.dispatch.kubernetes.pojo.base - -import io.swagger.v3.oas.annotations.media.Schema - -data class CreateImagePullSecretRequest( - @get:Schema(title = "bcs请求路径", required = true) - val bcsUrl: String, - @get:Schema(title = "请求token", required = true) - val token: String, - @get:Schema(title = "k8s仓库信息", required = true) - val kubernetesRepo: KubernetesRepo -)