Jenkins plugin to access Kubernetes API

62 views
Skip to first unread message

olivier blanc

unread,
Oct 4, 2018, 1:15:19 PM10/4/18
to Jenkins Users
I am looking for a Jenkins plugin to interact with kubernetes api from jenkins pipeline.
Is there such a plugin ?
Thanks
Olivier

James Nord

unread,
Oct 5, 2018, 3:47:53 AM10/5/18
to jenkins...@googlegroups.com
Why do you want a plugin? Logic is best done in scripts that can be debugged locally outside of Jenkins leaving pipeline to do orchestration and reporting.  Is there a reason you can not use kubectl directly?

--
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/ef02beb7-1b4d-4ad9-8976-16af850c2bd7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

olivier blanc

unread,
Oct 5, 2018, 4:22:22 AM10/5/18
to Jenkins Users
I have a first version that works fine with scripts, I am trying to figure out if using API could be easier for my users.

The point is that I am running Jenkins outside kubernetes.
So to access Kube master, I have to open ssh and run kubectl with command line arguments, or prepare either Yaml/Json datas and send then through the Api.
All datas to build the pods are determine dynamically during the pipeline.

Thanks for your suggestions and help
Olivier

JonathanRRogers

unread,
Oct 7, 2018, 1:10:38 AM10/7/18
to Jenkins Users
There are several Kubernetes plugins, but I suspect the "Kubernetes Cli" plugin might be most applicable to your situation.

sagar utekar

unread,
Oct 21, 2018, 7:59:24 AM10/21/18
to Jenkins Users
Have you solved this issue, I am also looking for solution for same thing.

olivier blanc

unread,
Oct 21, 2018, 4:14:51 PM10/21/18
to Jenkins Users
I tried Kubernetes cli plugin and it feets pretty well to my needs.
Regards
Olivier

olivier blanc

unread,
Oct 22, 2018, 12:54:48 AM10/22/18
to Jenkins Users
Here is a code example :

label = "worker-${UUID.randomUUID().toString()}"
cloudName="MyCloudNameDefinedInJenkinsMainConfiguration"

podTemplate (
    cloud: cloudName,
    label: label,
    yaml: """
apiVersion: v1
kind: Pod
metadata:
  labels:
    some-label: ${label}
spec:
  containers:
    - name: jnlp
      image: ${registry_docker}/dgfip/jnlp-slave-k8s:latest
    - name: maven-jdk8
      image: ${registry_docker}/tools/maven:3.5.3-jdk-8
      imagePullPolicy: Always
      command:
      - cat
      tty: true
      volumeMounts:
      - mountPath: /root/.m2
        name: m2
  volumes: 
    - name: m2
      persistentVolumeClaim:
        claimName: m2-pvc
  imagePullSecrets:
    - name: regsecret-registry
"""
)
{
    node (label) {
      stage('Préparation environnement')
{
           
          // Example to create namespace
                name =" myNamespace"
nom_secret_k8s = "regsecret-registry"
user_secret_k8s = "dockerUser"
pass_secret_k8s="dockerPassword"
email_secret_k8s="nor...@somehost.somedomain"
                auth="${user_secret_k8s}:${pass_secret_k8s}".bytes.encodeBase64().toString()
json='{"auths":{"https://my-registry:port":{"username":"${user_secret_k8s}","password":"${pass_secret_k8s}","email":"${email_secret_k8s}","auth":"${auth}"}}}'
yamlStructure = [
"apiVersion": "v1",
"data": [
".dockerconfigjson": "${json}.bytes.encodeBase64().toString()
],
"kind": "Secret",
"metadata": [
"name": "${nom_secret_k8s}",
"namespace": "${name}"
],
]

yamlSecretFile= "${WORKSPACE}/${env.BUILD_TAG}_secret.yaml"
writeYaml file: yamlSecretFile, data: yamlStructure
withKubeConfig(
caCertificate: kube_ca,
credentialsId: kube_credentials,
serverUrl: kube_api_url
) {
script {
try {
sh "kubectl create namespace ${name} && kubectl apply -f ${yamlSecretFile} --validate=false"
}
catch (error) {
print "creating namespace and/or secret :"
print error.toString()
print error.getMessage()
print error.printStackTrace()
throw error
}
finally {
sh " rm ${yamlSecretFile}"
}
}
}
...

You will have to deal with string replacement in the last part. But this is the idea.
You also hava to setup properly you kubernetes cli  configuration in jenkins main configuration.
You will also have to provide a JNLP slave client with Kubectl installed.
Here is the Dockerfile that I used :

FROM jenkinsci/jnlp-slave:alpine

MAINTAINER Me M...@SomeDomain.com>


ENV KUBE_LATEST_VERSION="v1.12"


USER root

WORKDIR /

RUN apk add --update -t deps curl tar gzip ca-certificates git \

 && curl -L https://storage.googleapis.com/kubernetes-release/release/${KUBE_LATEST_VERSION}/bin/linux/amd64/kubectl -o /usr/local/bin/kubectl \

 && chmod +x /usr/local/bin/kubectl \

 && apk del --purge deps \

 && rm /var/cache/apk/*


USER jenkins




Let me know if you need more
Reply all
Reply to author
Forward
0 new messages