From 71acbf999db54c73b2d489bf5b4ed65e4fcf3e1e Mon Sep 17 00:00:00 2001 From: jeanjerome Date: Sun, 20 Aug 2023 00:44:30 +0200 Subject: [PATCH] ref(15): Start testing wild integration with Jenkins --- vars/ContainerConfig.groovy | 76 +++++++++++++++++++++++++++++++++++++ vars/wildPipeline.groovy | 5 +-- 2 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 vars/ContainerConfig.groovy diff --git a/vars/ContainerConfig.groovy b/vars/ContainerConfig.groovy new file mode 100644 index 0000000..2b801dd --- /dev/null +++ b/vars/ContainerConfig.groovy @@ -0,0 +1,76 @@ +class ContainerConfig { + + static Map getContainerConfig(selectedContainerNames = null) { + def wildPath = getContext(hudson.FilePath) + "./wild-workdir" + def appConfig = readYaml(file: "${wildPath}/config/containers-config.yaml") + + if (selectedContainerNames) { + appConfig.container = appConfig.container.findAll { container -> + selectedContainerNames.contains(container.name) + } + } + + return appConfig + } + + static String generateContainerTemplate(Map appConfig) { + def containerTemplates = appConfig.container.collect { container -> + def envVars = container.envVars.collect { envVar -> + """ + - name: ${envVar.name} + value: "${envVar.value}" + """ + }.join('\n') + + def volumeMounts = container.volumeMounts.collect { volumeMount -> + """ + - name: ${volumeMount.name} + mountPath: ${volumeMount.mountPath} + readOnly: ${volumeMount.readOnly} + """ + }.join('\n') + + def volumes = container.volumes.collect { volume -> + """ + - name: ${volume.name} + persistentVolumeClaim: + claimName: ${volume.claimName} + """ + }.join('\n') + + """ + - name: ${container.name} + image: ${container.image} + command: + - "${container.command}" + tty: ${container.tty} + ${envVars ? "env:\n${envVars}" : ''} + resources: + limits: + cpu: ${container.resources.cpu.limit} + memory: ${container.resources.memory.limit} + requests: + cpu: ${container.resources.cpu.request} + memory: ${container.resources.memory.request} + securityContext: + runAsNonRoot: true + runAsUser: ${container.securityContext.runAsUser} + ${volumeMounts ? "volumeMounts:\n${volumeMounts}" : ''} + ${volumes ? "volumes:\n${volumes}" : ''} + """ + } + + def containerTemplate = """ +apiVersion: v1 +kind: Pod +metadata: + labels: + name: ${appConfig.container.labels.name} +spec: + containers: +${containerTemplates.join('\n')} +""" + + return containerTemplate + } +} diff --git a/vars/wildPipeline.groovy b/vars/wildPipeline.groovy index 9e898cd..f1bb6ad 100644 --- a/vars/wildPipeline.groovy +++ b/vars/wildPipeline.groovy @@ -74,9 +74,8 @@ def call() { } def runContainerNames = ["bash", "maven"] - def runConfig = readAppConfig(runContainerNames) - //def k8s_containers_run = libraryResource('config/k8s/containers-init.yaml') - def k8s_containers_run = generateContainerTemplate(runConfig) + def runContainerConfig = ContainerConfig.getContainerConfig(runContainerNames) + def k8s_containers_run = ContainerConfig.generateContainerTemplate(runContainerConfig) podTemplate( label: pod_run_label,