Skip to content

Commit f85ca54

Browse files
nihussmannschnatterer
authored andcommitted
change templating with yamlSlurper to templateToMap function
1 parent 6dc9be6 commit f85ca54

File tree

2 files changed

+21
-27
lines changed

2 files changed

+21
-27
lines changed

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

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@ import com.cloudogu.gitops.utils.CommandExecutor
1212
import com.cloudogu.gitops.utils.FileSystemUtils
1313
import com.cloudogu.gitops.utils.K8sClient
1414
import com.cloudogu.gitops.utils.MapUtils
15-
import com.cloudogu.gitops.utils.TemplatingEngine
1615
import freemarker.template.Configuration
1716
import freemarker.template.DefaultObjectWrapperBuilder
1817
import groovy.util.logging.Slf4j
19-
import groovy.yaml.YamlSlurper
2018
import io.micronaut.core.annotation.Order
2119
import jakarta.inject.Singleton
2220

@@ -28,7 +26,7 @@ class Jenkins extends Feature {
2826
static final String HELM_VALUES_PATH = "jenkins/values.ftl.yaml"
2927

3028
String namespace = 'default'
31-
29+
3230
private Config config
3331
private CommandExecutor commandExecutor
3432
private FileSystemUtils fileSystemUtils
@@ -82,21 +80,17 @@ class Jenkins extends Feature {
8280
new Tuple2('jenkins-admin-password', config.jenkins.password))
8381

8482
def helmConfig = config.jenkins.helm
85-
def templatedMap = new YamlSlurper().parseText(
86-
new TemplatingEngine().template(new File(HELM_VALUES_PATH),
87-
[dockerGid : findDockerGid(),
88-
config: config,
89-
// Allow for using static classes inside the templates
90-
statics: new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_32).build()
91-
.getStaticModels(),
92-
])) as Map
93-
94-
def valuesFromConfig = helmConfig.values
95-
96-
def mergedMap = MapUtils.deepMerge(valuesFromConfig, templatedMap)
97-
98-
def tmpHelmValues = fileSystemUtils.createTempFile()
99-
fileSystemUtils.writeYaml(mergedMap, tmpHelmValues.toFile())
83+
def templatedMap = templateToMap(HELM_VALUES_PATH,
84+
[
85+
dockerGid: findDockerGid(),
86+
config : config,
87+
// Allow for using static classes inside the templates
88+
statics : new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_32).build()
89+
.getStaticModels(),
90+
])
91+
92+
def mergedMap = MapUtils.deepMerge(helmConfig.values, templatedMap)
93+
def tempValuesPath = fileSystemUtils.writeTempFile(mergedMap)
10094

10195
deployer.deployFeature(
10296
helmConfig.repoURL,
@@ -105,10 +99,10 @@ class Jenkins extends Feature {
10599
helmConfig.version,
106100
namespace,
107101
'jenkins',
108-
tmpHelmValues
102+
tempValuesPath
109103
)
110104
}
111-
105+
112106
commandExecutor.execute("${fileSystemUtils.rootDir}/scripts/jenkins/init-jenkins.sh", [
113107
TRACE : config.application.trace,
114108
INTERNAL_JENKINS : config.jenkins.internal,
@@ -204,8 +198,8 @@ class Jenkins extends Feature {
204198
def etcGroup = k8sClient.run("tmp-docker-gid-grepper-${new Random().nextInt(10000)}",
205199
'irrelevant' /* Redundant, but mandatory param */, namespace, createGidGrepperOverrides(),
206200
'--restart=Never', '-ti', '--rm', '--quiet')
207-
// --quiet is necessary to avoid 'pod deleted' output
208-
201+
// --quiet is necessary to avoid 'pod deleted' output
202+
209203
def lines = etcGroup.split('\n')
210204
for (String it : lines) {
211205
def parts = it.split(":")
@@ -233,7 +227,7 @@ class Jenkins extends Feature {
233227
'name' : 'tmp-docker-gid-grepper',
234228
// We use the same image for several tasks for performance and maintenance reasons
235229
'image' : "${config.jenkins.internalBashImage}",
236-
'args' : [ 'cat', '/etc/group' ],
230+
'args' : ['cat', '/etc/group'],
237231
'volumeMounts': [
238232
[
239233
'name' : 'group',

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -311,10 +311,10 @@ me:x:1000:''')
311311
private Jenkins createJenkins() {
312312
new Jenkins(config, commandExecutor, new FileSystemUtils() {
313313
@Override
314-
Path createTempFile() {
315-
def ret = super.createTempFile()
316-
temporaryYamlFile = Path.of(ret.toString().replace(".ftl", "")) // Path after template invocation
317-
314+
Path writeTempFile(Map mergeMap) {
315+
def ret = super.writeTempFile(mergeMap)
316+
temporaryYamlFile = Path.of(ret.toString().replace(".ftl", ""))
317+
// Path after template invocation
318318
return ret
319319
}
320320
}, globalPropertyManager, jobManger, userManager, prometheusConfigurator, deploymentStrategy, k8sClient)

0 commit comments

Comments
 (0)