Skip to content

Commit ec53879

Browse files
authored
Merge pull request #14 from cloudogu/feature/argocd_helm_helmrepo
Feature/argocd helm helmrepo
2 parents fc8c663 + 734fb50 commit ec53879

File tree

18 files changed

+90
-77
lines changed

18 files changed

+90
-77
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ application: 'spring-petclinic' // Name of the application. Used as a
255255
```
256256

257257
```
258-
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)
258+
gitopsTool: 'ARGO' // Name of the gitops tool. Currently supporting 'FLUX' (for now only fluxV1) and 'ARGO'
259259
```
260260

261261
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:

src/com/cloudogu/gitopsbuildlib/deployment/Deployment.groovy

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.cloudogu.gitopsbuildlib.deployment
33
abstract class Deployment {
44

55
protected static String getKubectlImage() { 'lachlanevenson/k8s-kubectl:v1.19.3' }
6+
protected String extraResourcesFolder = ""
67

78
static String getConfigDir() { '.config' }
89

@@ -25,22 +26,23 @@ abstract class Deployment {
2526
abstract preValidation(String stage)
2627
abstract postValidation(String stage)
2728

29+
2830
def validate(String stage) {
2931
gitopsConfig.validators.each { validatorConfig ->
3032
script.echo "Executing validator ${validatorConfig.key}"
31-
validatorConfig.value.validator.validate(validatorConfig.value.enabled, "${stage}/${gitopsConfig.application}", validatorConfig.value.config, gitopsConfig.deployments)
33+
validatorConfig.value.validator.validate(validatorConfig.value.enabled, "${stage}/${gitopsConfig.application}", validatorConfig.value.config, gitopsConfig)
3234
}
3335
}
3436

3537
def createFoldersAndCopyK8sResources(String stage) {
3638
def sourcePath = gitopsConfig.deployments.sourcePath
3739
def application = gitopsConfig.application
3840

39-
script.sh "mkdir -p ${stage}/${application}/extraResources/"
41+
script.sh "mkdir -p ${stage}/${application}/${extraResourcesFolder}"
4042
script.sh "mkdir -p ${configDir}/"
4143
// copy extra resources like sealed secrets
42-
script.echo "Copying k8s payload from application repo to gitOps Repo: '${sourcePath}/${stage}/*' to '${stage}/${application}/extraResources/'"
43-
script.sh "cp -r ${script.env.WORKSPACE}/${sourcePath}/${stage}/* ${stage}/${application}/extraResources/ || true"
44+
script.echo "Copying k8s payload from application repo to gitOps Repo: '${sourcePath}/${stage}/*' to '${stage}/${application}/${extraResourcesFolder}'"
45+
script.sh "cp -r ${script.env.WORKSPACE}/${sourcePath}/${stage}/* ${stage}/${application}/${extraResourcesFolder} || true"
4446
script.sh "cp ${script.env.WORKSPACE}/*.yamllint.yaml ${configDir}/ || true"
4547
}
4648

src/com/cloudogu/gitopsbuildlib/deployment/helm/Helm.groovy

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class Helm extends Deployment {
1515

1616
Helm(def script, def gitopsConfig) {
1717
super(script, gitopsConfig)
18+
this.extraResourcesFolder = "extraResources"
1819
if (gitopsConfig.deployments.helm.repoType == 'GIT') {
1920
chartRepo = new GitRepo(script)
2021
} else if (gitopsConfig.deployments.helm.repoType == 'HELM') {
@@ -49,11 +50,8 @@ class Helm extends Deployment {
4950
@Override
5051
def postValidation(String stage) {
5152
def helmConfig = gitopsConfig.deployments.helm
52-
5353
// clean the gitrepo helm chart folder since the helmRelease.yaml ist now created
54-
if (helmConfig.repoType == 'GIT') {
55-
script.sh "rm -rf chart || true"
56-
}
54+
script.sh "rm -rf chart || true"
5755
}
5856

5957
private void updateYamlValue(String yamlFilePath, Map helmConfig) {

src/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/ArgoCDRelease.groovy

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,33 @@ class ArgoCDRelease extends HelmRelease{
1818
if (helmConfig.repoType == 'GIT') {
1919
helmRelease = createResourcesFromGitRepo(helmConfig, application, mergedValuesFile)
2020
} else if (helmConfig.repoType == 'HELM') {
21-
// TODO not yet implemented
21+
helmRelease = createResourcesFromHelmRepo(helmConfig, application, mergedValuesFile)
2222
}
2323
return helmRelease
2424
}
2525

2626
private String createResourcesFromGitRepo(Map helmConfig, String application, String mergedValuesFile) {
27-
String helmRelease = ""
2827

2928
def chartPath = ''
3029
if (helmConfig.containsKey('chartPath')) {
3130
chartPath = helmConfig.chartPath
3231
}
3332

33+
return createHelmRelease(chartPath as String, application, mergedValuesFile)
34+
}
35+
36+
private String createResourcesFromHelmRepo(Map helmConfig, String application, String mergedValuesFile) {
37+
return createHelmRelease(helmConfig.chartName as String, application, mergedValuesFile)
38+
}
39+
40+
private String createHelmRelease(String chartPath, String application, String mergedValuesFile) {
41+
String helmRelease = ""
3442
dockerWrapper.withHelm {
3543
String templateScript = "helm template ${application} chart/${chartPath} -f ${mergedValuesFile}"
3644
helmRelease = script.sh returnStdout: true, script: templateScript
3745
}
38-
// 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
46+
47+
// 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
3948
helmRelease = helmRelease.replaceAll("(?m)^[ \t]*\r?\n", "")
4049
return helmRelease
4150
}

src/com/cloudogu/gitopsbuildlib/deployment/helm/repotype/HelmRepo.groovy

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,34 @@ class HelmRepo extends RepoType{
88

99
@Override
1010
String mergeValues(Map helmConfig, String[] valuesFiles) {
11+
12+
if (helmConfig.containsKey('credentialsId') && helmConfig.credentialsId) {
13+
script.withCredentials([
14+
script.usernamePassword(
15+
credentialsId: helmConfig.credentialsId,
16+
usernameVariable: 'USERNAME',
17+
passwordVariable: 'PASSWORD')
18+
]) {
19+
String credentialArgs = " --username ${script.USERNAME} --password ${script.PASSWORD}"
20+
return mergeValuesFiles(helmConfig, valuesFiles, credentialArgs)
21+
}
22+
} else {
23+
return mergeValuesFiles(helmConfig, valuesFiles)
24+
}
25+
}
26+
27+
private String mergeValuesFiles(Map helmConfig, String[] valuesFiles, String credentialArgs = "") {
1128
String merge = ""
1229

1330
withHelm {
14-
script.sh "helm repo add chartRepo ${helmConfig.repoUrl}"
31+
script.sh "helm repo add chartRepo ${helmConfig.repoUrl}${credentialArgs}"
1532
script.sh "helm repo update"
16-
script.sh "helm pull chartRepo/${helmConfig.chartName} --version=${helmConfig.version} --untar --untardir=${script.env.WORKSPACE}/chart"
17-
String helmScript = "helm values ${script.env.WORKSPACE}/chart/${helmConfig.chartName} ${valuesFilesWithParameter(valuesFiles)}"
33+
// helm pull also executes helm dependency so we don't need to do it in this step
34+
script.sh "helm pull chartRepo/${helmConfig.chartName} --version=${helmConfig.version} --untar --untardir=chart"
35+
String helmScript = "helm values chart/${helmConfig.chartName} ${valuesFilesWithParameter(valuesFiles)}"
1836
merge = script.sh returnStdout: true, script: helmScript
1937
}
2038

21-
script.sh "rm -rf ${script.env.WORKSPACE}/chart || true"
22-
2339
return merge
2440
}
2541
}

src/com/cloudogu/gitopsbuildlib/deployment/plain/Plain.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Plain extends Deployment{
1919

2020
private updateImage(String stage) {
2121
gitopsConfig.deployments.plain.updateImages.each {
22-
def deploymentFilePath = "${stage}/${gitopsConfig.application}/${gitopsConfig.deployments.sourcePath}/${it['filename']}"
22+
def deploymentFilePath = "${stage}/${gitopsConfig.application}/${it['filename']}"
2323
def data = script.readYaml file: deploymentFilePath
2424
def containers = data.spec.template.spec.containers
2525
def containerName = it['containerName']

src/com/cloudogu/gitopsbuildlib/validation/HelmKubeval.groovy

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.cloudogu.gitopsbuildlib.validation
22

3-
import com.cloudogu.gitopsbuildlib.docker.DockerWrapper
4-
53
class HelmKubeval extends Validator {
64

75
HelmKubeval(def script) {
@@ -11,33 +9,16 @@ class HelmKubeval extends Validator {
119
@Override
1210
void validate(String targetDirectory, Map config, Map deployments) {
1311
if (deployments.containsKey('helm')) {
14-
if (deployments.helm.repoType == 'GIT') {
15-
// script.dir("${targetDirectory}/chart") {
16-
// def git = (deployments.helm.containsKey('credentialsId'))
17-
// ? script.cesBuildLib.Git.new(script, deployments.helm.credentialsId)
18-
// : script.cesBuildLib.Git.new(script)
19-
// git url: deployments.helm.repoUrl, branch: 'main', changelog: false, poll: false
20-
//
21-
// if(deployments.helm.containsKey('version') && deployments.helm.version) {
22-
// git.checkout(deployments.helm.version)
23-
// }
24-
// }
25-
26-
def chartPath = ''
27-
if (deployments.helm.containsKey('chartPath')) {
28-
chartPath = deployments.helm.chartPath
29-
}
3012

31-
withDockerImage(config.image) {
32-
script.sh "helm kubeval chart/${chartPath} -v ${config.k8sSchemaVersion}"
33-
}
13+
def chartDir = ''
14+
if (deployments.helm.containsKey('chartPath')) {
15+
chartDir = deployments.helm.chartPath
16+
} else if ( deployments.helm.containsKey('chartName')) {
17+
chartDir = deployments.helm.chartName
18+
}
3419

35-
} else if (deployments.helm.repoType == 'HELM') {
36-
withDockerImage(config.image) {
37-
script.sh "helm repo add chartRepo ${deployments.helm.repoUrl}"
38-
script.sh "helm repo update"
39-
script.sh "helm kubeval chartRepo/${deployments.helm.chartName} --version=${deployments.helm.version} -v ${config.k8sSchemaVersion}"
40-
}
20+
withDockerImage(config.image) {
21+
script.sh "helm kubeval chart/${chartDir} -v ${config.k8sSchemaVersion}"
4122
}
4223
}
4324
}
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
package com.cloudogu.gitopsbuildlib.validation
22

3-
import com.cloudogu.gitopsbuildlib.docker.DockerWrapper
4-
53
/**
64
* Validates all yaml-resources within the target-directory against the specs of the given k8s version
75
*/
86
class Kubeval extends Validator {
97

10-
118
Kubeval(def script) {
129
super(script)
1310
}
1411

1512
@Override
1613
void validate(String targetDirectory, Map config, Map deployments) {
1714
withDockerImage(config.image) {
18-
script.sh "kubeval -d ${targetDirectory} -v ${config.k8sSchemaVersion} --strict"
15+
script.sh "kubeval -d ${targetDirectory} -v ${config.k8sSchemaVersion} --strict --ignore-missing-schemas"
1916
}
2017
}
2118
}

src/com/cloudogu/gitopsbuildlib/validation/Validator.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ abstract class Validator {
1212
dockerWrapper = new DockerWrapper(script)
1313
}
1414

15-
void validate(boolean enabled, String targetDirectory, Map config, Map deployments) {
15+
void validate(boolean enabled, String targetDirectory, Map config, Map gitopsConfig) {
1616
if (enabled) {
17-
validate(targetDirectory, config, deployments)
17+
validate(targetDirectory, config, gitopsConfig)
1818
} else {
1919
script.echo "Skipping validator ${this.getClass().getSimpleName()} because it is configured as enabled=false"
2020
}

src/com/cloudogu/gitopsbuildlib/validation/Yamllint.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Yamllint extends Validator {
1616
}
1717

1818
@Override
19-
void validate(String targetDirectory, Map config, Map deployments) {
19+
void validate(String targetDirectory, Map config, Map gitopsConfig) {
2020
withDockerImage(config.image) {
2121
script.sh "yamllint " +
2222
"${config.profile ? "-d ${config.profile} " : ''}" +

0 commit comments

Comments
 (0)