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

Azure update #107

Merged
merged 1 commit into from
Jan 15, 2025
Merged
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 @@ -19,6 +19,7 @@
import kr.co.mcmp.softwarecatalog.SoftwareCatalog;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
@RequiredArgsConstructor
Expand All @@ -29,26 +30,32 @@ public Release deployHelmChart(KubernetesClient client, String namespace, Softwa
String clusterName) {
Path tempConfigFile = null;
K8sClusterDto dto = api.getK8sClusterByName(namespace, clusterName);
String cspType = dto.getConnectionConfig().getProviderName().toUpperCase();
log.info("CSP Type: {}", cspType);

try {
addHelmRepository(catalog);
String kubeconfig = dto.getCspViewK8sClusterDetail().getAccessInfo().getKubeconfig();

// GCP의 경우 gke-gcloud-auth-plugin 관련 설정 제거
if ("GCP".equalsIgnoreCase(cspType)) {
kubeconfig = kubeconfig.replaceAll("command:\\s*gke-gcloud-auth-plugin", "")
.replaceAll("apiVersion:\\s*client.authentication.k8s.io/.*\\n", "")
.replaceAll("installHint:.*\\n", "")
.replaceAll("provideClusterInfo:.*\\n", "");
}
tempConfigFile = createTempKubeconfigFile(kubeconfig);

InstallCommand installCommand = createInstallCommand(catalog, namespace, tempConfigFile);
applyCspSpecificSettings(installCommand, cspType);
InstallCommand installCommand = Helm.install(
catalog.getHelmChart().getRepositoryName() + "/" + catalog.getHelmChart().getChartName())
.withKubeConfig(tempConfigFile)
.withName(catalog.getHelmChart().getChartName())
.withNamespace(namespace)
.withVersion(catalog.getHelmChart().getChartVersion())
.set("replicaCount", catalog.getMinReplicas())
.set("image.repository", catalog.getHelmChart().getImageRepository())
.set("image.tag", "latest")
.set("image.pullPolicy", "Always")
.set("service.port", catalog.getDefaultPort())
.set("resources.requests.cpu", catalog.getMinCpu().toString())
.set("resources.requests.memory", catalog.getMinMemory() + "Mi")
.set("resources.limits.cpu", catalog.getRecommendedCpu().toString())
.set("resources.limits.memory", catalog.getRecommendedMemory() + "Mi")
.set("persistence.enabled", false)
.set("securityContext.enabled", false)
.set("serviceAccount.create", true)
.withTimeout(300)
.waitReady();

// HPA 설정
if (Boolean.TRUE.equals(catalog.getHpaEnabled())) {
installCommand
.set("autoscaling.enabled", true)
Expand All @@ -59,60 +66,19 @@ public Release deployHelmChart(KubernetesClient client, String namespace, Softwa
}

Release result = installCommand.call();
log.info("Helm Chart '{}' 배포 완료 - namespace: {}, CSP: {}",
log.info("Helm Chart '{}' 배포 완료 - namespace: {}",
catalog.getHelmChart().getChartName(),
namespace,
cspType);
namespace);
return result;

} catch (Exception e) {
log.error("Helm Chart 배포 중 오류 발생 - CSP: {}", cspType, e);
log.error("Helm Chart 배포 중 오류 발생", e);
throw new RuntimeException("Helm Chart 배포 실패", e);
} finally {
deleteTempFile(tempConfigFile);
}
}


private InstallCommand createInstallCommand(SoftwareCatalog catalog, String namespace, Path configFile) {
return Helm.install(catalog.getHelmChart().getRepositoryName() + "/" + catalog.getHelmChart().getChartName())
.withKubeConfig(configFile)
.withName(catalog.getHelmChart().getChartName())
.withNamespace(namespace)
.withVersion(catalog.getHelmChart().getChartVersion())
.set("replicaCount", catalog.getMinReplicas())
.set("image.repository", catalog.getHelmChart().getImageRepository())
.set("image.tag", "latest")
.set("image.pullPolicy", "Always")
.set("service.port", catalog.getDefaultPort())
.set("resources.requests.cpu", catalog.getMinCpu().toString())
.set("resources.requests.memory", catalog.getMinMemory() + "Mi")
.set("resources.limits.cpu", catalog.getRecommendedCpu().toString())
.set("resources.limits.memory", catalog.getRecommendedMemory() + "Mi")
.set("persistence.enabled", false)
.set("securityContext.enabled", false)
.set("serviceAccount.create", true)
.withTimeout(300)
.waitReady();
}

private void applyCspSpecificSettings(InstallCommand command, String cspType) {
log.info("Applying CSP specific settings for: {}", cspType);
switch (cspType) {
case "GCP":
command.set("gcp.auth.enabled", false)
.set("serviceAccount.annotations.iam\\.gke\\.io/gcp-service-account", "false")
.set("rbac.create", true);
break;
case "AZURE":
command.set("azure.auth.enabled", false)
.set("serviceAccount.annotations.azure\\.workload\\.identity/use", "false");
break;
default:
log.warn("알 수 없는 CSP 타입: {}", cspType);
}
}

private void addHelmRepository(SoftwareCatalog catalog) throws Exception {
Helm.repo().add()
.withName(catalog.getHelmChart().getRepositoryName())
Expand Down Expand Up @@ -141,27 +107,17 @@ public void uninstallHelmChart(String namespace, SoftwareCatalog catalog, String
Path tempConfigFile = null;
try {
K8sClusterDto dto = api.getK8sClusterByName(namespace, clusterName);
String cspType = dto.getConnectionConfig().getProviderName().toUpperCase();
String kubeconfig = dto.getCspViewK8sClusterDetail().getAccessInfo().getKubeconfig();

// GCP의 경우 gke-gcloud-auth-plugin 관련 설정 제거
if ("GCP".equalsIgnoreCase(cspType)) {
kubeconfig = kubeconfig.replaceAll("command:\\s*gke-gcloud-auth-plugin", "")
.replaceAll("apiVersion:\\s*client.authentication.k8s.io/.*\\n", "")
.replaceAll("installHint:.*\\n", "")
.replaceAll("provideClusterInfo:.*\\n", "");
}
tempConfigFile = createTempKubeconfigFile(kubeconfig);

String result = Helm.uninstall(catalog.getHelmChart().getChartName())
.withKubeConfig(tempConfigFile)
.withNamespace(namespace)
.call();

log.info("Helm Release '{}' 삭제 완료 - namespace: {}, CSP: {}",
log.info("Helm Release '{}' 삭제 완료 - namespace: {}",
catalog.getHelmChart().getChartName(),
namespace,
cspType);
namespace);

} catch (Exception e) {
log.error("Helm Release 삭제 실패", e);
Expand Down