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="noreply@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}"
],
"type": "kubernetes.io/dockerconfigjson"
]

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

-- 
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-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jenkinsci-users/6cca8c90-09ce-4b07-b7bd-8c935c5026d5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to