From abf11c7d717491a3693a2cb46f4d2b9e867482da Mon Sep 17 00:00:00 2001 From: Marek Markiewka Date: Fri, 16 Apr 2021 13:53:14 +0200 Subject: [PATCH 01/17] first draft of argocd_helm_gitrepo implementation Signed-off-by: Marek Markiewka --- .../deployment/{ => helm}/Helm.groovy | 33 ++++++++++---- .../helmrelease/ArgoCDGitRepoRelease.groovy | 44 +++++++++++++++++++ .../helmrelease/FluxV1GitRepoRelease.groovy | 29 ++++++++++++ .../helmrelease/FluxV1HelmRepoRelease.groovy | 29 ++++++++++++ .../helm/helmrelease/HelmRelease.groovy | 27 ++++++++++++ .../{ => helm}/repotype/GitRepo.groovy | 29 +++--------- .../{ => helm}/repotype/HelmRepo.groovy | 24 +--------- .../deployment/helm/repotype/RepoType.groovy | 22 ++++++++++ .../deployment/{ => plain}/Plain.groovy | 4 +- .../deployment/repotype/RepoType.groovy | 38 ---------------- .../gitopsbuildlib/DeployViaGitopsTest.groovy | 13 +++--- .../gitopsbuildlib/deployment/HelmTest.groovy | 1 + .../deployment/PlainTest.groovy | 1 + .../{ => helm}/repotype/GitRepoTest.groovy | 2 +- .../{ => helm}/repotype/HelmRepoTest.groovy | 2 +- .../{ => helm}/repotype/RepoTypeTest.groovy | 2 +- vars/deployViaGitops.groovy | 8 ++-- 17 files changed, 201 insertions(+), 107 deletions(-) rename src/com/cloudogu/gitopsbuildlib/deployment/{ => helm}/Helm.groovy (62%) create mode 100644 src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/ArgoCDGitRepoRelease.groovy create mode 100644 src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/FluxV1GitRepoRelease.groovy create mode 100644 src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/FluxV1HelmRepoRelease.groovy create mode 100644 src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/HelmRelease.groovy rename src/com/cloudogu/gitopsbuildlib/deployment/{ => helm}/repotype/GitRepo.groovy (66%) rename src/com/cloudogu/gitopsbuildlib/deployment/{ => helm}/repotype/HelmRepo.groovy (59%) create mode 100644 src/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/RepoType.groovy rename src/com/cloudogu/gitopsbuildlib/deployment/{ => plain}/Plain.groovy (89%) delete mode 100644 src/com/cloudogu/gitopsbuildlib/deployment/repotype/RepoType.groovy rename test/com/cloudogu/gitopsbuildlib/deployment/{ => helm}/repotype/GitRepoTest.groovy (96%) rename test/com/cloudogu/gitopsbuildlib/deployment/{ => helm}/repotype/HelmRepoTest.groovy (96%) rename test/com/cloudogu/gitopsbuildlib/deployment/{ => helm}/repotype/RepoTypeTest.groovy (95%) diff --git a/src/com/cloudogu/gitopsbuildlib/deployment/Helm.groovy b/src/com/cloudogu/gitopsbuildlib/deployment/helm/Helm.groovy similarity index 62% rename from src/com/cloudogu/gitopsbuildlib/deployment/Helm.groovy rename to src/com/cloudogu/gitopsbuildlib/deployment/helm/Helm.groovy index a561710..477543c 100644 --- a/src/com/cloudogu/gitopsbuildlib/deployment/Helm.groovy +++ b/src/com/cloudogu/gitopsbuildlib/deployment/helm/Helm.groovy @@ -1,19 +1,34 @@ -package com.cloudogu.gitopsbuildlib.deployment +package com.cloudogu.gitopsbuildlib.deployment.helm -import com.cloudogu.gitopsbuildlib.deployment.repotype.GitRepo -import com.cloudogu.gitopsbuildlib.deployment.repotype.HelmRepo -import com.cloudogu.gitopsbuildlib.deployment.repotype.RepoType +import com.cloudogu.gitopsbuildlib.deployment.Deployment +import com.cloudogu.gitopsbuildlib.deployment.helm.helmrelease.ArgoCDGitRepoRelease +import com.cloudogu.gitopsbuildlib.deployment.helm.helmrelease.FluxV1GitRepoRelease +import com.cloudogu.gitopsbuildlib.deployment.helm.helmrelease.FluxV1HelmRepoRelease +import com.cloudogu.gitopsbuildlib.deployment.helm.helmrelease.HelmRelease +import com.cloudogu.gitopsbuildlib.deployment.helm.repotype.GitRepo +import com.cloudogu.gitopsbuildlib.deployment.helm.repotype.HelmRepo +import com.cloudogu.gitopsbuildlib.deployment.helm.repotype.RepoType class Helm extends Deployment { - protected RepoType helm + protected RepoType helmRepo + protected HelmRelease helmRelease Helm(def script, def gitopsConfig) { super(script, gitopsConfig) + if(gitopsConfig.gitopsTool == 'FLUX') { + if (gitopsConfig.deployments.helm.repoType == 'GIT') { + helmRelease = new FluxV1GitRepoRelease(script) + } else if (gitopsConfig.deployments.helm.repoType == 'HELM') { + helmRelease = new FluxV1HelmRepoRelease(script) + } + } else if(gitopsConfig.gitopsTool == 'ARGO_CD') { + helmRelease = new ArgoCDGitRepoRelease(script) + } if (gitopsConfig.deployments.helm.repoType == 'GIT') { - helm = new GitRepo(script) + helmRepo = new GitRepo(script) } else if (gitopsConfig.deployments.helm.repoType == 'HELM') { - helm = new HelmRepo(script) + helmRepo = new HelmRepo(script) } } @@ -26,10 +41,10 @@ class Helm extends Deployment { // writing the merged-values.yaml via writeYaml into a file has the advantage, that it gets formatted as valid yaml // This makes it easier to read in and indent for the inline use in the helmRelease. // It enables us to reuse the `fileToInlineYaml` function, without writing a complex formatting logic. - script.writeFile file: "${stage}/${application}/mergedValues.yaml", text: helm.mergeValues(helmConfig, ["${script.env.WORKSPACE}/${sourcePath}/values-${stage}.yaml", "${script.env.WORKSPACE}/${sourcePath}/values-shared.yaml"] as String[]) + script.writeFile file: "${stage}/${application}/mergedValues.yaml", text: helmRepo.mergeValues(helmConfig, ["${script.env.WORKSPACE}/${sourcePath}/values-${stage}.yaml", "${script.env.WORKSPACE}/${sourcePath}/values-shared.yaml"] as String[]) updateYamlValue("${stage}/${application}/mergedValues.yaml", helmConfig) - script.writeFile file: "${stage}/${application}/helmRelease.yaml", text: helm.createHelmRelease(helmConfig, application, getNamespace(stage), "${stage}/${application}/mergedValues.yaml") + script.writeFile file: "${stage}/${application}/helmRelease.yaml", text: helmRelease.create(helmConfig, application, getNamespace(stage), "${stage}/${application}/mergedValues.yaml") // since the values are already inline (helmRelease.yaml) we do not need to commit them into the gitops repo script.sh "rm ${stage}/${application}/mergedValues.yaml" } diff --git a/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/ArgoCDGitRepoRelease.groovy b/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/ArgoCDGitRepoRelease.groovy new file mode 100644 index 0000000..9182d98 --- /dev/null +++ b/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/ArgoCDGitRepoRelease.groovy @@ -0,0 +1,44 @@ +package com.cloudogu.gitopsbuildlib.deployment.helm.helmrelease + +class ArgoCDGitRepoRelease extends HelmRelease{ + + protected static String getHelmImage() { 'ghcr.io/cloudogu/helm:3.5.4-1' } + + ArgoCDGitRepoRelease(def script) { + super(script) + } + + @Override + String create(Map helmConfig, String application, String namespace, String valuesFile) { + def credentialsId = helmConfig.credentialsId + def myGit = script.cesBuildLib.Git.new(this, credentialsId) + + script.dir("${script.env.WORKSPACE}/helmChart") { + script.git credentialsId: credentialsId, url: helmConfig.helmChartRepository, branch: 'main', changelog: false, poll: false + if(helmConfig.containsKey('version') && helmConfig.version) { + myGit.fetch() + myGit.checkout(helmConfig.version) + } + } + + String helmRelease = "" + + withHelm { + script.dir("${script.env.WORKSPACE}/helmChart") { + script.sh "helm dep update ." + String templateScript = "helm template ${application} . -f ${valuesFile}" + helmRelease = script.sh returnStdout: true, script: templateScript + } + } + + return helmRelease + } + + void withHelm(Closure body) { + script.cesBuildLib.Docker.new(script).image(helmImage).inside( + "${script.pwd().equals(script.env.WORKSPACE) ? '' : "-v ${script.env.WORKSPACE}:${script.env.WORKSPACE}"}" + ) { + body() + } + } +} diff --git a/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/FluxV1GitRepoRelease.groovy b/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/FluxV1GitRepoRelease.groovy new file mode 100644 index 0000000..c02ae44 --- /dev/null +++ b/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/FluxV1GitRepoRelease.groovy @@ -0,0 +1,29 @@ +package com.cloudogu.gitopsbuildlib.deployment.helm.helmrelease + +class FluxV1GitRepoRelease extends HelmRelease{ + + FluxV1GitRepoRelease(def script) { + super(script) + } + + @Override + String create(Map helmConfig, String application, String namespace, String valuesFile) { + def values = fileToInlineYaml(valuesFile) + return """apiVersion: helm.fluxcd.io/v1 +kind: HelmRelease +metadata: + name: ${application} + namespace: ${namespace} + annotations: + fluxcd.io/automated: "false" +spec: + releaseName: ${application} + chart: + git: ${helmConfig.repoUrl} + ref: ${helmConfig.version} + path: . + values: +${values} +""" + } +} diff --git a/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/FluxV1HelmRepoRelease.groovy b/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/FluxV1HelmRepoRelease.groovy new file mode 100644 index 0000000..0c60e22 --- /dev/null +++ b/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/FluxV1HelmRepoRelease.groovy @@ -0,0 +1,29 @@ +package com.cloudogu.gitopsbuildlib.deployment.helm.helmrelease + +class FluxV1HelmRepoRelease extends HelmRelease{ + + FluxV1HelmRepoRelease(def script) { + super(script) + } + + @Override + String create(Map helmConfig, String application, String namespace, String valuesFile) { + def values = fileToInlineYaml(valuesFile) + return """apiVersion: helm.fluxcd.io/v1 +kind: HelmRelease +metadata: + name: ${application} + namespace: ${namespace} + annotations: + fluxcd.io/automated: "false" +spec: + releaseName: ${application} + chart: + repository: ${helmConfig.repoUrl} + name: ${helmConfig.chartName} + version: ${helmConfig.version} + values: +${values} +""" + } +} diff --git a/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/HelmRelease.groovy b/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/HelmRelease.groovy new file mode 100644 index 0000000..013a8d6 --- /dev/null +++ b/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/HelmRelease.groovy @@ -0,0 +1,27 @@ +package com.cloudogu.gitopsbuildlib.deployment.helm.helmrelease + +abstract class HelmRelease { + + protected script + + HelmRelease(def script) { + this.script = script + } + + abstract String create(Map helmConfig, String application, String namespace, String valuesFile) + + String fileToInlineYaml(String fileContents) { + String values = "" + String indent = " " + String fileContent = script.readFile fileContents + fileContent.split("\n").each { line -> + if(line.size() > 0) { + values += indent + line + "\n" + } else { + values += line + "\n" + } + } + // remove unnecessary last blank line + return values.substring(0, values.lastIndexOf('\n')) + } +} diff --git a/src/com/cloudogu/gitopsbuildlib/deployment/repotype/GitRepo.groovy b/src/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/GitRepo.groovy similarity index 66% rename from src/com/cloudogu/gitopsbuildlib/deployment/repotype/GitRepo.groovy rename to src/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/GitRepo.groovy index 2f390f4..4af4afa 100644 --- a/src/com/cloudogu/gitopsbuildlib/deployment/repotype/GitRepo.groovy +++ b/src/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/GitRepo.groovy @@ -1,4 +1,4 @@ -package com.cloudogu.gitopsbuildlib.deployment.repotype +package com.cloudogu.gitopsbuildlib.deployment.helm.repotype class GitRepo extends RepoType { @@ -14,11 +14,17 @@ class GitRepo extends RepoType { _files += "-f $it " } + def myGit = script.cesBuildLib.Git.new(this, helmConfig.credentialsId) + script.dir("${script.env.WORKSPACE}/chart") { if (helmConfig.containsKey('credentialsId')) { script.git credentialsId: helmConfig.credentialsId, url: helmConfig.repoUrl, branch: 'main', changelog: false, poll: false + myGit.fetch() + myGit.checkout(helmConfig.version) } else { script.git url: helmConfig.repoUrl, branch: 'main', changelog: false, poll: false + myGit.fetch() + myGit.checkout(helmConfig.version) } } @@ -36,25 +42,4 @@ class GitRepo extends RepoType { return merge } - - @Override - String createHelmRelease(Map helmConfig, String application, String namespace, String valuesFile) { - def values = fileToInlineYaml(valuesFile) - return """apiVersion: helm.fluxcd.io/v1 -kind: HelmRelease -metadata: - name: ${application} - namespace: ${namespace} - annotations: - fluxcd.io/automated: "false" -spec: - releaseName: ${application} - chart: - git: ${helmConfig.repoUrl} - ref: ${helmConfig.version} - path: . - values: -${values} -""" - } } diff --git a/src/com/cloudogu/gitopsbuildlib/deployment/repotype/HelmRepo.groovy b/src/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/HelmRepo.groovy similarity index 59% rename from src/com/cloudogu/gitopsbuildlib/deployment/repotype/HelmRepo.groovy rename to src/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/HelmRepo.groovy index ca5337d..11e1106 100644 --- a/src/com/cloudogu/gitopsbuildlib/deployment/repotype/HelmRepo.groovy +++ b/src/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/HelmRepo.groovy @@ -1,4 +1,4 @@ -package com.cloudogu.gitopsbuildlib.deployment.repotype +package com.cloudogu.gitopsbuildlib.deployment.helm.repotype class HelmRepo extends RepoType{ @@ -26,26 +26,4 @@ class HelmRepo extends RepoType{ return merge } - - - @Override - String createHelmRelease(Map helmConfig, String application, String namespace, String valuesFile) { - def values = fileToInlineYaml(valuesFile) - return """apiVersion: helm.fluxcd.io/v1 -kind: HelmRelease -metadata: - name: ${application} - namespace: ${namespace} - annotations: - fluxcd.io/automated: "false" -spec: - releaseName: ${application} - chart: - repository: ${helmConfig.repoUrl} - name: ${helmConfig.chartName} - version: ${helmConfig.version} - values: -${values} -""" - } } diff --git a/src/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/RepoType.groovy b/src/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/RepoType.groovy new file mode 100644 index 0000000..a06ea95 --- /dev/null +++ b/src/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/RepoType.groovy @@ -0,0 +1,22 @@ +package com.cloudogu.gitopsbuildlib.deployment.helm.repotype + +abstract class RepoType { + + protected static String getHelmImage() { 'ghcr.io/cloudogu/helm:3.5.4-1' } + + protected script + + RepoType(def script) { + this.script = script + } + + abstract mergeValues(Map helmConfig, String[] files) + + void withHelm(Closure body) { + script.cesBuildLib.Docker.new(script).image(helmImage).inside( + "${script.pwd().equals(script.env.WORKSPACE) ? '' : "-v ${script.env.WORKSPACE}:${script.env.WORKSPACE}"}" + ) { + body() + } + } +} diff --git a/src/com/cloudogu/gitopsbuildlib/deployment/Plain.groovy b/src/com/cloudogu/gitopsbuildlib/deployment/plain/Plain.groovy similarity index 89% rename from src/com/cloudogu/gitopsbuildlib/deployment/Plain.groovy rename to src/com/cloudogu/gitopsbuildlib/deployment/plain/Plain.groovy index 2b87019..052984e 100644 --- a/src/com/cloudogu/gitopsbuildlib/deployment/Plain.groovy +++ b/src/com/cloudogu/gitopsbuildlib/deployment/plain/Plain.groovy @@ -1,4 +1,6 @@ -package com.cloudogu.gitopsbuildlib.deployment +package com.cloudogu.gitopsbuildlib.deployment.plain + +import com.cloudogu.gitopsbuildlib.deployment.Deployment class Plain extends Deployment{ diff --git a/src/com/cloudogu/gitopsbuildlib/deployment/repotype/RepoType.groovy b/src/com/cloudogu/gitopsbuildlib/deployment/repotype/RepoType.groovy deleted file mode 100644 index 2cb19e6..0000000 --- a/src/com/cloudogu/gitopsbuildlib/deployment/repotype/RepoType.groovy +++ /dev/null @@ -1,38 +0,0 @@ -package com.cloudogu.gitopsbuildlib.deployment.repotype - -abstract class RepoType { - - protected static String getHelmImage() { 'ghcr.io/cloudogu/helm:3.4.1-1' } - - protected script - - RepoType(def script) { - this.script = script - } - - abstract createHelmRelease(Map helmConfig, String application, String namespace, String valuesFile) - abstract mergeValues(Map helmConfig, String[] files) - - String fileToInlineYaml(String fileContents) { - String values = "" - String indent = " " - String fileContent = script.readFile fileContents - fileContent.split("\n").each { line -> - if(line.size() > 0) { - values += indent + line + "\n" - } else { - values += line + "\n" - } - } - // remove unnecessary last blank line - return values.substring(0, values.lastIndexOf('\n')) - } - - void withHelm(Closure body) { - script.cesBuildLib.Docker.new(script).image(helmImage).inside( - "${script.pwd().equals(script.env.WORKSPACE) ? '' : "-v ${script.env.WORKSPACE}:${script.env.WORKSPACE}"}" - ) { - body() - } - } -} diff --git a/test/com/cloudogu/gitopsbuildlib/DeployViaGitopsTest.groovy b/test/com/cloudogu/gitopsbuildlib/DeployViaGitopsTest.groovy index 5df75b2..8a04e30 100644 --- a/test/com/cloudogu/gitopsbuildlib/DeployViaGitopsTest.groovy +++ b/test/com/cloudogu/gitopsbuildlib/DeployViaGitopsTest.groovy @@ -16,7 +16,8 @@ import static org.mockito.ArgumentMatchers.anyString import static org.mockito.ArgumentMatchers.eq import static org.mockito.Mockito.* -@TestInstance(TestInstance.Lifecycle.PER_CLASS) +// Lifecycle.PER_METHOD is slower than PER_CLASS but we had issues regarding shared state (by executing all tests there was different behaviour than executing single tests in isolation). +@TestInstance(TestInstance.Lifecycle.PER_METHOD) class DeployViaGitopsTest extends BasePipelineTest { class CesBuildLibMock { @@ -99,14 +100,12 @@ class DeployViaGitopsTest extends BasePipelineTest { qa : [deployDirectly: false] ] - @BeforeAll - void setUp() throws Exception { - scriptRoots += 'vars' - super.setUp() - } - @BeforeEach void init() { + super.setUp() + + scriptRoots += 'vars' + deployViaGitops = loadScript('vars/deployViaGitops.groovy') binding.getVariable('currentBuild').result = 'SUCCESS' setupGlobals(deployViaGitops) diff --git a/test/com/cloudogu/gitopsbuildlib/deployment/HelmTest.groovy b/test/com/cloudogu/gitopsbuildlib/deployment/HelmTest.groovy index a88b5d7..eb3db8e 100644 --- a/test/com/cloudogu/gitopsbuildlib/deployment/HelmTest.groovy +++ b/test/com/cloudogu/gitopsbuildlib/deployment/HelmTest.groovy @@ -1,6 +1,7 @@ package com.cloudogu.gitopsbuildlib.deployment import com.cloudogu.gitopsbuildlib.ScriptMock +import com.cloudogu.gitopsbuildlib.deployment.helm.Helm import org.junit.jupiter.api.* import static org.assertj.core.api.Assertions.assertThat diff --git a/test/com/cloudogu/gitopsbuildlib/deployment/PlainTest.groovy b/test/com/cloudogu/gitopsbuildlib/deployment/PlainTest.groovy index 972195e..0f0d625 100644 --- a/test/com/cloudogu/gitopsbuildlib/deployment/PlainTest.groovy +++ b/test/com/cloudogu/gitopsbuildlib/deployment/PlainTest.groovy @@ -1,6 +1,7 @@ package com.cloudogu.gitopsbuildlib.deployment import com.cloudogu.gitopsbuildlib.ScriptMock +import com.cloudogu.gitopsbuildlib.deployment.plain.Plain import org.junit.jupiter.api.* import static org.assertj.core.api.Assertions.assertThat diff --git a/test/com/cloudogu/gitopsbuildlib/deployment/repotype/GitRepoTest.groovy b/test/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/GitRepoTest.groovy similarity index 96% rename from test/com/cloudogu/gitopsbuildlib/deployment/repotype/GitRepoTest.groovy rename to test/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/GitRepoTest.groovy index 17ed48e..3b925a5 100644 --- a/test/com/cloudogu/gitopsbuildlib/deployment/repotype/GitRepoTest.groovy +++ b/test/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/GitRepoTest.groovy @@ -1,4 +1,4 @@ -package com.cloudogu.gitopsbuildlib.deployment.repotype +package com.cloudogu.gitopsbuildlib.deployment.helm.repotype import com.cloudogu.gitopsbuildlib.ScriptMock import org.junit.jupiter.api.* diff --git a/test/com/cloudogu/gitopsbuildlib/deployment/repotype/HelmRepoTest.groovy b/test/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/HelmRepoTest.groovy similarity index 96% rename from test/com/cloudogu/gitopsbuildlib/deployment/repotype/HelmRepoTest.groovy rename to test/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/HelmRepoTest.groovy index 593a8ff..1df1031 100644 --- a/test/com/cloudogu/gitopsbuildlib/deployment/repotype/HelmRepoTest.groovy +++ b/test/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/HelmRepoTest.groovy @@ -1,4 +1,4 @@ -package com.cloudogu.gitopsbuildlib.deployment.repotype +package com.cloudogu.gitopsbuildlib.deployment.helm.repotype import com.cloudogu.gitopsbuildlib.ScriptMock import org.junit.jupiter.api.* diff --git a/test/com/cloudogu/gitopsbuildlib/deployment/repotype/RepoTypeTest.groovy b/test/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/RepoTypeTest.groovy similarity index 95% rename from test/com/cloudogu/gitopsbuildlib/deployment/repotype/RepoTypeTest.groovy rename to test/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/RepoTypeTest.groovy index 5ed5ea7..4be6b39 100644 --- a/test/com/cloudogu/gitopsbuildlib/deployment/repotype/RepoTypeTest.groovy +++ b/test/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/RepoTypeTest.groovy @@ -1,4 +1,4 @@ -package com.cloudogu.gitopsbuildlib.deployment.repotype +package com.cloudogu.gitopsbuildlib.deployment.helm.repotype import com.cloudogu.gitopsbuildlib.ScriptMock import org.junit.jupiter.api.* diff --git a/vars/deployViaGitops.groovy b/vars/deployViaGitops.groovy index 110f001..bf92529 100644 --- a/vars/deployViaGitops.groovy +++ b/vars/deployViaGitops.groovy @@ -1,15 +1,15 @@ #!groovy import com.cloudogu.gitopsbuildlib.* import com.cloudogu.gitopsbuildlib.deployment.Deployment -import com.cloudogu.gitopsbuildlib.deployment.Helm -import com.cloudogu.gitopsbuildlib.deployment.Plain +import com.cloudogu.gitopsbuildlib.deployment.helm.Helm +import com.cloudogu.gitopsbuildlib.deployment.plain.Plain import com.cloudogu.gitopsbuildlib.scm.SCMManager import com.cloudogu.gitopsbuildlib.scm.SCMProvider import com.cloudogu.gitopsbuildlib.validation.HelmKubeval import com.cloudogu.gitopsbuildlib.validation.Kubeval import com.cloudogu.gitopsbuildlib.validation.Yamllint -String getHelmImage() { 'ghcr.io/cloudogu/helm:3.4.1-1' } +String getHelmImage() { 'ghcr.io/cloudogu/helm:3.5.4-1' } List getMandatoryFields() { return [ @@ -21,7 +21,7 @@ Map getDefaultConfig() { return [ cesBuildLibRepo : 'https://github.com/cloudogu/ces-build-lib', - cesBuildLibVersion : '1.45.0', + cesBuildLibVersion : '1.46.1', cesBuildLibCredentialsId: '', mainBranch : 'main', deployments : [ From c54d3b16cc5e07c9c053adc1c06593e41fad8064 Mon Sep 17 00:00:00 2001 From: Marek Markiewka Date: Fri, 16 Apr 2021 13:58:15 +0200 Subject: [PATCH 02/17] first draft of argocd_helm_gitrepo implementation Signed-off-by: Marek Markiewka --- .../gitopsbuildlib/deployment/HelmTest.groovy | 6 +- .../helm/repotype/GitRepoTest.groovy | 81 ++++++++++--------- .../helm/repotype/HelmRepoTest.groovy | 81 ++++++++++--------- .../helm/repotype/RepoTypeTest.groovy | 72 ++++++++--------- 4 files changed, 120 insertions(+), 120 deletions(-) diff --git a/test/com/cloudogu/gitopsbuildlib/deployment/HelmTest.groovy b/test/com/cloudogu/gitopsbuildlib/deployment/HelmTest.groovy index eb3db8e..31c94dd 100644 --- a/test/com/cloudogu/gitopsbuildlib/deployment/HelmTest.groovy +++ b/test/com/cloudogu/gitopsbuildlib/deployment/HelmTest.groovy @@ -12,6 +12,7 @@ class HelmTest { def dockerMock = scriptMock.dockerMock def helmGit = new Helm(scriptMock.mock, [ application: 'testapp', + gitopsTool: 'FLUX', stages: [ staging: [ namespace: 'fluxv1-staging' @@ -37,6 +38,7 @@ class HelmTest { ]) def helmHelm = new Helm(scriptMock.mock, [ application: 'testapp', + gitopsTool: 'FLUX', stages: [ staging: [ namespace: 'fluxv1-staging' @@ -66,7 +68,7 @@ class HelmTest { void 'creating helm release with git repo'() { helmGit.createPreValidation('staging') - assertThat(dockerMock.actualImages[0]).isEqualTo('ghcr.io/cloudogu/helm:3.4.1-1') + assertThat(dockerMock.actualImages[0]).contains('ghcr.io/cloudogu/helm:') assertThat(scriptMock.actualShArgs[0]).isEqualTo('[returnStdout:true, script:helm values workspace/chart/chartPath -f workspace/k8s/values-staging.yaml -f workspace/k8s/values-shared.yaml ]') assertThat(scriptMock.actualShArgs[1]).isEqualTo('rm -rf workspace/chart || true') assertThat(scriptMock.actualShArgs[2]).isEqualTo('rm staging/testapp/mergedValues.yaml') @@ -105,7 +107,7 @@ class HelmTest { void 'creating helm release with helm repo'() { helmHelm.createPreValidation('staging') - assertThat(dockerMock.actualImages[0]).isEqualTo('ghcr.io/cloudogu/helm:3.4.1-1') + assertThat(dockerMock.actualImages[0]).contains('ghcr.io/cloudogu/helm:') assertThat(scriptMock.actualShArgs[0]).isEqualTo('helm repo add chartRepo repoUrl') assertThat(scriptMock.actualShArgs[1]).isEqualTo('helm repo update') assertThat(scriptMock.actualShArgs[2]).isEqualTo('helm pull chartRepo/chartName --version=1.0 --untar --untardir=workspace/chart') diff --git a/test/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/GitRepoTest.groovy b/test/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/GitRepoTest.groovy index 3b925a5..b17e315 100644 --- a/test/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/GitRepoTest.groovy +++ b/test/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/GitRepoTest.groovy @@ -26,45 +26,46 @@ class GitRepoTest { assertThat(scriptMock.actualGitArgs[0]).isEqualTo('[url:url, branch:main, changelog:false, poll:false]') } - @Test - void 'create helm release yields correct helmRelease'() { - def helmRelease = gitRepo.createHelmRelease([ - repoUrl: 'url', - chartName: 'chartName', - version: '1.0' - ], - 'app', - 'namespace', - 'valuesFile') - - assertThat(helmRelease).isEqualTo('''\ -apiVersion: helm.fluxcd.io/v1 -kind: HelmRelease -metadata: - name: app - namespace: namespace - annotations: - fluxcd.io/automated: "false" -spec: - releaseName: app - chart: - git: url - ref: 1.0 - path: . - values: - --- - #this part is only for PlainTest regarding updating the image name - spec: - template: - spec: - containers: - - name: 'application\' - image: 'oldImageName' - #this part is only for HelmTest regarding changing the yaml values - to: - be: - changed: 'oldValue' -''') - } + //TODO implement in fluxv1gitrepo class +// @Test +// void 'create helm release yields correct helmRelease'() { +// def helmRelease = gitRepo.create([ +// repoUrl: 'url', +// chartName: 'chartName', +// version: '1.0' +// ], +// 'app', +// 'namespace', +// 'valuesFile') +// +// assertThat(helmRelease).isEqualTo('''\ +//apiVersion: helm.fluxcd.io/v1 +//kind: HelmRelease +//metadata: +// name: app +// namespace: namespace +// annotations: +// fluxcd.io/automated: "false" +//spec: +// releaseName: app +// chart: +// git: url +// ref: 1.0 +// path: . +// values: +// --- +// #this part is only for PlainTest regarding updating the image name +// spec: +// template: +// spec: +// containers: +// - name: 'application\' +// image: 'oldImageName' +// #this part is only for HelmTest regarding changing the yaml values +// to: +// be: +// changed: 'oldValue' +//''') +// } } diff --git a/test/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/HelmRepoTest.groovy b/test/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/HelmRepoTest.groovy index 1df1031..c21fc65 100644 --- a/test/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/HelmRepoTest.groovy +++ b/test/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/HelmRepoTest.groovy @@ -27,45 +27,46 @@ class HelmRepoTest { assertThat(scriptMock.actualShArgs[4]).isEqualTo('rm -rf workspace/chart || true') } - @Test - void 'create helm release yields correct helmRelease'() { - def helmRelease = helmRepo.createHelmRelease([ - repoUrl: 'url', - chartName: 'chartName', - version: '1.0' - ], - 'app', - 'namespace', - 'valuesFile') - - assertThat(helmRelease).isEqualTo('''\ -apiVersion: helm.fluxcd.io/v1 -kind: HelmRelease -metadata: - name: app - namespace: namespace - annotations: - fluxcd.io/automated: "false" -spec: - releaseName: app - chart: - repository: url - name: chartName - version: 1.0 - values: - --- - #this part is only for PlainTest regarding updating the image name - spec: - template: - spec: - containers: - - name: 'application\' - image: 'oldImageName' - #this part is only for HelmTest regarding changing the yaml values - to: - be: - changed: 'oldValue' -''') - } + //TODO implement in fluxv1helmrepo class +// @Test +// void 'create helm release yields correct helmRelease'() { +// def helmRelease = helmRepo.create([ +// repoUrl: 'url', +// chartName: 'chartName', +// version: '1.0' +// ], +// 'app', +// 'namespace', +// 'valuesFile') +// +// assertThat(helmRelease).isEqualTo('''\ +//apiVersion: helm.fluxcd.io/v1 +//kind: HelmRelease +//metadata: +// name: app +// namespace: namespace +// annotations: +// fluxcd.io/automated: "false" +//spec: +// releaseName: app +// chart: +// repository: url +// name: chartName +// version: 1.0 +// values: +// --- +// #this part is only for PlainTest regarding updating the image name +// spec: +// template: +// spec: +// containers: +// - name: 'application\' +// image: 'oldImageName' +// #this part is only for HelmTest regarding changing the yaml values +// to: +// be: +// changed: 'oldValue' +//''') +// } } diff --git a/test/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/RepoTypeTest.groovy b/test/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/RepoTypeTest.groovy index 4be6b39..6b25d77 100644 --- a/test/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/RepoTypeTest.groovy +++ b/test/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/RepoTypeTest.groovy @@ -6,42 +6,38 @@ import static org.assertj.core.api.Assertions.assertThat class RepoTypeTest { - def scriptMock = new ScriptMock() - def repoType = new RepoTypeUnderTest(scriptMock.mock) - - @Test - void 'inline yaml test'() { - def output = repoType.fileToInlineYaml('filepath') - assertThat(scriptMock.actualReadFileArgs[0]).isEqualTo('filepath') - assertThat(output).isEqualTo('''\ - --- - #this part is only for PlainTest regarding updating the image name - spec: - template: - spec: - containers: - - name: 'application\' - image: 'oldImageName' - #this part is only for HelmTest regarding changing the yaml values - to: - be: - changed: 'oldValue\'''') - } - - class RepoTypeUnderTest extends RepoType { - - RepoTypeUnderTest(Object script) { - super(script) - } - - @Override - def createHelmRelease(Map helmConfig, String application, String namespace, String valuesFile) { - return null - } - - @Override - def mergeValues(Map helmConfig, String[] files) { - return null - } - } + //TODO implement in helmrelease class +// def scriptMock = new ScriptMock() +// def repoType = new RepoTypeUnderTest(scriptMock.mock) +// +// @Test +// void 'inline yaml test'() { +// def output = repoType.fileToInlineYaml('filepath') +// assertThat(scriptMock.actualReadFileArgs[0]).isEqualTo('filepath') +// assertThat(output).isEqualTo('''\ +// --- +// #this part is only for PlainTest regarding updating the image name +// spec: +// template: +// spec: +// containers: +// - name: 'application\' +// image: 'oldImageName' +// #this part is only for HelmTest regarding changing the yaml values +// to: +// be: +// changed: 'oldValue\'''') +// } +// +// class RepoTypeUnderTest extends RepoType { +// +// RepoTypeUnderTest(Object script) { +// super(script) +// } +// +// @Override +// def mergeValues(Map helmConfig, String[] files) { +// return null +// } +// } } From d6514be2f6713c1ab75404339051127f48c7867b Mon Sep 17 00:00:00 2001 From: Marek Markiewka Date: Fri, 16 Apr 2021 15:56:55 +0200 Subject: [PATCH 03/17] bugfixing Signed-off-by: Marek Markiewka --- .../helm/helmrelease/ArgoCDGitRepoRelease.groovy | 7 ++++--- .../gitopsbuildlib/deployment/helm/repotype/GitRepo.groovy | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/ArgoCDGitRepoRelease.groovy b/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/ArgoCDGitRepoRelease.groovy index 9182d98..f4f0dcb 100644 --- a/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/ArgoCDGitRepoRelease.groovy +++ b/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/ArgoCDGitRepoRelease.groovy @@ -11,22 +11,23 @@ class ArgoCDGitRepoRelease extends HelmRelease{ @Override String create(Map helmConfig, String application, String namespace, String valuesFile) { def credentialsId = helmConfig.credentialsId - def myGit = script.cesBuildLib.Git.new(this, credentialsId) + def myGit = script.cesBuildLib.Git.new(script, credentialsId) script.dir("${script.env.WORKSPACE}/helmChart") { - script.git credentialsId: credentialsId, url: helmConfig.helmChartRepository, branch: 'main', changelog: false, poll: false + script.git credentialsId: credentialsId, url: helmConfig.repoUrl, branch: 'main', changelog: false, poll: false if(helmConfig.containsKey('version') && helmConfig.version) { myGit.fetch() myGit.checkout(helmConfig.version) } } + String valusFileLocation = "${script.env.WORKSPACE}/.configRepoTempDir/${valuesFile}" String helmRelease = "" withHelm { script.dir("${script.env.WORKSPACE}/helmChart") { script.sh "helm dep update ." - String templateScript = "helm template ${application} . -f ${valuesFile}" + String templateScript = "helm template ${application} . -f ${valusFileLocation}" helmRelease = script.sh returnStdout: true, script: templateScript } } diff --git a/src/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/GitRepo.groovy b/src/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/GitRepo.groovy index 4af4afa..856d955 100644 --- a/src/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/GitRepo.groovy +++ b/src/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/GitRepo.groovy @@ -14,7 +14,7 @@ class GitRepo extends RepoType { _files += "-f $it " } - def myGit = script.cesBuildLib.Git.new(this, helmConfig.credentialsId) + def myGit = script.cesBuildLib.Git.new(script, helmConfig.credentialsId) script.dir("${script.env.WORKSPACE}/chart") { if (helmConfig.containsKey('credentialsId')) { From 499bc59ac79b53aefb2a52e2d0be2d09f6c5bc29 Mon Sep 17 00:00:00 2001 From: Marek Markiewka Date: Fri, 16 Apr 2021 16:58:32 +0200 Subject: [PATCH 04/17] typo Signed-off-by: Marek Markiewka --- .../deployment/helm/helmrelease/ArgoCDGitRepoRelease.groovy | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/ArgoCDGitRepoRelease.groovy b/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/ArgoCDGitRepoRelease.groovy index f4f0dcb..272a6b5 100644 --- a/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/ArgoCDGitRepoRelease.groovy +++ b/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/ArgoCDGitRepoRelease.groovy @@ -21,13 +21,13 @@ class ArgoCDGitRepoRelease extends HelmRelease{ } } - String valusFileLocation = "${script.env.WORKSPACE}/.configRepoTempDir/${valuesFile}" + String valuesFileLocation = "${script.env.WORKSPACE}/.configRepoTempDir/${valuesFile}" String helmRelease = "" withHelm { script.dir("${script.env.WORKSPACE}/helmChart") { script.sh "helm dep update ." - String templateScript = "helm template ${application} . -f ${valusFileLocation}" + String templateScript = "helm template ${application} . -f ${valuesFileLocation}" helmRelease = script.sh returnStdout: true, script: templateScript } } From 44ff92cba90916b1c15ea7b23b85fc106af7fd81 Mon Sep 17 00:00:00 2001 From: Marek Markiewka Date: Mon, 19 Apr 2021 08:43:25 +0200 Subject: [PATCH 05/17] refactoring; bugfixing Signed-off-by: Marek Markiewka --- .../deployment/helm/Helm.groovy | 32 ++++++------ ...epoRelease.groovy => ArgoCDRelease.groovy} | 34 ++++++++----- .../helmrelease/FluxV1GitRepoRelease.groovy | 29 ----------- .../helmrelease/FluxV1HelmRepoRelease.groovy | 29 ----------- .../helm/helmrelease/FluxV1Release.groovy | 51 +++++++++++++++++++ .../deployment/helm/repotype/GitRepo.groovy | 42 +++++++-------- .../deployment/helm/repotype/HelmRepo.groovy | 8 +-- .../deployment/helm/repotype/RepoType.groovy | 8 +++ 8 files changed, 118 insertions(+), 115 deletions(-) rename src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/{ArgoCDGitRepoRelease.groovy => ArgoCDRelease.groovy} (53%) delete mode 100644 src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/FluxV1GitRepoRelease.groovy delete mode 100644 src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/FluxV1HelmRepoRelease.groovy create mode 100644 src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/FluxV1Release.groovy diff --git a/src/com/cloudogu/gitopsbuildlib/deployment/helm/Helm.groovy b/src/com/cloudogu/gitopsbuildlib/deployment/helm/Helm.groovy index 477543c..cc0a917 100644 --- a/src/com/cloudogu/gitopsbuildlib/deployment/helm/Helm.groovy +++ b/src/com/cloudogu/gitopsbuildlib/deployment/helm/Helm.groovy @@ -1,9 +1,8 @@ package com.cloudogu.gitopsbuildlib.deployment.helm import com.cloudogu.gitopsbuildlib.deployment.Deployment -import com.cloudogu.gitopsbuildlib.deployment.helm.helmrelease.ArgoCDGitRepoRelease -import com.cloudogu.gitopsbuildlib.deployment.helm.helmrelease.FluxV1GitRepoRelease -import com.cloudogu.gitopsbuildlib.deployment.helm.helmrelease.FluxV1HelmRepoRelease +import com.cloudogu.gitopsbuildlib.deployment.helm.helmrelease.ArgoCDRelease +import com.cloudogu.gitopsbuildlib.deployment.helm.helmrelease.FluxV1Release import com.cloudogu.gitopsbuildlib.deployment.helm.helmrelease.HelmRelease import com.cloudogu.gitopsbuildlib.deployment.helm.repotype.GitRepo import com.cloudogu.gitopsbuildlib.deployment.helm.repotype.HelmRepo @@ -11,24 +10,20 @@ import com.cloudogu.gitopsbuildlib.deployment.helm.repotype.RepoType class Helm extends Deployment { - protected RepoType helmRepo + protected RepoType chartRepo protected HelmRelease helmRelease Helm(def script, def gitopsConfig) { super(script, gitopsConfig) - if(gitopsConfig.gitopsTool == 'FLUX') { - if (gitopsConfig.deployments.helm.repoType == 'GIT') { - helmRelease = new FluxV1GitRepoRelease(script) - } else if (gitopsConfig.deployments.helm.repoType == 'HELM') { - helmRelease = new FluxV1HelmRepoRelease(script) - } - } else if(gitopsConfig.gitopsTool == 'ARGO_CD') { - helmRelease = new ArgoCDGitRepoRelease(script) - } if (gitopsConfig.deployments.helm.repoType == 'GIT') { - helmRepo = new GitRepo(script) + chartRepo = new GitRepo(script) } else if (gitopsConfig.deployments.helm.repoType == 'HELM') { - helmRepo = new HelmRepo(script) + chartRepo = new HelmRepo(script) + } + if(gitopsConfig.gitopsTool == 'FLUX') { + helmRelease = new FluxV1Release(script) + } else if(gitopsConfig.gitopsTool == 'ARGO_CD') { + helmRelease = new ArgoCDRelease(script) } } @@ -41,12 +36,17 @@ class Helm extends Deployment { // writing the merged-values.yaml via writeYaml into a file has the advantage, that it gets formatted as valid yaml // This makes it easier to read in and indent for the inline use in the helmRelease. // It enables us to reuse the `fileToInlineYaml` function, without writing a complex formatting logic. - script.writeFile file: "${stage}/${application}/mergedValues.yaml", text: helmRepo.mergeValues(helmConfig, ["${script.env.WORKSPACE}/${sourcePath}/values-${stage}.yaml", "${script.env.WORKSPACE}/${sourcePath}/values-shared.yaml"] as String[]) + script.writeFile file: "${stage}/${application}/mergedValues.yaml", text: chartRepo.mergeValues(helmConfig, ["${script.env.WORKSPACE}/${sourcePath}/values-${stage}.yaml", "${script.env.WORKSPACE}/${sourcePath}/values-shared.yaml"] as String[]) updateYamlValue("${stage}/${application}/mergedValues.yaml", helmConfig) script.writeFile file: "${stage}/${application}/helmRelease.yaml", text: helmRelease.create(helmConfig, application, getNamespace(stage), "${stage}/${application}/mergedValues.yaml") + // since the values are already inline (helmRelease.yaml) we do not need to commit them into the gitops repo script.sh "rm ${stage}/${application}/mergedValues.yaml" + // clean the gitrepo helm chart folder since the helmRelease.yaml ist now created + if (helmConfig.repoType == 'GIT') { + script.sh "rm -rf ${script.env.WORKSPACE}/chart || true" + } } @Override diff --git a/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/ArgoCDGitRepoRelease.groovy b/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/ArgoCDRelease.groovy similarity index 53% rename from src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/ArgoCDGitRepoRelease.groovy rename to src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/ArgoCDRelease.groovy index 272a6b5..554f777 100644 --- a/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/ArgoCDGitRepoRelease.groovy +++ b/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/ArgoCDRelease.groovy @@ -1,37 +1,43 @@ package com.cloudogu.gitopsbuildlib.deployment.helm.helmrelease -class ArgoCDGitRepoRelease extends HelmRelease{ +class ArgoCDRelease extends HelmRelease{ protected static String getHelmImage() { 'ghcr.io/cloudogu/helm:3.5.4-1' } - ArgoCDGitRepoRelease(def script) { + ArgoCDRelease(def script) { super(script) } @Override String create(Map helmConfig, String application, String namespace, String valuesFile) { - def credentialsId = helmConfig.credentialsId - def myGit = script.cesBuildLib.Git.new(script, credentialsId) - - script.dir("${script.env.WORKSPACE}/helmChart") { - script.git credentialsId: credentialsId, url: helmConfig.repoUrl, branch: 'main', changelog: false, poll: false - if(helmConfig.containsKey('version') && helmConfig.version) { - myGit.fetch() - myGit.checkout(helmConfig.version) - } - } String valuesFileLocation = "${script.env.WORKSPACE}/.configRepoTempDir/${valuesFile}" String helmRelease = "" + if (helmConfig.repoType == 'GIT') { + helmRelease = gitRepoRelease(helmConfig, application, valuesFileLocation) + } else if (helmConfig.repoType == 'HELM') { + // TODO not yet implemented + } + return helmRelease + } + + private String gitRepoRelease(Map helmConfig, String application, String valuesFileLocation) { + String helmRelease = "" + + def chartPath = '' + if (helmConfig.containsKey('chartPath')) { + chartPath = helmConfig.chartPath + } withHelm { - script.dir("${script.env.WORKSPACE}/helmChart") { + script.dir("${script.env.WORKSPACE}/chart/${chartPath}") { script.sh "helm dep update ." String templateScript = "helm template ${application} . -f ${valuesFileLocation}" helmRelease = script.sh returnStdout: true, script: templateScript } } - + // this line removes all empty lines since helm template creates some and the helm validator will throw an error if there are emtpy lines present + helmRelease.replaceAll("(?m)^[ \t]*\r?\n", "") return helmRelease } diff --git a/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/FluxV1GitRepoRelease.groovy b/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/FluxV1GitRepoRelease.groovy deleted file mode 100644 index c02ae44..0000000 --- a/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/FluxV1GitRepoRelease.groovy +++ /dev/null @@ -1,29 +0,0 @@ -package com.cloudogu.gitopsbuildlib.deployment.helm.helmrelease - -class FluxV1GitRepoRelease extends HelmRelease{ - - FluxV1GitRepoRelease(def script) { - super(script) - } - - @Override - String create(Map helmConfig, String application, String namespace, String valuesFile) { - def values = fileToInlineYaml(valuesFile) - return """apiVersion: helm.fluxcd.io/v1 -kind: HelmRelease -metadata: - name: ${application} - namespace: ${namespace} - annotations: - fluxcd.io/automated: "false" -spec: - releaseName: ${application} - chart: - git: ${helmConfig.repoUrl} - ref: ${helmConfig.version} - path: . - values: -${values} -""" - } -} diff --git a/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/FluxV1HelmRepoRelease.groovy b/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/FluxV1HelmRepoRelease.groovy deleted file mode 100644 index 0c60e22..0000000 --- a/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/FluxV1HelmRepoRelease.groovy +++ /dev/null @@ -1,29 +0,0 @@ -package com.cloudogu.gitopsbuildlib.deployment.helm.helmrelease - -class FluxV1HelmRepoRelease extends HelmRelease{ - - FluxV1HelmRepoRelease(def script) { - super(script) - } - - @Override - String create(Map helmConfig, String application, String namespace, String valuesFile) { - def values = fileToInlineYaml(valuesFile) - return """apiVersion: helm.fluxcd.io/v1 -kind: HelmRelease -metadata: - name: ${application} - namespace: ${namespace} - annotations: - fluxcd.io/automated: "false" -spec: - releaseName: ${application} - chart: - repository: ${helmConfig.repoUrl} - name: ${helmConfig.chartName} - version: ${helmConfig.version} - values: -${values} -""" - } -} diff --git a/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/FluxV1Release.groovy b/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/FluxV1Release.groovy new file mode 100644 index 0000000..1fe3b13 --- /dev/null +++ b/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/FluxV1Release.groovy @@ -0,0 +1,51 @@ +package com.cloudogu.gitopsbuildlib.deployment.helm.helmrelease + +class FluxV1Release extends HelmRelease{ + + FluxV1Release(def script) { + super(script) + } + + @Override + String create(Map helmConfig, String application, String namespace, String valuesFile) { + def values = fileToInlineYaml(valuesFile) + def chart = getChart(helmConfig) + return """apiVersion: helm.fluxcd.io/v1 +kind: HelmRelease +metadata: + name: ${application} + namespace: ${namespace} + annotations: + fluxcd.io/automated: "false" +spec: + releaseName: ${application} + chart: ${chart} + values: +${values} +""" + } + + private String gitRepoChart(Map helmConfig) { + return """ + git: ${helmConfig.repoUrl} + ref: ${helmConfig.version} + path: . +""" + } + + private String helmRepoChart(Map helmConfig) { + return """ + repository: ${helmConfig.repoUrl} + name: ${helmConfig.chartName} + version: ${helmConfig.version} +""" + } + + private String getChart(Map helmConfig) { + if (helmConfig.repoType == 'GIT') { + return gitRepoChart(helmConfig) + } else if (helmConfig.repoType == 'HELM') { + return helmRepoChart(helmConfig) + } + } +} diff --git a/src/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/GitRepo.groovy b/src/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/GitRepo.groovy index 856d955..aa7510f 100644 --- a/src/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/GitRepo.groovy +++ b/src/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/GitRepo.groovy @@ -7,26 +7,10 @@ class GitRepo extends RepoType { } @Override - String mergeValues(Map helmConfig, String[] files) { + String mergeValues(Map helmConfig, String[] valuesFiles) { String merge = "" - String _files = "" - files.each { - _files += "-f $it " - } - - def myGit = script.cesBuildLib.Git.new(script, helmConfig.credentialsId) - script.dir("${script.env.WORKSPACE}/chart") { - if (helmConfig.containsKey('credentialsId')) { - script.git credentialsId: helmConfig.credentialsId, url: helmConfig.repoUrl, branch: 'main', changelog: false, poll: false - myGit.fetch() - myGit.checkout(helmConfig.version) - } else { - script.git url: helmConfig.repoUrl, branch: 'main', changelog: false, poll: false - myGit.fetch() - myGit.checkout(helmConfig.version) - } - } + prepareGitRepo(helmConfig) def chartPath = '' if (helmConfig.containsKey('chartPath')) { @@ -34,12 +18,28 @@ class GitRepo extends RepoType { } withHelm { - String helmScript = "helm values ${script.env.WORKSPACE}/chart/${chartPath} ${_files}" + String helmScript = "helm values ${script.env.WORKSPACE}/chart/${chartPath} ${valuesFilesWithParameter(valuesFiles)}" merge = script.sh returnStdout: true, script: helmScript } - script.sh "rm -rf ${script.env.WORKSPACE}/chart || true" - return merge } + + private prepareGitRepo(Map helmConfig) { + def myGit + + script.dir("${script.env.WORKSPACE}/chart") { + if (helmConfig.containsKey('credentialsId')) { + script.git credentialsId: helmConfig.credentialsId, url: helmConfig.repoUrl, branch: 'main', changelog: false, poll: false + myGit = script.cesBuildLib.Git.new(script, helmConfig.credentialsId) + } else { + script.git url: helmConfig.repoUrl, branch: 'main', changelog: false, poll: false + myGit = script.cesBuildLib.Git.new(script) + } + if(helmConfig.containsKey('version') && helmConfig.version) { + myGit.fetch() + myGit.checkout(helmConfig.version) + } + } + } } diff --git a/src/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/HelmRepo.groovy b/src/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/HelmRepo.groovy index 11e1106..e8a9639 100644 --- a/src/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/HelmRepo.groovy +++ b/src/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/HelmRepo.groovy @@ -7,18 +7,14 @@ class HelmRepo extends RepoType{ } @Override - String mergeValues(Map helmConfig, String[] files) { + String mergeValues(Map helmConfig, String[] valuesFiles) { String merge = "" - String _files = "" - files.each { - _files += "-f $it " - } withHelm { script.sh "helm repo add chartRepo ${helmConfig.repoUrl}" script.sh "helm repo update" script.sh "helm pull chartRepo/${helmConfig.chartName} --version=${helmConfig.version} --untar --untardir=${script.env.WORKSPACE}/chart" - String helmScript = "helm values ${script.env.WORKSPACE}/chart/${helmConfig.chartName} ${_files}" + String helmScript = "helm values ${script.env.WORKSPACE}/chart/${helmConfig.chartName} ${valuesFilesWithParameter(valuesFiles)}" merge = script.sh returnStdout: true, script: helmScript } diff --git a/src/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/RepoType.groovy b/src/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/RepoType.groovy index a06ea95..7f60b6e 100644 --- a/src/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/RepoType.groovy +++ b/src/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/RepoType.groovy @@ -19,4 +19,12 @@ abstract class RepoType { body() } } + + protected String valuesFilesWithParameter(String[] valuesFiles) { + String valuesFilesWithParameter = "" + valuesFiles.each { + valuesFilesWithParameter += "-f $it " + } + return valuesFilesWithParameter + } } From 62544b8e04d9f1733e0f02f85356042d787c95c0 Mon Sep 17 00:00:00 2001 From: Marek Markiewka Date: Mon, 19 Apr 2021 11:42:17 +0200 Subject: [PATCH 06/17] added tests Signed-off-by: Marek Markiewka --- .../helm/helmrelease/ArgoCDRelease.groovy | 2 +- .../helm/helmrelease/FluxV1Release.groovy | 8 +- .../deployment/helm/repotype/GitRepo.groovy | 1 + .../cloudogu/gitopsbuildlib/GitMock.groovy | 1 - .../cloudogu/gitopsbuildlib/ScriptMock.groovy | 9 +- .../gitopsbuildlib/deployment/HelmTest.groovy | 5 +- .../helm/helmrelease/ArgoCDReleaseTest.groovy | 47 ++++++++++ .../helm/helmrelease/FluxV1ReleaseTest.groovy | 94 +++++++++++++++++++ .../helm/helmrelease/HelmReleaseTest.groovy | 43 +++++++++ .../helm/repotype/GitRepoTest.groovy | 44 --------- .../helm/repotype/HelmRepoTest.groovy | 43 --------- .../helm/repotype/RepoTypeTest.groovy | 63 +++++-------- .../validation/HelmKubevalTest.groovy | 1 - 13 files changed, 222 insertions(+), 139 deletions(-) create mode 100644 test/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/ArgoCDReleaseTest.groovy create mode 100644 test/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/FluxV1ReleaseTest.groovy create mode 100644 test/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/HelmReleaseTest.groovy diff --git a/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/ArgoCDRelease.groovy b/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/ArgoCDRelease.groovy index 554f777..7729d0d 100644 --- a/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/ArgoCDRelease.groovy +++ b/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/ArgoCDRelease.groovy @@ -37,7 +37,7 @@ class ArgoCDRelease extends HelmRelease{ } } // this line removes all empty lines since helm template creates some and the helm validator will throw an error if there are emtpy lines present - helmRelease.replaceAll("(?m)^[ \t]*\r?\n", "") + helmRelease = helmRelease.replaceAll("(?m)^[ \t]*\r?\n", "") return helmRelease } diff --git a/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/FluxV1Release.groovy b/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/FluxV1Release.groovy index 1fe3b13..9e47dd8 100644 --- a/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/FluxV1Release.groovy +++ b/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/FluxV1Release.groovy @@ -19,7 +19,7 @@ metadata: fluxcd.io/automated: "false" spec: releaseName: ${application} - chart: ${chart} + chart:${chart} values: ${values} """ @@ -29,16 +29,14 @@ ${values} return """ git: ${helmConfig.repoUrl} ref: ${helmConfig.version} - path: . -""" + path: .""" } private String helmRepoChart(Map helmConfig) { return """ repository: ${helmConfig.repoUrl} name: ${helmConfig.chartName} - version: ${helmConfig.version} -""" + version: ${helmConfig.version}""" } private String getChart(Map helmConfig) { diff --git a/src/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/GitRepo.groovy b/src/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/GitRepo.groovy index aa7510f..af630f5 100644 --- a/src/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/GitRepo.groovy +++ b/src/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/GitRepo.groovy @@ -29,6 +29,7 @@ class GitRepo extends RepoType { def myGit script.dir("${script.env.WORKSPACE}/chart") { + if (helmConfig.containsKey('credentialsId')) { script.git credentialsId: helmConfig.credentialsId, url: helmConfig.repoUrl, branch: 'main', changelog: false, poll: false myGit = script.cesBuildLib.Git.new(script, helmConfig.credentialsId) diff --git a/test/com/cloudogu/gitopsbuildlib/GitMock.groovy b/test/com/cloudogu/gitopsbuildlib/GitMock.groovy index a7b040b..58eb21c 100644 --- a/test/com/cloudogu/gitopsbuildlib/GitMock.groovy +++ b/test/com/cloudogu/gitopsbuildlib/GitMock.groovy @@ -3,7 +3,6 @@ package com.cloudogu.ces.cesbuildlib import static org.mockito.Mockito.mock class GitMock { - List actualArgs = new LinkedList<>() Git createMock() { Git gitMock = mock(Git.class) diff --git a/test/com/cloudogu/gitopsbuildlib/ScriptMock.groovy b/test/com/cloudogu/gitopsbuildlib/ScriptMock.groovy index e109221..fb1ab81 100644 --- a/test/com/cloudogu/gitopsbuildlib/ScriptMock.groovy +++ b/test/com/cloudogu/gitopsbuildlib/ScriptMock.groovy @@ -13,7 +13,7 @@ class ScriptMock { List actualEchoArgs = new LinkedList<>() List actualReadYamlArgs = new LinkedList<>() List actualGitArgs = new LinkedList<>() - String actualDir + List actualDir = new LinkedList<>() def configYaml = '''\ --- #this part is only for PlainTest regarding updating the image name @@ -39,8 +39,9 @@ to: new: { args -> return dockerMock.createMock() } ], Git: [ - new: { args, creds -> return gitMock.createMock() }, - checkout: { 'checkout' } + new: { args -> return gitMock.createMock() }, + fetch: { gitMock.setFetch() }, + checkout: { args -> gitMock.actualCheckoutArgs(args) } ], ], docker: dockerMock.createMock(), @@ -55,6 +56,6 @@ to: env : [ WORKSPACE: 'workspace' ], - dir: { dir, closure -> actualDir = dir; return closure.call() } + dir: { dir, closure -> println(dir); actualDir += dir.toString(); return closure.call() } ] } diff --git a/test/com/cloudogu/gitopsbuildlib/deployment/HelmTest.groovy b/test/com/cloudogu/gitopsbuildlib/deployment/HelmTest.groovy index 31c94dd..5d3fe93 100644 --- a/test/com/cloudogu/gitopsbuildlib/deployment/HelmTest.groovy +++ b/test/com/cloudogu/gitopsbuildlib/deployment/HelmTest.groovy @@ -50,6 +50,7 @@ class HelmTest { repoType: 'HELM', repoUrl: 'repoUrl', chartName: 'chartName', + credentials: 'creds', version: '1.0' ] ], @@ -70,8 +71,8 @@ class HelmTest { assertThat(dockerMock.actualImages[0]).contains('ghcr.io/cloudogu/helm:') assertThat(scriptMock.actualShArgs[0]).isEqualTo('[returnStdout:true, script:helm values workspace/chart/chartPath -f workspace/k8s/values-staging.yaml -f workspace/k8s/values-shared.yaml ]') - assertThat(scriptMock.actualShArgs[1]).isEqualTo('rm -rf workspace/chart || true') - assertThat(scriptMock.actualShArgs[2]).isEqualTo('rm staging/testapp/mergedValues.yaml') + assertThat(scriptMock.actualShArgs[1]).isEqualTo('rm staging/testapp/mergedValues.yaml') + assertThat(scriptMock.actualShArgs[2]).isEqualTo('rm -rf workspace/chart || true') assertThat(scriptMock.actualGitArgs[0]).isEqualTo('[url:repoUrl, branch:main, changelog:false, poll:false]') assertThat(scriptMock.actualWriteFileArgs[0]).isEqualTo('[file:staging/testapp/mergedValues.yaml, text:[[returnStdout:true, script:helm values workspace/chart/chartPath -f workspace/k8s/values-staging.yaml -f workspace/k8s/values-shared.yaml ]]]') assertThat(scriptMock.actualWriteFileArgs[1]).isEqualTo('[file:staging/testapp/helmRelease.yaml, text:apiVersion: helm.fluxcd.io/v1\n' + diff --git a/test/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/ArgoCDReleaseTest.groovy b/test/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/ArgoCDReleaseTest.groovy new file mode 100644 index 0000000..2b0f4b8 --- /dev/null +++ b/test/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/ArgoCDReleaseTest.groovy @@ -0,0 +1,47 @@ +package com.cloudogu.gitopsbuildlib.deployment.helm.helmrelease + +import com.cloudogu.gitopsbuildlib.ScriptMock +import org.junit.jupiter.api.Test + +import static org.assertj.core.api.Assertions.assertThat + +class ArgoCDReleaseTest { + + def scriptMock = new ScriptMock() + def argoCdReleaseTest = new ArgoCDRelease(scriptMock.mock) + + @Test + void 'correct helm release with git repo and chartPath'() { + argoCdReleaseTest.create([ + repoType: 'GIT', + repoUrl: 'url', + chartName: 'chartName', + chartPath: 'path', + version: '1.0' + ], + 'app', + 'namespace', + 'this/is/a/valusfile') + + assertThat(scriptMock.actualShArgs[0]).isEqualTo('helm dep update .') + assertThat(scriptMock.actualShArgs[1]).isEqualTo('[returnStdout:true, script:helm template app . -f workspace/.configRepoTempDir/this/is/a/valusfile]') + assertThat(scriptMock.actualDir[0]).isEqualTo('workspace/chart/path') + } + + @Test + void 'correct helm release with git repo without chartPath'() { + argoCdReleaseTest.create([ + repoType: 'GIT', + repoUrl: 'url', + chartName: 'chartName', + version: '1.0' + ], + 'app', + 'namespace', + 'this/is/a/valusfile') + + assertThat(scriptMock.actualShArgs[0]).isEqualTo('helm dep update .') + assertThat(scriptMock.actualShArgs[1]).isEqualTo('[returnStdout:true, script:helm template app . -f workspace/.configRepoTempDir/this/is/a/valusfile]') + assertThat(scriptMock.actualDir[0]).isEqualTo('workspace/chart/') + } +} diff --git a/test/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/FluxV1ReleaseTest.groovy b/test/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/FluxV1ReleaseTest.groovy new file mode 100644 index 0000000..c0ef668 --- /dev/null +++ b/test/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/FluxV1ReleaseTest.groovy @@ -0,0 +1,94 @@ +package com.cloudogu.gitopsbuildlib.deployment.helm.helmrelease + +import com.cloudogu.gitopsbuildlib.ScriptMock +import org.junit.jupiter.api.Test + +import static org.assertj.core.api.Assertions.assertThat + +class FluxV1ReleaseTest { + + def scriptMock = new ScriptMock() + def fluxV1Release = new FluxV1Release(scriptMock.mock) + + @Test + void 'correct helm release with git repo'() { + def output = fluxV1Release.create([ + repoType: 'GIT', + repoUrl: 'url', + chartName: 'chartName', + version: '1.0' + ], + 'app', + 'namespace', + 'this/is/a/valusfile') + + assertThat(output).isEqualTo("""apiVersion: helm.fluxcd.io/v1 +kind: HelmRelease +metadata: + name: app + namespace: namespace + annotations: + fluxcd.io/automated: "false" +spec: + releaseName: app + chart: + git: url + ref: 1.0 + path: . + values: + --- + #this part is only for PlainTest regarding updating the image name + spec: + template: + spec: + containers: + - name: 'application' + image: 'oldImageName' + #this part is only for HelmTest regarding changing the yaml values + to: + be: + changed: 'oldValue' +""") + } + + @Test + void 'correct helm release with helm repo'() { + def output = fluxV1Release.create([ + repoType: 'HELM', + repoUrl: 'url', + chartName: 'chartName', + version: '1.0' + ], + 'app', + 'namespace', + 'this/is/a/valusfile') + + assertThat(output).isEqualTo("""apiVersion: helm.fluxcd.io/v1 +kind: HelmRelease +metadata: + name: app + namespace: namespace + annotations: + fluxcd.io/automated: "false" +spec: + releaseName: app + chart: + repository: url + name: chartName + version: 1.0 + values: + --- + #this part is only for PlainTest regarding updating the image name + spec: + template: + spec: + containers: + - name: 'application' + image: 'oldImageName' + #this part is only for HelmTest regarding changing the yaml values + to: + be: + changed: 'oldValue' +""") + } +} diff --git a/test/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/HelmReleaseTest.groovy b/test/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/HelmReleaseTest.groovy new file mode 100644 index 0000000..9253d4d --- /dev/null +++ b/test/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/HelmReleaseTest.groovy @@ -0,0 +1,43 @@ +package com.cloudogu.gitopsbuildlib.deployment.helm.helmrelease + +import com.cloudogu.gitopsbuildlib.ScriptMock +import org.junit.jupiter.api.Test + +import static org.assertj.core.api.Assertions.assertThat + +class HelmReleaseTest { + + def scriptMock = new ScriptMock() + def repoType = new HelmReleaseUnderTest(scriptMock.mock) + + @Test + void 'inline yaml test'() { + def output = repoType.fileToInlineYaml('filepath') + assertThat(scriptMock.actualReadFileArgs[0]).isEqualTo('filepath') + assertThat(output).isEqualTo('''\ + --- + #this part is only for PlainTest regarding updating the image name + spec: + template: + spec: + containers: + - name: 'application\' + image: 'oldImageName' + #this part is only for HelmTest regarding changing the yaml values + to: + be: + changed: 'oldValue\'''') + } + + class HelmReleaseUnderTest extends HelmRelease { + + HelmReleaseUnderTest(Object script) { + super(script) + } + + @Override + String create(Map helmConfig, String application, String namespace, String valuesFile) { + return null + } + } +} diff --git a/test/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/GitRepoTest.groovy b/test/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/GitRepoTest.groovy index b17e315..61bd02a 100644 --- a/test/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/GitRepoTest.groovy +++ b/test/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/GitRepoTest.groovy @@ -22,50 +22,6 @@ class GitRepoTest { ] as String[]) assertThat(scriptMock.actualShArgs[0]).isEqualTo('[returnStdout:true, script:helm values workspace/chart/chartPath -f file1 -f file2 ]') - assertThat(scriptMock.actualShArgs[1]).isEqualTo('rm -rf workspace/chart || true') assertThat(scriptMock.actualGitArgs[0]).isEqualTo('[url:url, branch:main, changelog:false, poll:false]') } - - //TODO implement in fluxv1gitrepo class -// @Test -// void 'create helm release yields correct helmRelease'() { -// def helmRelease = gitRepo.create([ -// repoUrl: 'url', -// chartName: 'chartName', -// version: '1.0' -// ], -// 'app', -// 'namespace', -// 'valuesFile') -// -// assertThat(helmRelease).isEqualTo('''\ -//apiVersion: helm.fluxcd.io/v1 -//kind: HelmRelease -//metadata: -// name: app -// namespace: namespace -// annotations: -// fluxcd.io/automated: "false" -//spec: -// releaseName: app -// chart: -// git: url -// ref: 1.0 -// path: . -// values: -// --- -// #this part is only for PlainTest regarding updating the image name -// spec: -// template: -// spec: -// containers: -// - name: 'application\' -// image: 'oldImageName' -// #this part is only for HelmTest regarding changing the yaml values -// to: -// be: -// changed: 'oldValue' -//''') -// } - } diff --git a/test/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/HelmRepoTest.groovy b/test/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/HelmRepoTest.groovy index c21fc65..1b4ec30 100644 --- a/test/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/HelmRepoTest.groovy +++ b/test/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/HelmRepoTest.groovy @@ -26,47 +26,4 @@ class HelmRepoTest { assertThat(scriptMock.actualShArgs[3]).isEqualTo('[returnStdout:true, script:helm values workspace/chart/chartName -f file1 -f file2 ]') assertThat(scriptMock.actualShArgs[4]).isEqualTo('rm -rf workspace/chart || true') } - - //TODO implement in fluxv1helmrepo class -// @Test -// void 'create helm release yields correct helmRelease'() { -// def helmRelease = helmRepo.create([ -// repoUrl: 'url', -// chartName: 'chartName', -// version: '1.0' -// ], -// 'app', -// 'namespace', -// 'valuesFile') -// -// assertThat(helmRelease).isEqualTo('''\ -//apiVersion: helm.fluxcd.io/v1 -//kind: HelmRelease -//metadata: -// name: app -// namespace: namespace -// annotations: -// fluxcd.io/automated: "false" -//spec: -// releaseName: app -// chart: -// repository: url -// name: chartName -// version: 1.0 -// values: -// --- -// #this part is only for PlainTest regarding updating the image name -// spec: -// template: -// spec: -// containers: -// - name: 'application\' -// image: 'oldImageName' -// #this part is only for HelmTest regarding changing the yaml values -// to: -// be: -// changed: 'oldValue' -//''') -// } - } diff --git a/test/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/RepoTypeTest.groovy b/test/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/RepoTypeTest.groovy index 6b25d77..e65fc9d 100644 --- a/test/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/RepoTypeTest.groovy +++ b/test/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/RepoTypeTest.groovy @@ -1,43 +1,30 @@ -package com.cloudogu.gitopsbuildlib.deployment.helm.repotype +package com.cloudogu.gitopsbuildlib.deployment.helm.repotype; -import com.cloudogu.gitopsbuildlib.ScriptMock -import org.junit.jupiter.api.* -import static org.assertj.core.api.Assertions.assertThat +import com.cloudogu.gitopsbuildlib.ScriptMock; +import org.junit.jupiter.api.Test + +import static org.assertj.core.api.Assertions.assertThat; class RepoTypeTest { - //TODO implement in helmrelease class -// def scriptMock = new ScriptMock() -// def repoType = new RepoTypeUnderTest(scriptMock.mock) -// -// @Test -// void 'inline yaml test'() { -// def output = repoType.fileToInlineYaml('filepath') -// assertThat(scriptMock.actualReadFileArgs[0]).isEqualTo('filepath') -// assertThat(output).isEqualTo('''\ -// --- -// #this part is only for PlainTest regarding updating the image name -// spec: -// template: -// spec: -// containers: -// - name: 'application\' -// image: 'oldImageName' -// #this part is only for HelmTest regarding changing the yaml values -// to: -// be: -// changed: 'oldValue\'''') -// } -// -// class RepoTypeUnderTest extends RepoType { -// -// RepoTypeUnderTest(Object script) { -// super(script) -// } -// -// @Override -// def mergeValues(Map helmConfig, String[] files) { -// return null -// } -// } + def scriptMock = new ScriptMock() + def repoType = new RepoTypeUnderTest(scriptMock.mock) + + @Test + void 'values files getting parameters attached'() { + def output = repoType.valuesFilesWithParameter(['file1.yaml', 'file2.yaml'] as String[]) + assertThat(output).isEqualTo('-f file1.yaml -f file2.yaml ') + } + + class RepoTypeUnderTest extends RepoType { + + RepoTypeUnderTest(Object script) { + super(script) + } + + @Override + Object mergeValues(Map helmConfig, String[] files) { + return null; + } + } } diff --git a/test/com/cloudogu/gitopsbuildlib/validation/HelmKubevalTest.groovy b/test/com/cloudogu/gitopsbuildlib/validation/HelmKubevalTest.groovy index f10f42f..3321a82 100644 --- a/test/com/cloudogu/gitopsbuildlib/validation/HelmKubevalTest.groovy +++ b/test/com/cloudogu/gitopsbuildlib/validation/HelmKubevalTest.groovy @@ -25,7 +25,6 @@ class HelmKubevalTest { repoType: 'GIT', repoUrl: 'chartRepo/namespace/repoPath', chartPath: 'chartPath', - credentialsId: 'creds', version: 'version' ] ] From 2db29c443d2be575932901ad69279e1f511db444 Mon Sep 17 00:00:00 2001 From: Marek Markiewka Date: Mon, 19 Apr 2021 13:31:27 +0200 Subject: [PATCH 07/17] bugfixing Signed-off-by: Marek Markiewka --- .../cloudogu/gitopsbuildlib/deployment/Deployment.groovy | 8 ++++---- .../cloudogu/gitopsbuildlib/deployment/helm/Helm.groovy | 4 ++-- .../cloudogu/gitopsbuildlib/deployment/plain/Plain.groovy | 4 ++-- .../cloudogu/gitopsbuildlib/validation/HelmKubeval.groovy | 6 +++++- .../gitopsbuildlib/deployment/DeploymentTest.groovy | 4 ++-- .../cloudogu/gitopsbuildlib/deployment/HelmTest.groovy | 4 ++-- .../cloudogu/gitopsbuildlib/deployment/PlainTest.groovy | 2 +- .../gitopsbuildlib/validation/HelmKubevalTest.groovy | 5 +++-- 8 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/com/cloudogu/gitopsbuildlib/deployment/Deployment.groovy b/src/com/cloudogu/gitopsbuildlib/deployment/Deployment.groovy index 9a41e94..8aa4a25 100644 --- a/src/com/cloudogu/gitopsbuildlib/deployment/Deployment.groovy +++ b/src/com/cloudogu/gitopsbuildlib/deployment/Deployment.groovy @@ -17,13 +17,13 @@ abstract class Deployment { def create(String stage) { createFoldersAndCopyK8sResources(stage) createFileConfigmaps(stage) - createPreValidation(stage) + preValidation(stage) validate(stage) - createPostValidation(stage) + postValidation(stage) } - abstract createPreValidation(String stage) - abstract createPostValidation(String stage) + abstract preValidation(String stage) + abstract postValidation(String stage) def validate(String stage) { gitopsConfig.validators.each { validatorConfig -> diff --git a/src/com/cloudogu/gitopsbuildlib/deployment/helm/Helm.groovy b/src/com/cloudogu/gitopsbuildlib/deployment/helm/Helm.groovy index cc0a917..d713bed 100644 --- a/src/com/cloudogu/gitopsbuildlib/deployment/helm/Helm.groovy +++ b/src/com/cloudogu/gitopsbuildlib/deployment/helm/Helm.groovy @@ -28,7 +28,7 @@ class Helm extends Deployment { } @Override - def createPreValidation(String stage) { + def preValidation(String stage) { def helmConfig = gitopsConfig.deployments.helm def application = gitopsConfig.application def sourcePath = gitopsConfig.deployments.sourcePath @@ -50,7 +50,7 @@ class Helm extends Deployment { } @Override - def createPostValidation(String stage) { + def postValidation(String stage) { } private void updateYamlValue(String yamlFilePath, Map helmConfig) { diff --git a/src/com/cloudogu/gitopsbuildlib/deployment/plain/Plain.groovy b/src/com/cloudogu/gitopsbuildlib/deployment/plain/Plain.groovy index 052984e..11729fd 100644 --- a/src/com/cloudogu/gitopsbuildlib/deployment/plain/Plain.groovy +++ b/src/com/cloudogu/gitopsbuildlib/deployment/plain/Plain.groovy @@ -9,11 +9,11 @@ class Plain extends Deployment{ } @Override - def createPreValidation(String stage) { + def preValidation(String stage) { } @Override - def createPostValidation(String stage) { + def postValidation(String stage) { updateImage(stage) } diff --git a/src/com/cloudogu/gitopsbuildlib/validation/HelmKubeval.groovy b/src/com/cloudogu/gitopsbuildlib/validation/HelmKubeval.groovy index d8136fb..33b619d 100644 --- a/src/com/cloudogu/gitopsbuildlib/validation/HelmKubeval.groovy +++ b/src/com/cloudogu/gitopsbuildlib/validation/HelmKubeval.groovy @@ -15,7 +15,10 @@ class HelmKubeval extends Validator { ? script.cesBuildLib.Git.new(script, deployments.helm.credentialsId) : script.cesBuildLib.Git.new(script) git url: deployments.helm.repoUrl, branch: 'main', changelog: false, poll: false - git.checkout(deployments.helm.version) + + if(deployments.helm.containsKey('version') && deployments.helm.version) { + git.checkout(deployments.helm.version) + } } def chartPath = '' @@ -24,6 +27,7 @@ class HelmKubeval extends Validator { } withDockerImage(config.image) { + script.sh "helm dep update ${targetDirectory}/chart/${chartPath}" script.sh "helm kubeval ${targetDirectory}/chart/${chartPath} -v ${config.k8sSchemaVersion}" } diff --git a/test/com/cloudogu/gitopsbuildlib/deployment/DeploymentTest.groovy b/test/com/cloudogu/gitopsbuildlib/deployment/DeploymentTest.groovy index 5708ac9..adc74c8 100644 --- a/test/com/cloudogu/gitopsbuildlib/deployment/DeploymentTest.groovy +++ b/test/com/cloudogu/gitopsbuildlib/deployment/DeploymentTest.groovy @@ -106,12 +106,12 @@ class DeploymentTest { } @Override - def createPreValidation(String stage) { + def preValidation(String stage) { return null } @Override - def createPostValidation(String stage) { + def postValidation(String stage) { return null } } diff --git a/test/com/cloudogu/gitopsbuildlib/deployment/HelmTest.groovy b/test/com/cloudogu/gitopsbuildlib/deployment/HelmTest.groovy index 5d3fe93..b04ce87 100644 --- a/test/com/cloudogu/gitopsbuildlib/deployment/HelmTest.groovy +++ b/test/com/cloudogu/gitopsbuildlib/deployment/HelmTest.groovy @@ -67,7 +67,7 @@ class HelmTest { @Test void 'creating helm release with git repo'() { - helmGit.createPreValidation('staging') + helmGit.preValidation('staging') assertThat(dockerMock.actualImages[0]).contains('ghcr.io/cloudogu/helm:') assertThat(scriptMock.actualShArgs[0]).isEqualTo('[returnStdout:true, script:helm values workspace/chart/chartPath -f workspace/k8s/values-staging.yaml -f workspace/k8s/values-shared.yaml ]') @@ -106,7 +106,7 @@ class HelmTest { @Test void 'creating helm release with helm repo'() { - helmHelm.createPreValidation('staging') + helmHelm.preValidation('staging') assertThat(dockerMock.actualImages[0]).contains('ghcr.io/cloudogu/helm:') assertThat(scriptMock.actualShArgs[0]).isEqualTo('helm repo add chartRepo repoUrl') diff --git a/test/com/cloudogu/gitopsbuildlib/deployment/PlainTest.groovy b/test/com/cloudogu/gitopsbuildlib/deployment/PlainTest.groovy index 0f0d625..bfbeb9b 100644 --- a/test/com/cloudogu/gitopsbuildlib/deployment/PlainTest.groovy +++ b/test/com/cloudogu/gitopsbuildlib/deployment/PlainTest.groovy @@ -26,7 +26,7 @@ class PlainTest { @Test void 'successful update'() { - plain.createPostValidation('staging') + plain.postValidation('staging') assertThat(scriptMock.actualReadYamlArgs[0]).isEqualTo('[file:staging/testApp/k8s/deployment.yaml]') assertThat(scriptMock.actualWriteYamlArgs[0]).isEqualTo('[file:staging/testApp/k8s/deployment.yaml, data:[spec:[template:[spec:[containers:[[image:imageNameReplacedTest, name:application]]]]], to:[be:[changed:oldValue]]], overwrite:true]') diff --git a/test/com/cloudogu/gitopsbuildlib/validation/HelmKubevalTest.groovy b/test/com/cloudogu/gitopsbuildlib/validation/HelmKubevalTest.groovy index 3321a82..4aa71dd 100644 --- a/test/com/cloudogu/gitopsbuildlib/validation/HelmKubevalTest.groovy +++ b/test/com/cloudogu/gitopsbuildlib/validation/HelmKubevalTest.groovy @@ -30,8 +30,9 @@ class HelmKubevalTest { ] ) assertThat(dockerMock.actualImages[0]).isEqualTo('img') - assertThat(scriptMock.actualShArgs[0]).isEqualTo('helm kubeval target/chart/chartPath -v 1.5') - assertThat(scriptMock.actualShArgs[1]).isEqualTo('rm -rf target/chart') + assertThat(scriptMock.actualShArgs[0]).isEqualTo('helm dep update target/chart/chartPath') + assertThat(scriptMock.actualShArgs[1]).isEqualTo('helm kubeval target/chart/chartPath -v 1.5') + assertThat(scriptMock.actualShArgs[2]).isEqualTo('rm -rf target/chart') } @Test From 4588ff77995ecb2aa58629e54c7ae218702d6f7d Mon Sep 17 00:00:00 2001 From: Marek Markiewka Date: Mon, 19 Apr 2021 14:13:56 +0200 Subject: [PATCH 08/17] readme updated Signed-off-by: Marek Markiewka --- README.md | 39 +++++++++---------- .../deployment/helm/Helm.groovy | 2 +- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index c05a9a8..56815c4 100644 --- a/README.md +++ b/README.md @@ -15,11 +15,8 @@ working example bundled with the complete infrastructure for a gitops deep dive. - [Real life examples](#real-life-examples) - [Usage](#usage) - [Default Folder Structure](#default-folder-structure) - - [FluxV1](#fluxv1) - - [Plain-k8s](#plain-k8s) - - [Helm](#helm) - - [FluxV2](#fluxv2) - - [ArgoCD](#argocd) + - [Plain-k8s](#plain-k8s) + - [Helm](#helm) - [GitOps-Config](#gitops-config) - [SCM-Provider](#scm-provider) - [Stages](#stages) @@ -120,6 +117,7 @@ def gitopsConfig = [ cesBuildLibVersion: /* Default: a recent cesBuildLibVersion see deployViaGitops.groovy */ , cesBuildLibCredentialsId: /* Default: '', empty due to default public github repo */, application: 'spring-petclinic', + gitopsTool: 'FLUX' mainBranch: 'master' /* Default: 'main' */, deployments: [ sourcePath: 'k8s' /* Default: 'k8s' */, @@ -157,9 +155,14 @@ deployViaGitops(gitopsConfig) ### Real life examples **FluxV1:** -* [using petclinic with helm](https://github.com/cloudogu/k8s-gitops-playground/blob/main/applications/petclinic/fluxv1/helm/Jenkinsfile) -* [using petclinic with plain-k8s](https://github.com/cloudogu/k8s-gitops-playground/blob/main/applications/petclinic/fluxv1/plain-k8s/Jenkinsfile) -* [using helm with nginx and extra resources](https://github.com/cloudogu/k8s-gitops-playground/blob/main/applications/nginx/fluxv1/Jenkinsfile) +* [using petclinic with helm and extra k8s-resources and extra files](https://github.com/cloudogu/k8s-gitops-playground/blob/main/applications/petclinic/fluxv1/helm/Jenkinsfile) +* [using petclinic with plain-k8s and extra files](https://github.com/cloudogu/k8s-gitops-playground/blob/main/applications/petclinic/fluxv1/plain-k8s/Jenkinsfile) +* [using nginx with helm and extra files](https://github.com/cloudogu/k8s-gitops-playground/blob/main/applications/nginx/fluxv1/Jenkinsfile) + +**ArgoCD:** +* [using petclinic with helm and extra k8s-resources and extra files](https://github.com/cloudogu/k8s-gitops-playground/blob/main/applications/petclinic/argocd/helm/Jenkinsfile) +* [using petclinic with plain-k8s](https://github.com/cloudogu/k8s-gitops-playground/blob/main/applications/petclinic/argocd/plain-k8s/Jenkinsfile) + --- @@ -201,9 +204,7 @@ A default project structure in your application repo could look like the example and/or helm resources bundled in a folder. This specific resources folder (here `k8s`) will later be specified by the `sourcePath` within the deployments section of your `gitopsConfig`. -### FluxV1 - -#### Plain-k8s +### Plain-k8s ``` ├── application ├── config.yamllint.yaml // not necessarily needed @@ -217,7 +218,7 @@ and/or helm resources bundled in a folder. This specific resources folder (here └── service.yaml ``` -#### Helm +### Helm ``` ├── application ├── config.yamllint.yaml // not necessarily needed @@ -238,14 +239,6 @@ and/or helm resources bundled in a folder. This specific resources folder (here └── values-staging.yaml ``` -### FluxV2 - -Upcoming - -### ArgoCD - -Upcoming - --- ## GitOps-Config @@ -254,12 +247,16 @@ You can find a complete yet simple example [here](#examples). **Properties** -First of all there are some mandatory properties e.g. the information about your gitops repository and the application repository. +First of all there are some mandatory properties e.g. the information about your gitops repository, the application repository and the gitops tool to be used. ``` application: 'spring-petclinic' // Name of the application. Used as a folder in GitOps repo ``` +``` +gitopsTool: 'ARGO' // Name of the gitops tool. Currently supporting 'FLUX' (for now only fluxV1) and 'ARGO' (for now supporting only helm charts from git repos) +``` + and some optional parameters (below are the defaults) for the configuration of the dependency to the ces-build-lib or the default name for the git branch: ``` cesBuildLibRepo: 'https://github.com/cloudogu/ces-build-lib', diff --git a/src/com/cloudogu/gitopsbuildlib/deployment/helm/Helm.groovy b/src/com/cloudogu/gitopsbuildlib/deployment/helm/Helm.groovy index d713bed..13e27f7 100644 --- a/src/com/cloudogu/gitopsbuildlib/deployment/helm/Helm.groovy +++ b/src/com/cloudogu/gitopsbuildlib/deployment/helm/Helm.groovy @@ -22,7 +22,7 @@ class Helm extends Deployment { } if(gitopsConfig.gitopsTool == 'FLUX') { helmRelease = new FluxV1Release(script) - } else if(gitopsConfig.gitopsTool == 'ARGO_CD') { + } else if(gitopsConfig.gitopsTool == 'ARGO') { helmRelease = new ArgoCDRelease(script) } } From e97cf6ed534406fdeab2a7580cfa0948aa5b2f5b Mon Sep 17 00:00:00 2001 From: Marek Markiewka Date: Mon, 19 Apr 2021 14:16:51 +0200 Subject: [PATCH 09/17] readme updated Signed-off-by: Marek Markiewka --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 56815c4..bac16a0 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,7 @@ def gitopsConfig = [ repositoryUrl: 'fluxv1/gitops' ], application: 'spring-petclinic', + gitopsTool: 'FLUX', stages: [ staging: [ deployDirectly: true From e06ae42619214a8bb4ae2977f5596dbdfc8b09e1 Mon Sep 17 00:00:00 2001 From: Marek Markiewka Date: Wed, 21 Apr 2021 17:47:22 +0200 Subject: [PATCH 10/17] refactoring Signed-off-by: Marek Markiewka --- .../deployment/helm/Helm.groovy | 24 ---- .../helm/helmrelease/ArgoCDRelease.groovy | 10 +- .../helm/helmrelease/FluxV1Release.groovy | 4 +- .../helm/helmrelease/HelmRelease.groovy | 2 +- .../deployment/helm/repotype/GitRepo.groovy | 15 +-- .../gitopsbuildlib/DeployViaGitopsTest.groovy | 2 +- .../cloudogu/gitopsbuildlib/DockerMock.groovy | 3 +- .../cloudogu/gitopsbuildlib/GitMock.groovy | 4 +- .../cloudogu/gitopsbuildlib/ScriptMock.groovy | 2 - .../deployment/DeploymentTest.groovy | 36 +++--- .../gitopsbuildlib/deployment/HelmTest.groovy | 112 +++++++++--------- 11 files changed, 95 insertions(+), 119 deletions(-) diff --git a/src/com/cloudogu/gitopsbuildlib/deployment/helm/Helm.groovy b/src/com/cloudogu/gitopsbuildlib/deployment/helm/Helm.groovy index 13e27f7..49e4096 100644 --- a/src/com/cloudogu/gitopsbuildlib/deployment/helm/Helm.groovy +++ b/src/com/cloudogu/gitopsbuildlib/deployment/helm/Helm.groovy @@ -68,28 +68,4 @@ class Helm extends Deployment { } script.writeYaml file: yamlFilePath, data: data, overwrite: true } - - - //TODO helmValuesFromFile not yet implemented -// private String createFromFileValues(String stage, Map gitopsConfig) { -// String values = "" -// -// gitopsConfig.helmValuesFromFile.each { -// if (stage in it['stage']) { -// values = fileToInlineYaml(it['key'], "${script.env.WORKSPACE}/k8s/${it['file']}") -// } -// } -// return values -// } -// -// private String fileToInlineYaml(String key, String filePath) { -// String values = "" -// String indent = " " -// -// def fileContent = readFile filePath -// values += "\n ${key}: |\n${indent}" -// values += fileContent.split("\\n").join("\n" + indent) -// -// return values -// } } diff --git a/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/ArgoCDRelease.groovy b/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/ArgoCDRelease.groovy index 7729d0d..a1574ba 100644 --- a/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/ArgoCDRelease.groovy +++ b/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/ArgoCDRelease.groovy @@ -9,19 +9,19 @@ class ArgoCDRelease extends HelmRelease{ } @Override - String create(Map helmConfig, String application, String namespace, String valuesFile) { + String create(Map helmConfig, String application, String namespace, String mergedValuesFile) { - String valuesFileLocation = "${script.env.WORKSPACE}/.configRepoTempDir/${valuesFile}" + String mergedValuesFileLocation = "${script.env.WORKSPACE}/.configRepoTempDir/${mergedValuesFile}" String helmRelease = "" if (helmConfig.repoType == 'GIT') { - helmRelease = gitRepoRelease(helmConfig, application, valuesFileLocation) + helmRelease = createResourcesFromGitRepo(helmConfig, application, mergedValuesFileLocation) } else if (helmConfig.repoType == 'HELM') { // TODO not yet implemented } return helmRelease } - private String gitRepoRelease(Map helmConfig, String application, String valuesFileLocation) { + private String createResourcesFromGitRepo(Map helmConfig, String application, String mergedValuesFileLocation) { String helmRelease = "" def chartPath = '' @@ -32,7 +32,7 @@ class ArgoCDRelease extends HelmRelease{ withHelm { script.dir("${script.env.WORKSPACE}/chart/${chartPath}") { script.sh "helm dep update ." - String templateScript = "helm template ${application} . -f ${valuesFileLocation}" + String templateScript = "helm template ${application} . -f ${mergedValuesFileLocation}" helmRelease = script.sh returnStdout: true, script: templateScript } } diff --git a/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/FluxV1Release.groovy b/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/FluxV1Release.groovy index 9e47dd8..c9ec63d 100644 --- a/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/FluxV1Release.groovy +++ b/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/FluxV1Release.groovy @@ -7,8 +7,8 @@ class FluxV1Release extends HelmRelease{ } @Override - String create(Map helmConfig, String application, String namespace, String valuesFile) { - def values = fileToInlineYaml(valuesFile) + String create(Map helmConfig, String application, String namespace, String mergedValuesFile) { + def values = fileToInlineYaml(mergedValuesFile) def chart = getChart(helmConfig) return """apiVersion: helm.fluxcd.io/v1 kind: HelmRelease diff --git a/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/HelmRelease.groovy b/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/HelmRelease.groovy index 013a8d6..7342fff 100644 --- a/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/HelmRelease.groovy +++ b/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/HelmRelease.groovy @@ -8,7 +8,7 @@ abstract class HelmRelease { this.script = script } - abstract String create(Map helmConfig, String application, String namespace, String valuesFile) + abstract String create(Map helmConfig, String application, String namespace, String mergedValuesFile) String fileToInlineYaml(String fileContents) { String values = "" diff --git a/src/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/GitRepo.groovy b/src/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/GitRepo.groovy index af630f5..a6f026e 100644 --- a/src/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/GitRepo.groovy +++ b/src/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/GitRepo.groovy @@ -26,20 +26,21 @@ class GitRepo extends RepoType { } private prepareGitRepo(Map helmConfig) { - def myGit + def git script.dir("${script.env.WORKSPACE}/chart") { if (helmConfig.containsKey('credentialsId')) { - script.git credentialsId: helmConfig.credentialsId, url: helmConfig.repoUrl, branch: 'main', changelog: false, poll: false - myGit = script.cesBuildLib.Git.new(script, helmConfig.credentialsId) + git = script.cesBuildLib.Git.new(script, helmConfig.credentialsId) } else { - script.git url: helmConfig.repoUrl, branch: 'main', changelog: false, poll: false - myGit = script.cesBuildLib.Git.new(script) + git = script.cesBuildLib.Git.new(script) } + + git url: helmConfig.repoUrl, branch: 'main', changelog: false, poll: false + if(helmConfig.containsKey('version') && helmConfig.version) { - myGit.fetch() - myGit.checkout(helmConfig.version) + git.fetch() + git.checkout(helmConfig.version) } } } diff --git a/test/com/cloudogu/gitopsbuildlib/DeployViaGitopsTest.groovy b/test/com/cloudogu/gitopsbuildlib/DeployViaGitopsTest.groovy index 8a04e30..2378eec 100644 --- a/test/com/cloudogu/gitopsbuildlib/DeployViaGitopsTest.groovy +++ b/test/com/cloudogu/gitopsbuildlib/DeployViaGitopsTest.groovy @@ -1,6 +1,6 @@ package com.cloudogu.gitopsbuildlib -import com.cloudogu.ces.cesbuildlib.DockerMock + import com.cloudogu.ces.cesbuildlib.Git import com.cloudogu.gitopsbuildlib.validation.Kubeval import com.cloudogu.gitopsbuildlib.validation.Yamllint diff --git a/test/com/cloudogu/gitopsbuildlib/DockerMock.groovy b/test/com/cloudogu/gitopsbuildlib/DockerMock.groovy index 474ce92..72308f6 100644 --- a/test/com/cloudogu/gitopsbuildlib/DockerMock.groovy +++ b/test/com/cloudogu/gitopsbuildlib/DockerMock.groovy @@ -1,5 +1,6 @@ -package com.cloudogu.ces.cesbuildlib +package com.cloudogu.gitopsbuildlib +import com.cloudogu.ces.cesbuildlib.Docker import org.mockito.invocation.InvocationOnMock import org.mockito.stubbing.Answer diff --git a/test/com/cloudogu/gitopsbuildlib/GitMock.groovy b/test/com/cloudogu/gitopsbuildlib/GitMock.groovy index 58eb21c..3362b48 100644 --- a/test/com/cloudogu/gitopsbuildlib/GitMock.groovy +++ b/test/com/cloudogu/gitopsbuildlib/GitMock.groovy @@ -1,4 +1,6 @@ -package com.cloudogu.ces.cesbuildlib +package com.cloudogu.gitopsbuildlib + +import com.cloudogu.ces.cesbuildlib.Git import static org.mockito.Mockito.mock diff --git a/test/com/cloudogu/gitopsbuildlib/ScriptMock.groovy b/test/com/cloudogu/gitopsbuildlib/ScriptMock.groovy index fb1ab81..b27d653 100644 --- a/test/com/cloudogu/gitopsbuildlib/ScriptMock.groovy +++ b/test/com/cloudogu/gitopsbuildlib/ScriptMock.groovy @@ -1,7 +1,5 @@ package com.cloudogu.gitopsbuildlib -import com.cloudogu.ces.cesbuildlib.DockerMock -import com.cloudogu.ces.cesbuildlib.GitMock import groovy.yaml.YamlSlurper class ScriptMock { diff --git a/test/com/cloudogu/gitopsbuildlib/deployment/DeploymentTest.groovy b/test/com/cloudogu/gitopsbuildlib/deployment/DeploymentTest.groovy index adc74c8..a6696d1 100644 --- a/test/com/cloudogu/gitopsbuildlib/deployment/DeploymentTest.groovy +++ b/test/com/cloudogu/gitopsbuildlib/deployment/DeploymentTest.groovy @@ -78,24 +78,24 @@ class DeploymentTest { assertThat(scriptMock.actualShArgs[0]).isEqualTo('[returnStdout:true, script:KUBECONFIG=pwd/.kube/config kubectl create configmap index --from-file=index.html=workspace/k8s/../index.html --dry-run=client -o yaml -n fluxv1-staging]') - assertThat(scriptMock.actualWriteFileArgs[0]).isEqualTo('[file:pwd/.kube/config, text:apiVersion: v1\n' + - 'clusters:\n' + - '- cluster:\n' + - ' certificate-authority-data: DATA+OMITTED\n' + - ' server: https://localhost\n' + - ' name: self-hosted-cluster\n' + - 'contexts:\n' + - '- context:\n' + - ' cluster: self-hosted-cluster\n' + - ' user: svcs-acct-dply\n' + - ' name: svcs-acct-context\n' + - 'current-context: svcs-acct-context\n' + - 'kind: Config\n' + - 'preferences: {}\n' + - 'users:\n' + - '- name: svcs-acct-dply\n' + - ' user:\n' + - ' token: DATA+OMITTED]') + assertThat(scriptMock.actualWriteFileArgs[0]).isEqualTo('''[file:pwd/.kube/config, text:apiVersion: v1 +clusters: +- cluster: + certificate-authority-data: DATA+OMITTED + server: https://localhost + name: self-hosted-cluster +contexts: +- context: + cluster: self-hosted-cluster + user: svcs-acct-dply + name: svcs-acct-context +current-context: svcs-acct-context +kind: Config +preferences: {} +users: +- name: svcs-acct-dply + user: + token: DATA+OMITTED]''') assertThat(scriptMock.actualWriteFileArgs[1]).contains('[file:staging/app/generatedResources/index.yaml') } diff --git a/test/com/cloudogu/gitopsbuildlib/deployment/HelmTest.groovy b/test/com/cloudogu/gitopsbuildlib/deployment/HelmTest.groovy index b04ce87..e73a524 100644 --- a/test/com/cloudogu/gitopsbuildlib/deployment/HelmTest.groovy +++ b/test/com/cloudogu/gitopsbuildlib/deployment/HelmTest.groovy @@ -57,9 +57,7 @@ class HelmTest { fileConfigmaps: [ [ name : "index", - sourceFilePath : "../index.html", // relative to deployments.sourcePath - // additional feature in the future. current default folder is '../generated-resources' - // destinationFilePath: "../generated-resources/html/" // realtive to deployments.sourcePath + sourceFilePath : "../index.html", stage: ["staging"] ] ] @@ -75,33 +73,33 @@ class HelmTest { assertThat(scriptMock.actualShArgs[2]).isEqualTo('rm -rf workspace/chart || true') assertThat(scriptMock.actualGitArgs[0]).isEqualTo('[url:repoUrl, branch:main, changelog:false, poll:false]') assertThat(scriptMock.actualWriteFileArgs[0]).isEqualTo('[file:staging/testapp/mergedValues.yaml, text:[[returnStdout:true, script:helm values workspace/chart/chartPath -f workspace/k8s/values-staging.yaml -f workspace/k8s/values-shared.yaml ]]]') - assertThat(scriptMock.actualWriteFileArgs[1]).isEqualTo('[file:staging/testapp/helmRelease.yaml, text:apiVersion: helm.fluxcd.io/v1\n' + - 'kind: HelmRelease\n' + - 'metadata:\n' + - ' name: testapp\n' + - ' namespace: fluxv1-staging\n' + - ' annotations:\n' + - ' fluxcd.io/automated: "false"\n' + - 'spec:\n' + - ' releaseName: testapp\n' + - ' chart:\n' + - ' git: repoUrl\n' + - ' ref: null\n' + - ' path: .\n' + - ' values:\n' + - ' ---\n' + - ' #this part is only for PlainTest regarding updating the image name\n' + - ' spec:\n' + - ' template:\n' + - ' spec:\n' + - ' containers:\n' + - ' - name: \'application\'\n' + - ' image: \'oldImageName\'\n' + - ' #this part is only for HelmTest regarding changing the yaml values\n' + - ' to:\n' + - ' be:\n' + - ' changed: \'oldValue\'\n' + - ']') + assertThat(scriptMock.actualWriteFileArgs[1]).isEqualTo('''[file:staging/testapp/helmRelease.yaml, text:apiVersion: helm.fluxcd.io/v1 +kind: HelmRelease +metadata: + name: testapp + namespace: fluxv1-staging + annotations: + fluxcd.io/automated: "false" +spec: + releaseName: testapp + chart: + git: repoUrl + ref: null + path: . + values: + --- + #this part is only for PlainTest regarding updating the image name + spec: + template: + spec: + containers: + - name: \'application\' + image: \'oldImageName\' + #this part is only for HelmTest regarding changing the yaml values + to: + be: + changed: \'oldValue\' +]''') } @Test @@ -119,32 +117,32 @@ class HelmTest { 'text:[helm repo add chartRepo repoUrl, helm repo update, ' + 'helm pull chartRepo/chartName --version=1.0 --untar --untardir=workspace/chart, ' + '[returnStdout:true, script:helm values workspace/chart/chartName -f workspace/k8s/values-staging.yaml -f workspace/k8s/values-shared.yaml ]]]') - assertThat(scriptMock.actualWriteFileArgs[1]).isEqualTo('[file:staging/testapp/helmRelease.yaml, text:apiVersion: helm.fluxcd.io/v1\n' + - 'kind: HelmRelease\n' + - 'metadata:\n' + - ' name: testapp\n' + - ' namespace: fluxv1-staging\n' + - ' annotations:\n' + - ' fluxcd.io/automated: "false"\n' + - 'spec:\n' + - ' releaseName: testapp\n' + - ' chart:\n' + - ' repository: repoUrl\n' + - ' name: chartName\n' + - ' version: 1.0\n' + - ' values:\n' + - ' ---\n' + - ' #this part is only for PlainTest regarding updating the image name\n' + - ' spec:\n' + - ' template:\n' + - ' spec:\n' + - ' containers:\n' + - ' - name: \'application\'\n' + - ' image: \'oldImageName\'\n' + - ' #this part is only for HelmTest regarding changing the yaml values\n' + - ' to:\n' + - ' be:\n' + - ' changed: \'oldValue\'\n' + - ']') + assertThat(scriptMock.actualWriteFileArgs[1]).isEqualTo('''[file:staging/testapp/helmRelease.yaml, text:apiVersion: helm.fluxcd.io/v1 +kind: HelmRelease +metadata: + name: testapp + namespace: fluxv1-staging + annotations: + fluxcd.io/automated: "false" +spec: + releaseName: testapp + chart: + repository: repoUrl + name: chartName + version: 1.0 + values: + --- + #this part is only for PlainTest regarding updating the image name + spec: + template: + spec: + containers: + - name: \'application\' + image: \'oldImageName\' + #this part is only for HelmTest regarding changing the yaml values + to: + be: + changed: \'oldValue\' +]''') } } From b9e474411d88d09b20b9cb38fa9c1dc2e8f54966 Mon Sep 17 00:00:00 2001 From: Marek Markiewka Date: Thu, 22 Apr 2021 10:21:06 +0200 Subject: [PATCH 11/17] implemented dockerwrapper Signed-off-by: Marek Markiewka --- .../helm/helmrelease/ArgoCDRelease.groovy | 15 +++------ .../deployment/helm/repotype/RepoType.groovy | 10 +++--- .../docker/DockerWrapper.groovy | 31 +++++++++++++++++++ .../validation/HelmKubeval.groovy | 2 ++ .../gitopsbuildlib/validation/Kubeval.groovy | 3 ++ .../validation/Validator.groovy | 13 +++----- .../gitopsbuildlib/validation/Yamllint.groovy | 2 ++ .../gitopsbuildlib/deployment/HelmTest.groovy | 1 - .../helm/repotype/GitRepoTest.groovy | 1 - .../validation/ValidatorTest.groovy | 1 + 10 files changed, 54 insertions(+), 25 deletions(-) create mode 100644 src/com/cloudogu/gitopsbuildlib/docker/DockerWrapper.groovy diff --git a/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/ArgoCDRelease.groovy b/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/ArgoCDRelease.groovy index a1574ba..035a86c 100644 --- a/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/ArgoCDRelease.groovy +++ b/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/ArgoCDRelease.groovy @@ -1,11 +1,14 @@ package com.cloudogu.gitopsbuildlib.deployment.helm.helmrelease +import com.cloudogu.gitopsbuildlib.docker.DockerWrapper + class ArgoCDRelease extends HelmRelease{ - protected static String getHelmImage() { 'ghcr.io/cloudogu/helm:3.5.4-1' } + protected DockerWrapper dockerWrapper ArgoCDRelease(def script) { super(script) + dockerWrapper = new DockerWrapper(script) } @Override @@ -29,7 +32,7 @@ class ArgoCDRelease extends HelmRelease{ chartPath = helmConfig.chartPath } - withHelm { + dockerWrapper.withHelm { script.dir("${script.env.WORKSPACE}/chart/${chartPath}") { script.sh "helm dep update ." String templateScript = "helm template ${application} . -f ${mergedValuesFileLocation}" @@ -40,12 +43,4 @@ class ArgoCDRelease extends HelmRelease{ helmRelease = helmRelease.replaceAll("(?m)^[ \t]*\r?\n", "") return helmRelease } - - void withHelm(Closure body) { - script.cesBuildLib.Docker.new(script).image(helmImage).inside( - "${script.pwd().equals(script.env.WORKSPACE) ? '' : "-v ${script.env.WORKSPACE}:${script.env.WORKSPACE}"}" - ) { - body() - } - } } diff --git a/src/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/RepoType.groovy b/src/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/RepoType.groovy index 7f60b6e..00f65c6 100644 --- a/src/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/RepoType.groovy +++ b/src/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/RepoType.groovy @@ -1,21 +1,21 @@ package com.cloudogu.gitopsbuildlib.deployment.helm.repotype -abstract class RepoType { +import com.cloudogu.gitopsbuildlib.docker.DockerWrapper - protected static String getHelmImage() { 'ghcr.io/cloudogu/helm:3.5.4-1' } +abstract class RepoType { protected script + protected DockerWrapper dockerWrapper RepoType(def script) { this.script = script + dockerWrapper = new DockerWrapper(script) } abstract mergeValues(Map helmConfig, String[] files) void withHelm(Closure body) { - script.cesBuildLib.Docker.new(script).image(helmImage).inside( - "${script.pwd().equals(script.env.WORKSPACE) ? '' : "-v ${script.env.WORKSPACE}:${script.env.WORKSPACE}"}" - ) { + dockerWrapper.withHelm { body() } } diff --git a/src/com/cloudogu/gitopsbuildlib/docker/DockerWrapper.groovy b/src/com/cloudogu/gitopsbuildlib/docker/DockerWrapper.groovy new file mode 100644 index 0000000..bea2205 --- /dev/null +++ b/src/com/cloudogu/gitopsbuildlib/docker/DockerWrapper.groovy @@ -0,0 +1,31 @@ +package com.cloudogu.gitopsbuildlib.docker + +class DockerWrapper { + + protected static String getHelmImage() { 'ghcr.io/cloudogu/helm:3.5.4-1' } + + protected def script + + DockerWrapper(def script) { + this.script = script + } + + void withDockerImage(String image, Closure body) { + script.docker.image(image).inside( + // Allow accessing WORKSPACE even when we are in a child dir (using "dir() {}") + "${script.pwd().equals(script.env.WORKSPACE) ? '' : "-v ${script.env.WORKSPACE}:${script.env.WORKSPACE} "}" + + // Avoid: "ERROR: The container started but didn't run the expected command" + '--entrypoint=""' + ) { + body() + } + } + + void withHelm(Closure body) { + script.cesBuildLib.Docker.new(script).image(helmImage).inside( + "${script.pwd().equals(script.env.WORKSPACE) ? '' : "-v ${script.env.WORKSPACE}:${script.env.WORKSPACE}"}" + ) { + body() + } + } +} diff --git a/src/com/cloudogu/gitopsbuildlib/validation/HelmKubeval.groovy b/src/com/cloudogu/gitopsbuildlib/validation/HelmKubeval.groovy index 33b619d..7135a2b 100644 --- a/src/com/cloudogu/gitopsbuildlib/validation/HelmKubeval.groovy +++ b/src/com/cloudogu/gitopsbuildlib/validation/HelmKubeval.groovy @@ -1,5 +1,7 @@ package com.cloudogu.gitopsbuildlib.validation +import com.cloudogu.gitopsbuildlib.docker.DockerWrapper + class HelmKubeval extends Validator { HelmKubeval(def script) { diff --git a/src/com/cloudogu/gitopsbuildlib/validation/Kubeval.groovy b/src/com/cloudogu/gitopsbuildlib/validation/Kubeval.groovy index 863c1a8..db57428 100644 --- a/src/com/cloudogu/gitopsbuildlib/validation/Kubeval.groovy +++ b/src/com/cloudogu/gitopsbuildlib/validation/Kubeval.groovy @@ -1,10 +1,13 @@ package com.cloudogu.gitopsbuildlib.validation +import com.cloudogu.gitopsbuildlib.docker.DockerWrapper + /** * Validates all yaml-resources within the target-directory against the specs of the given k8s version */ class Kubeval extends Validator { + Kubeval(def script) { super(script) } diff --git a/src/com/cloudogu/gitopsbuildlib/validation/Validator.groovy b/src/com/cloudogu/gitopsbuildlib/validation/Validator.groovy index a89bef0..294a610 100644 --- a/src/com/cloudogu/gitopsbuildlib/validation/Validator.groovy +++ b/src/com/cloudogu/gitopsbuildlib/validation/Validator.groovy @@ -1,11 +1,15 @@ package com.cloudogu.gitopsbuildlib.validation +import com.cloudogu.gitopsbuildlib.docker.DockerWrapper + abstract class Validator { protected script + protected DockerWrapper dockerWrapper Validator(def script) { this.script = script + dockerWrapper = new DockerWrapper(script) } void validate(boolean enabled, String targetDirectory, Map config, Map deployments) { @@ -19,13 +23,6 @@ abstract class Validator { abstract protected void validate(String targetDirectory, Map config, Map deployments) protected void withDockerImage(String image, Closure body) { - script.docker.image(image).inside( - // Allow accessing WORKSPACE even when we are in a child dir (using "dir() {}") - "${script.pwd().equals(script.env.WORKSPACE) ? '' : "-v ${script.env.WORKSPACE}:${script.env.WORKSPACE} "}" + - // Avoid: "ERROR: The container started but didn't run the expected command" - '--entrypoint=""' - ) { - body() - } + dockerWrapper.withDockerImage(image, body) } } diff --git a/src/com/cloudogu/gitopsbuildlib/validation/Yamllint.groovy b/src/com/cloudogu/gitopsbuildlib/validation/Yamllint.groovy index 43d22f0..8427c77 100644 --- a/src/com/cloudogu/gitopsbuildlib/validation/Yamllint.groovy +++ b/src/com/cloudogu/gitopsbuildlib/validation/Yamllint.groovy @@ -1,5 +1,7 @@ package com.cloudogu.gitopsbuildlib.validation +import com.cloudogu.gitopsbuildlib.docker.DockerWrapper + /** * Checks for correct YAML syntax using yamllint * diff --git a/test/com/cloudogu/gitopsbuildlib/deployment/HelmTest.groovy b/test/com/cloudogu/gitopsbuildlib/deployment/HelmTest.groovy index e73a524..855f3df 100644 --- a/test/com/cloudogu/gitopsbuildlib/deployment/HelmTest.groovy +++ b/test/com/cloudogu/gitopsbuildlib/deployment/HelmTest.groovy @@ -71,7 +71,6 @@ class HelmTest { assertThat(scriptMock.actualShArgs[0]).isEqualTo('[returnStdout:true, script:helm values workspace/chart/chartPath -f workspace/k8s/values-staging.yaml -f workspace/k8s/values-shared.yaml ]') assertThat(scriptMock.actualShArgs[1]).isEqualTo('rm staging/testapp/mergedValues.yaml') assertThat(scriptMock.actualShArgs[2]).isEqualTo('rm -rf workspace/chart || true') - assertThat(scriptMock.actualGitArgs[0]).isEqualTo('[url:repoUrl, branch:main, changelog:false, poll:false]') assertThat(scriptMock.actualWriteFileArgs[0]).isEqualTo('[file:staging/testapp/mergedValues.yaml, text:[[returnStdout:true, script:helm values workspace/chart/chartPath -f workspace/k8s/values-staging.yaml -f workspace/k8s/values-shared.yaml ]]]') assertThat(scriptMock.actualWriteFileArgs[1]).isEqualTo('''[file:staging/testapp/helmRelease.yaml, text:apiVersion: helm.fluxcd.io/v1 kind: HelmRelease diff --git a/test/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/GitRepoTest.groovy b/test/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/GitRepoTest.groovy index 61bd02a..7f543c1 100644 --- a/test/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/GitRepoTest.groovy +++ b/test/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/GitRepoTest.groovy @@ -22,6 +22,5 @@ class GitRepoTest { ] as String[]) assertThat(scriptMock.actualShArgs[0]).isEqualTo('[returnStdout:true, script:helm values workspace/chart/chartPath -f file1 -f file2 ]') - assertThat(scriptMock.actualGitArgs[0]).isEqualTo('[url:url, branch:main, changelog:false, poll:false]') } } diff --git a/test/com/cloudogu/gitopsbuildlib/validation/ValidatorTest.groovy b/test/com/cloudogu/gitopsbuildlib/validation/ValidatorTest.groovy index 1b64a81..9b131d8 100644 --- a/test/com/cloudogu/gitopsbuildlib/validation/ValidatorTest.groovy +++ b/test/com/cloudogu/gitopsbuildlib/validation/ValidatorTest.groovy @@ -1,6 +1,7 @@ package com.cloudogu.gitopsbuildlib.validation import com.cloudogu.gitopsbuildlib.ScriptMock +import com.cloudogu.gitopsbuildlib.docker.DockerWrapper import com.cloudogu.gitopsbuildlib.validation.Validator import org.junit.jupiter.api.Test import static org.assertj.core.api.Assertions.assertThat From b9bebf0b782f256cdd1de3e890310dfffe1eae10 Mon Sep 17 00:00:00 2001 From: Marek Markiewka Date: Thu, 22 Apr 2021 10:52:04 +0200 Subject: [PATCH 12/17] consistent helm chart path Signed-off-by: Marek Markiewka --- .../deployment/helm/Helm.groovy | 2 +- .../helm/helmrelease/ArgoCDRelease.groovy | 14 +++++------ .../helm/helmrelease/FluxV1Release.groovy | 8 +++++- .../deployment/helm/repotype/GitRepo.groovy | 8 +++--- .../validation/HelmKubeval.groovy | 25 +++++++++---------- 5 files changed, 30 insertions(+), 27 deletions(-) diff --git a/src/com/cloudogu/gitopsbuildlib/deployment/helm/Helm.groovy b/src/com/cloudogu/gitopsbuildlib/deployment/helm/Helm.groovy index 49e4096..ea37271 100644 --- a/src/com/cloudogu/gitopsbuildlib/deployment/helm/Helm.groovy +++ b/src/com/cloudogu/gitopsbuildlib/deployment/helm/Helm.groovy @@ -45,7 +45,7 @@ class Helm extends Deployment { script.sh "rm ${stage}/${application}/mergedValues.yaml" // clean the gitrepo helm chart folder since the helmRelease.yaml ist now created if (helmConfig.repoType == 'GIT') { - script.sh "rm -rf ${script.env.WORKSPACE}/chart || true" + script.sh "rm -rf ./chart || true" } } diff --git a/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/ArgoCDRelease.groovy b/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/ArgoCDRelease.groovy index 035a86c..74ccda4 100644 --- a/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/ArgoCDRelease.groovy +++ b/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/ArgoCDRelease.groovy @@ -14,17 +14,17 @@ class ArgoCDRelease extends HelmRelease{ @Override String create(Map helmConfig, String application, String namespace, String mergedValuesFile) { - String mergedValuesFileLocation = "${script.env.WORKSPACE}/.configRepoTempDir/${mergedValuesFile}" +// String mergedValuesFileLocation = "${script.env.WORKSPACE}/.configRepoTempDir/${mergedValuesFile}" String helmRelease = "" if (helmConfig.repoType == 'GIT') { - helmRelease = createResourcesFromGitRepo(helmConfig, application, mergedValuesFileLocation) + helmRelease = createResourcesFromGitRepo(helmConfig, application, mergedValuesFile) } else if (helmConfig.repoType == 'HELM') { // TODO not yet implemented } return helmRelease } - private String createResourcesFromGitRepo(Map helmConfig, String application, String mergedValuesFileLocation) { + private String createResourcesFromGitRepo(Map helmConfig, String application, String mergedValuesFile) { String helmRelease = "" def chartPath = '' @@ -33,11 +33,9 @@ class ArgoCDRelease extends HelmRelease{ } dockerWrapper.withHelm { - script.dir("${script.env.WORKSPACE}/chart/${chartPath}") { - script.sh "helm dep update ." - String templateScript = "helm template ${application} . -f ${mergedValuesFileLocation}" - helmRelease = script.sh returnStdout: true, script: templateScript - } + script.sh "helm dep update ./chart/${chartPath}" + String templateScript = "helm template ${application} ./chart/${chartPath} -f ${mergedValuesFile}" + helmRelease = script.sh returnStdout: true, script: templateScript } // this line removes all empty lines since helm template creates some and the helm validator will throw an error if there are emtpy lines present helmRelease = helmRelease.replaceAll("(?m)^[ \t]*\r?\n", "") diff --git a/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/FluxV1Release.groovy b/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/FluxV1Release.groovy index c9ec63d..0d3bf7c 100644 --- a/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/FluxV1Release.groovy +++ b/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/FluxV1Release.groovy @@ -26,10 +26,16 @@ ${values} } private String gitRepoChart(Map helmConfig) { + + def chartPath = "." + if (helmConfig.containsKey('chartPath') && helmConfig.chartPath) { + chartPath = helmConfig.chartPath + } + return """ git: ${helmConfig.repoUrl} ref: ${helmConfig.version} - path: .""" + path: ${chartPath}""" } private String helmRepoChart(Map helmConfig) { diff --git a/src/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/GitRepo.groovy b/src/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/GitRepo.groovy index a6f026e..1c0b1c5 100644 --- a/src/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/GitRepo.groovy +++ b/src/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/GitRepo.groovy @@ -10,7 +10,7 @@ class GitRepo extends RepoType { String mergeValues(Map helmConfig, String[] valuesFiles) { String merge = "" - prepareGitRepo(helmConfig) + getHelmChartFromGitRepo(helmConfig) def chartPath = '' if (helmConfig.containsKey('chartPath')) { @@ -18,17 +18,17 @@ class GitRepo extends RepoType { } withHelm { - String helmScript = "helm values ${script.env.WORKSPACE}/chart/${chartPath} ${valuesFilesWithParameter(valuesFiles)}" + String helmScript = "helm values ./chart/${chartPath} ${valuesFilesWithParameter(valuesFiles)}" merge = script.sh returnStdout: true, script: helmScript } return merge } - private prepareGitRepo(Map helmConfig) { + private getHelmChartFromGitRepo(Map helmConfig) { def git - script.dir("${script.env.WORKSPACE}/chart") { + script.dir("./chart") { if (helmConfig.containsKey('credentialsId')) { git = script.cesBuildLib.Git.new(script, helmConfig.credentialsId) diff --git a/src/com/cloudogu/gitopsbuildlib/validation/HelmKubeval.groovy b/src/com/cloudogu/gitopsbuildlib/validation/HelmKubeval.groovy index 7135a2b..51b7abf 100644 --- a/src/com/cloudogu/gitopsbuildlib/validation/HelmKubeval.groovy +++ b/src/com/cloudogu/gitopsbuildlib/validation/HelmKubeval.groovy @@ -12,16 +12,16 @@ class HelmKubeval extends Validator { void validate(String targetDirectory, Map config, Map deployments) { if (deployments.containsKey('helm')) { if (deployments.helm.repoType == 'GIT') { - script.dir("${targetDirectory}/chart") { - def git = (deployments.helm.containsKey('credentialsId')) - ? script.cesBuildLib.Git.new(script, deployments.helm.credentialsId) - : script.cesBuildLib.Git.new(script) - git url: deployments.helm.repoUrl, branch: 'main', changelog: false, poll: false - - if(deployments.helm.containsKey('version') && deployments.helm.version) { - git.checkout(deployments.helm.version) - } - } +// script.dir("${targetDirectory}/chart") { +// def git = (deployments.helm.containsKey('credentialsId')) +// ? script.cesBuildLib.Git.new(script, deployments.helm.credentialsId) +// : script.cesBuildLib.Git.new(script) +// git url: deployments.helm.repoUrl, branch: 'main', changelog: false, poll: false +// +// if(deployments.helm.containsKey('version') && deployments.helm.version) { +// git.checkout(deployments.helm.version) +// } +// } def chartPath = '' if (deployments.helm.containsKey('chartPath')) { @@ -29,11 +29,10 @@ class HelmKubeval extends Validator { } withDockerImage(config.image) { - script.sh "helm dep update ${targetDirectory}/chart/${chartPath}" - script.sh "helm kubeval ${targetDirectory}/chart/${chartPath} -v ${config.k8sSchemaVersion}" + script.sh "helm dep update ./chart/${chartPath}" + script.sh "helm kubeval ./chart/${chartPath} -v ${config.k8sSchemaVersion}" } - script.sh "rm -rf ${targetDirectory}/chart" } else if (deployments.helm.repoType == 'HELM') { withDockerImage(config.image) { script.sh "helm repo add chartRepo ${deployments.helm.repoUrl}" From 17d6aee72af72614cc7522ebed88f2937435adde Mon Sep 17 00:00:00 2001 From: Marek Markiewka Date: Thu, 22 Apr 2021 12:07:45 +0200 Subject: [PATCH 13/17] refactoring Signed-off-by: Marek Markiewka --- .../gitopsbuildlib/deployment/helm/Helm.groovy | 11 ++++++----- .../deployment/helm/helmrelease/ArgoCDRelease.groovy | 6 ++---- .../deployment/helm/repotype/GitRepo.groovy | 5 +++-- .../gitopsbuildlib/validation/HelmKubeval.groovy | 3 +-- .../gitopsbuildlib/deployment/HelmTest.groovy | 12 +++++------- .../helm/helmrelease/ArgoCDReleaseTest.groovy | 10 ++++------ .../deployment/helm/repotype/GitRepoTest.groovy | 2 +- .../gitopsbuildlib/validation/HelmKubevalTest.groovy | 5 ++--- 8 files changed, 24 insertions(+), 30 deletions(-) diff --git a/src/com/cloudogu/gitopsbuildlib/deployment/helm/Helm.groovy b/src/com/cloudogu/gitopsbuildlib/deployment/helm/Helm.groovy index ea37271..aac9e83 100644 --- a/src/com/cloudogu/gitopsbuildlib/deployment/helm/Helm.groovy +++ b/src/com/cloudogu/gitopsbuildlib/deployment/helm/Helm.groovy @@ -39,18 +39,19 @@ class Helm extends Deployment { script.writeFile file: "${stage}/${application}/mergedValues.yaml", text: chartRepo.mergeValues(helmConfig, ["${script.env.WORKSPACE}/${sourcePath}/values-${stage}.yaml", "${script.env.WORKSPACE}/${sourcePath}/values-shared.yaml"] as String[]) updateYamlValue("${stage}/${application}/mergedValues.yaml", helmConfig) - script.writeFile file: "${stage}/${application}/helmRelease.yaml", text: helmRelease.create(helmConfig, application, getNamespace(stage), "${stage}/${application}/mergedValues.yaml") + + script.writeFile file: "${stage}/${application}/applicationRelease.yaml", text: helmRelease.create(helmConfig, application, getNamespace(stage), "${stage}/${application}/mergedValues.yaml") // since the values are already inline (helmRelease.yaml) we do not need to commit them into the gitops repo script.sh "rm ${stage}/${application}/mergedValues.yaml" - // clean the gitrepo helm chart folder since the helmRelease.yaml ist now created - if (helmConfig.repoType == 'GIT') { - script.sh "rm -rf ./chart || true" - } } @Override def postValidation(String stage) { + // clean the gitrepo helm chart folder since the helmRelease.yaml ist now created + if (helmConfig.repoType == 'GIT') { + script.sh "rm -rf chart || true" + } } private void updateYamlValue(String yamlFilePath, Map helmConfig) { diff --git a/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/ArgoCDRelease.groovy b/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/ArgoCDRelease.groovy index 74ccda4..9ee96bb 100644 --- a/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/ArgoCDRelease.groovy +++ b/src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/ArgoCDRelease.groovy @@ -14,7 +14,6 @@ class ArgoCDRelease extends HelmRelease{ @Override String create(Map helmConfig, String application, String namespace, String mergedValuesFile) { -// String mergedValuesFileLocation = "${script.env.WORKSPACE}/.configRepoTempDir/${mergedValuesFile}" String helmRelease = "" if (helmConfig.repoType == 'GIT') { helmRelease = createResourcesFromGitRepo(helmConfig, application, mergedValuesFile) @@ -33,11 +32,10 @@ class ArgoCDRelease extends HelmRelease{ } dockerWrapper.withHelm { - script.sh "helm dep update ./chart/${chartPath}" - String templateScript = "helm template ${application} ./chart/${chartPath} -f ${mergedValuesFile}" + String templateScript = "helm template ${application} chart/${chartPath} -f ${mergedValuesFile}" helmRelease = script.sh returnStdout: true, script: templateScript } - // this line removes all empty lines since helm template creates some and the helm validator will throw an error if there are emtpy lines present + // this line removes all empty lines since helm template creates some and the helm kubeval validator will throw an error if there are emtpy lines present helmRelease = helmRelease.replaceAll("(?m)^[ \t]*\r?\n", "") return helmRelease } diff --git a/src/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/GitRepo.groovy b/src/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/GitRepo.groovy index 1c0b1c5..acdb073 100644 --- a/src/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/GitRepo.groovy +++ b/src/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/GitRepo.groovy @@ -18,7 +18,8 @@ class GitRepo extends RepoType { } withHelm { - String helmScript = "helm values ./chart/${chartPath} ${valuesFilesWithParameter(valuesFiles)}" + script.sh "helm dep update chart/${chartPath}" + String helmScript = "helm values chart/${chartPath} ${valuesFilesWithParameter(valuesFiles)}" merge = script.sh returnStdout: true, script: helmScript } @@ -28,7 +29,7 @@ class GitRepo extends RepoType { private getHelmChartFromGitRepo(Map helmConfig) { def git - script.dir("./chart") { + script.dir("chart") { if (helmConfig.containsKey('credentialsId')) { git = script.cesBuildLib.Git.new(script, helmConfig.credentialsId) diff --git a/src/com/cloudogu/gitopsbuildlib/validation/HelmKubeval.groovy b/src/com/cloudogu/gitopsbuildlib/validation/HelmKubeval.groovy index 51b7abf..766ed53 100644 --- a/src/com/cloudogu/gitopsbuildlib/validation/HelmKubeval.groovy +++ b/src/com/cloudogu/gitopsbuildlib/validation/HelmKubeval.groovy @@ -29,8 +29,7 @@ class HelmKubeval extends Validator { } withDockerImage(config.image) { - script.sh "helm dep update ./chart/${chartPath}" - script.sh "helm kubeval ./chart/${chartPath} -v ${config.k8sSchemaVersion}" + script.sh "helm kubeval chart/${chartPath} -v ${config.k8sSchemaVersion}" } } else if (deployments.helm.repoType == 'HELM') { diff --git a/test/com/cloudogu/gitopsbuildlib/deployment/HelmTest.groovy b/test/com/cloudogu/gitopsbuildlib/deployment/HelmTest.groovy index 855f3df..39db159 100644 --- a/test/com/cloudogu/gitopsbuildlib/deployment/HelmTest.groovy +++ b/test/com/cloudogu/gitopsbuildlib/deployment/HelmTest.groovy @@ -29,9 +29,7 @@ class HelmTest { fileConfigmaps: [ [ name : "index", - sourceFilePath : "../index.html", // relative to deployments.sourcePath - // additional feature in the future. current default folder is '../generated-resources' - // destinationFilePath: "../generated-resources/html/" // realtive to deployments.sourcePath + sourceFilePath : "../index.html", stage: ["staging"] ] ] @@ -68,10 +66,10 @@ class HelmTest { helmGit.preValidation('staging') assertThat(dockerMock.actualImages[0]).contains('ghcr.io/cloudogu/helm:') - assertThat(scriptMock.actualShArgs[0]).isEqualTo('[returnStdout:true, script:helm values workspace/chart/chartPath -f workspace/k8s/values-staging.yaml -f workspace/k8s/values-shared.yaml ]') + assertThat(scriptMock.actualShArgs[0]).isEqualTo('[returnStdout:true, script:helm values ./chart/chartPath -f workspace/k8s/values-staging.yaml -f workspace/k8s/values-shared.yaml ]') assertThat(scriptMock.actualShArgs[1]).isEqualTo('rm staging/testapp/mergedValues.yaml') - assertThat(scriptMock.actualShArgs[2]).isEqualTo('rm -rf workspace/chart || true') - assertThat(scriptMock.actualWriteFileArgs[0]).isEqualTo('[file:staging/testapp/mergedValues.yaml, text:[[returnStdout:true, script:helm values workspace/chart/chartPath -f workspace/k8s/values-staging.yaml -f workspace/k8s/values-shared.yaml ]]]') + assertThat(scriptMock.actualShArgs[2]).isEqualTo('rm -rf ./chart || true') + assertThat(scriptMock.actualWriteFileArgs[0]).isEqualTo('[file:staging/testapp/mergedValues.yaml, text:[[returnStdout:true, script:helm values ./chart/chartPath -f workspace/k8s/values-staging.yaml -f workspace/k8s/values-shared.yaml ]]]') assertThat(scriptMock.actualWriteFileArgs[1]).isEqualTo('''[file:staging/testapp/helmRelease.yaml, text:apiVersion: helm.fluxcd.io/v1 kind: HelmRelease metadata: @@ -84,7 +82,7 @@ spec: chart: git: repoUrl ref: null - path: . + path: chartPath values: --- #this part is only for PlainTest regarding updating the image name diff --git a/test/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/ArgoCDReleaseTest.groovy b/test/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/ArgoCDReleaseTest.groovy index 2b0f4b8..211b228 100644 --- a/test/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/ArgoCDReleaseTest.groovy +++ b/test/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/ArgoCDReleaseTest.groovy @@ -23,9 +23,8 @@ class ArgoCDReleaseTest { 'namespace', 'this/is/a/valusfile') - assertThat(scriptMock.actualShArgs[0]).isEqualTo('helm dep update .') - assertThat(scriptMock.actualShArgs[1]).isEqualTo('[returnStdout:true, script:helm template app . -f workspace/.configRepoTempDir/this/is/a/valusfile]') - assertThat(scriptMock.actualDir[0]).isEqualTo('workspace/chart/path') + assertThat(scriptMock.actualShArgs[0]).isEqualTo('helm dep update ./chart/path') + assertThat(scriptMock.actualShArgs[1]).isEqualTo('[returnStdout:true, script:helm template app ./chart/path -f this/is/a/valusfile]') } @Test @@ -40,8 +39,7 @@ class ArgoCDReleaseTest { 'namespace', 'this/is/a/valusfile') - assertThat(scriptMock.actualShArgs[0]).isEqualTo('helm dep update .') - assertThat(scriptMock.actualShArgs[1]).isEqualTo('[returnStdout:true, script:helm template app . -f workspace/.configRepoTempDir/this/is/a/valusfile]') - assertThat(scriptMock.actualDir[0]).isEqualTo('workspace/chart/') + assertThat(scriptMock.actualShArgs[0]).isEqualTo('helm dep update ./chart/') + assertThat(scriptMock.actualShArgs[1]).isEqualTo('[returnStdout:true, script:helm template app ./chart/ -f this/is/a/valusfile]') } } diff --git a/test/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/GitRepoTest.groovy b/test/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/GitRepoTest.groovy index 7f543c1..eac0b4c 100644 --- a/test/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/GitRepoTest.groovy +++ b/test/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/GitRepoTest.groovy @@ -21,6 +21,6 @@ class GitRepoTest { 'file2' ] as String[]) - assertThat(scriptMock.actualShArgs[0]).isEqualTo('[returnStdout:true, script:helm values workspace/chart/chartPath -f file1 -f file2 ]') + assertThat(scriptMock.actualShArgs[0]).isEqualTo('[returnStdout:true, script:helm values ./chart/chartPath -f file1 -f file2 ]') } } diff --git a/test/com/cloudogu/gitopsbuildlib/validation/HelmKubevalTest.groovy b/test/com/cloudogu/gitopsbuildlib/validation/HelmKubevalTest.groovy index 4aa71dd..611c188 100644 --- a/test/com/cloudogu/gitopsbuildlib/validation/HelmKubevalTest.groovy +++ b/test/com/cloudogu/gitopsbuildlib/validation/HelmKubevalTest.groovy @@ -30,9 +30,8 @@ class HelmKubevalTest { ] ) assertThat(dockerMock.actualImages[0]).isEqualTo('img') - assertThat(scriptMock.actualShArgs[0]).isEqualTo('helm dep update target/chart/chartPath') - assertThat(scriptMock.actualShArgs[1]).isEqualTo('helm kubeval target/chart/chartPath -v 1.5') - assertThat(scriptMock.actualShArgs[2]).isEqualTo('rm -rf target/chart') + assertThat(scriptMock.actualShArgs[0]).isEqualTo('helm dep update ./chart/chartPath') + assertThat(scriptMock.actualShArgs[1]).isEqualTo('helm kubeval ./chart/chartPath -v 1.5') } @Test From ae4da35b44ae6b3acd1e2a3e5f51296a2b9879ce Mon Sep 17 00:00:00 2001 From: Marek Markiewka Date: Thu, 22 Apr 2021 12:32:12 +0200 Subject: [PATCH 14/17] bugfixes Signed-off-by: Marek Markiewka --- .../gitopsbuildlib/deployment/helm/Helm.groovy | 2 ++ .../gitopsbuildlib/deployment/HelmTest.groovy | 12 ++++++------ .../helm/helmrelease/ArgoCDReleaseTest.groovy | 6 ++---- .../deployment/helm/repotype/GitRepoTest.groovy | 3 ++- .../gitopsbuildlib/validation/HelmKubevalTest.groovy | 3 +-- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/com/cloudogu/gitopsbuildlib/deployment/helm/Helm.groovy b/src/com/cloudogu/gitopsbuildlib/deployment/helm/Helm.groovy index aac9e83..fb52fc7 100644 --- a/src/com/cloudogu/gitopsbuildlib/deployment/helm/Helm.groovy +++ b/src/com/cloudogu/gitopsbuildlib/deployment/helm/Helm.groovy @@ -48,6 +48,8 @@ class Helm extends Deployment { @Override def postValidation(String stage) { + def helmConfig = gitopsConfig.deployments.helm + // clean the gitrepo helm chart folder since the helmRelease.yaml ist now created if (helmConfig.repoType == 'GIT') { script.sh "rm -rf chart || true" diff --git a/test/com/cloudogu/gitopsbuildlib/deployment/HelmTest.groovy b/test/com/cloudogu/gitopsbuildlib/deployment/HelmTest.groovy index 39db159..fd3f7a4 100644 --- a/test/com/cloudogu/gitopsbuildlib/deployment/HelmTest.groovy +++ b/test/com/cloudogu/gitopsbuildlib/deployment/HelmTest.groovy @@ -66,11 +66,11 @@ class HelmTest { helmGit.preValidation('staging') assertThat(dockerMock.actualImages[0]).contains('ghcr.io/cloudogu/helm:') - assertThat(scriptMock.actualShArgs[0]).isEqualTo('[returnStdout:true, script:helm values ./chart/chartPath -f workspace/k8s/values-staging.yaml -f workspace/k8s/values-shared.yaml ]') - assertThat(scriptMock.actualShArgs[1]).isEqualTo('rm staging/testapp/mergedValues.yaml') - assertThat(scriptMock.actualShArgs[2]).isEqualTo('rm -rf ./chart || true') - assertThat(scriptMock.actualWriteFileArgs[0]).isEqualTo('[file:staging/testapp/mergedValues.yaml, text:[[returnStdout:true, script:helm values ./chart/chartPath -f workspace/k8s/values-staging.yaml -f workspace/k8s/values-shared.yaml ]]]') - assertThat(scriptMock.actualWriteFileArgs[1]).isEqualTo('''[file:staging/testapp/helmRelease.yaml, text:apiVersion: helm.fluxcd.io/v1 + assertThat(scriptMock.actualShArgs[0]).isEqualTo('helm dep update chart/chartPath') + assertThat(scriptMock.actualShArgs[1]).isEqualTo('[returnStdout:true, script:helm values chart/chartPath -f workspace/k8s/values-staging.yaml -f workspace/k8s/values-shared.yaml ]') + assertThat(scriptMock.actualShArgs[2]).isEqualTo('rm staging/testapp/mergedValues.yaml') + assertThat(scriptMock.actualWriteFileArgs[0]).isEqualTo('[file:staging/testapp/mergedValues.yaml, text:[helm dep update chart/chartPath, [returnStdout:true, script:helm values chart/chartPath -f workspace/k8s/values-staging.yaml -f workspace/k8s/values-shared.yaml ]]]') + assertThat(scriptMock.actualWriteFileArgs[1]).isEqualTo('''[file:staging/testapp/applicationRelease.yaml, text:apiVersion: helm.fluxcd.io/v1 kind: HelmRelease metadata: name: testapp @@ -114,7 +114,7 @@ spec: 'text:[helm repo add chartRepo repoUrl, helm repo update, ' + 'helm pull chartRepo/chartName --version=1.0 --untar --untardir=workspace/chart, ' + '[returnStdout:true, script:helm values workspace/chart/chartName -f workspace/k8s/values-staging.yaml -f workspace/k8s/values-shared.yaml ]]]') - assertThat(scriptMock.actualWriteFileArgs[1]).isEqualTo('''[file:staging/testapp/helmRelease.yaml, text:apiVersion: helm.fluxcd.io/v1 + assertThat(scriptMock.actualWriteFileArgs[1]).isEqualTo('''[file:staging/testapp/applicationRelease.yaml, text:apiVersion: helm.fluxcd.io/v1 kind: HelmRelease metadata: name: testapp diff --git a/test/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/ArgoCDReleaseTest.groovy b/test/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/ArgoCDReleaseTest.groovy index 211b228..39607d7 100644 --- a/test/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/ArgoCDReleaseTest.groovy +++ b/test/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/ArgoCDReleaseTest.groovy @@ -23,8 +23,7 @@ class ArgoCDReleaseTest { 'namespace', 'this/is/a/valusfile') - assertThat(scriptMock.actualShArgs[0]).isEqualTo('helm dep update ./chart/path') - assertThat(scriptMock.actualShArgs[1]).isEqualTo('[returnStdout:true, script:helm template app ./chart/path -f this/is/a/valusfile]') + assertThat(scriptMock.actualShArgs[0]).isEqualTo('[returnStdout:true, script:helm template app chart/path -f this/is/a/valusfile]') } @Test @@ -39,7 +38,6 @@ class ArgoCDReleaseTest { 'namespace', 'this/is/a/valusfile') - assertThat(scriptMock.actualShArgs[0]).isEqualTo('helm dep update ./chart/') - assertThat(scriptMock.actualShArgs[1]).isEqualTo('[returnStdout:true, script:helm template app ./chart/ -f this/is/a/valusfile]') + assertThat(scriptMock.actualShArgs[0]).isEqualTo('[returnStdout:true, script:helm template app chart/ -f this/is/a/valusfile]') } } diff --git a/test/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/GitRepoTest.groovy b/test/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/GitRepoTest.groovy index eac0b4c..7e17742 100644 --- a/test/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/GitRepoTest.groovy +++ b/test/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/GitRepoTest.groovy @@ -21,6 +21,7 @@ class GitRepoTest { 'file2' ] as String[]) - assertThat(scriptMock.actualShArgs[0]).isEqualTo('[returnStdout:true, script:helm values ./chart/chartPath -f file1 -f file2 ]') + assertThat(scriptMock.actualShArgs[0]).isEqualTo('helm dep update chart/chartPath') + assertThat(scriptMock.actualShArgs[1]).isEqualTo('[returnStdout:true, script:helm values chart/chartPath -f file1 -f file2 ]') } } diff --git a/test/com/cloudogu/gitopsbuildlib/validation/HelmKubevalTest.groovy b/test/com/cloudogu/gitopsbuildlib/validation/HelmKubevalTest.groovy index 611c188..7af0860 100644 --- a/test/com/cloudogu/gitopsbuildlib/validation/HelmKubevalTest.groovy +++ b/test/com/cloudogu/gitopsbuildlib/validation/HelmKubevalTest.groovy @@ -30,8 +30,7 @@ class HelmKubevalTest { ] ) assertThat(dockerMock.actualImages[0]).isEqualTo('img') - assertThat(scriptMock.actualShArgs[0]).isEqualTo('helm dep update ./chart/chartPath') - assertThat(scriptMock.actualShArgs[1]).isEqualTo('helm kubeval ./chart/chartPath -v 1.5') + assertThat(scriptMock.actualShArgs[0]).isEqualTo('helm kubeval chart/chartPath -v 1.5') } @Test From c70b410ea99e82ec1bc4f31378686b0078095b4b Mon Sep 17 00:00:00 2001 From: Marek Markiewka Date: Thu, 22 Apr 2021 12:48:36 +0200 Subject: [PATCH 15/17] changed folder naming Signed-off-by: Marek Markiewka --- .../cloudogu/gitopsbuildlib/deployment/Deployment.groovy | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/com/cloudogu/gitopsbuildlib/deployment/Deployment.groovy b/src/com/cloudogu/gitopsbuildlib/deployment/Deployment.groovy index 8aa4a25..8d9190d 100644 --- a/src/com/cloudogu/gitopsbuildlib/deployment/Deployment.groovy +++ b/src/com/cloudogu/gitopsbuildlib/deployment/Deployment.groovy @@ -36,11 +36,11 @@ abstract class Deployment { def sourcePath = gitopsConfig.deployments.sourcePath def application = gitopsConfig.application - script.sh "mkdir -p ${stage}/${application}/${sourcePath}/" + script.sh "mkdir -p ${stage}/${application}/extraResources/" script.sh "mkdir -p ${configDir}/" // copy extra resources like sealed secrets - script.echo "Copying k8s payload from application repo to gitOps Repo: '${sourcePath}/${stage}/*' to '${stage}/${application}/${sourcePath}'" - script.sh "cp -r ${script.env.WORKSPACE}/${sourcePath}/${stage}/* ${stage}/${application}/${sourcePath}/ || true" + script.echo "Copying k8s payload from application repo to gitOps Repo: '${sourcePath}/${stage}/*' to '${stage}/${application}/extraResources/'" + script.sh "cp -r ${script.env.WORKSPACE}/${sourcePath}/${stage}/* ${stage}/${application}/extraResources/ || true" script.sh "cp ${script.env.WORKSPACE}/*.yamllint.yaml ${configDir}/ || true" } From 3bf236aa39a9325899f24a853e2c8e937502a55c Mon Sep 17 00:00:00 2001 From: Marek Markiewka Date: Thu, 22 Apr 2021 13:01:10 +0200 Subject: [PATCH 16/17] bugfixes Signed-off-by: Marek Markiewka --- src/com/cloudogu/gitopsbuildlib/validation/Kubeval.groovy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/com/cloudogu/gitopsbuildlib/validation/Kubeval.groovy b/src/com/cloudogu/gitopsbuildlib/validation/Kubeval.groovy index db57428..f8c86aa 100644 --- a/src/com/cloudogu/gitopsbuildlib/validation/Kubeval.groovy +++ b/src/com/cloudogu/gitopsbuildlib/validation/Kubeval.groovy @@ -15,7 +15,7 @@ class Kubeval extends Validator { @Override void validate(String targetDirectory, Map config, Map deployments) { withDockerImage(config.image) { - script.sh "kubeval -d ${targetDirectory}/${deployments.sourcePath} -v ${config.k8sSchemaVersion} --strict" + script.sh "kubeval -d ${targetDirectory} -v ${config.k8sSchemaVersion} --strict" } } } From 01e7a6ab13695753f2ae5c34c50f40f1abc5b682 Mon Sep 17 00:00:00 2001 From: Marek Markiewka Date: Thu, 22 Apr 2021 15:12:18 +0200 Subject: [PATCH 17/17] fixed tests Signed-off-by: Marek Markiewka --- .../gitopsbuildlib/deployment/DeploymentTest.groovy | 6 +++--- .../cloudogu/gitopsbuildlib/validation/KubevalTest.groovy | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test/com/cloudogu/gitopsbuildlib/deployment/DeploymentTest.groovy b/test/com/cloudogu/gitopsbuildlib/deployment/DeploymentTest.groovy index a6696d1..f28b974 100644 --- a/test/com/cloudogu/gitopsbuildlib/deployment/DeploymentTest.groovy +++ b/test/com/cloudogu/gitopsbuildlib/deployment/DeploymentTest.groovy @@ -56,10 +56,10 @@ class DeploymentTest { @Test void 'creating folders for plain deployment'() { deploymentUnderTest.createFoldersAndCopyK8sResources('staging',) - assertThat(scriptMock.actualEchoArgs[0]).isEqualTo('Copying k8s payload from application repo to gitOps Repo: \'k8s/staging/*\' to \'staging/app/k8s\'') - assertThat(scriptMock.actualShArgs[0]).isEqualTo('mkdir -p staging/app/k8s/') + assertThat(scriptMock.actualEchoArgs[0]).isEqualTo('Copying k8s payload from application repo to gitOps Repo: \'k8s/staging/*\' to \'staging/app/extraResources/\'') + assertThat(scriptMock.actualShArgs[0]).isEqualTo('mkdir -p staging/app/extraResources/') assertThat(scriptMock.actualShArgs[1]).isEqualTo('mkdir -p .config/') - assertThat(scriptMock.actualShArgs[2]).isEqualTo('cp -r workspace/k8s/staging/* staging/app/k8s/ || true') + assertThat(scriptMock.actualShArgs[2]).isEqualTo('cp -r workspace/k8s/staging/* staging/app/extraResources/ || true') assertThat(scriptMock.actualShArgs[3]).isEqualTo('cp workspace/*.yamllint.yaml .config/ || true') } diff --git a/test/com/cloudogu/gitopsbuildlib/validation/KubevalTest.groovy b/test/com/cloudogu/gitopsbuildlib/validation/KubevalTest.groovy index accf77d..53b5358 100644 --- a/test/com/cloudogu/gitopsbuildlib/validation/KubevalTest.groovy +++ b/test/com/cloudogu/gitopsbuildlib/validation/KubevalTest.groovy @@ -24,7 +24,7 @@ class KubevalTest { ) assertThat(dockerMock.actualImages[0]).isEqualTo('img') assertThat(scriptMock.actualShArgs[0]).isEqualTo( - 'kubeval -d target/k8s -v 1.5 --strict' + 'kubeval -d target -v 1.5 --strict' ) } }