Re: Jenkins plugin to access Kubernetes API

2018-10-21 Thread olivier blanc
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.


Re: Jenkins plugin to access Kubernetes API

2018-10-21 Thread olivier blanc
I tried Kubernetes cli plugin and it feets pretty well to my needs.
Regards
Olivier

-- 
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/0ca5a5ef-c18a-49b3-8a09-ea0bdd435c5a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Jenkins plugin to access Kubernetes API

2018-10-21 Thread sagar utekar
On Thursday, October 4, 2018 at 10:45:19 PM UTC+5:30, olivier blanc wrote:
> I am looking for a Jenkins plugin to interact with kubernetes api from 
> jenkins pipeline.
> Is there such a plugin ?
> Thanks
> Olivier

Have you solved this issue, I am also looking for solution for same thing.

-- 
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/f59bfcb5--4a4f-9441-9e432b45e4d3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Jenkins plugin to access Kubernetes API

2018-10-06 Thread JonathanRRogers
On Friday, October 5, 2018 at 4:22:22 AM UTC-4, olivier blanc wrote:
>
> 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.
>
>
There are several Kubernetes plugins, but I suspect the "Kubernetes Cli" 
plugin might be most applicable to your situation.

https://plugins.jenkins.io/kubernetes-cli

-- 
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/33a50dd3-5698-42b7-b78a-7bf9d32f1650%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Jenkins plugin to access Kubernetes API

2018-10-05 Thread olivier blanc
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

-- 
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/ce3fe95c-cbcd-4b71-83c0-6b43c0dad4bc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Jenkins plugin to access Kubernetes API

2018-10-05 Thread James Nord
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?

On Thu, 4 Oct 2018, 18:15 olivier blanc,  wrote:

> I am looking for a Jenkins plugin to interact with kubernetes api from
> jenkins pipeline.
> Is there such a plugin ?
> Thanks
> Olivier
>
> --
> 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/ef02beb7-1b4d-4ad9-8976-16af850c2bd7%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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/CAPzq3pdN_Zox_%2Bqtiih98PdAffZiDX9Vm53zg8UQ-jYjx1DyCQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Jenkins plugin to access Kubernetes API

2018-10-04 Thread olivier blanc
I am looking for a Jenkins plugin to interact with kubernetes api from 
jenkins pipeline.
Is there such a plugin ?
Thanks
Olivier

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


Jenkins plugin to access Kubernetes API

2018-10-04 Thread olivier blanc
I am looking for a Jenkins plugin to interact with kubernetes api from 
jenkins pipeline.
Is there such a plugin ?
Thanks
Olivier

-- 
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/e91fb51d-5341-4b84-880e-86d690d6b8ef%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.