Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-48135

kubernetes-plugin should support multiple containers in declarative templates

    XMLWordPrintable

    Details

    • Similar Issues:

      Description

      e.g. something like this

      pipeline {
        agent {
          kubernetes {
            label 'mypod'
            podTemplate {
              containerTemplate {
                name 'maven'
                image 'maven:3.3.9-jdk-8-alpine'
                ttyEnabled true
                command 'cat'
              }
              containerTemplate {
                name 'node'
                image 'node:9.2'
                ttyEnabled true
                command 'cat'
              }
            }
          }
        }
        environment {
          CONTAINER_ENV_VAR = 'container-env-var-value'
        }
        stages {
          stage('Run maven') {
            steps {
              container('maven') {
                sh 'echo INSIDE_CONTAINER_ENV_VAR = ${CONTAINER_ENV_VAR}'
                sh 'mvn -version'
              }
            }
          }
          stage('Run npm') {
            steps {
              container('node') {
                sh 'echo INSIDE_CONTAINER_ENV_VAR = ${CONTAINER_ENV_VAR}'
                sh 'npm -version'
              }
            }
          }
        }
      }
      

        Attachments

          Issue Links

            Activity

            Hide
            csanchez Carlos Sanchez added a comment -

            This should be handled by JENKINS-48050

            Show
            csanchez Carlos Sanchez added a comment - This should be handled by JENKINS-48050
            Hide
            jstrachan James Strachan added a comment -

            I don't see how JENKINS-48050 is gonna help define podTemplates and the associated kubernetes metadata (volumes, volume mounts, serviceAccounts, secrets et al)

            Show
            jstrachan James Strachan added a comment - I don't see how  JENKINS-48050  is gonna help define podTemplates and the associated kubernetes metadata (volumes, volume mounts, serviceAccounts, secrets et al)
            Hide
            jstrachan James Strachan added a comment -

            I had a little spike trying to implement this - so far I'm stuck on how to populate KubernetesDeclarativeAgent with a podTemplate and nested containerTemplate objects using declarative syntax; I've yet to find a sample of passing a list of objects into a setter via declarative

            Show
            jstrachan James Strachan added a comment - I had a little spike trying to implement this - so far I'm stuck on how to populate KubernetesDeclarativeAgent with a podTemplate and nested containerTemplate objects using declarative syntax; I've yet to find a sample of passing a list of objects into a setter via declarative
            Hide
            jstrachan James Strachan added a comment -

            BTW here's an attempt at an implementation:

            https://github.com/jstrachan/kubernetes-plugin/tree/48135

            it mostly seems to be working apart from I've not yet figured out the right declarative magic to be able to specify a podTemplate with > 1 containerTemplates inside it. I managed to get 1 container specified at least https://github.com/jstrachan/kubernetes-plugin/blob/48135/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/declarativePod.groovy#L5-L17

            but haven't worked out how to declaratively specify > 1 containerTemplates yet

            Show
            jstrachan James Strachan added a comment - BTW here's an attempt at an implementation: https://github.com/jstrachan/kubernetes-plugin/tree/48135 it mostly seems to be working apart from I've not yet figured out the right declarative magic to be able to specify a podTemplate with > 1 containerTemplates inside it. I managed to get 1 container specified at least https://github.com/jstrachan/kubernetes-plugin/blob/48135/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/declarativePod.groovy#L5-L17 but haven't worked out how to declaratively specify > 1 containerTemplates yet
            Hide
            jorge07 Jorge Arco added a comment -

            Very interested on this. If I can help, ping me

            Show
            jorge07 Jorge Arco added a comment - Very interested on this. If I can help, ping me
            Hide
            mitchellmaler Mitchell Maler added a comment -

            We are trying to move teams to using K8S build agents and using the declarative syntax instead of the scripted pipelines. What is the status of bringing podTemplate to the declarative syntax?

            Show
            mitchellmaler Mitchell Maler added a comment - We are trying to move teams to using K8S build agents and using the declarative syntax instead of the scripted pipelines. What is the status of bringing podTemplate to the declarative syntax?
            Hide
            csanchez Carlos Sanchez added a comment -

            There is work in JENKINS-46336 to allow multiple containers in declarative

            And a PR about podTemplate in progress in https://github.com/jenkinsci/kubernetes-plugin/pull/260

            Show
            csanchez Carlos Sanchez added a comment - There is work in JENKINS-46336 to allow multiple containers in declarative And a PR about podTemplate in progress in https://github.com/jenkinsci/kubernetes-plugin/pull/260
            Hide
            zougi Frederic Rousseau added a comment -

            Changed assignee. indeed a lot of people, including me, are actively waiting the draft replacement for PR260 Andrew told us he'll come up with 2 months ago

            Show
            zougi Frederic Rousseau added a comment - Changed assignee. indeed a lot of people, including me, are actively waiting the draft replacement for PR260 Andrew told us he'll come up with 2 months ago
            Hide
            csanchez Carlos Sanchez added a comment -

            I have created a PR that allows multiple containers using the yaml syntax

            https://github.com/jenkinsci/kubernetes-plugin/pull/306

            pipeline {
              agent {
                kubernetes {
                  label 'mypod'
                  defaultContainer: 'jnlp'
                  yaml """
            apiVersion: v1
            kind: Pod
            metadata:
              labels:
                some-label: some-label-value
            spec:
              containers:
              - name: maven
                image: maven:alpine
                command:
                - cat
                tty: true
              - name: busybox
                image: busybox
                command:
                - cat
                tty: true
            """
                }
              }
              stages {
                stage('Run maven') {
                  steps {
                    container('maven') {
                      sh 'mvn -version'
                    }
                    container('busybox') {
                      sh '/bin/busybox'
                    }
                  }
                }
              }
            }
            
            Show
            csanchez Carlos Sanchez added a comment - I have created a PR that allows multiple containers using the yaml syntax https://github.com/jenkinsci/kubernetes-plugin/pull/306 pipeline { agent { kubernetes { label 'mypod' defaultContainer: 'jnlp' yaml """ apiVersion: v1 kind: Pod metadata: labels: some-label: some-label-value spec: containers: - name: maven image: maven:alpine command: - cat tty: true - name: busybox image: busybox command: - cat tty: true """ } } stages { stage( 'Run maven' ) { steps { container( 'maven' ) { sh 'mvn -version' } container( 'busybox' ) { sh '/bin/busybox' } } } } }
            Hide
            kamalay Lokesh Kamalay added a comment -

            Thanks Carlos, may I know in which version your fix is included.

            Show
            kamalay Lokesh Kamalay added a comment - Thanks Carlos, may I know in which version your fix is included.
            Show
            csanchez Carlos Sanchez added a comment - https://github.com/jenkinsci/kubernetes-plugin/blob/master/CHANGELOG.md
            Hide
            mwyraz Michael michael@wyraz.de added a comment -

            Hello,

            I think this Issue should be re-openend until podTemplate with multiple containers is supported (as in the initial request). The YAML is a valid workaround but adds another language (yaml) to the groovy file which reduces the readability of the Jenkinsfile.

            Kind regards,

            Michael.

             

             

            Show
            mwyraz Michael michael@wyraz.de added a comment - Hello, I think this Issue should be re-openend until podTemplate with multiple containers is supported (as in the initial request). The YAML is a valid workaround but adds another language (yaml) to the groovy file which reduces the readability of the Jenkinsfile. Kind regards, Michael.    
            Hide
            zougi Frederic Rousseau added a comment -

            ideally you exclude the yaml in a separate file (ie: kube.yaml) and load it from there. In practice because we need a node to use a function like readFile.. I ended up putting the text in the pipeline. Which is bad because now we have lots of duplicates. Indeed, I don't know if completing the implementation of podTemplate is a good idea, but there is definitely something to do here.

             

            Show
            zougi Frederic Rousseau added a comment - ideally you exclude the yaml in a separate file (ie: kube.yaml) and load it from there. In practice because we need a node to use a function like readFile.. I ended up putting the text in the pipeline. Which is bad because now we have lots of duplicates. Indeed, I don't know if completing the implementation of podTemplate is a good idea, but there is definitely something to do here.  

              People

              Assignee:
              csanchez Carlos Sanchez
              Reporter:
              jstrachan James Strachan
              Votes:
              6 Vote for this issue
              Watchers:
              12 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: