Skip to content

Commit

Permalink
Merge pull request #17 from st-yclee/main
Browse files Browse the repository at this point in the history
yaml generator 작업(deployments, service, configmap)
  • Loading branch information
st-yclee authored Sep 3, 2024
2 parents 096ce5b + c308495 commit 78bd521
Show file tree
Hide file tree
Showing 5 changed files with 262 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/main/java/kr/co/mcmp/api/manifest/YamlManifestController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package kr.co.mcmp.api.manifest;

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.response.ResponseWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "YamlManifestController - Yaml Generator 관련")
@RequestMapping("/manifest/v1/generator")
@RestController
@RequiredArgsConstructor
public class YamlManifestController {

private final YamlManifestService manifestService;

@Operation(summary = "Deployments Yaml 생성")
@PostMapping("/yaml/deployments")
public ResponseEntity<ResponseWrapper<String>> generateYamlDeployments(
@RequestBody K8SDeploymentsDto deployments) {
manifestService.generateYamlDeployments(deployments);
return ResponseEntity.ok(new ResponseWrapper<>("Deployments Yaml Generated Successfully"));
}

@Operation(summary = "Service Yaml 생성")
@PostMapping("/yaml/service")
public ResponseEntity<ResponseWrapper<String>> generateYamlService(
@RequestBody K8SServiceDto service) {
manifestService.generateYamlService(service);
return ResponseEntity.ok(new ResponseWrapper<>("Service Yaml Generated Successfully"));
}

@Operation(summary = "ConfigMap Yaml 생성")
@PostMapping("/yaml/configmap")
public ResponseEntity<ResponseWrapper<String>> generateYamlConfigMap(
@RequestBody K8SConfigMapDto configMap) {
manifestService.generateYamlConfigMap(configMap);
return ResponseEntity.ok(new ResponseWrapper<>("ConfigMap Yaml Generated Successfully"));
}
}
49 changes: 49 additions & 0 deletions src/main/java/kr/co/mcmp/api/manifest/YamlManifestService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package kr.co.mcmp.api.manifest;

import com.fasterxml.jackson.annotation.JsonInclude;
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 lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

@Log4j2
@Service
@RequiredArgsConstructor
public class YamlManifestService {

private final ObjectMapper mapper = new ObjectMapper(new YAMLFactory())
.setSerializationInclusion(JsonInclude.Include.NON_NULL);

public String generateYamlDeployments(K8SDeploymentsDto deployments) {
return generateYaml(deployments);
}

public String generateYamlService(K8SServiceDto service) {
return generateYaml(service);
}

public String generateYamlConfigMap(K8SConfigMapDto configMap) {
return generateYaml(configMap);
}

private <T> String generateYaml(T dto) {
try {
String yamlText = mapper.writeValueAsString(dto);
log.info("Text: {}", yamlText);
return yamlText;
} catch (JsonProcessingException e) {
throw new RuntimeException("Yaml Generation Failed", e);
}
}

private String removeQuotesYaml(String yamlText) {
yamlText = yamlText.replaceAll(": \"([^\"]*)\"", ": $1");
yamlText = yamlText.replaceAll(": \"(\\d+)\"", ": $1");
return yamlText;
}
}
30 changes: 30 additions & 0 deletions src/main/java/kr/co/mcmp/api/manifest/k8s/K8SConfigMapDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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 K8SConfigMapDto {

private String apiVersion;
private String kind;
private MetadataDto metadata;
private Map<String, String> data = null;

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

private String apiVersion;
private String kind;
private MetadataDto metadata;
private SpecDto spec;

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
private static class MetadataDto {
private String name;
private String namespace;
private Map<String, Object> labels = null;
}

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
private static class SpecDto {
private Integer replicas;
private SelectorDto selector;
private TemplateDto template;

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
private static class SelectorDto {
private Map<String, Object> matchLabels = null;
}

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

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

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
private static class ContainerDto {
private String name;
private String image;
private List<PortDto> ports = null;

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
private static class PortDto {
private Integer containerPort;
}
}
}
}
}
}
52 changes: 52 additions & 0 deletions src/main/java/kr/co/mcmp/api/manifest/k8s/K8SServiceDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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 K8SServiceDto {

private String apiVersion;
private String kind;
private MetadataDto metadata;
private SpecDto spec;

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
private static class MetadataDto {
private String name;
private String namespace;
private Map<String, Object> labels = null;
}

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
private static class SpecDto {
private Map<String, Object> selector = null;
private List<PortsDto> ports = null;
private String type;

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
private static class PortsDto {
private String protocol;
private Integer port;
private Integer targetPort;
private Integer nodePort;
}
}
}

0 comments on commit 78bd521

Please sign in to comment.