Skip to content

Commit a07def3

Browse files
authored
Merge branch 'master' into computer-info-permission
2 parents b699ee8 + e10afe4 commit a07def3

File tree

236 files changed

+5808
-2110
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

236 files changed

+5808
-2110
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* @jenkinsci/kubernetes-plugin-developers

.github/dependabot.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
version: 2
2+
updates:
3+
- package-ecosystem: maven
4+
directory: "/"
5+
schedule:
6+
interval: daily
7+
open-pull-requests-limit: 10
8+
ignore:
9+
- dependency-name: org.apache.commons:commons-lang3
10+
versions:
11+
- "> 3.8.1"

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@
1010
nbactions.xml
1111
*.bak
1212
/.vscode/
13+
14+
**/.DS_Store

Jenkinsfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ parallel kind: {
2727
node('maven-11') {
2828
timeout(60) {
2929
checkout scm
30-
sh 'mvn -B -ntp -s settings-azure.xml -Dset.changelist -Dmaven.test.failure.ignore clean install'
30+
sh 'mvn -B -ntp -Dset.changelist -Dmaven.test.failure.ignore clean install'
3131
infra.prepareToPublishIncrementals()
3232
junit 'target/surefire-reports/*.xml'
3333
}

README.md

Lines changed: 123 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,46 @@ For that some environment variables are automatically injected:
2121
* `JENKINS_AGENT_NAME`: the name of the Jenkins agent
2222
* `JENKINS_NAME`: the name of the Jenkins agent (Deprecated. Only here for backwards compatibility)
2323

24-
Tested with [`jenkins/jnlp-slave`](https://hub.docker.com/r/jenkins/jnlp-slave),
25-
see the [Docker image source code](https://github.com/jenkinsci/docker-jnlp-slave).
24+
Tested with [`jenkins/inbound-agent`](https://hub.docker.com/r/jenkins/inbound-agent),
25+
see the [Docker image source code](https://github.com/jenkinsci/docker-inbound-agent).
2626

2727
It is not required to run the Jenkins master inside Kubernetes.
28+
# Generic Setup
29+
### Prerequisites
30+
* A running Kubernetes cluster 1.14 or later. For OpenShift users, this means OpenShift Container Platform 4.x.
31+
* A Jenkins instance installed
32+
* The Jenkins Kubernetes plugin installed
33+
34+
It should be noted that the main reason to use the global pod template definition is to migrate a huge corpus of
35+
existing projects (incl. freestyle) to run on Kubernetes without changing job definitions. New users setting up new
36+
Kubernetes builds should use the podTemplate step as shown in the example snippets [here](https://github.com/jenkinsci/kubernetes-plugin/pull/707)
37+
38+
Fill in the Kubernetes plugin configuration. In order to do that, you will open the Jenkins UI and navigate to
39+
**Manage Jenkins -> Configure System -> Cloud -> Kubernetes** and enter in the *Kubernetes URL* and *Jenkins URL*
40+
appropriately, this is unless Jenkins is running in Kubernetes in which case the defaults work.
41+
42+
Supported credentials include:
43+
44+
* Username/password
45+
* Secret File (kubeconfig file)
46+
* Secret text (Token-based authentication) (OpenShift)
47+
* Google Service Account from private key (GKE authentication)
48+
* X.509 Client Certificate
49+
50+
To test this connection is successful you can use the **Test Connection** button to ensure there is
51+
adequate communication from Jenkins to the Kubernetes cluster, as seen below
52+
53+
![image](images/cloud-configuration.png)
54+
55+
In addition to that, in the **Kubernetes Pod Template** section, we need to configure the image that will be used to
56+
spin up the agent pod. We do not recommend overriding the `jnlp` container except under unusual circumstances.
57+
for your agent, you can use the default Jenkins agent image available in [Docker Hub](https://hub.docker.com). In the
58+
‘Kubernetes Pod Template’ section you need to specify the following (the rest of the configuration is up to you):
59+
Kubernetes Pod Template Name - can be any and will be shown as a prefix for unique generated agent’ names, which will
60+
be run automatically during builds
61+
Docker image - the docker image name that will be used as a reference to spin up a new Jenkins agent, as seen below
62+
63+
![image](images/pod-template-configuration.png)
2864

2965
# Kubernetes Cloud Configuration
3066

@@ -35,6 +71,12 @@ If _Kubernetes URL_ is not set, the connection options will be autoconfigured fr
3571

3672
When running the Jenkins master outside of Kubernetes you will need to set the credential to secret text. The value of the credential will be the token of the service account you created for Jenkins in the cluster the agents will run on.
3773

74+
If you check **WebSocket** then agents will connect over HTTP(S) rather than the Jenkins service TCP port.
75+
This is unnecessary when the Jenkins master runs in the same Kubernetes cluster,
76+
but can greatly simplify setup when agents are in an external cluster
77+
and the Jenkins master is not directly accessible (for example, it is behind a reverse proxy).
78+
See [JEP-222](https://jenkins.io/jep/222) for more.
79+
3880
### Restricting what jobs can use your configured cloud
3981

4082
Clouds can be configured to only allow certain jobs to use them.
@@ -78,7 +120,7 @@ Find more examples in the [examples dir](examples).
78120
The default jnlp agent image used can be customized by adding it to the template
79121

80122
```groovy
81-
containerTemplate(name: 'jnlp', image: 'jenkins/jnlp-slave:3.35-5-alpine', args: '${computer.jnlpmac} ${computer.name}'),
123+
containerTemplate(name: 'jnlp', image: 'jenkins/inbound-agent:4.3-4-alpine', args: '${computer.jnlpmac} ${computer.name}'),
82124
```
83125

84126
or with the yaml syntax
@@ -89,15 +131,15 @@ kind: Pod
89131
spec:
90132
containers:
91133
- name: jnlp
92-
image: 'jenkins/jnlp-slave:3.35-5-alpine'
134+
image: 'jenkins/inbound-agent:4.3-4-alpine'
93135
args: ['\$(JENKINS_SECRET)', '\$(JENKINS_NAME)']
94136
```
95137

96138
### Container Group Support
97139

98140
Multiple containers can be defined for the agent pod, with shared resources, like mounts. Ports in each container can be accessed as in any Kubernetes pod, by using `localhost`.
99141

100-
The `container` statement allows to execute commands directly into each container. This feature is considered **ALPHA** as there are still some problems with concurrent execution and pipeline resumption
142+
The `container` statement allows to execute commands directly into each container.
101143

102144
```groovy
103145
podTemplate(containers: [
@@ -156,13 +198,14 @@ Either way it provides access to the following fields:
156198
* **annotations** Annotations to apply to the pod.
157199
* **inheritFrom** List of one or more pod templates to inherit from *(more details below)*.
158200
* **slaveConnectTimeout** Timeout in seconds for an agent to be online *(more details below)*.
159-
* **podRetention** Controls the behavior of keeping slave pods. Can be 'never()', 'onFailure()', 'always()', or 'default()' - if empty will default to deleting the pod after `activeDeadlineSeconds` has passed.
201+
* **podRetention** Controls the behavior of keeping agent pods. Can be 'never()', 'onFailure()', 'always()', or 'default()' - if empty will default to deleting the pod after `activeDeadlineSeconds` has passed.
160202
* **activeDeadlineSeconds** If `podRetention` is set to 'never()' or 'onFailure()', pod is deleted after this deadline is passed.
161203
* **idleMinutes** Allows the Pod to remain active for reuse until the configured number of minutes has passed since the last step was executed on it.
162204
* **showRawYaml** Enable or disable the output of the raw Yaml file. Defaults to `true`
163205
* **runAsUser** The user ID to run all containers in the pod as.
164206
* **runAsGroup** The group ID to run all containers in the pod as.
165207
* **hostNetwork** Use the hosts network.
208+
* **workspaceVolume** The type of volume to use for the workspace. Can be `emptyDirWorkspaceVolume` (default), `dynamicPVC()`, `hostPathWorkspaceVolume()`, `nfsWorkspaceVolume()`, or `persistentVolumeClaimWorkspaceVolume()`.
166209

167210
The `containerTemplate` is a template of container that will be added to the pod. Again, its configurable via the user interface or via pipeline and allows you to set the following fields:
168211

@@ -541,10 +584,38 @@ containerLog 'mongodb'
541584

542585
Also see the online help and [examples/containerLog.groovy](examples/containerLog.groovy).
543586

587+
# Running on OpenShift
588+
589+
## Random UID problem
590+
591+
OpenShift runs containers using a 'random' UID that is overriding what is specified in Docker images.
592+
For this reason, you may end up with the following warning in your build
593+
594+
```
595+
[WARNING] HOME is set to / in the jnlp container. You may encounter troubles when using tools or ssh client. This usually happens if the uid doesnt have any entry in /etc/passwd. Please add a user to your Dockerfile or set the HOME environment variable to a valid directory in the pod template definition.
596+
```
597+
598+
At the moment the jenkinsci agent image is not built for OpenShift and will issue this warning.
599+
600+
This issue can be circumvented in various ways:
601+
* build a docker image for OpenShift in order to behave when running using an arbitrary uid.
602+
* override HOME environment variable in the pod spec to use `/home/jenkins` and mount a volume to `/home/jenkins` to ensure the user running the container can write to it
603+
604+
See this [example](examples/openshift-home-yaml.groovy) configuration.
605+
606+
## Running with OpenShift 3
607+
608+
OpenShift 3 is based on an older version of Kubernetes, which is not anymore directly supported since Kubernetes plugin version 1.26.0.
609+
610+
To get agents working for Openshift 3, add this `Node Selector` to your Pod Templates:
611+
```
612+
beta.kubernetes.io/os=linux
613+
```
614+
544615
# Windows support
545616

546617
You can run pods on Windows if your cluster has Windows nodes.
547-
See the [example](examples/windows.groovy).
618+
See the [example](src/main/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/samples/windows.groovy).
548619

549620
# Constraints
550621

@@ -556,7 +627,7 @@ Other containers must run a long running process, so the container does not exit
556627
just runs something and exit then it should be overridden with something like `cat` with `ttyEnabled: true`.
557628

558629
**WARNING**
559-
If you want to provide your own Docker image for the JNLP slave, you **must** name the container `jnlp` so it overrides the default one. Failing to do so will result in two slaves trying to concurrently connect to the master.
630+
If you want to provide your own Docker image for the JNLP agent, you **must** name the container `jnlp` so it overrides the default one. Failing to do so will result in two agents trying to concurrently connect to the master.
560631

561632

562633

@@ -629,6 +700,33 @@ at `DEBUG` level.
629700

630701
kubectl get pods -o name --selector=jenkins=slave --all-namespaces | xargs -I {} kubectl delete {}
631702

703+
## Pipeline `sh` step hangs when multiple containers are used
704+
To debug this you need to set `-Dorg.jenkinsci.plugins.durabletask.BourneShellScript.LAUNCH_DIAGNOSTICS=true` system property
705+
and then restart the pipeline. Most likely in the console log you will see the following:
706+
```console
707+
sh: can't create /home/jenkins/agent/workspace/thejob@tmp/durable-e0b7cd27/jenkins-log.txt: Permission denied
708+
sh: can't create /home/jenkins/agent/workspace/thejob@tmp/durable-e0b7cd27/jenkins-result.txt.tmp: Permission denied
709+
mv: can't rename '/home/jenkins/agent/workspace/thejob@tmp/durable-e0b7cd27/jenkins-result.txt.tmp': No such file or directory
710+
touch: /home/jenkins/agent/workspace/thejob@tmp/durable-e0b7cd27/jenkins-log.txt: Permission denied
711+
touch: /home/jenkins/agent/workspace/thejob@tmp/durable-e0b7cd27/jenkins-log.txt: Permission denied
712+
touch: /home/jenkins/agent/workspace/thejob@tmp/durable-e0b7cd27/jenkins-log.txt: Permission denied
713+
```
714+
Usually this happens when UID of the user in `jnlp` container differs from the one in other container(s).
715+
All containers you use should have the same UID of the user, also this can be achieved by setting `securityContext`:
716+
```yaml
717+
apiVersion: v1
718+
kind: Pod
719+
spec:
720+
securityContext:
721+
runAsUser: 1000 # default UID of jenkins user in default jnlp image
722+
containers:
723+
- name: maven
724+
image: maven:3.3.9-jdk-8-alpine
725+
command:
726+
- cat
727+
tty: true
728+
```
729+
632730
# Building and Testing
633731
634732
Integration tests will use the currently configured context autodetected from kube config file or service account.
@@ -674,6 +772,21 @@ just run as
674772

675773
mvn clean install -Pmicrok8s
676774

775+
This assumes that from a pod, the host system is accessible as IP address `10.1.1.1`.
776+
It might be some variant such as `10.1.37.1`,
777+
in which case you would need to set `-DconnectorHost=… -Djenkins.host.address=…` instead.
778+
To see the actual address, try:
779+
780+
```bash
781+
ifdata -pa cni0
782+
```
783+
784+
Or to verify the networking inside a pod:
785+
786+
```bash
787+
kubectl run --rm --image=praqma/network-multitool --restart=Never --attach sh ip route | fgrep 'default via'
788+
```
789+
677790
### Integration Tests in a Different Cluster
678791

679792
Try
@@ -719,14 +832,6 @@ Get the url to connect to with
719832

720833
minikube service jenkins --namespace kubernetes-plugin --url
721834

722-
## Running with a remote Kubernetes Cloud in AWS EKS
723-
724-
EKS enforces authentication to the cluster through [aws-iam-authenticator](https://docs.aws.amazon.com/eks/latest/userguide/install-aws-iam-authenticator.html). The token expires after 15 minutes
725-
so the kubernetes client cache needs to be set to something below this by setting a [java argument](https://support.cloudbees.com/hc/en-us/articles/209715698-How-to-add-Java-arguments-to-Jenkins-), like so:
726-
```
727-
JAVA_ARGS="-Dorg.csanchez.jenkins.plugins.kubernetes.clients.cacheExpiration=60"
728-
```
729-
730835
## Running in Google Container Engine GKE
731836

732837
Assuming you created a Kubernetes cluster named `jenkins` this is how to run both Jenkins and agents there.
@@ -780,7 +885,7 @@ Set `Container Cap` to a reasonable number for tests, i.e. 3.
780885

781886
Add an image with
782887

783-
* Docker image: `jenkins/jnlp-slave`
888+
* Docker image: `jenkins/inbound-agent`
784889
* Jenkins agent root directory: `/home/jenkins/agent`
785890

786891
![image](configuration.png)
@@ -813,7 +918,7 @@ Note: the JVM will use the memory `requests` as the heap limit (-Xmx)
813918
## Building
814919

815920
docker build -t csanchez/jenkins-kubernetes .
816-
921+
817922
# Related Projects
818923

819924
* [Kubernetes Pipeline plugin](https://github.com/jenkinsci/kubernetes-pipeline-plugin): pipeline extension to provide native support for using Kubernetes pods, secrets and volumes to perform builds

README_zh.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ For that some environment variables are automatically injected:
1616
* `JENKINS_AGENT_NAME`: Jenkins 节点的名称
1717
* `JENKINS_NAME`: Jenkins 节点的名称(已废弃,保持向后兼容)
1818

19-
使用镜像 [`jenkins/jnlp-slave`](https://hub.docker.com/r/jenkins/jnlp-slave) 做的测试。
20-
查看[Docker 镜像源码](https://github.com/jenkinsci/docker-jnlp-slave)
19+
使用镜像 [`jenkins/inbound-agent`](https://hub.docker.com/r/jenkins/inbound-agent) 做的测试。
20+
查看[Docker 镜像源码](https://github.com/jenkinsci/docker-inbound-agent)
2121

2222
Jenkins master 可以不在 Kubernetes 上运行。
2323

@@ -76,7 +76,7 @@ podTemplate(label: label) {
7676
可以在模板中自定义 `jnlp` 节点镜像
7777

7878
```groovy
79-
containerTemplate(name: 'jnlp', image: 'jenkins/jnlp-slave:3.35-5-alpine', args: '${computer.jnlpmac} ${computer.name}'),
79+
containerTemplate(name: 'jnlp', image: 'jenkins/inbound-agent:4.3-4-alpine', args: '${computer.jnlpmac} ${computer.name}'),
8080
```
8181

8282
或者使用 `yaml` 语法
@@ -87,7 +87,7 @@ kind: Pod
8787
spec:
8888
containers:
8989
- name: jnlp
90-
image: 'jenkins/jnlp-slave:3.35-5-alpine'
90+
image: 'jenkins/inbound-agent:4.3-4-alpine'
9191
args: ['\$(JENKINS_SECRET)', '\$(JENKINS_NAME)']
9292
```
9393
@@ -724,7 +724,7 @@ This can be done checking _Enable proxy compatibility_ under Manage Jenkins -> C
724724
725725
使用下面的方式添加一个镜像:
726726
727-
* Docker 镜像:`jenkins/jnlp-slave`
727+
* Docker 镜像:`jenkins/inbound-agent`
728728
* Jenkins 节点根目录:`/home/jenkins`
729729
730730
![image](configuration.png)

examples/README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
# Kubernetes plugin pipeline examples
1+
# Kubernetes plugin Pipeline examples
22

3-
In this dir you can find several pipeline examples.
3+
In this directory you can find several Pipeline examples.
44

5-
For more examples that are continuously tested, check the [`/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline`](/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline)
6-
resources directory.
5+
For more examples that are automatically tested, check:
6+
7+
* [live samples](../src/main/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/samples)
8+
* [test scripts](../src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline)

examples/declarative-multiple-containers.groovy

Lines changed: 0 additions & 37 deletions
This file was deleted.

examples/declarative.groovy

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)