From bb8daeac880109b61407707c424c5d6baa7aa77c Mon Sep 17 00:00:00 2001 From: chenzhihang <709011834@qq.com> Date: Thu, 24 Oct 2024 14:04:49 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=83=A8=E7=BD=B2?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- k8s/build.sh | 19 ------------------- k8s/build_and_deploy.sh | 28 +++++++++++++++++++++++----- 2 files changed, 23 insertions(+), 24 deletions(-) diff --git a/k8s/build.sh b/k8s/build.sh index a84e56f1..e8f19b3c 100755 --- a/k8s/build.sh +++ b/k8s/build.sh @@ -41,25 +41,6 @@ fi baseDir="/home/somuns/ci4s" cd ${baseDir} -# 拉取指定分支的最新代码 -echo "Checking out and pulling branch $branch..." - -git stash -git checkout $branch -if [ $? -ne 0 ]; then - echo "切换到分支 $branch 失败,请检查分支名称是否正确!" - exit 1 -fi - -git stash -git pull origin $branch -if [ $? -ne 0 ]; then - echo "拉取代码失败,请检查网络或联系管理员!" - exit 1 -fi - -chmod +777 ${baseDir}/k8s/*.sh - # 创建目录 mkdir -p ${baseDir}/k8s/dockerfiles/jar mkdir -p ${baseDir}/k8s/dockerfiles/html diff --git a/k8s/build_and_deploy.sh b/k8s/build_and_deploy.sh index eacc9c6b..8a9aee0d 100755 --- a/k8s/build_and_deploy.sh +++ b/k8s/build_and_deploy.sh @@ -3,11 +3,6 @@ #记录开始时间 startTime=$(date +%s) -# 登录到目标环境 -baseDir="/home/somuns/ci4s" -cd ${baseDir} - - #build # 默认参数 branch="master" @@ -50,6 +45,29 @@ if [[ ! " ${valid_envs[@]} " =~ " $env " ]]; then exit 1 fi +# 拉取指定分支的最新代码 +echo "Checking out and pulling branch $branch..." + +git stash +git checkout $branch +if [ $? -ne 0 ]; then + echo "切换到分支 $branch 失败,请检查分支名称是否正确!" + exit 1 +fi + +git stash +git pull origin $branch +if [ $? -ne 0 ]; then + echo "拉取代码失败,请检查网络或联系管理员!" + exit 1 +fi + +chmod +777 ${baseDir}/k8s/*.sh + +# 登录到目标环境 +baseDir="/home/somuns/ci4s" +cd ${baseDir} + echo "start build" sh ${baseDir}/k8s/build.sh -b ${branch} -s ${service} if [ $? -ne 0 ]; then From e355ae69dfc5b45121c2137d17194ecb996a91d9 Mon Sep 17 00:00:00 2001 From: chenzhihang <709011834@qq.com> Date: Tue, 29 Oct 2024 13:57:53 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=83=A8=E7=BD=B2?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- k8s/build_and_deploy.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/k8s/build_and_deploy.sh b/k8s/build_and_deploy.sh index 8a9aee0d..ef582cf3 100755 --- a/k8s/build_and_deploy.sh +++ b/k8s/build_and_deploy.sh @@ -3,6 +3,10 @@ #记录开始时间 startTime=$(date +%s) +# 登录到目标环境 +baseDir="/home/somuns/ci4s" +cd ${baseDir} + #build # 默认参数 branch="master" @@ -64,10 +68,6 @@ fi chmod +777 ${baseDir}/k8s/*.sh -# 登录到目标环境 -baseDir="/home/somuns/ci4s" -cd ${baseDir} - echo "start build" sh ${baseDir}/k8s/build.sh -b ${branch} -s ${service} if [ $? -ne 0 ]; then From 7206d8a838e715692ffcdd8cda06de9edc08035e Mon Sep 17 00:00:00 2001 From: chenzhihang <709011834@qq.com> Date: Tue, 29 Oct 2024 14:33:29 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E5=88=9D=E5=A7=8B=E7=8A=B6=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/ruoyi/platform/service/impl/ServiceServiceImpl.java | 1 + 1 file changed, 1 insertion(+) diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ServiceServiceImpl.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ServiceServiceImpl.java index 5cd35fb7..6075f9bc 100644 --- a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ServiceServiceImpl.java +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ServiceServiceImpl.java @@ -116,6 +116,7 @@ public class ServiceServiceImpl implements ServiceService { LoginUser loginUser = SecurityUtils.getLoginUser(); serviceVersion.setCreateBy(loginUser.getUsername()); serviceVersion.setUpdateBy(loginUser.getUsername()); + serviceVersion.setRunState(Constant.Pending); serviceDao.insertServiceVersion(serviceVersion); runServiceVersion(serviceVersion.getId()); return serviceVersion; From fb84e83c3df1de0db8de94c5a6ec6f018a92ffd4 Mon Sep 17 00:00:00 2001 From: chenzhihang <709011834@qq.com> Date: Wed, 30 Oct 2024 09:31:27 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E4=BF=AE=E6=94=B9docker=E5=AE=B9=E5=99=A8?= =?UTF-8?q?=E9=83=A8=E7=BD=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/ruoyi/platform/utils/K8sClientUtil.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/K8sClientUtil.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/K8sClientUtil.java index 14d4c795..e89b2e87 100644 --- a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/K8sClientUtil.java +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/K8sClientUtil.java @@ -4,6 +4,8 @@ import com.alibaba.fastjson2.JSON; import com.ruoyi.platform.constant.Constant; import com.ruoyi.platform.domain.DevEnvironment; import com.ruoyi.platform.mapper.ComputingResourceDao; +import io.fabric8.kubernetes.api.model.SecurityContext; +import io.fabric8.kubernetes.api.model.SecurityContextBuilder; import io.kubernetes.client.Exec; import io.kubernetes.client.custom.IntOrString; import io.kubernetes.client.custom.Quantity; @@ -624,7 +626,10 @@ public class K8sClientUtil { public V1Pod createPodWithEnv(String podName, String namespace, String proxyUrl, String mountPath, String pvcName, String image) { CoreV1Api api = new CoreV1Api(apiClient); - V1PodList v1PodList = null; + + V1SecurityContext v1SecurityContext = new V1SecurityContext(); + v1SecurityContext.setPrivileged(true); + V1Pod pod = new V1PodBuilder() .withNewMetadata() .withName(podName) @@ -633,6 +638,7 @@ public class K8sClientUtil { .addNewContainer() .withName(podName) .withImage(image) // 替换为您实际要使用的镜像名称 + .withSecurityContext(v1SecurityContext) .withVolumeMounts(new V1VolumeMount().name("workspace").mountPath(mountPath)) .withNewSecurityContext().withNewPrivileged(true).endSecurityContext() .addNewEnv() From b2f1e6b24fcef3dafb020ed795fbf42c1f92567b Mon Sep 17 00:00:00 2001 From: chenzhihang <709011834@qq.com> Date: Wed, 30 Oct 2024 15:43:33 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E9=9B=86=E7=BE=A4=E5=86=85=E7=9B=B4?= =?UTF-8?q?=E6=8E=A5=E8=AE=BF=E9=97=AEk8s=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/ruoyi/platform/utils/K8sClientUtil.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/K8sClientUtil.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/K8sClientUtil.java index e89b2e87..fae8b0bf 100644 --- a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/K8sClientUtil.java +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/K8sClientUtil.java @@ -4,8 +4,6 @@ import com.alibaba.fastjson2.JSON; import com.ruoyi.platform.constant.Constant; import com.ruoyi.platform.domain.DevEnvironment; import com.ruoyi.platform.mapper.ComputingResourceDao; -import io.fabric8.kubernetes.api.model.SecurityContext; -import io.fabric8.kubernetes.api.model.SecurityContextBuilder; import io.kubernetes.client.Exec; import io.kubernetes.client.custom.IntOrString; import io.kubernetes.client.custom.Quantity; @@ -15,6 +13,7 @@ import io.kubernetes.client.openapi.apis.AppsV1Api; import io.kubernetes.client.openapi.apis.CoreV1Api; import io.kubernetes.client.openapi.models.*; import io.kubernetes.client.util.ClientBuilder; +import io.kubernetes.client.util.Config; import io.kubernetes.client.util.credentials.AccessTokenAuthentication; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.StringUtils; @@ -64,9 +63,10 @@ public class K8sClientUtil { this.http = http; this.token = token; try { - this.apiClient = new ClientBuilder(). - setBasePath(http).setVerifyingSsl(false). - setAuthentication(new AccessTokenAuthentication(token)).build(); +// this.apiClient = new ClientBuilder(). +// setBasePath(http).setVerifyingSsl(false). +// setAuthentication(new AccessTokenAuthentication(token)).build(); + this.apiClient = Config.fromCluster(); } catch (Exception e) { log.error("构建K8s-Client异常", e); throw new RuntimeException("构建K8s-Client异常");