Skip to content

Commit 91c28fa

Browse files
authored
Merge pull request #28 from cloudogu/feature/global_k8s_version
Feature/global k8s version
2 parents 3f47bf9 + 7215eae commit 91c28fa

7 files changed

Lines changed: 54 additions & 16 deletions

File tree

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
- Specify `--kube-version ` for `helm template` and kubectl image through the `k8sVersion` parameter.
12+
⚠️ `k8sVersion` has a default value, so this is no breaking change per se. However, depending on the helm chart used,
13+
the rendered result might look different from before where no `--kube-version` parameter was used.
14+
We recommend setting the `k8sVersion`. Double-check the commits in your GitOps repo.
15+
16+
### Changed
17+
- Changed kubectl image from `lachlanevenson/k8s-kubectl` to `bitnami/kubectl`, because it is available for every k8s version out there
18+
19+
### Fixed
20+
- Don't fail when no `values-shared.yaml` is provided
21+
1022
## [0.2.0](https://github.com/cloudogu/gitops-build-lib/releases/tag/0.2.0) - 2023-03-10
1123

1224
### Added

README.md

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ gitops-folder in git. For production it will open a PR with the changes.
130130

131131
```groovy
132132
def gitopsConfig = [
133+
k8sVersion: '1.24.8', /* Default: '1.24.8' */
133134
scm: [
134135
provider: 'SCMManager',
135136
credentialsId: 'scmm-user',
@@ -337,6 +338,13 @@ First of all there are some mandatory properties e.g. the information about your
337338
* `cesBuildLibRepo: 'https://github.com/cloudogu/ces-build-lib'`
338339
* `cesBuildLibVersion: '1.62.0'`
339340
* `mainBranch: 'main'`
341+
* Optional: `k8sVersion: '1.24.8'` Is used for target k8s version in helm template in Argo CD deployments with helm. Is also used to determine the kubectl version, when no specific buildImage is specified.
342+
It is recommended to use a Jenkins environment variable to specify the version, so that you don't have to bump every pipeline after a k8s version upgrade in your cluster, e.g.
343+
```
344+
def gitopsConfig = [
345+
k8sVersion: env.K8S_VERSION_TEAM_A
346+
]
347+
```
340348
341349
---
342350
@@ -348,12 +356,13 @@ All of these have set default images, but you can change them if you wish to.
348356
def gitopsConfig = [
349357
buildImages: [
350358
// These are used to run helm and kubectl commands in the core logic
351-
helm: 'ghcr.io/cloudogu/helm:3.5.4-1',
352-
kubectl: 'lachlanevenson/k8s-kubectl:v1.19.3',
359+
helm: 'ghcr.io/cloudogu/helm:3.11.1-2',
360+
// if you specify k8sVersion parameter, then by default bitnami/kubectl:${k8sVersion} will be used
361+
kubectl: 'bitnami/kubectl:1.24.8',
353362
// These are used for each specific validator via an imageRef property inside the validators config. See [Validators] for examples.
354-
kubeval: 'ghcr.io/cloudogu/helm:3.5.4-1',
355-
helmKubeval: 'ghcr.io/cloudogu/helm:3.5.4-1',
356-
yamllint: 'cytopia/yamllint:1.25-0.7'
363+
kubeval: 'ghcr.io/cloudogu/helm:3.11.1-2',
364+
helmKubeval: 'ghcr.io/cloudogu/helm:3.11.1-2',
365+
yamllint: 'cytopia/yamllint:1.25-0.9'
357366
]
358367
]
359368
```
@@ -613,6 +622,7 @@ We decided to generate plain k8s Resources from Helm applications before we push
613622

614623
- With ArgoCD you can only set one source for your application. In case of helm it is common to have a source Repository for your chart and a scource Repository for your configuration files (values.yaml). In order to use two different sources for your helm application you will need some sort of workaround (e.g. Helm dependencies in `Chart.yaml`).
615624
- ArgoCD itself uses `helm template` to apply plain k8s Resources to the cluster. By templating the helm application before pushing to the gitops repository, we have the same resources in our repository as in our cluster. Which leads to increased transparency.
625+
The parameter `k8sVersion` from the gitops config is used as a parameter with `--kube-version` in order to template version-specific manifests such as changed api versions.
616626

617627
---
618628

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,13 @@ class Helm extends Deployment {
4242
// writing the merged-values.yaml via writeYaml into a file has the advantage, that it gets formatted as valid yaml
4343
// This makes it easier to read in and indent for the inline use in the helmRelease.
4444
// It enables us to reuse the `fileToInlineYaml` function, without writing a complex formatting logic.
45-
script.writeFile file: "${script.env.WORKSPACE}/${helmChartTempDir}/mergedValues.yaml", text: mergeValuesFiles(gitopsConfig, ["${script.env.WORKSPACE}/${sourcePath}/values-${stage}.yaml", "${script.env.WORKSPACE}/${sourcePath}/values-shared.yaml"] as String[])
45+
46+
def valueFiles = ["${script.env.WORKSPACE}/${sourcePath}/values-${stage}.yaml"]
47+
// only add values-shared.yaml, if it exists
48+
if (script.fileExists("${script.env.WORKSPACE}/${sourcePath}/values-shared.yaml")) {
49+
valueFiles.add("${script.env.WORKSPACE}/${sourcePath}/values-shared.yaml")
50+
}
51+
script.writeFile file: "${script.env.WORKSPACE}/${helmChartTempDir}/mergedValues.yaml", text: mergeValuesFiles(gitopsConfig, valueFiles as String[])
4652

4753
updateYamlValue("${script.env.WORKSPACE}/${helmChartTempDir}/mergedValues.yaml", gitopsConfig)
4854

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,17 @@ class ArgoCDRelease extends HelmRelease {
3333
chartPath = helmConfig.chartPath
3434
}
3535

36-
return createHelmRelease(chartPath as String, application, namespace, gitopsConfig.buildImages.helm, mergedValuesFile)
36+
return createHelmRelease(gitopsConfig, chartPath as String, application, namespace, gitopsConfig.buildImages.helm, mergedValuesFile)
3737
}
3838

3939
private String createResourcesFromHelmRepo(Map gitopsConfig, String application, String namespace, String mergedValuesFile) {
40-
return createHelmRelease(gitopsConfig.deployments.helm.chartName, application, namespace, gitopsConfig.buildImages.helm, mergedValuesFile)
40+
return createHelmRelease(gitopsConfig, gitopsConfig.deployments.helm.chartName, application, namespace, gitopsConfig.buildImages.helm, mergedValuesFile)
4141
}
4242

43-
private String createHelmRelease(def chartPath, String application, String namespace, def helmImageConfig, String mergedValuesFile) {
43+
private String createHelmRelease(Map gitopsConfig, def chartPath, String application, String namespace, def helmImageConfig, String mergedValuesFile) {
4444
String helmRelease = ""
4545
dockerWrapper.withDockerImage(helmImageConfig) {
46-
String templateScript = "helm template ${application} ${script.env.WORKSPACE}/.helmChartTempDir/chart/${chartPath} -n ${namespace} -f ${mergedValuesFile}"
46+
String templateScript = "helm template ${application} ${script.env.WORKSPACE}/.helmChartTempDir/chart/${chartPath} -n ${namespace} --kube-version ${gitopsConfig.k8sVersion} -f ${mergedValuesFile}"
4747
helmRelease = script.sh returnStdout: true, script: templateScript
4848
}
4949

test/com/cloudogu/gitopsbuildlib/ScriptMock.groovy

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ to:
3232
List<String> actualWriteYamlArgs = new LinkedList<>()
3333
List<String> actualReadFileArgs = new LinkedList<>()
3434
List<String> actualWriteFileArgs = new LinkedList<>()
35+
List<String> actualFileExistsArgs = new LinkedList<>()
3536

3637
def mock =
3738
[
@@ -52,6 +53,7 @@ to:
5253
writeYaml: { args -> actualWriteYamlArgs += args.toString() },
5354
readFile : { args -> actualReadFileArgs += args.toString(); return configYaml},
5455
writeFile: { args -> actualWriteFileArgs += args.toString() },
56+
fileExists: { args -> actualFileExistsArgs += args.toString(); return true },
5557
env : [
5658
WORKSPACE: 'workspace'
5759
],

test/com/cloudogu/gitopsbuildlib/deployment/helm/helmrelease/ArgoCDReleaseTest.groovy

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class ArgoCDReleaseTest {
1313
@Test
1414
void 'correct helm release with git repo and chartPath'() {
1515
argoCdReleaseTest.create([
16+
k8sVersion: '1.24.8',
1617
application: 'app',
1718
deployments: [
1819
helm: [
@@ -33,12 +34,13 @@ class ArgoCDReleaseTest {
3334
'this/is/a/valuesfile')
3435

3536
assertThat(scriptMock.dockerMock.actualImages[0]).isEqualTo('helmImg')
36-
assertThat(scriptMock.actualShArgs[0]).isEqualTo('[returnStdout:true, script:helm template app workspace/.helmChartTempDir/chart/path -n namespace -f this/is/a/valuesfile]')
37+
assertThat(scriptMock.actualShArgs[0]).isEqualTo('[returnStdout:true, script:helm template app workspace/.helmChartTempDir/chart/path -n namespace --kube-version 1.24.8 -f this/is/a/valuesfile]')
3738
}
3839

3940
@Test
4041
void 'correct helm release with git repo without chartPath'() {
4142
argoCdReleaseTest.create([
43+
k8sVersion: '1.24.8',
4244
application: 'app',
4345
deployments: [
4446
helm: [
@@ -58,12 +60,13 @@ class ArgoCDReleaseTest {
5860
'this/is/a/valuesfile')
5961

6062
assertThat(scriptMock.dockerMock.actualImages[0]).isEqualTo('helmImg')
61-
assertThat(scriptMock.actualShArgs[0]).isEqualTo('[returnStdout:true, script:helm template app workspace/.helmChartTempDir/chart/ -n namespace -f this/is/a/valuesfile]')
63+
assertThat(scriptMock.actualShArgs[0]).isEqualTo('[returnStdout:true, script:helm template app workspace/.helmChartTempDir/chart/ -n namespace --kube-version 1.24.8 -f this/is/a/valuesfile]')
6264
}
6365

6466
@Test
6567
void 'correct helm release with helm repo'() {
6668
argoCdReleaseTest.create([
69+
k8sVersion: '1.24.8',
6770
application: 'app',
6871
deployments: [
6972
helm: [
@@ -83,6 +86,6 @@ class ArgoCDReleaseTest {
8386
'this/is/a/valuesfile')
8487

8588
assertThat(scriptMock.dockerMock.actualImages[0]).isEqualTo('helmImg')
86-
assertThat(scriptMock.actualShArgs[0]).isEqualTo('[returnStdout:true, script:helm template app workspace/.helmChartTempDir/chart/chartName -n namespace -f this/is/a/valuesfile]')
89+
assertThat(scriptMock.actualShArgs[0]).isEqualTo('[returnStdout:true, script:helm template app workspace/.helmChartTempDir/chart/chartName -n namespace --kube-version 1.24.8 -f this/is/a/valuesfile]')
8790
}
8891
}

vars/deployViaGitops.groovy

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,14 @@ List getMandatoryFields() {
1818
]
1919
}
2020

21-
Map createDefaultConfig() {
21+
Map createDefaultConfig(String k8sVersion) {
22+
23+
if (k8sVersion == null || k8sVersion == ""){
24+
k8sVersion = "1.24.8"
25+
}
2226

2327
return [
28+
k8sVersion : "${k8sVersion}",
2429
cesBuildLibRepo : 'https://github.com/cloudogu/ces-build-lib',
2530
cesBuildLibVersion : '1.62.0',
2631
cesBuildLibCredentialsId: '',
@@ -32,7 +37,7 @@ Map createDefaultConfig() {
3237
],
3338
kubectl: [
3439
credentialsId: '',
35-
image: 'lachlanevenson/k8s-kubectl:v1.24.8'
40+
image: "bitnami/kubectl:${k8sVersion}"
3641
],
3742
// We use the helm image (that also contains kubeval plugin) to speed up builds by allowing to reuse image
3843
kubeval: [
@@ -91,7 +96,7 @@ Map createDefaultConfig() {
9196

9297
void call(Map gitopsConfig) {
9398
// Merge default config with the one passed as parameter
94-
gitopsConfig = mergeMaps(createDefaultConfig(), gitopsConfig)
99+
gitopsConfig = mergeMaps(createDefaultConfig(gitopsConfig.k8sVersion as String), gitopsConfig)
95100
if (validateConfig(gitopsConfig)) {
96101
cesBuildLib = initCesBuildLib(gitopsConfig.cesBuildLibRepo, gitopsConfig.cesBuildLibVersion, gitopsConfig.cesBuildLibCredentialsId)
97102
deploy(gitopsConfig)

0 commit comments

Comments
 (0)