barmanthewise
barmanthewise

Reputation: 377

Jenkins Kubernetes - Use custom Agent pod spec from the Controller pod

I have deployed a Jenkins instance in a Kubernetes cluster using this helm chart. Inside the instance, several declarative pipelines are configured as JCasC. These pipelines need a custom agent pod with several containers. At the moment the yaml spec, let's call them jenkins.pod.yaml, are declared in the repositories that host the application code, where the Jenkinsfile also is, where i fetch them easily like this.

pipeline {
  agent {
    kubernetes {
      yamlFile 'jenkins.pod.yaml'
      retries 2
    }
  }
...
  stages {
    stage('Build war') {
      steps {
        container('builder') {
          sh 'some command in the 'builder' container'
        }
      }
    }
...

This is not ideal since the jenkins.pod.yaml files contain strings that can vary by branch, such as the container registry name, and it would be error prone to have to edit that in the code repo.

My idea was to install the jenkins.pod.yamls in the jenkins controller pod, properly templated, and fetch them with a label from the Jenkinsfile. There is indeed a agent.podTemplate value in the helm chart that seems to allow configuring a custom agent pod spec, but i could not fetch it in the Jenkinsfile, resulting in either errors or the standard jnlp image being used.

I've tried setting agent.podTemplates in the chart to my custom template

agent:
  podTemplates:
   custom-jenkins-label: |
     - name: python
       label: custom-jenkins-label
       serviceAccount: jenkins
       containers:
...

and changing the Jenkinsfile as

pipeline {
  agent {
    label 'custom-jenkins-label'
  }
...

from other answers on this website, but to no avail. An alternative would be to mount these jenkins.pod.yaml agent pod spec files into a volume in the jenkins controller pod, but this also does not seem to work using the controller.volumes/mount helm chart values. The agent does not see these files.

Has anyone solved this?

Upvotes: 0

Views: 1114

Answers (1)

barmanthewise
barmanthewise

Reputation: 377

Found the solution. It was all good except the podTemplate section has a very weird syntax, so I was providing the wrong pod template. I recommend using hte podTemplate.yaml field and specifying a standard kubernetes pod template there.

  podTemplates:
    my-agent: |
      - name: my-agent
        label: my-agent
        namespace: pipeline
        yaml: |-
          apiVersion: v1
          kind: Pod
          spec:
...

Upvotes: 0

Related Questions