|
@@ -0,0 +1,109 @@
|
|
|
|
|
+package nju.seec.SEECdemo.logic.api.k8s.vo;
|
|
|
|
|
+
|
|
|
|
|
+import io.kubernetes.client.models.*;
|
|
|
|
|
+import lombok.Data;
|
|
|
|
|
+import lombok.NoArgsConstructor;
|
|
|
|
|
+import org.hibernate.validator.constraints.Range;
|
|
|
|
|
+
|
|
|
|
|
+import javax.validation.constraints.NotEmpty;
|
|
|
|
|
+import javax.validation.constraints.NotNull;
|
|
|
|
|
+import java.util.*;
|
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
+
|
|
|
|
|
+import static nju.seec.SEECdemo.logic.api.k8s.vo.Constants.APPLICATION_LABEL;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * author: rale
|
|
|
|
|
+ * createdAt: 12/23/18
|
|
|
|
|
+ */
|
|
|
|
|
+@Data
|
|
|
|
|
+@NoArgsConstructor
|
|
|
|
|
+public class Deployment {
|
|
|
|
|
+
|
|
|
|
|
+ private static final String DEPLOYMENT_VERSION = "apps/v1";
|
|
|
|
|
+
|
|
|
|
|
+ private static final String DEPLOYMENT_KIND = "Deployment";
|
|
|
|
|
+
|
|
|
|
|
+ private static final String SERVICE_VERSION = "v1";
|
|
|
|
|
+
|
|
|
|
|
+ private static final String SERVICE_KIND = "Service";
|
|
|
|
|
+
|
|
|
|
|
+ @NotEmpty
|
|
|
|
|
+ private String name;
|
|
|
|
|
+
|
|
|
|
|
+ @NotNull
|
|
|
|
|
+ private String namespace;
|
|
|
|
|
+
|
|
|
|
|
+ //@todo 加上部署应用的类型,如数据库,web应用
|
|
|
|
|
+
|
|
|
|
|
+ @Range(min = 1, max = 3)
|
|
|
|
|
+ private int replicas;
|
|
|
|
|
+
|
|
|
|
|
+ private List<Container> containers = new ArrayList<>();
|
|
|
|
|
+
|
|
|
|
|
+ private Set<ServicePort> servicePorts = new HashSet<>();
|
|
|
|
|
+
|
|
|
|
|
+ public V1Deployment toV1Deployment(){
|
|
|
|
|
+ V1Deployment v1Deployment = new V1DeploymentBuilder()
|
|
|
|
|
+ .withApiVersion(DEPLOYMENT_VERSION)
|
|
|
|
|
+ .withKind(DEPLOYMENT_KIND)
|
|
|
|
|
+ .withMetadata(this.toV1ObjectMeta())
|
|
|
|
|
+ .withSpec(this.toV1DeploymentSpec())
|
|
|
|
|
+ .build();
|
|
|
|
|
+ return v1Deployment;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public V1Service toV1Service() {
|
|
|
|
|
+ V1Service v1Service = new V1ServiceBuilder()
|
|
|
|
|
+ .withApiVersion(SERVICE_VERSION)
|
|
|
|
|
+ .withKind(SERVICE_KIND)
|
|
|
|
|
+ .withMetadata(this.toV1ObjectMeta())
|
|
|
|
|
+ .withSpec(this.toV1ServiceSpec())
|
|
|
|
|
+ .build();
|
|
|
|
|
+ return v1Service;
|
|
|
|
|
+ }
|
|
|
|
|
+ private V1ObjectMeta toV1ObjectMeta(){
|
|
|
|
|
+ return new V1ObjectMetaBuilder()
|
|
|
|
|
+ .withName(name)
|
|
|
|
|
+ .withNamespace(namespace)
|
|
|
|
|
+ .build();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private V1LabelSelector toDeploymentV1LabelSelector() {
|
|
|
|
|
+ return new V1LabelSelectorBuilder()
|
|
|
|
|
+ .withMatchLabels(Collections.singletonMap(APPLICATION_LABEL, name))
|
|
|
|
|
+ .build();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private V1PodTemplateSpec toPodSpecTemplate(){
|
|
|
|
|
+ V1ObjectMeta v1ObjectMeta = new V1ObjectMetaBuilder()
|
|
|
|
|
+ .withLabels(Collections.singletonMap(APPLICATION_LABEL, name))
|
|
|
|
|
+ .build();
|
|
|
|
|
+ V1PodSpec v1PodSpec = new V1PodSpecBuilder()
|
|
|
|
|
+ .addAllToContainers(containers.stream().map(Container::toV1Container).collect(Collectors.toList()))
|
|
|
|
|
+ .withImagePullSecrets(new V1LocalObjectReference().name("regcred"))
|
|
|
|
|
+ .build();
|
|
|
|
|
+ V1PodTemplateSpec v1PodTemplateSpec = new V1PodTemplateSpecBuilder()
|
|
|
|
|
+ .withMetadata(v1ObjectMeta)
|
|
|
|
|
+ .withSpec(v1PodSpec)
|
|
|
|
|
+ .build();
|
|
|
|
|
+ return v1PodTemplateSpec;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private V1DeploymentSpec toV1DeploymentSpec(){
|
|
|
|
|
+ return new V1DeploymentSpecBuilder()
|
|
|
|
|
+ .withReplicas(replicas)
|
|
|
|
|
+ .withSelector(this.toDeploymentV1LabelSelector())
|
|
|
|
|
+ .withTemplate(this.toPodSpecTemplate())
|
|
|
|
|
+ .build();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private V1ServiceSpec toV1ServiceSpec(){
|
|
|
|
|
+ V1ServiceSpec serviceSpec = new V1ServiceSpecBuilder()
|
|
|
|
|
+ .withSelector(Collections.singletonMap(APPLICATION_LABEL, name))
|
|
|
|
|
+ .withPorts(servicePorts.stream().map(ServicePort::toV1ServicePort).collect(Collectors.toList()))
|
|
|
|
|
+ .build();
|
|
|
|
|
+ return serviceSpec;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+}
|