Skip to content

Commit fd61a55

Browse files
author
hyun123
committed
update for oss exception
1 parent 027a69c commit fd61a55

File tree

10 files changed

+108
-84
lines changed

10 files changed

+108
-84
lines changed

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ dependencies {
104104
implementation 'io.fabric8:kubernetes-client:6.10.0'
105105
implementation 'io.fabric8:kubernetes-model-metrics:6.10.0'
106106

107-
// implementation 'com.marcnuri.helm-java:helm-java:0.0.13'
108-
implementation 'com.marcnuri.helm-java:helm-java:0.0.9'
107+
implementation 'com.marcnuri.helm-java:helm-java:0.0.13'
108+
// implementation 'com.marcnuri.helm-java:helm-java:0.0.9'
109109

110110
implementation 'com.github.docker-java:docker-java:3.3.0'
111111
implementation 'com.github.docker-java:docker-java-transport-httpclient5:3.3.0'

src/main/java/kr/co/mcmp/externalrepo/ArtifactHubInteface.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package kr.co.mcmp.externalrepo;
22

33
import kr.co.mcmp.externalrepo.model.ArtifactHubPackage;
4-
import kr.co.mcmp.externalrepo.model.ArtifactHubRespository;
4+
import kr.co.mcmp.externalrepo.model.ArtifactHubRepository;
55
import org.springframework.cloud.openfeign.FeignClient;
66
import org.springframework.web.bind.annotation.GetMapping;
77
import org.springframework.web.bind.annotation.RequestParam;
@@ -13,7 +13,7 @@ public interface ArtifactHubInteface {
1313

1414
@GetMapping(value="/api/v1/repositories/search")
1515
//https://artifacthub.io/api/v1/repositories/search?offset=0&limit=5&kind=0&name=argo
16-
List<ArtifactHubRespository> searchRepository(@RequestParam("name") String helm);
16+
List<ArtifactHubRepository> searchRepository(@RequestParam("name") String helm);
1717

1818
@GetMapping(value="/api/v1/packages/search")
1919
ArtifactHubPackage searchPackage(@RequestParam("ts_query_web") String helm, @RequestParam(required=false, value="kind", defaultValue="0") String kind);

src/main/java/kr/co/mcmp/externalrepo/ExternalRepoInterface.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package kr.co.mcmp.externalrepo;
22

33
import kr.co.mcmp.externalrepo.model.ArtifactHubPackage;
4-
import kr.co.mcmp.externalrepo.model.ArtifactHubRespository;
4+
import kr.co.mcmp.externalrepo.model.ArtifactHubRepository;
55
import kr.co.mcmp.externalrepo.model.DockerHubCatalog;
66

77
import java.util.List;
88

99

1010
public interface ExternalRepoInterface {
1111

12-
List<ArtifactHubRespository> searchArtifactHubRepository(String keyword);
12+
List<ArtifactHubRepository> searchArtifactHubRepository(String keyword);
1313

1414
ArtifactHubPackage searchArtifactHubPackage(String keyword);
1515

src/main/java/kr/co/mcmp/externalrepo/ExternalRepoService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package kr.co.mcmp.externalrepo;
22

33
import kr.co.mcmp.externalrepo.model.ArtifactHubPackage;
4-
import kr.co.mcmp.externalrepo.model.ArtifactHubRespository;
4+
import kr.co.mcmp.externalrepo.model.ArtifactHubRepository;
55
import kr.co.mcmp.externalrepo.model.DockerHubCatalog;
66
import lombok.extern.slf4j.Slf4j;
77

@@ -20,7 +20,7 @@ public class ExternalRepoService {
2020
@Autowired
2121
private DockerHubInterface dockerInt;
2222

23-
public List<ArtifactHubRespository> searchArtifactHubRepository(String keyword){
23+
public List<ArtifactHubRepository> searchArtifactHubRepository(String keyword){
2424
return artfInt.searchRepository(keyword);
2525

2626
}

src/main/java/kr/co/mcmp/externalrepo/model/ArtifactHubRespository.java renamed to src/main/java/kr/co/mcmp/externalrepo/model/ArtifactHubRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import lombok.Data;
44

55
@Data
6-
public class ArtifactHubRespository {
6+
public class ArtifactHubRepository {
77

88
public String repository_id;
99
public String name;

src/main/java/kr/co/mcmp/softwarecatalog/docker/service/DockerMonitoringService.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,7 @@ private List<DeploymentHistory> getActiveDeployments() {
6565
.max(Comparator.comparing(DeploymentHistory::getExecutedAt))
6666
.orElse(null)))
6767
.filter(Objects::nonNull)
68-
.filter(d -> ("RUN".equalsIgnoreCase(d.getActionType().name())
69-
|| "INSTALL".equalsIgnoreCase(d.getActionType().name()))
70-
&& "SUCCESS".equalsIgnoreCase(d.getStatus()))
68+
.filter(d -> ("RUN".equalsIgnoreCase(d.getActionType().name()) || "INSTALL".equalsIgnoreCase(d.getActionType().name())) && "SUCCESS".equalsIgnoreCase(d.getStatus()))
7169
.collect(Collectors.toList());
7270
}
7371

@@ -102,17 +100,14 @@ private void updateContainerHealth(DeploymentHistory deployment) {
102100

103101
private boolean isThresholdExceeded(SoftwareCatalog catalog, ContainerHealthInfo healthInfo) {
104102
if (healthInfo.getCpuUsage() != null && healthInfo.getMemoryUsage() != null) {
105-
boolean cpuExceeded = catalog.getCpuThreshold() != null
106-
&& healthInfo.getCpuUsage() > catalog.getCpuThreshold();
107-
boolean memoryExceeded = catalog.getMemoryThreshold() != null
108-
&& healthInfo.getMemoryUsage() > catalog.getMemoryThreshold();
103+
boolean cpuExceeded = catalog.getCpuThreshold() != null && healthInfo.getCpuUsage() > catalog.getCpuThreshold();
104+
boolean memoryExceeded = catalog.getMemoryThreshold() != null && healthInfo.getMemoryUsage() > catalog.getMemoryThreshold();
109105
return cpuExceeded || memoryExceeded;
110106
}
111107
return false;
112108
}
113109

114-
private void updateApplicationStatus(ApplicationStatus status, DeploymentHistory deployment,
115-
ContainerHealthInfo healthInfo) {
110+
private void updateApplicationStatus(ApplicationStatus status, DeploymentHistory deployment, ContainerHealthInfo healthInfo) {
116111
status.setCatalog(deployment.getCatalog());
117112
status.setStatus(healthInfo.getStatus());
118113
status.setDeploymentType(deployment.getDeploymentType());
Lines changed: 90 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
package kr.co.mcmp.softwarecatalog.kubernetes.service;
22

33
import java.io.IOException;
4+
import java.io.StringWriter;
45
import java.net.URI;
56
import java.nio.charset.StandardCharsets;
67
import java.nio.file.Files;
78
import java.nio.file.Path;
89

910
import org.springframework.stereotype.Service;
11+
import org.yaml.snakeyaml.DumperOptions;
12+
import org.yaml.snakeyaml.Yaml;
1013

1114
import com.marcnuri.helm.Helm;
1215
import com.marcnuri.helm.InstallCommand;
1316
import com.marcnuri.helm.Release;
1417

1518
import io.fabric8.kubernetes.client.Config;
1619
import io.fabric8.kubernetes.client.KubernetesClient;
17-
import kr.co.mcmp.ape.cbtumblebug.api.CbtumblebugRestApi;
18-
import kr.co.mcmp.ape.cbtumblebug.dto.K8sClusterDto;
1920
import kr.co.mcmp.softwarecatalog.SoftwareCatalog;
2021
import lombok.RequiredArgsConstructor;
2122
import lombok.extern.slf4j.Slf4j;
@@ -24,23 +25,46 @@
2425
@Slf4j
2526
@RequiredArgsConstructor
2627
public class HelmChartService {
27-
private final CbtumblebugRestApi api;
2828

29-
public Release deployHelmChart(KubernetesClient client, String namespace, SoftwareCatalog catalog,
30-
String clusterName) {
31-
Path tempConfigFile = null;
32-
K8sClusterDto dto = api.getK8sClusterByName(namespace, clusterName);
29+
30+
31+
public String convertConfigToYaml(Config config) {
32+
// SnakeYAML 설정
33+
DumperOptions options = new DumperOptions();
34+
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); // 블록 스타일 (가독성이 좋음)
35+
Yaml yaml = new Yaml(options);
36+
37+
// Config 객체를 YAML로 직렬화
38+
StringWriter writer = new StringWriter();
39+
yaml.dump(config, writer);
40+
41+
return writer.toString();
42+
}
43+
public Release deployHelmChart(KubernetesClient client, String namespace, SoftwareCatalog catalog) {
44+
Path tempKubeconfigPath = null;
45+
3346
try {
47+
// Helm repository 추가
3448
addHelmRepository(catalog);
35-
String kubeconfig = dto.getCspViewK8sClusterDetail().getAccessInfo().getKubeconfig();
36-
tempConfigFile = createTempKubeconfigFile(kubeconfig);
3749

50+
Config kubeConfig = client.getConfiguration();
51+
log.info("===========================kubeconfig");
52+
log.info(kubeConfig.toString()); // Config 객체 로그 확인
53+
log.info("===========================================");
54+
55+
// Config 객체를 YAML로 변환
56+
String kubeconfigYaml = convertConfigToYaml(kubeConfig); // Config를 YAML 형식으로 직렬화
57+
log.info(kubeconfigYaml);
58+
// 임시 kubeconfig 파일 생성
59+
tempKubeconfigPath = createTempKubeconfigFile(kubeConfig.toString()); // YAML 파일로 저장
60+
61+
// Helm 설치 명령어 생성
3862
InstallCommand installCommand = Helm.install(
3963
catalog.getHelmChart().getRepositoryName() + "/" + catalog.getHelmChart().getChartName())
40-
.withKubeConfig(tempConfigFile)
64+
.withKubeConfig(tempKubeconfigPath) // Path 타입으로 kubeconfig 전달
4165
.withName(catalog.getHelmChart().getChartName())
4266
.withNamespace(namespace)
43-
// .withVersion(catalog.getHelmChart().getChartVersion())
67+
.withVersion(catalog.getHelmChart().getChartVersion())
4468
.set("replicaCount", catalog.getMinReplicas())
4569
.set("image.repository", catalog.getHelmChart().getImageRepository())
4670
.set("image.tag", "latest")
@@ -49,81 +73,86 @@ public Release deployHelmChart(KubernetesClient client, String namespace, Softwa
4973
.set("resources.requests.cpu", catalog.getMinCpu().toString())
5074
.set("resources.requests.memory", catalog.getMinMemory() + "Mi")
5175
.set("resources.limits.cpu", catalog.getRecommendedCpu().toString())
52-
.set("resources.limits.memory", catalog.getRecommendedMemory() + "Mi")
53-
.set("persistence.enabled", false)
54-
.set("securityContext.enabled", false)
55-
.set("serviceAccount.create", true)
56-
// .with(300)
57-
.waitReady();
76+
.set("resources.limits.memory", catalog.getRecommendedMemory() + "Mi");
5877

78+
// HPA 설정 추가
5979
if (Boolean.TRUE.equals(catalog.getHpaEnabled())) {
6080
installCommand
61-
.set("autoscaling.enabled", true)
62-
.set("autoscaling.minReplicas", catalog.getMinReplicas())
63-
.set("autoscaling.maxReplicas", catalog.getMaxReplicas())
64-
.set("autoscaling.targetCPUUtilizationPercentage", catalog.getCpuThreshold())
65-
.set("autoscaling.targetMemoryUtilizationPercentage", catalog.getMemoryThreshold());
81+
.set("autoscaling.enabled", true)
82+
.set("autoscaling.minReplicas", catalog.getMinReplicas())
83+
.set("autoscaling.maxReplicas", catalog.getMaxReplicas())
84+
.set("autoscaling.targetCPUUtilizationPercentage", catalog.getCpuThreshold().intValue())
85+
.set("autoscaling.targetMemoryUtilizationPercentage", catalog.getMemoryThreshold().intValue());
6686
}
6787

88+
// Helm 차트 설치 실행
6889
Release result = installCommand.call();
69-
log.info("Helm Chart '{}' 배포 완료 - namespace: {}",
70-
catalog.getHelmChart().getChartName(),
71-
namespace);
90+
91+
log.info("Helm Chart '{}' 버전 '{}'가 네임스페이스 '{}'에 배포됨 (HPA: {})",
92+
catalog.getHelmChart().getChartName(),
93+
"latest",
94+
namespace,
95+
catalog.getHpaEnabled());
7296
return result;
7397

7498
} catch (Exception e) {
7599
log.error("Helm Chart 배포 중 오류 발생", e);
76100
throw new RuntimeException("Helm Chart 배포 실패", e);
77101
} finally {
78-
deleteTempFile(tempConfigFile);
102+
// 임시 kubeconfig 파일 삭제
103+
if (tempKubeconfigPath != null) {
104+
try {
105+
deleteTempFile(tempKubeconfigPath);
106+
} catch (IOException e) {
107+
log.warn("임시 kubeconfig 파일 삭제 실패: {}", tempKubeconfigPath, e);
108+
}
109+
}
110+
}
111+
}
112+
113+
public void uninstallHelmChart(String namespace, SoftwareCatalog catalog) {
114+
try {
115+
String result = Helm.uninstall(catalog.getHelmChart().getChartName())
116+
.withNamespace(namespace)
117+
.call();
118+
119+
boolean deleted = result != null && !result.isEmpty();
120+
121+
if (deleted) {
122+
log.info("Helm Release '{}' 가 네임스페이스 '{}'에서 삭제됨",
123+
catalog.getHelmChart().getChartName(), namespace);
124+
} else {
125+
log.warn("Helm Release '{}' 삭제 실패",
126+
catalog.getHelmChart().getChartName());
127+
}
128+
} catch (Exception e) {
129+
log.error("Helm Release 삭제 중 오류 발생", e);
130+
throw new RuntimeException("Helm Release 삭제 실패", e);
79131
}
80132
}
81133

82134
private void addHelmRepository(SoftwareCatalog catalog) throws Exception {
83135
Helm.repo().add()
84-
.withName(catalog.getHelmChart().getRepositoryName())
85-
.withUrl(URI.create(catalog.getHelmChart().getChartRepositoryUrl()))
86-
.call();
136+
.withName(catalog.getHelmChart().getRepositoryName())
137+
.withUrl(URI.create(catalog.getHelmChart().getChartRepositoryUrl()))
138+
.call();
87139
Helm.repo().update();
88140
}
89141

142+
// kubeconfig 형식이 유효한지 확인하는 메서드
143+
private boolean isValidKubeconfig(String kubeconfig) {
144+
return kubeconfig.contains("apiVersion") && kubeconfig.contains("kind");
145+
}
146+
147+
// 임시 kubeconfig 파일 생성
90148
private Path createTempKubeconfigFile(String kubeconfig) throws IOException {
91149
Path tempFile = Files.createTempFile("kubeconfig", ".yaml");
92150
Files.write(tempFile, kubeconfig.getBytes(StandardCharsets.UTF_8));
93151
return tempFile;
94152
}
95153

96-
private void deleteTempFile(Path tempFile) {
97-
if (tempFile != null) {
98-
try {
99-
Files.deleteIfExists(tempFile);
100-
} catch (IOException e) {
101-
log.warn("임시 파일 삭제 실패: {}", tempFile, e);
102-
}
103-
}
104-
}
105-
106-
public void uninstallHelmChart(String namespace, SoftwareCatalog catalog, String clusterName) {
107-
Path tempConfigFile = null;
108-
try {
109-
K8sClusterDto dto = api.getK8sClusterByName(namespace, clusterName);
110-
String kubeconfig = dto.getCspViewK8sClusterDetail().getAccessInfo().getKubeconfig();
111-
tempConfigFile = createTempKubeconfigFile(kubeconfig);
112-
113-
String result = Helm.uninstall(catalog.getHelmChart().getChartName())
114-
.withKubeConfig(tempConfigFile)
115-
.withNamespace(namespace)
116-
.call();
117-
118-
log.info("Helm Release '{}' 삭제 완료 - namespace: {}",
119-
catalog.getHelmChart().getChartName(),
120-
namespace);
121-
122-
} catch (Exception e) {
123-
log.error("Helm Release 삭제 실패", e);
124-
throw new RuntimeException("Helm Release 삭제 실패", e);
125-
} finally {
126-
deleteTempFile(tempConfigFile);
127-
}
154+
// 임시 kubeconfig 파일 삭제
155+
private void deleteTempFile(Path tempFile) throws IOException {
156+
Files.deleteIfExists(tempFile);
128157
}
129158
}

src/main/java/kr/co/mcmp/softwarecatalog/kubernetes/service/KubernetesDeployService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public DeploymentHistory deployApplication(String namespace, String clusterName,
3434
// KubernetesClient client = clientFactory.getClient(namespace, clusterName);
3535
namespaceService.ensureNamespaceExists(client, namespace);
3636

37-
Release result = helmChartService.deployHelmChart(client, namespace, catalog, clusterName);
37+
Release result = helmChartService.deployHelmChart(client, namespace, catalog);
3838

3939
String podStatus = KubernetesUtils.getPodStatus(client, namespace, catalog.getHelmChart().getChartName());
4040
Integer servicePort = KubernetesUtils.getServicePort(client, namespace,
@@ -68,7 +68,7 @@ public DeploymentHistory stopApplication(String namespace, String clusterName, S
6868
try {
6969
KubernetesClient client = clientFactory.getClient(namespace, clusterName);
7070

71-
helmChartService.uninstallHelmChart(namespace, catalog, clusterName);
71+
helmChartService.uninstallHelmChart(namespace, catalog);
7272

7373
String podStatus = KubernetesUtils.getPodStatus(client, namespace, catalog.getHelmChart().getChartName());
7474
Integer servicePort = KubernetesUtils.getServicePort(client, namespace,

src/main/java/kr/co/mcmp/softwarecatalog/kubernetes/service/KubernetesOperationService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public void stopApplication(String namespace, String clusterName, SoftwareCatalo
7373

7474
public void uninstallApplication(String namespace, String clusterName, SoftwareCatalog catalog, String username) {
7575
try {
76-
helmChartService.uninstallHelmChart(namespace, catalog,clusterName);
76+
// helmChartService.uninstallHelmChart(namespace, catalog,clusterName);
7777
} catch (Exception e) {
7878
log.error("애플리케이션 제거 중 오류 발생", e);
7979
throw new RuntimeException("애플리케이션 제거 실패", e);

src/main/resources/import.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,8 @@ VALUES
335335
-- Insert into oss
336336
INSERT INTO oss (oss_idx, oss_type_idx, oss_name, oss_desc, oss_url, oss_username, oss_password)
337337
VALUES
338-
(1, 1, 'NEXUS', 'Application Repository', 'http://mc-application-manager-sonatype-nexus:8081', admin, 123456);
338+
(1, 1, 'NEXUS', 'Application Repository', 'http://mc-application-manager-sonatype-nexus:8081', 'admin', 'cZr3xQjjYfwMQNBO/ebJbQ==');
339339
INSERT INTO oss (oss_idx, oss_type_idx, oss_name, oss_desc, oss_url, oss_username, oss_password)
340340
VALUES
341-
(2, 2, 'Ape', 'Application Provisioning Engine', 'http://mc-application-manager-jenkins:8080', admin, 123456);
341+
(2, 2, 'Ape', 'Application Provisioning Engine', 'http://mc-application-manager-jenkins:8080', 'admin', 'cZr3xQjjYfwMQNBO/ebJbQ==');
342342

0 commit comments

Comments
 (0)