-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJenkinsfile
406 lines (384 loc) · 16.7 KB
/
Jenkinsfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
def setDescription() {
def item = Jenkins.instance.getItemByFullName(env.JOB_NAME)
item.setDescription("<h5><span style=\"color:#138D75\">KubeSpray Automation v2.21.0 <a href='https://github.com/kubernetes-sigs/kubespray/releases/tag/v2.21.0'>https://github.com/kubernetes-sigs/kubespray/releases/tag/v2.21.0'</a>. Before running this pipeline, make sure to read the README.MD from <a href='https://github.com/escanoru/KubeSpray_Jenkins'>https://github.com/escanoru/KubeSpray_Jenkins</a></span></h5>")
item.save()
}
setDescription()
def inventorySample = '''# ## Configure 'ip' variable to bind kubernetes services on a
# ## different ip than the default iface
# ## We should set etcd_member_name for etcd cluster. The node that is not a etcd member do not need to set the value, or can set the empty string value.
[all:vars]
## You can create a vault containing user_sudo_pass with the password of your nodes or just created a new key and replace user_sudo_pass with such key withing the curly braces
ansible_become_pass='{{ user_sudo_pass }}'
[all]
node1 ansible_host=95.54.0.12 # ip=10.3.0.1 etcd_member_name=etcd1
node2 ansible_host=95.54.0.13 # ip=10.3.0.2 etcd_member_name=etcd2
node3 ansible_host=95.54.0.14 # ip=10.3.0.3 etcd_member_name=etcd3
# ## configure a bastion host if your nodes are not directly reachable
# [bastion]
# bastion ansible_host=x.x.x.x installation_user=some_user
[kube_control_plane]
node1
node2
node3
[etcd]
node1
node2
node3
[kube_node]
node4
node5
node6
[calico_rr]
# Calico advanced options: https://github.com/kubernetes-sigs/kubespray/blob/master/docs/calico.md
[k8s_cluster:children]
kube_control_plane
kube_node
calico_rr'''
def externalLB = '''---
## Only fill this if you will use a external load balancer
apiserver_loadbalancer_domain_name: "elb.some.domain"
loadbalancer_apiserver:
address: 1.2.3.4
port: 8383
'''
def addons = '''---
## More configurations can be found at inventory/mycluster/group_vars/k8s_cluster/addons.yml
dashboard_enabled: false
helm_enabled: false
registry_enabled: false
metrics_server_enabled: true
ingress_nginx_enabled: false
cert_manager_enabled: false
'''
pipeline {
agent { label 'ansible' }
options {
ansiColor('gnome-terminal')
buildDiscarder(logRotator(daysToKeepStr: '90'))
}
parameters {
string(
name: 'ansible_installation',
description: '<h5>Name of the Ansible installation, this is set on the "Global Tool Configuration" on Jenkins</h5>'
)
string(
name: 'installation_user',
defaultValue: 'REPLACE_THIS',
description: '<h5>Username that will run the installation</h5>'
)
string(
name: 'private_key_credential',
defaultValue: 'REPLACE_THIS',
description: '<h5>Jenkins credential holding the private key to connect to the target nodes</h5>'
)
string(
name: 'ansible_vault_credential',
defaultValue: 'REPLACE_THIS',
description: '<h5>Jenkins credential holding the vault</h5>'
)
string(
name: 'decrypt_vault_key_credential',
defaultValue: 'REPLACE_THIS',
description: '<h5>Jenkins credential holding the key to decrypt the Ansible vault</h5>'
)
booleanParam(
name: 'reset_k8s_cluster',
defaultValue: true,
description: 'Uninstall previous installations K8s and set OS requirements, this will force a reboot whether K8s is installed or not'
)
booleanParam(
name: 'only_reset_k8s_cluster',
defaultValue: false,
description: 'Only uninstall, if this is set to true then the other stages will be skiped'
)
booleanParam(
name: 'restart_node',
defaultValue: false,
description: 'Recommended after resseting k8s'
)
booleanParam(
name: 'run_requirements',
defaultValue: true,
description: 'Set OS requirements'
)
booleanParam(
name: 'set_firewalld',
defaultValue: true,
description: 'if set to false firewalld will be stopped and disable'
)
booleanParam(
name: 'install_kubespray',
defaultValue: true,
description: 'Set OS requirements'
)
text(
name: 'inventory',
defaultValue: "${inventorySample}",
description: ''
)
string(
name: 'kube_version',
defaultValue: 'v1.25.6',
description: '<h5>Kubernetes version, found on inventory/mycluster/group_vars/k8s_cluster/k8s-cluster.yml</h5>'
)
string(
name: 'cluster_name',
defaultValue: 'cluster.local',
description: 'Found on inventory/mycluster/group_vars/k8s_cluster/k8s-cluster.yml'
)
string(
name: 'http_proxy',
defaultValue: '',
description: '<h5>e.g http://my_proxy.com:8080, found on inventory/mycluster/group_vars/all/all.yml</h5>'
)
string(
name: 'https_proxy',
defaultValue: '',
description: '<h5>e.g http://my_proxy.com:8080, found on inventory/mycluster/group_vars/all/all.yml</h5>'
)
string(
name: 'no_proxy',
defaultValue: '127.0.0.1,localhost,10.233.0.1,10.233.0.3,10.233.0.10,169.254.25.10',
description: 'Default is the minimum requiered, found on inventory/mycluster/group_vars/all/all.yml'
)
string(
name: 'nodelocaldns_ip',
defaultValue: '10.233.0.10',
description: 'The recommended value for clusterDNS in "KubeletConfiguration" is: 10.233.0.10, found on inventory/mycluster/group_vars/k8s_cluster/k8s-cluster.yml'
)
choice(
name: 'kube_network_plugin',
choices: ['calico','flannel','cilium','weave','cloud','canal'],
description: 'Found on inventory/mycluster/group_vars/k8s_cluster/k8s-cluster.yml'
)
choice(
name: 'container_manager',
choices: ['containerd','crio'],
description: 'Found on inventory/mycluster/group_vars/k8s_cluster/k8s-cluster.yml'
)
choice(
name: 'resolvconf_mode',
choices: ['host_resolvconf','none'],
description: 'Found on inventory/mycluster/group_vars/k8s_cluster/k8s-cluster.yml'
)
choice(
name: 'kube_proxy_mode',
choices: ['iptables','ipvs'],
description: 'Found on inventory/mycluster/group_vars/k8s_cluster/k8s-cluster.yml'
)
booleanParam(
name: 'use_localhost_as_kubeapi_loadbalancer',
defaultValue: true,
description: 'Found on inventory/mycluster/group_vars/all/all.yml'
)
booleanParam(
name: 'use_external_load_balancer',
defaultValue: false,
description: 'Custom parameter to determine if LB will be used'
)
text(
name: 'externalLB',
defaultValue: "${externalLB}",
description: ''
)
text(
name: 'K8sAddons',
defaultValue: "${addons}",
description: ''
)
string(
name: 'local_release_dir',
defaultValue: '/tmp/local_release_dir',
description: "Where the binaries will be downloaded. Note: ensure that you've enough disk space (about 1G)"
)
}
stages {
stage('Clonning KubeSpray project') {
steps {
sh '''
cd ${WORKSPACE}/
git clone -b v2.21.0 https://github.com/kubernetes-sigs/kubespray.git
cd kubespray
echo "running whoami" && whoami
'''
}
}
stage('Reset K8s Cluster') {
when {
expression { params.reset_k8s_cluster == true }
}
steps {
withCredentials([file(credentialsId: "${params.ansible_vault_credential}", variable: 'VAULT_FILE')]) {
// Pass the vault file to a file where is accessible by the roles, this info remains encrypted.
// Using the "writeFile file" function helper won't work as we would only write the name of the file itsel instead of the contents of it.
sh '''
set -x
cat $VAULT_FILE > ${WORKSPACE}/roles/ansible_data_vault.yml
'''
ansiblePlaybook(
installation: "${params.ansible_installation}",
playbook: "${env.WORKSPACE}/kubespray/reset.yml",
inventoryContent: "${params.inventory}",
disableHostKeyChecking : true,
become: true,
credentialsId: "${params.private_key_credential}",
vaultCredentialsId: "${params.decrypt_vault_key_credential}",
forks: 20,
colorized: true,
extras: "--user ${params.installation_user} -e '@${WORKSPACE}/roles/ansible_data_vault.yml' --ssh-extra-args='-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null' --flush-cache -vv",
extraVars: [
http_proxy: "${params.http_proxy}",
https_proxy: "${params.https_proxy}",
no_proxy: "${params.no_proxy}",
reset_confirmation: 'yes'
]
)
}
}
}
stage('Reboot Nodes') {
when {
expression { params.restart_node == true }
}
steps {
withCredentials([file(credentialsId: "${params.ansible_vault_credential}", variable: 'VAULT_FILE')]) {
sh '''
set -x
cat $VAULT_FILE > ${WORKSPACE}/roles/ansible_data_vault.yml
'''
ansiblePlaybook(
installation: "${params.ansible_installation}",
playbook: "${env.WORKSPACE}/roles/Requirements/reboot_target_nodes.yaml",
inventoryContent: "${params.inventory}",
disableHostKeyChecking : true,
become: true,
credentialsId: "${params.private_key_credential}",
vaultCredentialsId: "${params.decrypt_vault_key_credential}",
colorized: true,
extras: "--user ${params.installation_user} -e '@${WORKSPACE}/roles/ansible_data_vault.yml' --ssh-extra-args=' -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null' --flush-cache -v",
extraVars: [
jenkins_workspace: "${env.WORKSPACE}/"
]
)
}
}
}
stage('Running OS requirements K8s') {
when {
expression { params.run_requirements == true && params.only_reset_k8s_cluster == false }
}
steps {
withCredentials([file(credentialsId: "${params.ansible_vault_credential}", variable: 'VAULT_FILE')]) {
sh '''
set -x
cat $VAULT_FILE > ${WORKSPACE}/roles/ansible_data_vault.yml
'''
ansiblePlaybook(
installation: "${params.ansible_installation}",
playbook: "${env.WORKSPACE}/roles/Requirements/main.yaml",
inventoryContent: "${params.inventory}",
disableHostKeyChecking : true,
become: true,
credentialsId: "${params.private_key_credential}",
vaultCredentialsId: "${params.decrypt_vault_key_credential}",
forks: 20,
colorized: true,
extras: "--user ${params.installation_user} -e '@${WORKSPACE}/roles/ansible_data_vault.yml' --ssh-extra-args=' -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null' --flush-cache -v",
extraVars: [
jenkins_workspace: "${env.WORKSPACE}/",
ansible_user: "${params.installation_user}",
http_proxy: "${params.http_proxy}",
https_proxy: "${params.https_proxy}",
no_proxy: "${params.no_proxy}",
set_firewalld: "${params.set_firewalld}"
]
)
}
}
}
stage('Running KubeSpray') {
when {
expression { params.install_kubespray == true && params.only_reset_k8s_cluster == false }
}
steps {
script {
withCredentials([file(credentialsId: "${params.ansible_vault_credential}", variable: 'VAULT_FILE')]) {
writeFile file: "${WORKSPACE}/external_lb_vars.yml", text: "${params.externalLB}"
writeFile file: "${WORKSPACE}/K8sAddons.yml", text: "${params.K8sAddons}"
sh '''
set -x
cat $VAULT_FILE > ${WORKSPACE}/roles/ansible_data_vault.yml
'''
retry(1) {
if (params.use_external_load_balancer) {
sh 'echo Running with use_external_load_balancer'
ansiblePlaybook(
installation: "${params.ansible_installation}",
playbook: "${env.WORKSPACE}/kubespray/cluster.yml",
inventoryContent: "${params.inventory}",
disableHostKeyChecking: true,
become: true,
credentialsId: "${params.private_key_credential}",
vaultCredentialsId: "${params.decrypt_vault_key_credential}",
forks: 20,
colorized: true,
extras: "--user ${params.installation_user} -e '@${WORKSPACE}/external_lb_vars.yml' -e '@${WORKSPACE}/roles/ansible_data_vault.yml' -e '@${WORKSPACE}/K8sAddons.yml' --ssh-extra-args=' -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null' --flush-cache -vv",
extraVars: [
http_proxy: "${params.http_proxy}",
https_proxy: "${params.https_proxy}",
no_proxy: "${params.no_proxy}",
kube_version: "${params.kube_version}",
cluster_name: "${params.cluster_name}",
kube_proxy_mode: "${params.kube_proxy_mode}",
dashboard_enabled: "${params.dashboard_enabled}",
helm_enabled: "${params.helm_enabled}",
registry_enabled: "${params.registry_enabled}",
metrics_server_enabled: "${params.metrics_server_enabled}",
ingress_nginx_enabled: "${params.ingress_nginx_enabled}",
cert_manager_enabled: "${params.cert_manager_enabled}",
loadbalancer_apiserver_localhost: false,
use_localhost_as_kubeapi_loadbalancer: "${params.use_localhost_as_kubeapi_loadbalancer}",
nodelocaldns_ip: "${params.nodelocaldns_ip}",
local_release_dir: "${params.local_release_dir}"
]
)
} else {
ansiblePlaybook(
installation: "${params.ansible_installation}",
playbook: "${env.WORKSPACE}/kubespray/cluster.yml",
inventoryContent: "${params.inventory}",
disableHostKeyChecking: true,
become: true,
credentialsId: "${params.private_key_credential}",
vaultCredentialsId: "${params.decrypt_vault_key_credential}",
forks: 20,
colorized: true,
extras: "--user ${params.installation_user} -e '@${WORKSPACE}/roles/ansible_data_vault.yml' -e '@${WORKSPACE}/K8sAddons.yml' --ssh-extra-args=' -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null' --flush-cache -vv",
extraVars: [
http_proxy: "${params.http_proxy}",
https_proxy: "${params.https_proxy}",
no_proxy: "${params.no_proxy}",
kube_version: "${params.kube_version}",
cluster_name: "${params.cluster_name}",
kube_proxy_mode: "${params.kube_proxy_mode}",
loadbalancer_apiserver_localhost: true,
use_localhost_as_kubeapi_loadbalancer: "${params.use_localhost_as_kubeapi_loadbalancer}",
nodelocaldns_ip: "${params.nodelocaldns_ip}",
local_release_dir: "${params.local_release_dir}"
]
)
}
}
}
}
}
}
}
post {
always {
echo 'Cleaning up the workspace'
deleteDir()
}
}
}