Skip to content

Commit 60da2d4

Browse files
authored
Adds the possibility to deepmerge null values if necessary (#254)
* feat: allowing to merge null values into maps via deepMerge bug: fixing scm path
1 parent 5b216bf commit 60da2d4

File tree

8 files changed

+39
-20
lines changed

8 files changed

+39
-20
lines changed

src/main/groovy/com/cloudogu/gitops/features/CertManager.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ class CertManager extends Feature implements FeatureWithImage {
102102
if (config.scmm.internal) {
103103
new URI('http://scmm-scm-manager.default.svc.cluster.local/scm')
104104
} else {
105-
new URI("${config.scmm.url}/scm")
105+
new URI("${config.scmm.url}")
106106
}
107107
}
108108

src/main/groovy/com/cloudogu/gitops/features/ExternalSecretsOperator.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class ExternalSecretsOperator extends Feature implements FeatureWithImage {
9999
if (config.scmm.internal) {
100100
new URI('http://scmm-scm-manager.default.svc.cluster.local/scm')
101101
} else {
102-
new URI("${config.scmm.url}/scm")
102+
new URI("${config.scmm.url}")
103103
}
104104
}
105105

src/main/groovy/com/cloudogu/gitops/features/IngressNginx.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class IngressNginx extends Feature implements FeatureWithImage {
9797
if (config.scmm.internal) {
9898
new URI('http://scmm-scm-manager.default.svc.cluster.local/scm')
9999
} else {
100-
new URI("${config.scmm.url}/scm")
100+
new URI("${config.scmm.url}")
101101
}
102102
}
103103
}

src/main/groovy/com/cloudogu/gitops/features/Mailhog.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class Mailhog extends Feature implements FeatureWithImage {
115115
if (config.scmm.internal) {
116116
new URI('http://scmm-scm-manager.default.svc.cluster.local/scm')
117117
} else {
118-
new URI("${config.scmm.url}/scm")
118+
new URI("${config.scmm.url}")
119119
}
120120
}
121121
}

src/main/groovy/com/cloudogu/gitops/features/PrometheusStack.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ class PrometheusStack extends Feature implements FeatureWithImage {
187187
if (config.scmm.internal) {
188188
new URI('http://scmm-scm-manager.default.svc.cluster.local/scm')
189189
} else {
190-
new URI("${config.scmm.url}/scm")
190+
new URI("${config.scmm.url}")
191191
}
192192
}
193193

src/main/groovy/com/cloudogu/gitops/features/Vault.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ class Vault extends Feature implements FeatureWithImage {
160160
if (config.scmm.internal) {
161161
new URI('http://scmm-scm-manager.default.svc.cluster.local/scm')
162162
} else {
163-
new URI("${config.scmm.url}/scm")
163+
new URI("${config.scmm.url}")
164164
}
165165
}
166166
}
Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,32 @@
11
package com.cloudogu.gitops.utils
22

33
class MapUtils {
4-
4+
55
static Map deepMerge(Map src, Map target) {
6-
src.forEach(
7-
(key, value) -> { if (value != null) target.merge(key, value, (oldVal, newVal) -> {
8-
if (oldVal instanceof Map) {
9-
if (!newVal instanceof Map) {
10-
throw new RuntimeException("Can't merge config, different types, map vs other: Map ${oldVal}; Other ${newVal}")
11-
}
12-
return deepMerge(newVal as Map, oldVal)
13-
} else {
6+
src.each { key, value ->
7+
if (value == null) {
8+
// If the value in src is null, set the key in target to null
9+
target[key] = null
10+
} else {
11+
target.merge(key, value) { oldVal, newVal ->
12+
// Case 1: If both values are Maps, perform a deep merge
13+
if (oldVal instanceof Map && newVal instanceof Map) {
14+
return deepMerge(newVal, oldVal) // Recursively merge the maps
15+
}
16+
// Case 2: If newVal is null, set the key to null in target
17+
if (newVal == null) {
18+
return null
19+
}
20+
// Case 3: If oldVal is null, use newVal
21+
if (oldVal == null) {
1422
return newVal
1523
}
16-
})
17-
})
24+
// Case 4: Default, return the new value (newVal takes priority)
25+
return newVal
26+
}
27+
}
28+
}
1829
return target
1930
}
31+
2032
}

src/test/groovy/com/cloudogu/gitops/features/PrometheusStackTest.groovy

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import com.cloudogu.gitops.utils.*
77
import groovy.yaml.YamlSlurper
88
import org.junit.jupiter.api.Test
99
import org.mockito.ArgumentCaptor
10+
1011
import java.nio.file.Files
1112
import java.nio.file.Path
1213

@@ -294,7 +295,7 @@ policies:
294295

295296
def additionalScrapeConfigs = parseActualYaml()['prometheus']['prometheusSpec']['additionalScrapeConfigs'] as List
296297
assertThat(((additionalScrapeConfigs[0]['static_configs'] as List)[0]['targets'] as List)[0]).isEqualTo('localhost:9091')
297-
assertThat(additionalScrapeConfigs[0]['metrics_path']).isEqualTo('/prefix/scm/api/v2/metrics/prometheus')
298+
assertThat(additionalScrapeConfigs[0]['metrics_path']).isEqualTo('/prefix/api/v2/metrics/prometheus')
298299
assertThat(additionalScrapeConfigs[0]['scheme']).isEqualTo('https')
299300

300301
// scrape config for jenkins is unchanged
@@ -544,9 +545,14 @@ policies:
544545
@Test
545546
void 'Merges additional helm values merged with default values'() {
546547
config.features.monitoring.helm.values = [
547-
key: [
548+
key : [
548549
some: 'thing',
549550
one : 1
551+
],
552+
prometheus: [
553+
prometheusSpec: [
554+
scrapeConfigSelectorNilUsesHelmValues: null
555+
]
550556
]
551557
]
552558

@@ -555,6 +561,7 @@ policies:
555561

556562
assertThat(actual['key']['some']).isEqualTo('thing')
557563
assertThat(actual['key']['one']).isEqualTo(1)
564+
assertThat(actual['prometheus']['prometheusSpec']['scrapeConfigSelectorNilUsesHelmValues']).isEqualTo(null)
558565
}
559566

560567
@Test
@@ -572,7 +579,7 @@ policies:
572579
"test1-example-apps-production",
573580
"test1-secrets"
574581
))
575-
config.application.activeNamespaces= namespaceList
582+
config.application.activeNamespaces = namespaceList
576583
createStack().install()
577584
def actual = parseActualYaml()
578585

0 commit comments

Comments
 (0)