Kubernetes plugin configuration using groovy

291 views
Skip to first unread message

Alex

unread,
Aug 20, 2018, 9:54:20 AM8/20/18
to Jenkins Users
I am trying to configure the kubernetes plugin by using groovy at startup. 

The problem that I have is that I can see the configuration in the "Configure System" menu, but it's not working unless i click on "Save"

This is my groovy:

import org.csanchez.jenkins.plugins.kubernetes.*
import jenkins.model.*

def jenkins_instance = Jenkins.instance
def kubernetes = new KubernetesCloud('Kubernetes')

// Configure Kubernetes Cloud
kubernetes.setNamespace('jenkins')
kubernetes.setJenkinsUrl(jenkins_instance.getRootUrl())
kubernetes.setMaxRequestsPerHostStr('30')
kubernetes.setConnectTimeout(30)
kubernetes.setReadTimeout(30)
kubernetes.setUsageRestricted(false)

// Configure Jenkins Slave Container
jenkins_slave_image = System.getenv('JENKINS_SLAVE_IMAGE').toString()
def jenkins_container = new ContainerTemplate('jenkins-slave', jenkins_slave_image)
jenkins_container.setTtyEnabled(true)
jenkins_container.setName('jeknins-slave')

// Configure Jenkins Slave Pods
def jenkins_slave_pod = new PodTemplate()
jenkins_slave_pod.setNamespace('jenkins')
jenkins_slave_pod.setName('jenkins-slave')
jenkins_slave_pod.setContainers([jenkins_container])
jenkins_slave_pod.save()

// Save Changes
kubernetes.addTemplate(jenkins_slave_pod)
jenkins_instance.clouds.removeAll(KubernetesCloud)
jenkins_instance.clouds.addAll(kubernetes)
jenkins_instance.save()

Any thoughts on this?


Carlos Sanchez

unread,
Aug 20, 2018, 10:39:57 AM8/20/18
to jenkins...@googlegroups.com
Have you tried configuration as code plugin?

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/85bd4d52-5083-4536-ae26-61370ac984a4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

unguiculus

unread,
Jan 3, 2019, 10:07:26 AM1/3/19
to Jenkins Users
Did you manage to solve the issue? I'm actually having the same problem but I do use the CasC plugin as Carlos suggested. Everything looks ok in the UI but only starts working if I hit "Save".

I copied away the whole JENKINS_HOME before and after hitting "Save". As expected, the diff doesn't show any changes. I tried triggering a reload of the config via CLI and via Groovy init script. Nothing helped but manually saving in the UI.

Here's the relevant part from the jenkins.yaml:

jenkins:
  clouds
:
   
- kubernetes:
        name
: kubernetes
        serverUrl
: https://kubernetes.default
       
namespace: default
        jenkinsUrl
: http://jenkins-master:8080
        jenkinsTunnel
: jenkins-agent:50000
        templates
:
         
- name: k8s-agent
            idleMinutes
: 30
            containers
:
             
- name: jnlp
                image
: jenkinsci/jnlp-slave:3.27-1-alpine
                command
: ""
                args
: ""


Jenkins version: 2.150.1 LTS

Installed plugins:

configuration-as-code:1.4
workflow-aggregator:2.6
kubernetes:1.14.2
kubernetes-credentials-provider:0.10
timestamper:1.8.10
locale:1.4

I'd appreciate any help.

--Reinhard

Alex

unread,
Jan 3, 2019, 10:23:02 AM1/3/19
to Jenkins Users
Hi there,

It works for me with the CASC plugin like this:

jenkins:
  clouds
:
   
- kubernetes:
        name
: "Kubernetes"
        skipTlsVerify
: true
       
namespace: "jenkins"
        jenkinsUrl
: "http://localhost:8080"
        connectTimeout
: 0
        readTimeout
: 0
        containerCapStr
: 100
        maxRequestsPerHostStr
: 64
        retentionTimeout
: 5
        templates
:
         
- name: "jenkins-slave"
           
namespace: "jenkins"
            nodeUsageMode
: NORMAL
            containers
:

             
- name: "jnlp"
                image
: "jenkinsci/jnlp-slave"

                alwaysPullImage
: false
                workingDir
: "/home/jenkins"
                ttyEnabled
: true
                resourceRequestCpu
: "500m"
                resourceLimitCpu
: "1000m"
                resourceRequestMemory
: "1Gi"
                resourceLimitMemory
: "2Gi"
            idleMinutes
: "30"
            activeDeadlineSeconds
: "900"
            slaveConnectTimeout
: "120"
            serviceAccount
: "jenkins"


These are the plugins that i use:

configuration-as-code
configuration
-as-code-support
kubernetes
:1.13.3
kubernetes
-cd
crowd2
workflow
-aggregator
git
pipeline
-utility-steps

Alex
Reply all
Reply to author
Forward
0 new messages