Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

k8s dto validation 적용 #33

Merged
merged 3 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@Tag(name = "YamlManifestController - Yaml Generator 관련")
@RequestMapping("/manifest/v1/generator")
@RestController
Expand All @@ -22,39 +24,39 @@ public class YamlManifestController {
@Operation(summary = "Deployments Yaml 생성")
@PostMapping("/yaml/deployments")
public ResponseEntity<ResponseWrapper<String>> generateYamlDeployments(
@RequestBody K8SDeploymentDto deployments) {
@Valid @RequestBody K8SDeploymentDto deployments) {
String yaml = manifestService.generateYamlDeployments(deployments);
return ResponseEntity.ok(new ResponseWrapper<>(yaml));
}

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

@Operation(summary = "ConfigMap Yaml 생성")
@PostMapping("/yaml/configmap")
public ResponseEntity<ResponseWrapper<String>> generateYamlConfigMap(
@RequestBody K8SConfigMapDto configMap) {
@Valid @RequestBody K8SConfigMapDto configMap) {
String yaml = manifestService.generateYamlConfigMap(configMap);
return ResponseEntity.ok(new ResponseWrapper<>(yaml));
}

@Operation(summary = "Pod Yaml 생성")
@PostMapping("/yaml/pod")
public ResponseEntity<ResponseWrapper<String>> generateYamlPod(
@RequestBody K8SPodDto pod) {
@Valid @RequestBody K8SPodDto pod) {
String yaml = manifestService.generateYamlPod(pod);
return ResponseEntity.ok(new ResponseWrapper<>(yaml));
}

@Operation(summary = "Hpa Yaml 생성")
@PostMapping("/yaml/hpa")
public ResponseEntity<ResponseWrapper<String>> generateYamlHpa(
@RequestBody K8SHpaDto hpa) {
@Valid @RequestBody K8SHpaDto hpa) {
String yaml = manifestService.generateYamlHpa(hpa);
return ResponseEntity.ok(new ResponseWrapper<>(yaml));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import lombok.Getter;
import lombok.NoArgsConstructor;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Map;

@Getter
Expand All @@ -15,6 +18,8 @@ public class K8SConfigMapDto {

private final String apiVersion = "v1";
private final String kind = "ConfigMap";
@Valid
@NotNull
private ConfigMapMetadataDto metadata;
private Map<String, String> data = null;

Expand All @@ -23,6 +28,7 @@ public class K8SConfigMapDto {
@NoArgsConstructor
@AllArgsConstructor
public static class ConfigMapMetadataDto {
@NotBlank
private String name;
private String namespace;
private Map<String, String> labels = null;
Expand Down
39 changes: 36 additions & 3 deletions src/main/java/kr/co/mcmp/api/manifest/k8s/K8SDeploymentDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import lombok.Getter;
import lombok.NoArgsConstructor;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

Expand All @@ -16,14 +19,19 @@ public class K8SDeploymentDto {

private final String apiVersion = "apps/v1";
private final String kind = "Deployment";
@Valid
@NotNull
private DeploymentMetadataDto metadata;
@Valid
@NotNull
private DeploymentSpecDto spec;

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class DeploymentMetadataDto {
@NotBlank
private String name;
private String namespace;
private Map<String, String> labels = null;
Expand All @@ -35,38 +43,61 @@ public static class DeploymentMetadataDto {
@AllArgsConstructor
public static class DeploymentSpecDto {
private Integer replicas;
@Valid
@NotNull
private DeploymentSelectorDto selector;
@Valid
@NotNull
private DeploymentTemplateDto template;

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class DeploymentSelectorDto {
private Map<String, String> matchLabels = null;
@NotNull
private Map<String, String> matchLabels;
}

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class DeploymentTemplateDto {
private DeploymentMetadataDto metadata;
@Valid
@NotNull
private DeploymentPodMetadataDto metadata;
@Valid
@NotNull
private DeploymentPodSpecDto spec;

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class DeploymentPodMetadataDto {
private String name;
private String namespace;
@NotNull
private Map<String, String> labels;
}

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class DeploymentPodSpecDto {
private List<DeploymentPodContainerDto> containers = null;
@NotNull
private List<DeploymentPodContainerDto> containers;

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class DeploymentPodContainerDto {
@NotBlank
private String name;
@NotBlank
private String image;
private List<DeploymentPodEnvDto> env = null;
private List<DeploymentPodPortDto> ports = null;
Expand All @@ -76,6 +107,7 @@ public static class DeploymentPodContainerDto {
@NoArgsConstructor
@AllArgsConstructor
public static class DeploymentPodEnvDto {
@NotBlank
private String name;
private String value;
}
Expand All @@ -85,6 +117,7 @@ public static class DeploymentPodEnvDto {
@NoArgsConstructor
@AllArgsConstructor
public static class DeploymentPodPortDto {
@NotNull
private Integer containerPort;
}
}
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/kr/co/mcmp/api/manifest/k8s/K8SHpaDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import lombok.Getter;
import lombok.NoArgsConstructor;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Map;

@Getter
Expand All @@ -15,14 +18,19 @@ public class K8SHpaDto {

private final String apiVersion = "autoscaling/v1";
private final String kind = "HorizontalPodAutoscaler";
@Valid
@NotNull
private HpaMetadataDto metadata;
@Valid
@NotNull
private HpaSpecDto spec;

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class HpaMetadataDto {
@NotBlank
private String name;
private String namespace;
private Map<String, String> labels = null;
Expand All @@ -33,18 +41,26 @@ public static class HpaMetadataDto {
@NoArgsConstructor
@AllArgsConstructor
public static class HpaSpecDto {
@Valid
@NotNull
private HpaScaleTargetRefDto scaleTargetRef;
@NotNull
private Integer minReplicas;
@NotNull
private Integer maxReplicas;
@NotNull
private Integer targetCPUUtilizationPercentage;

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class HpaScaleTargetRefDto {
@NotBlank
private String apiVersion;
@NotBlank
private String kind;
@NotBlank
private String name;
}
}
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/kr/co/mcmp/api/manifest/k8s/K8SPodDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import lombok.Getter;
import lombok.NoArgsConstructor;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

Expand All @@ -16,14 +19,19 @@ public class K8SPodDto {

private final String apiVersion = "v1";
private final String kind = "Pod";
@Valid
@NotNull
private PodMetadataDto metadata;
@Valid
@NotNull
private PodSpecDto spec;

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class PodMetadataDto {
@NotBlank
private String name;
private String namespace;
private Map<String, String> labels = null;
Expand All @@ -34,15 +42,19 @@ public static class PodMetadataDto {
@NoArgsConstructor
@AllArgsConstructor
public static class PodSpecDto {
private List<PodContainerDto> containers = null;
@Valid
@NotNull
private List<PodContainerDto> containers;
private String restartPolicy;

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class PodContainerDto {
@NotBlank
private String name;
@NotBlank
private String image;
private List<PodEnvDto> env = null;
private List<PodPortDto> ports = null;
Expand Down
17 changes: 15 additions & 2 deletions src/main/java/kr/co/mcmp/api/manifest/k8s/K8SServiceDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import lombok.Getter;
import lombok.NoArgsConstructor;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

Expand All @@ -16,14 +19,19 @@ public class K8SServiceDto {

private final String apiVersion = "v1";
private final String kind = "Service";
@Valid
@NotNull
private ServiceMetadataDto metadata;
@Valid
@NotNull
private ServiceSpecDto spec;

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class ServiceMetadataDto {
@NotBlank
private String name;
private String namespace;
private Map<String, String> labels = null;
Expand All @@ -34,8 +42,11 @@ public static class ServiceMetadataDto {
@NoArgsConstructor
@AllArgsConstructor
public static class ServiceSpecDto {
private Map<String, String> selector = null;
private List<ServicePortsDto> ports = null;
@NotNull
private Map<String, String> selector;
@Valid
@NotNull
private List<ServicePortsDto> ports;
private String type;

@Getter
Expand All @@ -44,7 +55,9 @@ public static class ServiceSpecDto {
@AllArgsConstructor
public static class ServicePortsDto {
private String protocol;
@NotNull
private Integer port;
@NotNull
private Integer targetPort;
private Integer nodePort;
}
Expand Down