diff --git a/justfile b/justfile new file mode 100644 index 00000000..4aaaf173 --- /dev/null +++ b/justfile @@ -0,0 +1,2 @@ +test: + go test -tags testing ./... diff --git a/pkg/cluster/selfmanaged/install_self_managed_cluster_service.go b/pkg/cluster/selfmanaged/install_self_managed_cluster_service.go index cd47eb90..47a387e1 100644 --- a/pkg/cluster/selfmanaged/install_self_managed_cluster_service.go +++ b/pkg/cluster/selfmanaged/install_self_managed_cluster_service.go @@ -6,6 +6,7 @@ import ( "gopkg.in/yaml.v3" "os" "path/filepath" + "regexp" "strings" "github.com/qovery/qovery-cli/pkg/cluster" @@ -153,31 +154,26 @@ func (service *InstallSelfManagedClusterService) InstallCluster() (*string, erro if err != nil { return nil, err } - clusterHelmValuesContent := *resultClusterHelmValuesContent + helmValues := *resultClusterHelmValuesContent // inject the email for Cert Manager - clusterHelmValuesContent = strings.ReplaceAll(clusterHelmValuesContent, "acme@qovery.com", email) - - finalClusterHelmValuesContent := fmt.Sprintf("%s\n", clusterHelmValuesContent) + helmValues = strings.ReplaceAll(helmValues, "acme@qovery.com", email) + helmValues = fmt.Sprintf("%s\n", helmValues) // trim lines if they start with "qovery:" or if they contain "set-by-customer" - content, err := service.selfManagedClusterService.GetBaseHelmValuesContent(cloudProviderType) + qoveryHelmValues, err := service.selfManagedClusterService.GetBaseHelmValuesContent(cloudProviderType) if err != nil { return nil, err } - for _, line := range strings.Split(*content, "\n") { - if strings.HasPrefix(line, "qovery:") || strings.Contains(line, "set-by-customer") { - continue - } - finalClusterHelmValuesContent += line + "\n" - } + + helmValues += stripQoverySection(*qoveryHelmValues) if strings.Contains(kubernetesType, "Azure") { - contentWithAKSValues, err := injectAzureAKSValues(finalClusterHelmValuesContent) + contentWithAKSValues, err := injectAzureAKSValues(helmValues) if err != nil { return nil, err } - finalClusterHelmValuesContent = *contentWithAKSValues + helmValues = *contentWithAKSValues } // generate the helm values file and output it to the user to ./values-.yaml @@ -198,7 +194,7 @@ func (service *InstallSelfManagedClusterService) InstallCluster() (*string, erro return nil, err } - err = service.fileWriterService.WriteFile(helmValuesFileName, []byte(finalClusterHelmValuesContent), 0644) + err = service.fileWriterService.WriteFile(helmValuesFileName, []byte(helmValues), 0644) if err != nil { return nil, err @@ -209,6 +205,14 @@ func (service *InstallSelfManagedClusterService) InstallCluster() (*string, erro return nil, nil } +func stripQoverySection(qoveryHelmValues string) string { + // Erase the qovery: yaml section to replace it with correct fetched values for this cluster + // We can't use yaml parser here, because the yaml file contains anchor (&toto *toto) and parsing it will cause those + // anchors to be replaced with the incorrect values... + re := regexp.MustCompile("(?m)^qovery:\n( .*\n)+") + return re.ReplaceAllString(qoveryHelmValues, "") +} + func outputCommandsToInstallQoveryOnCluster(helmValuesFileName string) { // give instruction to the user to install the cluster utils.Println("") diff --git a/pkg/cluster/selfmanaged/install_self_managed_cluster_service_test.go b/pkg/cluster/selfmanaged/install_self_managed_cluster_service_test.go index 13116e99..578f6930 100644 --- a/pkg/cluster/selfmanaged/install_self_managed_cluster_service_test.go +++ b/pkg/cluster/selfmanaged/install_self_managed_cluster_service_test.go @@ -357,3 +357,143 @@ func TestReuseExistingCluster(t *testing.T) { assert.Nil(t, err) }) } + +func TestStripQoverySection(t *testing.T) { + helmValues := ` +services: + qovery: + qovery-cluster-agent: + enabled: true + qovery-shell-agent: + enabled: true + qovery-engine: + enabled: true + qovery-priority-class: + enabled: true + ingress: + ingress-nginx: + enabled: true + dns: + external-dns: + enabled: true + logging: + loki: + enabled: true + promtail: + enabled: true + certificates: + cert-manager: + enabled: true + cert-manager-configs: + enabled: true + qovery-cert-manager-webhook: + enabled: true + observability: + metrics-server: + enabled: true + aws: + q-storageclass-aws: + enabled: true + aws-ebs-csi-driver: + enabled: false + aws-load-balancer-controller: + enabled: false + gcp: + q-storageclass-gcp: + enabled: false + scaleway: + q-storageclass-scaleway: + enabled: false +qovery: + clusterId: &clusterId set-by-customer + clusterShortId: &clusterShortId set-by-customer + organizationId: &organizationId set-by-customer + jwtToken: &jwtToken set-by-customer + rootDomain: &rootDomain set-by-customer + domain: &domain set-by-customer + domainWildcard: &domainWildcard set-by-customer + qoveryDnsUrl: &qoveryDnsUrl set-by-customer + agentGatewayUrl: &agentGatewayUrl set-by-customer + engineGatewayUrl: &engineGatewayUrl set-by-customer + lokiUrl: &lokiUrl set-by-customer + promtailLokiUrl: &promtailLokiUrl set-by-customer + acmeEmailAddr: &acmeEmailAddr set-by-customer + externalDnsPrefix: &externalDnsPrefix set-by-customer + architectures: &architectures set-by-customer + engineVersion: &engineVersion set-by-customer + shellAgentVersion: &shellAgentVersion set-by-customer + clusterAgentVersion: &clusterAgentVersion set-by-customer +qovery-cluster-agent: + fullnameOverride: qovery-shell-agent + image: + tag: *clusterAgentVersion + environmentVariables: + CLUSTER_ID: *clusterId + CLUSTER_JWT_TOKEN: *jwtToken + GRPC_SERVER: *agentGatewayUrl + LOKI_URL: *lokiUrl + ORGANIZATION_ID: *organizationId + useSelfSignCertificate: true +` + resultHelmValues := ` +services: + qovery: + qovery-cluster-agent: + enabled: true + qovery-shell-agent: + enabled: true + qovery-engine: + enabled: true + qovery-priority-class: + enabled: true + ingress: + ingress-nginx: + enabled: true + dns: + external-dns: + enabled: true + logging: + loki: + enabled: true + promtail: + enabled: true + certificates: + cert-manager: + enabled: true + cert-manager-configs: + enabled: true + qovery-cert-manager-webhook: + enabled: true + observability: + metrics-server: + enabled: true + aws: + q-storageclass-aws: + enabled: true + aws-ebs-csi-driver: + enabled: false + aws-load-balancer-controller: + enabled: false + gcp: + q-storageclass-gcp: + enabled: false + scaleway: + q-storageclass-scaleway: + enabled: false +qovery-cluster-agent: + fullnameOverride: qovery-shell-agent + image: + tag: *clusterAgentVersion + environmentVariables: + CLUSTER_ID: *clusterId + CLUSTER_JWT_TOKEN: *jwtToken + GRPC_SERVER: *agentGatewayUrl + LOKI_URL: *lokiUrl + ORGANIZATION_ID: *organizationId + useSelfSignCertificate: true +` + t.Run("Should strip qovery section for yanl file", func(t *testing.T) { + ret := stripQoverySection(helmValues) + assert.Equal(t, resultHelmValues, ret) + }) +}