Skip to content

Commit

Permalink
Merge pull request #817 from smerle33/agent/to/arm64
Browse files Browse the repository at this point in the history
feat(terraform) Remove parameter `agentContainerImage` in favor of `agentLabel`
  • Loading branch information
smerle33 committed Jan 19, 2024
2 parents 8c29512 + ae74587 commit a1f77fa
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 106 deletions.
17 changes: 8 additions & 9 deletions test/groovy/TerraformStepTests.groovy
Expand Up @@ -86,9 +86,9 @@ class TerraformStepTests extends BaseTest {
// And a daily cron trigger for the job
assertTrue(assertMethodCallContainsPattern('cron', '@daily'))

// And the correct pod templates defined
assertTrue(assertMethodCallContainsPattern('containerTemplate', 'jenkinsciinfra/hashicorp-tools:')) // Not tag as it's managed by updatecli
assertTrue(assertMethodCallOccurrences('containerTemplate', 2)) // Only 1 container per pod, but 2 pod spawn (staging and production)
// And 2 nodes with default label are spawned
assertTrue(assertMethodCallContainsPattern('node', 'jnlp-linux-arm64'))
assertTrue(assertMethodCallOccurrences('node', 2))

// xterm color enabled (easier to read Terraform plans)
assertTrue(assertMethodCallContainsPattern('ansiColor', 'xterm'))
Expand Down Expand Up @@ -221,14 +221,14 @@ class TerraformStepTests extends BaseTest {
@Test
void itRunSuccessfullyWithCustomParameters() throws Exception {
def script = loadScript(scriptName)
final String customImage = 'hashicorp/terraform-full:0.13.0'
final String customLabel = 'jnlp-windows-amd64'

// When calling the shared library global function with custom parameters
script.call(
cronTriggerExpression: '@weekly',
stagingCredentials: stagingCustomCreds,
productionCredentials: productionCustomCreds,
agentContainerImage: customImage,
agentLabel: customLabel,
)
printCallStack()

Expand All @@ -244,9 +244,8 @@ class TerraformStepTests extends BaseTest {
// And the custom cron trigger
assertTrue(assertMethodCallContainsPattern('cron', '@weekly'))

// And the custom agent container template defined
assertFalse(assertMethodCallContainsPattern('containerTemplate', 'jenkinsciinfra/terraform:'))
assertTrue(assertMethodCallContainsPattern('containerTemplate', customImage))
assertTrue(assertMethodCallOccurrences('containerTemplate', 2))
// And 2 nodes with custom label are spawned
assertTrue(assertMethodCallContainsPattern('node', customLabel))
assertTrue(assertMethodCallOccurrences('node', 2))
}
}
57 changes: 0 additions & 57 deletions updatecli/updatecli.d/terraform-hashicorp.yml

This file was deleted.

50 changes: 10 additions & 40 deletions vars/terraform.groovy
Expand Up @@ -9,7 +9,7 @@ def call(userConfig = [:]) {
stagingCredentials: [], // No custom secrets for staging by default
productionCredentials: [], // No custom secrets for production by default
productionBranch: 'main', // Defaults to the principal branch
agentContainerImage: 'jenkinsciinfra/hashicorp-tools:1.0.62', // Version managed by updatecli
agentLabel: 'jnlp-linux-arm64', // replace agentContainerImage
runTests: false, // Executes the tests provided by the "calling" project, which should provide a tests/Makefile
runCommonTests: true, // Executes the default test suite from the shared tools repository (terratest)
]
Expand Down Expand Up @@ -50,7 +50,7 @@ def call(userConfig = [:]) {
if (!isBuildCauseUser) {
parallelStages['staging'] = {
stage('Staging') {
agentTemplate(finalConfig.agentContainerImage, {
agentTemplate(finalConfig.agentLabel, {
withCredentials(finalConfig.stagingCredentials) {
stage('🔎 Validate Terraform for Staging Environment') {
getInfraSharedTools(sharedToolsSubDir)
Expand All @@ -75,7 +75,7 @@ def call(userConfig = [:]) {

parallelStages['production'] = {
stage('Production') {
agentTemplate(finalConfig.agentContainerImage, {
agentTemplate(finalConfig.agentLabel, {
withCredentials(defaultConfig.productionCredentials) {
final String planFileName = 'terraform-plan-for-humans.txt'
def scmOutput
Expand Down Expand Up @@ -139,44 +139,14 @@ def call(userConfig = [:]) {
}
}

def agentTemplate(containerImage, body) {
podTemplate(
// Custom YAML definition to enforce no service account token (if Terraform uses kubernetes, it would grant it a wrong access)
yaml: '''
apiVersion: v1
kind: Pod
spec:
automountServiceAccountToken: false
nodeSelector:
kubernetes.azure.com/agentpool: infracipool
kubernetes.io/os: linux
tolerations:
- key: "jenkins"
operator: "Equal"
value: "infra.ci.jenkins.io"
effect: "NoSchedule"
- key: "kubernetes.azure.com/scalesetpriority"
operator: "Equal"
value: "spot"
effect: "NoSchedule"
resources:
limits:
cpu: 2
memory: 2Gi
requests:
cpu: 2
memory: 2Gi
''',
// The Docker image here is aimed at "1 container per pod" and is embedding Jenkins agent tooling
containers: [containerTemplate(name: 'jnlp', image: containerImage)]) {
node(POD_LABEL) {
timeout(time: 1, unit: 'HOURS') {
ansiColor('xterm') {
body.call()
}
}
}
def agentTemplate(agentLabel, body) {
node (agentLabel) {
timeout(time: 1, unit: 'HOURS') {
ansiColor('xterm') {
body.call()
}
}
}
}


Expand Down

0 comments on commit a1f77fa

Please sign in to comment.