Skip to content

Commit a444648

Browse files
authored
Merge pull request #9 from cloudogu/feature/set_default_stages
Feature/set default stages
2 parents 32f3e2a + e39b5bf commit a444648

File tree

4 files changed

+61
-31
lines changed

4 files changed

+61
-31
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,8 @@ mainBranch: 'main'
270270

271271
## Stages
272272
The GitOps-build-lib supports builds on multiple stages. A stage is defined by a name and contains a namespace (used to
273-
generate the resources) and a deployment-flag.
273+
generate the resources) and a deployment-flag. If no stages is passed into the gitops-config by the user, the default
274+
is set to:
274275

275276
```groovy
276277
def gitopsConfig = [
@@ -287,8 +288,9 @@ def gitopsConfig = [
287288
]
288289
```
289290

291+
The defaults above can be overwritten by providing an entry for 'stages' within your config.
290292
If it is set to deploy directly it will commit and push to your desired `gitops-folder` and therefore triggers a deployment. If it is set to false
291-
it will create a PR on your `gitops-folder`. **Remember** there are [ipmortant](#important-note) conventions regarding namespaces and the folder structure.
293+
it will create a PR on your `gitops-folder`. **Remember** there are [important](#important-note) conventions regarding namespaces and the folder structure.
292294

293295
### Namespaces
294296

test/com/cloudogu/gitopsbuildlib/DeployViaGitopsTest.groovy

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,17 @@ class DeployViaGitopsTest extends BasePipelineTest {
4242

4343
Map gitopsConfig(Map stages, Map deployments) {
4444
return [
45-
scmmCredentialsId : 'scmManagerCredentials',
46-
scmmConfigRepoUrl : 'configRepositoryUrl',
47-
scmmPullRequestBaseUrl: 'http://scmm-scm-manager/scm',
48-
scmmPullRequestRepo : 'fluxv1/gitops',
49-
cesBuildLibRepo : 'cesBuildLibRepo',
50-
cesBuildLibVersion : 'cesBuildLibVersion',
45+
scmmCredentialsId : 'scmManagerCredentials',
46+
scmmConfigRepoUrl : 'configRepositoryUrl',
47+
scmmPullRequestBaseUrl : 'http://scmm-scm-manager/scm',
48+
scmmPullRequestRepo : 'fluxv1/gitops',
49+
cesBuildLibRepo : 'cesBuildLibRepo',
50+
cesBuildLibVersion : 'cesBuildLibVersion',
5151
cesBuildLibCredentialsId: 'cesBuildLibCredentialsId',
52-
application : 'application',
53-
mainBranch : 'main',
54-
deployments : deployments,
55-
validators : [
52+
application : 'application',
53+
mainBranch : 'main',
54+
deployments : deployments,
55+
validators : [
5656
kubeval : [
5757
validator: new Kubeval(deployViaGitops),
5858
enabled : true,
@@ -73,14 +73,14 @@ class DeployViaGitopsTest extends BasePipelineTest {
7373
]
7474
]
7575
],
76-
stages : stages
76+
stages : stages
7777
]
7878
}
7979

8080
def plainDeployment = [
8181
sourcePath: 'k8s',
82-
plain : [
83-
updateImages : [
82+
plain : [
83+
updateImages: [
8484
[deploymentFilename: "deployment.yaml",
8585
containerName : 'application',
8686
imageName : 'newImageName']
@@ -95,7 +95,7 @@ class DeployViaGitopsTest extends BasePipelineTest {
9595
def multipleStages = [
9696
staging : [deployDirectly: true],
9797
production: [deployDirectly: false],
98-
qa : []
98+
qa : [deployDirectly: false]
9999
]
100100

101101
@BeforeAll
@@ -211,6 +211,29 @@ spec:
211211
deployViaGitops([cesBuildLibRepo: 'abc', cesBuildLibCredentialsId: 'testuser'])
212212
}
213213

214+
@Test
215+
void 'default stages defined as staging and production'() {
216+
deployViaGitops.metaClass.deploy = { Map actualGitOpsConfig ->
217+
assertThat(actualGitOpsConfig.stages.containsKey('staging')).isEqualTo(true)
218+
assertThat(actualGitOpsConfig.stages.containsKey('production')).isEqualTo(true)
219+
assertThat(actualGitOpsConfig.stages.staging.deployDirectly).isEqualTo(true)
220+
assertThat(actualGitOpsConfig.stages.production.deployDirectly).isEqualTo(false)
221+
}
222+
223+
deployViaGitops([:])
224+
}
225+
226+
@Test
227+
void 'stages definition gets overwritten rather than merged'() {
228+
deployViaGitops.metaClass.deploy = { Map actualGitOpsConfig ->
229+
assertThat(actualGitOpsConfig.stages.containsKey('staging')).isEqualTo(true)
230+
assertThat(actualGitOpsConfig.stages.containsKey('production')).isEqualTo(false)
231+
assertThat(actualGitOpsConfig.stages.staging.deployDirectly).isEqualTo(true)
232+
}
233+
234+
deployViaGitops(gitopsConfig(singleStages, plainDeployment))
235+
}
236+
214237
@Test
215238
void 'default validator can be disabled'() {
216239

@@ -327,9 +350,7 @@ spec:
327350

328351
ArgumentCaptor<String> argumentCaptor2 = ArgumentCaptor.forClass(String.class)
329352
verify(git).commit(argumentCaptor2.capture(), eq('staging'), anyString())
330-
println("before argument get value")
331353
assertThat(argumentCaptor2.getValue()).isEqualTo('[staging] #0001 backend/k8s-gitops@1234abcd')
332-
println("after argument get value")
333354

334355
argumentCaptor2 = ArgumentCaptor.forClass(String.class)
335356
verify(git).commit(argumentCaptor2.capture(), eq('production'), anyString())

test/com/cloudogu/gitopsbuildlib/validation/HelmKubevalTest.groovy

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,8 @@ class HelmKubevalTest {
3131
]
3232
)
3333
assertThat(dockerMock.actualImages[0]).isEqualTo('img')
34-
//assertThat(scriptMock.actualShArgs[0]).isEqualTo('git clone chartRepo/namespace/repoPath target/chart || true')
35-
//assertThat(scriptMock.actualShArgs[1]).isEqualTo('git --git-dir=target/chart/.git --work-tree=target/chart checkout version')
36-
//assertThat(scriptMock.actualShArgs[1]).isEqualTo('helm kubeval target/chart/chartPath -v 1.5')
37-
//assertThat(scriptMock.actualShArgs[2]).isEqualTo('rm -rf target/chart')
34+
assertThat(scriptMock.actualShArgs[0]).isEqualTo('helm kubeval target/chart/chartPath -v 1.5')
35+
assertThat(scriptMock.actualShArgs[1]).isEqualTo('rm -rf target/chart')
3836
}
3937

4038
@Test

vars/deployViaGitops.groovy

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ List getMandatoryFields() {
1818
Map getDefaultConfig() {
1919

2020
return [
21-
cesBuildLibRepo : 'https://github.com/cloudogu/ces-build-lib',
22-
cesBuildLibVersion: '1.45.0',
21+
cesBuildLibRepo : 'https://github.com/cloudogu/ces-build-lib',
22+
cesBuildLibVersion : '1.45.0',
2323
cesBuildLibCredentialsId: '',
24-
mainBranch : 'main',
25-
deployments : [
24+
mainBranch : 'main',
25+
deployments : [
2626
sourcePath: 'k8s',
2727
],
28-
validators : [
29-
kubeval : [
28+
validators : [
29+
kubeval : [
3030
validator: new Kubeval(this),
3131
enabled : true,
3232
config : [
@@ -44,7 +44,7 @@ Map getDefaultConfig() {
4444
k8sSchemaVersion: '1.18.1'
4545
]
4646
],
47-
yamllint: [
47+
yamllint : [
4848
validator: new Yamllint(this),
4949
enabled : true,
5050
config : [
@@ -54,6 +54,10 @@ Map getDefaultConfig() {
5454
profile: 'relaxed'
5555
]
5656
]
57+
],
58+
stages : [
59+
staging : [deployDirectly: true],
60+
production: [deployDirectly: false],
5761
]
5862
]
5963
}
@@ -69,7 +73,12 @@ void call(Map gitopsConfig) {
6973
def mergeMaps(Map a, Map b) {
7074
return b.inject(a.clone()) { map, entry ->
7175
if (map[entry.key] instanceof Map && entry.value instanceof Map) {
72-
map[entry.key] = mergeMaps(map[entry.key], entry.value)
76+
77+
// due to the stages being the definition of the environment its not a merge but overwriting
78+
if (entry.key == 'stages')
79+
map[entry.key] = entry.value
80+
else
81+
map[entry.key] = mergeMaps(map[entry.key], entry.value)
7382
} else {
7483
map[entry.key] = entry.value
7584
}
@@ -117,7 +126,7 @@ def validateDeploymentConfig(Map gitopsConfig) {
117126

118127
protected initCesBuildLib(cesBuildLibRepo, cesBuildLibVersion, credentialsId) {
119128
Map retrieverParams = [$class: 'GitSCMSource', remote: cesBuildLibRepo]
120-
if(credentialsId?.trim()) {
129+
if (credentialsId?.trim()) {
121130
retrieverParams << [credentialsId: credentialsId]
122131
}
123132

0 commit comments

Comments
 (0)