Skip to content

Commit

Permalink
Try to get image version from external
Browse files Browse the repository at this point in the history
This is a test commit.
Try to use template in the resource files and render the value from
a binding variable.
I'm not yet sure if I like this.
I think the usage of a shared library should be like lib@v0.0.1 to
get a deterministic result.
In case of a new docker image is required, update the lib to a new tag.
  • Loading branch information
liejuntao001 committed Oct 23, 2020
1 parent 25ef0a5 commit 52f3f1a
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
3 changes: 2 additions & 1 deletion resources/podtemplates/doxygen.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
spec:
containers:
- name: doxygen
image: hrektts/doxygen:latest
image: ${TEMPLATE_DOXYGEN_IMAGE}
#image: hrektts/doxygen:latest
# better to use your own
#image: docker.example.com/doxygen:v1.0.0
tty: true
51 changes: 51 additions & 0 deletions test/groovy/K8sAgentTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,35 @@ metadata:
image: hrektts/doxygen:latest
tty: true"""

def small_doxygen_external_yaml = """spec:
hostAliases:
- ip: "192.168.1.15"
hostnames:
- "jenkins.example.com"
volumes:
- hostPath:
path: /data/jenkins/repo_mirror
type: ""
name: volume-0
containers:
- name: jnlp
image: jenkinsci/jnlp-slave:3.29-1
imagePullPolicy: Always
command:
- /usr/local/bin/jenkins-slave
volumeMounts:
- mountPath: /home/jenkins/repo_cache
name: volume-0
resources:
limits:
memory: 8Gi
requests:
memory: 4Gi
cpu: 2
- name: doxygen
image: hrektts/doxygen:my_test_version
tty: true"""

def selector_yaml = """spec:
hostAliases:
- ip: "192.168.1.15"
Expand Down Expand Up @@ -314,6 +343,9 @@ metadata:
agent = loadScript("vars/k8sagent.groovy")
parser = new MyYaml()

helper.registerAllowedMethod('renderTemplate', [String, Map]) { s,opts ->
loadScript("vars/renderTemplate.groovy")(s, opts)
}
}

@Test
Expand Down Expand Up @@ -445,6 +477,25 @@ metadata:
//assertEquals "results", expected.entrySet(), ret.entrySet()
}

@Test
void testDoxygenExternalVersion() {

binding.setVariable('TEMPLATE_DOXYGEN_IMAGE', "hrektts/doxygen:my_test_version")

def expected_yaml = small_doxygen_external_yaml
def processed_yaml = parser.merge([expected_yaml.toString()])

def expected = [
cloud: 'kubernetes',
yaml : processed_yaml
]

def ret = agent(name: 'small+doxygen')

assertEquals "results", ret.entrySet().containsAll(expected.entrySet()), true
//assertEquals "results", expected.entrySet(), ret.entrySet()
}

@Test
void testNoSelector() {
def expected_yaml = base_yaml
Expand Down
15 changes: 15 additions & 0 deletions vars/k8sagent.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ def call(Map opts = [:]) {
String nodeSelector = opts.get('selector', '')
String jnlpImage = opts.get('jnlpImage', '')

String doxygen_image

try {
doxygen_image = "${TEMPLATE_DOXYGEN_IMAGE}"
} catch (e) {
//println("TEMPLATE_DOXYGEN_IMAGE not defined")
doxygen_image = 'hrektts/doxygen:latest'
}

Map template_vars = [:]
template_vars['TEMPLATE_DOXYGEN_IMAGE'] = doxygen_image

def ret = [:]

def comps = name.split('\\+|-').toList()
Expand All @@ -24,6 +36,7 @@ def call(Map opts = [:]) {
String template
for (c in comps) {
template = libraryResource 'podtemplates/' + c + '.yaml'
template = renderTemplate(template, template_vars)
templates.add(template)
}

Expand All @@ -46,6 +59,8 @@ spec:
templates.add(baseImage)
}



def myyaml = new MyYaml()
def final_template = myyaml.merge(templates)

Expand Down
6 changes: 6 additions & 0 deletions vars/renderTemplate.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import groovy.text.StreamingTemplateEngine

def call(input, variables) {
def engine = new StreamingTemplateEngine()
return engine.createTemplate(input).make(variables).toString()
}

0 comments on commit 52f3f1a

Please sign in to comment.