Skip to content

Commit

Permalink
Merge pull request #18 from st-yclee/main
Browse files Browse the repository at this point in the history
yaml generator 추가 작업(pod, hpa)
  • Loading branch information
st-yclee authored Sep 4, 2024
2 parents 78bd521 + e3b2cab commit 0fd9d61
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 25 deletions.
20 changes: 17 additions & 3 deletions src/main/java/kr/co/mcmp/api/manifest/YamlManifestController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -44,4 +42,20 @@ public ResponseEntity<ResponseWrapper<String>> generateYamlConfigMap(
manifestService.generateYamlConfigMap(configMap);
return ResponseEntity.ok(new ResponseWrapper<>("ConfigMap Yaml Generated Successfully"));
}

@Operation(summary = "Pod Yaml 생성")
@PostMapping("/yaml/pod")
public ResponseEntity<ResponseWrapper<String>> 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<ResponseWrapper<String>> generateYamlHpa(
@RequestBody K8SHpaDto hpa) {
manifestService.generateYamlHpa(hpa);
return ResponseEntity.ok(new ResponseWrapper<>("Hpa Yaml Generated Successfully"));
}
}
25 changes: 20 additions & 5 deletions src/main/java/kr/co/mcmp/api/manifest/YamlManifestService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 <T> String generateYaml(T dto) {
try {
String yamlText = mapper.writeValueAsString(dto);
Expand All @@ -41,9 +47,18 @@ private <T> 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);
}*/
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@
@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<String, String> data = null;

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
private static class MetadataDto {
public static class MetadataDto {
private String name;
private String namespace;
private Map<String, Object> labels = null;
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/kr/co/mcmp/api/manifest/k8s/K8SDeploymentsDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
@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;

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
private static class MetadataDto {
public static class MetadataDto {
private String name;
private String namespace;
private Map<String, Object> labels = null;
Expand All @@ -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;
Expand All @@ -42,30 +42,30 @@ private static class SpecDto {
@Builder
@NoArgsConstructor
@AllArgsConstructor
private static class SelectorDto {
public static class SelectorDto {
private Map<String, Object> matchLabels = null;
}

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
private static class TemplateDto {
public static class TemplateDto {
private MetadataDto metadata;
private PodSpecDto spec;

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
private static class PodSpecDto {
public static class PodSpecDto {
private List<ContainerDto> containers = null;

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
private static class ContainerDto {
public static class ContainerDto {
private String name;
private String image;
private List<PortDto> ports = null;
Expand All @@ -74,7 +74,7 @@ private static class ContainerDto {
@Builder
@NoArgsConstructor
@AllArgsConstructor
private static class PortDto {
public static class PortDto {
private Integer containerPort;
}
}
Expand Down
51 changes: 51 additions & 0 deletions src/main/java/kr/co/mcmp/api/manifest/k8s/K8SHpaDto.java
Original file line number Diff line number Diff line change
@@ -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<String, Object> 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;
}
}
}
71 changes: 71 additions & 0 deletions src/main/java/kr/co/mcmp/api/manifest/k8s/K8SPodDto.java
Original file line number Diff line number Diff line change
@@ -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<String, Object> labels = null;
}

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class SpecDto {
private List<ContainerDto> containers = null;
private String restartPolicy;

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class ContainerDto {
private String name;
private String image;
private List<PortDto> 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<String, Object> limits = null;
private Map<String, Object> requests = null;
}
}
}
}
10 changes: 5 additions & 5 deletions src/main/java/kr/co/mcmp/api/manifest/k8s/K8SServiceDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
@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;

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
private static class MetadataDto {
public static class MetadataDto {
private String name;
private String namespace;
private Map<String, Object> labels = null;
Expand All @@ -33,7 +33,7 @@ private static class MetadataDto {
@Builder
@NoArgsConstructor
@AllArgsConstructor
private static class SpecDto {
public static class SpecDto {
private Map<String, Object> selector = null;
private List<PortsDto> ports = null;
private String type;
Expand All @@ -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;
Expand Down

0 comments on commit 0fd9d61

Please sign in to comment.