|
|
@@ -1,6 +1,7 @@
|
|
|
package seecoder.devcloud.core.pipeline.handler;
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import seecoder.devcloud.api.ApplicationProperties;
|
|
|
import seecoder.devcloud.api.k8s.DeploymentApi;
|
|
|
import seecoder.devcloud.api.k8s.K8sConstants;
|
|
|
import seecoder.devcloud.api.k8s.SecretApi;
|
|
|
@@ -33,18 +34,12 @@ public class K8sDeploymentHandler extends AbstractHandler {
|
|
|
|
|
|
private final DeploymentApi deploymentApi;
|
|
|
|
|
|
+ private final ApplicationProperties applicationProperties;
|
|
|
|
|
|
- //容器所使用的镜像
|
|
|
- private String image;
|
|
|
+ private final static String DEFAULT_IMAGE_PULL_SECRET_NAME = "seecoder-devcloud-image-pull-secret";
|
|
|
|
|
|
- //副本数
|
|
|
- private int replicas;
|
|
|
-
|
|
|
- //
|
|
|
- private int port;
|
|
|
-
|
|
|
-
|
|
|
- public K8sDeploymentHandler() {
|
|
|
+ public K8sDeploymentHandler(ApplicationProperties applicationProperties) {
|
|
|
+ this.applicationProperties = SpringUtil.getBean(ApplicationProperties.class);
|
|
|
secretApi = SpringUtil.getBean(SecretApi.class);
|
|
|
deploymentApi = SpringUtil.getBean(DeploymentApi.class);
|
|
|
}
|
|
|
@@ -58,8 +53,10 @@ public class K8sDeploymentHandler extends AbstractHandler {
|
|
|
@Override
|
|
|
public void process(Context context) {
|
|
|
//配置初始化
|
|
|
- initConfig(context);
|
|
|
- String groupName = context.getNamespace();
|
|
|
+ String imageName = context.getConfigs().getOrDefault("imageName", "");
|
|
|
+ int port = Integer.parseInt(context.getConfigs().getOrDefault("port", "8000"));
|
|
|
+ int replicas = Integer.parseInt(context.getConfigs().getOrDefault("replicas", "1"));
|
|
|
+ String namespace = context.getNamespace();
|
|
|
String projectName = context.getProjectName();
|
|
|
|
|
|
//配置容器暴露端口
|
|
|
@@ -69,7 +66,7 @@ public class K8sDeploymentHandler extends AbstractHandler {
|
|
|
|
|
|
//配置容器
|
|
|
Container container = Container.builder()
|
|
|
- .image(image)
|
|
|
+ .image(imageName)
|
|
|
.name(projectName + K8sConstants.CONTAINER_SUFFIX)
|
|
|
.ports(Collections.singleton(containerPort))
|
|
|
.build();
|
|
|
@@ -80,21 +77,22 @@ public class K8sDeploymentHandler extends AbstractHandler {
|
|
|
.containers(Collections.singletonList(container))
|
|
|
.build();
|
|
|
deployment.setName(projectName + K8sConstants.DEPLOYMENT_SUFFIX);
|
|
|
- deployment.setNamespace(groupName);
|
|
|
+ deployment.setNamespace(namespace);
|
|
|
deployment.setLabel(K8sConstants.APPLICATION_LABEL, projectName);
|
|
|
|
|
|
- // 密钥来源:在命名空间创建时,创建对应的secret存放密钥,见 K8sNamespaceHandler.class
|
|
|
- // 将registry密钥作为imagePullSecrets注入, 使得此deployment可以访问私有镜像仓库
|
|
|
- List<String> registrySecrets = secretApi
|
|
|
- .getSecretListByType(
|
|
|
- groupName,
|
|
|
- SecretTypeEnum.REGISTRY)
|
|
|
- .stream()
|
|
|
- .map(SecretVO::getName)
|
|
|
- .collect(Collectors.toList()
|
|
|
- );
|
|
|
- deployment.setImagePullSecrets(registrySecrets);
|
|
|
|
|
|
+ // 检查镜像仓库secret,没有则创建。 设置镜像镜像secret
|
|
|
+ SecretVO secretVO = secretApi.getSecretByName(namespace, DEFAULT_IMAGE_PULL_SECRET_NAME);
|
|
|
+ if (secretVO == null) {
|
|
|
+ secretApi.createPrivateRegistrySecret(
|
|
|
+ namespace,
|
|
|
+ DEFAULT_IMAGE_PULL_SECRET_NAME,
|
|
|
+ applicationProperties.getDocker().getRegistry(),
|
|
|
+ applicationProperties.getDocker().getRegistryUsername(),
|
|
|
+ applicationProperties.getDocker().getRegistryPassword()
|
|
|
+ );
|
|
|
+ }
|
|
|
+ deployment.setImagePullSecrets(Collections.singletonList(secretVO.getName()));
|
|
|
|
|
|
try {
|
|
|
deploymentApi.create(deployment);
|
|
|
@@ -109,9 +107,4 @@ public class K8sDeploymentHandler extends AbstractHandler {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private void initConfig(Context context) {
|
|
|
- this.image = context.getConfigs().getOrDefault("image", "");
|
|
|
- this.port = Integer.parseInt(context.getConfigs().getOrDefault("port", "8000"));
|
|
|
- this.replicas = Integer.parseInt(context.getConfigs().getOrDefault("replicas", "1"));
|
|
|
- }
|
|
|
}
|