Skip to content

Instantly share code, notes, and snippets.

@jhoblitt
Created April 18, 2016 19:20
Show Gist options
  • Save jhoblitt/ce91b458526e3a03d365e2689db825f0 to your computer and use it in GitHub Desktop.
Save jhoblitt/ce91b458526e3a03d365e2689db825f0 to your computer and use it in GitHub Desktop.
configuring the kubernetes jenkins plugin via groovy
import org.csanchez.jenkins.plugins.kubernetes.*
import jenkins.model.*
def j = Jenkins.getInstance()
def k = new KubernetesCloud(
'jenkins-test',
null,
'https://130.211.146.130',
'default',
'https://citest.lsst.codes/',
'10', 0, 0, 5
)
k.setSkipTlsVerify(true)
k.setCredentialsId('ec5cf56b-71e9-4886-9f03-42934a399148')
def p = new PodTemplate('centos:6', null)
p.setName('centos6')
p.setLabel('centos6-docker')
p.setRemoteFs('/home/jenkins')
k.addTemplate(p)
p = new PodTemplate('lsstsqre/centos:7-docker', null)
p.setName('centos7')
p.setLabel('centos7-docker')
p.setRemoteFs('/home/jenkins')
k.addTemplate(p)
j.clouds.replace(k)
j.save()
@dpyoung2
Copy link

dpyoung2 commented Jul 5, 2017

Hello,

This is a handy script. Do you know of a way to script adding templates to an existing kubernetes config in jenkins? Your script adds a new kube cloud and pod templates. I just need to add pod templates to an existing kube cloud.

@jhoblitt
Copy link
Author

While I haven't tried it, I assume it should be a case of finding the KubernetesCloud object and calling #addTemplate on it.

@gurusrinivasamurthy
Copy link

@dpyoung2

Maybe something like this should help!

    def kcloud
    if (Jenkins.instance.clouds) {
        kcloud = Jenkins.instance.clouds.get(0)
        println "cloud found: ${Jenkins.instance.clouds}"
    } else {
        kcloud = new KubernetesCloud(conf.kubernetes.name)
        Jenkins.instance.clouds.add(kcloud)
        println "cloud added: ${Jenkins.instance.clouds}"
    }

@bittelc
Copy link

bittelc commented Jan 12, 2021

This is great, thanks for the public post!
I would love to now see how this script can be called at launch time (eg. the leader comes up, how do I now attach and launch this script to the leader), but I realize that's a totally different scope of work and investment than your quick work here.

Thanks again!

@jhoblitt
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment