All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.alauda.model.Kubernete Maven / Gradle / Ivy

package io.alauda.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.alauda.util.JacksonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class Kubernete implements Serializable {
    private static Logger logger = LoggerFactory.getLogger(Kubernete.class);

    public static final String KuberneteKindService = "Service";
    public static final String KuberneteKindHorizontalPodAutoscaler = "HorizontalPodAutoscaler";
    public static final String KuberneteKindDeployment = "Deployment";
    public static final String KuberneteKindStatefulSet = "StatefulSet";
    public static final String KuberneteKindDaemonSet = "DaemonSet";

    private String apiVersion;
    private String kind;
    private Metadata metadata;
    //JsonNode
    private transient Object spec;
    //JsonNode
    private transient Object status;

    @JsonIgnoreProperties(ignoreUnknown = true)
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    public static class Container implements Serializable {
        private String image;
        private String name;
        private String imagePullPolicy;
        private List env = new ArrayList<>();
        private List envFrom = new ArrayList<>();
        private List ports;
        private List args;
        private List command;
        private Map resources;
        private Map livenessProbe;
        private Map readinessProbe;
        private String terminationMessagePolicy;
        private String terminationMessagePath;
        private List volumeMounts;

        @Override
        public String toString() {
            return "Container{" +
                    "name='" + name + '\'' +
                    ", image='" + image + '\'' +
                    ", env=" + env +
                    ", envFrom=" + envFrom +
                    ", args=" + args +
                    ", command=" + command +
                    '}';
        }

        public static class EnvFrom implements Serializable {
            public EnvFrom(){}
            public EnvFrom(String name){
                this.configMapRef = new ConfigMapRef(name);
            }

            private ConfigMapRef configMapRef;

            public ConfigMapRef getConfigMapRef() {
                return configMapRef;
            }

            public void setConfigMapRef(ConfigMapRef configMapRef) {
                this.configMapRef = configMapRef;
            }

            @Override
            public String toString() {
                return "EnvFrom{" +
                        "configMapRef='" + configMapRef + "'" +
                        '}';
            }
        }


        public void addEnvFrom(EnvFrom item) {
            if (item == null) {
                return;
            }

            System.out.printf("add env from --> %s \n", item);
            envFrom.add(item);
        }

        public void addEnvFrom(String name) {
            EnvFrom e = new EnvFrom(name);
            addEnvFrom(e);
        }

        public static class ConfigMapRef implements Serializable{
            public ConfigMapRef(){}
            public ConfigMapRef(String name){
                this.name = name;
            }
            private String name;

            public String getName() {
                return name;
            }

            public void setName(String name) {
                this.name = name;
            }

            @Override
            public String toString() {
                return "configMapRef{" +
                        "name='" + name + "'" +
                        '}';
            }
        }


        public static class EnvValFrom implements Serializable {

            public EnvValFrom(){}

            public EnvValFrom(ConfigMapKeyRef ref){
                this.setConfigMapKeyRef(ref);
            }

            public EnvValFrom(String key, String name){
                this.setConfigMapKeyRef(new ConfigMapKeyRef(key,name));
            }

            public ConfigMapKeyRef getConfigMapKeyRef() {
                return configMapKeyRef;
            }

            public void setConfigMapKeyRef(ConfigMapKeyRef configMapKeyRef) {
                this.configMapKeyRef = configMapKeyRef;
            }

            private ConfigMapKeyRef configMapKeyRef;

            @Override
            public String toString() {
                return "EnvValFrom{" +
                        "configMapKeyRef='" + configMapKeyRef + "'" +
                        '}';
            }
        }

        public static class ConfigMapKeyRef implements Serializable{

            public  ConfigMapKeyRef(){}

            public ConfigMapKeyRef(String key, String name){
                this.setKey(key);
                this.setName(name);
            }

            @Override
            public String toString() {
                return "ConfigMapKeyRef{" +
                        "key='" + key + "'" +
                        ", name='" + name  + "'" +
                        '}';
            }

            public String getKey() {
                return key;
            }

            public void setKey(String key) {
                this.key = key;
            }

            public String getName() {
                return name;
            }

            public void setName(String name) {
                this.name = name;
            }

            private String key;
            private String name;
        }

        public static class EnvVar implements Serializable {
            private String name;
            private String value;
            private EnvValFrom valueFrom;

            public EnvVar() {
            }

            public EnvVar(String name, String value) {
                this.name = name;
                this.value = value;
            }

            public EnvVar(String name, String key, String from){
                this.name = name;
                this.valueFrom = new EnvValFrom(key, from);
            }

            @Override
            public String toString() {
                return "EnvVar{" +
                        "name='" + name + '\'' +
                        ", value='" + value + '\'' +
                        ", valueFrom='" + valueFrom + '\'' +
                        '}';
            }

            public EnvValFrom getValueFrom() {
                return valueFrom;
            }

            public void setValueFrom(EnvValFrom valueFrom) {
                this.valueFrom = valueFrom;
            }

            public String getName() {
                return name;
            }

            public void setName(String name) {
                this.name = name;
            }

            public String getValue() {
                return value;
            }

            public void setValue(String value) {
                this.value = value;
            }
        }

        public void addEnvVar(EnvVar envVar) {
            if (envVar == null) {
                return;
            }
            if (env.size() > 0) {
                System.out.printf("add env var --> %s \n", envVar);
                for (EnvVar oldEnv : env) {
                    if (oldEnv.getName().equals(envVar.getName())) {
                        oldEnv.setValue(envVar.getValue());
                        oldEnv.setValueFrom(envVar.getValueFrom());
                        return;
                    }
                }
            }
            env.add(envVar);
        }

        public void addEnvVar(String name, String value) {
            EnvVar e = new EnvVar(name, value);
            addEnvVar(e);
        }

        public void addEnvVar(String name, String key, String from) {
            EnvVar e = new EnvVar(name, key, from);
            addEnvVar(e);
        }

        public void addEnvVars(List envVars) {
            if (envVars != null && envVars.size() > 0) {
                for (EnvVar env : envVars) {
                    addEnvVar(env);
                }
            }
        }

        public void addEnvVars(Map envVars) {
            if (envVars != null && envVars.size() > 0) {
                for (Map.Entry entry : envVars.entrySet()) {
                    addEnvVar(entry.getKey(), entry.getValue());
                }
            }
        }

        public void addEnvFroms(List envFroms) {
            if (envFroms != null && envFroms.size() > 0) {
                for (EnvFrom env : envFroms) {
                    addEnvFrom(env);
                }
            }
        }


        public static class VolumeMount implements Serializable {
            private String mountPath;
            private String name;

            public String getMountPath() {
                return mountPath;
            }

            public void setMountPath(String mountPath) {
                this.mountPath = mountPath;
            }

            public String getName() {
                return name;
            }

            public void setName(String name) {
                this.name = name;
            }
        }

        // region Setter and Getter

        public void setImageTag(String imageTag) {
            String[] imageAndTag = this.image.split(":");
            if (imageAndTag.length == 1) {
                // index.alauda.cn/alauda/hello-world
                this.image = String.format("%s:%s", this.image, imageTag);
            } else if (imageAndTag.length == 2) {
                if (imageAndTag[0].contains("/")) {
                    // index.alauda.cn/alauda/hello-world:latest
                    this.image = String.format("%s:%s", imageAndTag[0], imageTag);
                } else {
                    // stagingindex.alauda.cn:5000/testorg001/hello-world
                    this.image = String.format("%s:%s", this.image, imageTag);
                }
            } else if (imageAndTag.length == 3) {
                // stagingindex.alauda.cn:5000/testorg001/hello-world:latest
                this.image = String.format("%s:%s:%s", imageAndTag[0], imageAndTag[1], imageTag);
            } else {
                logger.error(String.format("The repository [%s] is not valuable.", this.image));
            }
        }

        public String getImage() {
            return image;
        }

        public void setImage(String image) {
            this.image = image;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getImagePullPolicy() {
            return imagePullPolicy;
        }

        public void setImagePullPolicy(String imagePullPolicy) {
            this.imagePullPolicy = imagePullPolicy;
        }

        public List getEnv() {
            return env;
        }

        public void setEnv(List env) {
            this.env = env;
        }

        public List getEnvFrom() {
            return envFrom;
        }

        public void setEnvFrom(List envFrom) {
            this.envFrom = envFrom;
        }

        public List getPorts() {
            return ports;
        }

        public void setPorts(List ports) {
            this.ports = ports;
        }

        public List getArgs() {
            return args;
        }

        public void setArgs(List args) {
            this.args = args;
        }

        public List getCommand() {
            return command;
        }

        public void setCommand(List command) {
            this.command = command;
        }

        public Map getResources() {
            return resources;
        }

        public void setResources(Map resources) {
            this.resources = resources;
        }

        public Map getLivenessProbe() {
            return livenessProbe;
        }

        public void setLivenessProbe(Map livenessProbe) {
            this.livenessProbe = livenessProbe;
        }

        public Map getReadinessProbe() {
            return readinessProbe;
        }

        public void setReadinessProbe(Map readinessProbe) {
            this.readinessProbe = readinessProbe;
        }

        public String getTerminationMessagePolicy() {
            return terminationMessagePolicy;
        }

        public void setTerminationMessagePolicy(String terminationMessagePolicy) {
            this.terminationMessagePolicy = terminationMessagePolicy;
        }

        public String getTerminationMessagePath() {
            return terminationMessagePath;
        }

        public void setTerminationMessagePath(String terminationMessagePath) {
            this.terminationMessagePath = terminationMessagePath;
        }

        public List getVolumeMounts() {
            return volumeMounts;
        }

        public void setVolumeMounts(List volumeMounts) {
            this.volumeMounts = volumeMounts;
        }

        // endregion
    }

    @JsonIgnoreProperties(ignoreUnknown = true)
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    public static class Metadata implements Serializable {
        private String creationTimestamp;
        private Map labels;
        private String namespace;
        private String name;

        @Override
        public String toString() {
            return "Metadata{" +
                    "creationTimestamp=" + creationTimestamp +
                    ", labels=" + labels +
                    ", namespace='" + namespace + '\'' +
                    ", name='" + name + '\'' +
                    '}';
        }

        public String getCreationTimestamp() {
            return creationTimestamp;
        }

        public void setCreationTimestamp(String creationTimestamp) {
            this.creationTimestamp = creationTimestamp;
        }

        public Map getLabels() {
            return labels;
        }

        public void setLabels(Map labels) {
            this.labels = labels;
        }

        public String getNamespace() {
            return namespace;
        }

        public void setNamespace(String namespace) {
            this.namespace = namespace;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

    private ObjectNode retrieveSpec() {

        JsonNode node = new ObjectMapper().convertValue(spec, JsonNode.class);
        JsonNode jnSpec = node.path("template").path("spec");
        return (ObjectNode) jnSpec;
    }

    public List retrieveContainers() throws Exception {
        ObjectNode onSpec = retrieveSpec();
        String strContainers = onSpec.get("containers").toString();
        return JacksonUtil.json2Bean(strContainers, List.class, Container.class);

//        JSONArray cs = spec.getJSONObject("template").getJSONObject("spec").getJSONArray("containers");
//        List containers = JSONArray.toList(cs, Container.class);
//        return containers;
    }

    public void updateContainers(List containers) {
        ObjectNode onSpec = retrieveSpec();
        onSpec.putPOJO("containers", containers);

//        spec.getJSONObject("template").getJSONObject("spec").put("containers", containers);
    }

    @Override
    public String toString() {
        return "Kubernete{" +
                "apiVersion='" + apiVersion + '\'' +
                ", kind='" + kind + '\'' +
                ", metadata=" + metadata +
                ", spec=" + spec +
                ", status=" + status +
                '}';
    }

    // region Setter and Getter
    public String getApiVersion() {
        return apiVersion;
    }

    public void setApiVersion(String apiVersion) {
        this.apiVersion = apiVersion;
    }

    public String getKind() {
        return kind;
    }

    public void setKind(String kind) {
        this.kind = kind;
    }

    public Metadata getMetadata() {
        return metadata;
    }

    public void setMetadata(Metadata metadata) {
        this.metadata = metadata;
    }

    public Object getSpec() {
        return spec;
    }

    public void setSpec(Object spec) {
        this.spec = spec;
    }

    public Object getStatus() {
        return status;
    }

    public void setStatus(Object status) {
        this.status = status;
    }
    // endregion


}