diff --git a/src/main/java/kr/co/mcmp/api/manifest/YamlManifestController.java b/src/main/java/kr/co/mcmp/api/manifest/YamlManifestController.java index fb83ec4..6a03975 100644 --- a/src/main/java/kr/co/mcmp/api/manifest/YamlManifestController.java +++ b/src/main/java/kr/co/mcmp/api/manifest/YamlManifestController.java @@ -2,9 +2,7 @@ import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; -import kr.co.mcmp.api.manifest.k8s.K8SConfigMapDto; -import kr.co.mcmp.api.manifest.k8s.K8SDeploymentsDto; -import kr.co.mcmp.api.manifest.k8s.K8SServiceDto; +import kr.co.mcmp.api.manifest.k8s.*; import kr.co.mcmp.response.ResponseWrapper; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; @@ -44,4 +42,20 @@ public ResponseEntity> generateYamlConfigMap( manifestService.generateYamlConfigMap(configMap); return ResponseEntity.ok(new ResponseWrapper<>("ConfigMap Yaml Generated Successfully")); } + + @Operation(summary = "Pod Yaml 생성") + @PostMapping("/yaml/pod") + public ResponseEntity> generateYamlPod( + @RequestBody K8SPodDto pod) { + manifestService.generateYamlPod(pod); + return ResponseEntity.ok(new ResponseWrapper<>("Pod Yaml Generated Successfully")); + } + + @Operation(summary = "Hpa Yaml 생성") + @PostMapping("/yaml/hpa") + public ResponseEntity> generateYamlHpa( + @RequestBody K8SHpaDto hpa) { + manifestService.generateYamlHpa(hpa); + return ResponseEntity.ok(new ResponseWrapper<>("Hpa Yaml Generated Successfully")); + } } diff --git a/src/main/java/kr/co/mcmp/api/manifest/YamlManifestService.java b/src/main/java/kr/co/mcmp/api/manifest/YamlManifestService.java index 0e4a9ba..6828fd4 100644 --- a/src/main/java/kr/co/mcmp/api/manifest/YamlManifestService.java +++ b/src/main/java/kr/co/mcmp/api/manifest/YamlManifestService.java @@ -4,9 +4,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; -import kr.co.mcmp.api.manifest.k8s.K8SConfigMapDto; -import kr.co.mcmp.api.manifest.k8s.K8SDeploymentsDto; -import kr.co.mcmp.api.manifest.k8s.K8SServiceDto; +import kr.co.mcmp.api.manifest.k8s.*; import lombok.RequiredArgsConstructor; import lombok.extern.log4j.Log4j2; import org.springframework.stereotype.Service; @@ -31,6 +29,14 @@ public String generateYamlConfigMap(K8SConfigMapDto configMap) { return generateYaml(configMap); } + public String generateYamlPod(K8SPodDto pod) { + return generateYaml(pod); + } + + public String generateYamlHpa(K8SHpaDto hpa) { + return generateYaml(hpa); + } + private String generateYaml(T dto) { try { String yamlText = mapper.writeValueAsString(dto); @@ -41,9 +47,18 @@ private String generateYaml(T dto) { } } - private String removeQuotesYaml(String yamlText) { +/* private String removeQuotesYaml(String yamlText) { yamlText = yamlText.replaceAll(": \"([^\"]*)\"", ": $1"); yamlText = yamlText.replaceAll(": \"(\\d+)\"", ": $1"); return yamlText; - } + }*/ + +/* private void saveYaml(String yamlText) { + CommonUploadComponent.TextComponentDto textComponent = CommonUploadComponent.TextComponentDto.builder() + .filename("test") + .directory("/") + .text(yamlText) + .build(); + moduleComponentService.createComponentByText("nexus", "repo", textComponent); + }*/ } diff --git a/src/main/java/kr/co/mcmp/api/manifest/k8s/K8SConfigMapDto.java b/src/main/java/kr/co/mcmp/api/manifest/k8s/K8SConfigMapDto.java index 5d04143..e86866f 100644 --- a/src/main/java/kr/co/mcmp/api/manifest/k8s/K8SConfigMapDto.java +++ b/src/main/java/kr/co/mcmp/api/manifest/k8s/K8SConfigMapDto.java @@ -13,8 +13,8 @@ @AllArgsConstructor public class K8SConfigMapDto { - private String apiVersion; - private String kind; + private final String apiVersion = "v1"; + private final String kind = "ConfigMap"; private MetadataDto metadata; private Map data = null; @@ -22,7 +22,7 @@ public class K8SConfigMapDto { @Builder @NoArgsConstructor @AllArgsConstructor - private static class MetadataDto { + public static class MetadataDto { private String name; private String namespace; private Map labels = null; diff --git a/src/main/java/kr/co/mcmp/api/manifest/k8s/K8SDeploymentsDto.java b/src/main/java/kr/co/mcmp/api/manifest/k8s/K8SDeploymentsDto.java index 594872a..952b903 100644 --- a/src/main/java/kr/co/mcmp/api/manifest/k8s/K8SDeploymentsDto.java +++ b/src/main/java/kr/co/mcmp/api/manifest/k8s/K8SDeploymentsDto.java @@ -14,8 +14,8 @@ @AllArgsConstructor public class K8SDeploymentsDto { - private String apiVersion; - private String kind; + private final String apiVersion = "apps/v1"; + private final String kind = "Deployment"; private MetadataDto metadata; private SpecDto spec; @@ -23,7 +23,7 @@ public class K8SDeploymentsDto { @Builder @NoArgsConstructor @AllArgsConstructor - private static class MetadataDto { + public static class MetadataDto { private String name; private String namespace; private Map labels = null; @@ -33,7 +33,7 @@ private static class MetadataDto { @Builder @NoArgsConstructor @AllArgsConstructor - private static class SpecDto { + public static class SpecDto { private Integer replicas; private SelectorDto selector; private TemplateDto template; @@ -42,7 +42,7 @@ private static class SpecDto { @Builder @NoArgsConstructor @AllArgsConstructor - private static class SelectorDto { + public static class SelectorDto { private Map matchLabels = null; } @@ -50,7 +50,7 @@ private static class SelectorDto { @Builder @NoArgsConstructor @AllArgsConstructor - private static class TemplateDto { + public static class TemplateDto { private MetadataDto metadata; private PodSpecDto spec; @@ -58,14 +58,14 @@ private static class TemplateDto { @Builder @NoArgsConstructor @AllArgsConstructor - private static class PodSpecDto { + public static class PodSpecDto { private List containers = null; @Getter @Builder @NoArgsConstructor @AllArgsConstructor - private static class ContainerDto { + public static class ContainerDto { private String name; private String image; private List ports = null; @@ -74,7 +74,7 @@ private static class ContainerDto { @Builder @NoArgsConstructor @AllArgsConstructor - private static class PortDto { + public static class PortDto { private Integer containerPort; } } diff --git a/src/main/java/kr/co/mcmp/api/manifest/k8s/K8SHpaDto.java b/src/main/java/kr/co/mcmp/api/manifest/k8s/K8SHpaDto.java new file mode 100644 index 0000000..95fafea --- /dev/null +++ b/src/main/java/kr/co/mcmp/api/manifest/k8s/K8SHpaDto.java @@ -0,0 +1,51 @@ +package kr.co.mcmp.api.manifest.k8s; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.util.Map; + +@Getter +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class K8SHpaDto { + + private final String apiVersion = "autoscaling/v1"; + private final String kind = "HorizontalPodAutoscaler"; + private MetadataDto metadata; + private SpecDto spec; + + @Getter + @Builder + @NoArgsConstructor + @AllArgsConstructor + public static class MetadataDto { + private String name; + private String namespace; + private Map labels = null; + } + + @Getter + @Builder + @NoArgsConstructor + @AllArgsConstructor + public static class SpecDto { + private ScaleTargetRefDto scaleTargetRef; + private Integer minReplicas; + private Integer maxReplicas; + private Integer targetCPUUtilizationPercentage; + + @Getter + @Builder + @NoArgsConstructor + @AllArgsConstructor + public static class ScaleTargetRefDto { + private String apiVersion; + private String kind; + private String name; + } + } +} diff --git a/src/main/java/kr/co/mcmp/api/manifest/k8s/K8SPodDto.java b/src/main/java/kr/co/mcmp/api/manifest/k8s/K8SPodDto.java new file mode 100644 index 0000000..d2e4c62 --- /dev/null +++ b/src/main/java/kr/co/mcmp/api/manifest/k8s/K8SPodDto.java @@ -0,0 +1,71 @@ +package kr.co.mcmp.api.manifest.k8s; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.util.List; +import java.util.Map; + +@Getter +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class K8SPodDto { + + private final String apiVersion = "v1"; + private final String kind = "Pod"; + private MetadataDto metadata; + private SpecDto spec; + + @Getter + @Builder + @NoArgsConstructor + @AllArgsConstructor + public static class MetadataDto { + private String name; + private String namespace; + private Map labels = null; + } + + @Getter + @Builder + @NoArgsConstructor + @AllArgsConstructor + public static class SpecDto { + private List containers = null; + private String restartPolicy; + + @Getter + @Builder + @NoArgsConstructor + @AllArgsConstructor + public static class ContainerDto { + private String name; + private String image; + private List ports = null; + private ResourceDto resources; + + @Getter + @Builder + @NoArgsConstructor + @AllArgsConstructor + public static class PortDto { + private String name; + private Integer containerPort; + private Integer hostPort; + private String protocol; + } + + @Getter + @Builder + @NoArgsConstructor + @AllArgsConstructor + public static class ResourceDto { + private Map limits = null; + private Map requests = null; + } + } + } +} \ No newline at end of file diff --git a/src/main/java/kr/co/mcmp/api/manifest/k8s/K8SServiceDto.java b/src/main/java/kr/co/mcmp/api/manifest/k8s/K8SServiceDto.java index cc729da..8added1 100644 --- a/src/main/java/kr/co/mcmp/api/manifest/k8s/K8SServiceDto.java +++ b/src/main/java/kr/co/mcmp/api/manifest/k8s/K8SServiceDto.java @@ -14,8 +14,8 @@ @AllArgsConstructor public class K8SServiceDto { - private String apiVersion; - private String kind; + private final String apiVersion = "v1"; + private final String kind = "Service"; private MetadataDto metadata; private SpecDto spec; @@ -23,7 +23,7 @@ public class K8SServiceDto { @Builder @NoArgsConstructor @AllArgsConstructor - private static class MetadataDto { + public static class MetadataDto { private String name; private String namespace; private Map labels = null; @@ -33,7 +33,7 @@ private static class MetadataDto { @Builder @NoArgsConstructor @AllArgsConstructor - private static class SpecDto { + public static class SpecDto { private Map selector = null; private List ports = null; private String type; @@ -42,7 +42,7 @@ private static class SpecDto { @Builder @NoArgsConstructor @AllArgsConstructor - private static class PortsDto { + public static class PortsDto { private String protocol; private Integer port; private Integer targetPort;