/
Utils.groovy
626 lines (553 loc) · 19.3 KB
/
Utils.groovy
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
#!/usr/bin/groovy
package io.fabric8
import com.cloudbees.groovy.cps.NonCPS
import io.fabric8.kubernetes.client.DefaultKubernetesClient
import io.fabric8.kubernetes.client.KubernetesClient
import io.fabric8.openshift.client.DefaultOpenShiftClient
import io.fabric8.openshift.client.OpenShiftClient
import jenkins.model.Jenkins
import org.jenkinsci.plugins.workflow.job.WorkflowJob
@NonCPS
String environmentNamespace(String environment) {
try {
def answer = io.fabric8.kubernetes.api.environments.Environments.namespaceForEnvironment(environment)
if (answer) {
return answer;
}
} catch (e) {
echo "WARNING: Failed to invoke Environments.namespaceForEnvironment(environment) probably due to API whitelisting: ${e}"
e.printStackTrace()
}
String ns = getNamespace()
if (ns.endsWith("-jenkins")) {
ns = ns.substring(0, ns.lastIndexOf("-jenkins"))
}
return ns + "-${environment.toLowerCase()}"
}
@NonCPS
String testNamespace() {
return getUsersNamespace();
}
/**
* Loads the environments in the default user namespace
*/
@NonCPS
def environments() {
try {
return io.fabric8.kubernetes.api.environments.Environments.load()
} catch (e) {
echo "WARNING: Failed to invoke Environments.load() probably due to API whitelisting: ${e}"
e.printStackTrace()
}
// TODO can't do this in old jenkins which don't have this class yet
// return new io.fabric8.kubernetes.api.environments.Environments(getNamespace(), new HashMap())
return null
}
/**
* Loads the environments from the given namespace
*/
@NonCPS
def environments(String namespace) {
try {
return io.fabric8.kubernetes.api.environments.Environments.load(namespace)
} catch (e) {
echo "WARNING: Failed to invoke Environments.load(namespace) probably due to API whitelisting: ${e}"
e.printStackTrace()
}
// TODO can't do this in old jenkins which don't have this class yet
// return new io.fabric8.kubernetes.api.environments.Environments(namespace, new HashMap())
return null
}
/**
* Loads the environments from the user namespace
*/
@NonCPS
def pipelineConfiguration() {
try {
return io.fabric8.kubernetes.api.pipelines.PipelineConfiguration.loadPipelineConfiguration()
} catch (e) {
echo "WARNING: Failed to invoke Environments.loadPipelineConfiguration() probably due to API whitelisting: ${e}"
e.printStackTrace()
}
// TODO can't do this in old jenkins which don't have this class yet
// return new io.fabric8.kubernetes.api.pipelines.PipelineConfiguration()
return null
}
/**
* Loads the environments from the given namespace
*/
@NonCPS
def pipelineConfiguration(String namespace) {
try {
return io.fabric8.kubernetes.api.pipelines.PipelineConfiguration.loadPipelineConfiguration(namespace)
} catch (e) {
echo "WARNING: Failed to invoke PipelineConfiguration.loadPipelineConfiguration(namespace) probably due to API whitelisting: ${e}"
e.printStackTrace()
}
// TODO can't do this in old jenkins which don't have this class yet
//return new io.fabric8.kubernetes.api.pipelines.PipelineConfiguration()
return null
}
/**
* Returns true if the integration tests should be disabled
*/
@NonCPS
boolean isDisabledITests() {
boolean answer = false
try {
def config = pipelineConfiguration()
echo "Loaded PipelineConfiguration ${config}"
if (isCD()) {
answer = config.isDisableITestsCD()
} else if (isCI()) {
answer = config.isDisableITestsCI()
}
} catch (e) {
echo "WARNING: Failed to find the flag on the PipelineConfiguration object - probably due to the jenkins plugin `kubernetes-pipeline-plugin` version: ${e}"
e.printStackTrace()
}
return answer;
}
/**
* Returns true if we should use S2I to build docker images
*/
@NonCPS
def isUseOpenShiftS2IForBuilds() {
return !isUseDockerSocket()
}
/**
* Returns true if the current cluster can support S2I
*/
@NonCPS
def supportsOpenShiftS2I() {
DefaultOpenShiftClient client = new DefaultOpenShiftClient();
if (client.isAdaptable(OpenShiftClient.class)) {
try {
if (client.supportsOpenShiftAPIGroup("image.openshift.io")) {
return true
}
} catch (e) {
echo "WARNING: Failed to use the OpenShiftClient.supportsOpenShiftAPIGroup() API: ${e}"
e.printStackTrace()
return true
}
}
return false
}
/**
* Returns true if we should mount the docker socket for docker builds
*/
@NonCPS
boolean isUseDockerSocket() {
boolean answer = false
try {
def config = pipelineConfiguration()
echo "Loaded PipelineConfiguration ${config}"
def flag = config.getUseDockerSocketFlag()
if (flag != null) {
echo "Loaded the useDockerSocket flag ${flag}"
return flag ? true : false
}
} catch (e) {
echo "WARNING: Failed to find the getUseDockerSocketFlag() flag on the PipelineConfiguration object - probably due to the jenkins plugin `kubernetes-pipeline-plugin` version: ${e}"
e.printStackTrace()
}
return supportsOpenShiftS2I() ? false : true;
}
@NonCPS
String getDockerRegistry() {
def externalDockerRegistryURL = getUsersPipelineConfig('external-docker-registry-url')
if (externalDockerRegistryURL) {
return externalDockerRegistryURL
}
// fall back to the old < 4.x when the registry was in the same namespace
def registryHost = env.FABRIC8_DOCKER_REGISTRY_SERVICE_HOST
def registryPort = env.FABRIC8_DOCKER_REGISTRY_SERVICE_PORT
if (!registryHost || !registryPort) {
error "No external-docker-registry-url found in Jenkins configmap or no FABRIC8_DOCKER_REGISTRY_SERVICE_HOST FABRIC8_DOCKER_REGISTRY_SERVICE_PORT environment variables"
}
return registryHost + ':' + registryPort
}
@NonCPS
String getUsersPipelineConfig(k) {
// first lets check if we have the new pipeliens configmap in the users home namespace
KubernetesClient client = new DefaultKubernetesClient()
def ns = getUsersNamespace()
def r = client.configMaps().inNamespace(ns).withName('fabric8-pipelines').get()
if (!r) {
error "no fabric8-pipelines configmap found in namespace ${ns}"
}
def d = r.getData()
echo "looking for key ${k} in ${ns}/fabric8-pipelines configmap"
def v = d[k]
return v
}
// returns a map of the configmap data in a given namepspace
@NonCPS
String getConfigMap(ns, cm, key) {
// first lets check if we have the new pipeliens configmap in the users home namespace
KubernetesClient client = new DefaultKubernetesClient()
if (!ns) {
ns = getNamespace()
}
def r = client.configMaps().inNamespace(ns).withName(cm).get()
if (!r) {
error "no ${cm} configmap found in namespace ${ns}"
}
if (key) {
return r.getData()[key]
}
return r.getData()
}
@NonCPS
private Map<String, String> parseConfigMapData(final String input) {
final Map<String, String> map = new HashMap<String, String>();
for (String pair : input.split("\n")) {
String[] kv = pair.split(":");
map.put(kv[0].trim(), kv[1].trim());
}
return map;
}
@NonCPS
String getNamespace() {
KubernetesClient client = new DefaultKubernetesClient()
return client.getNamespace()
}
@NonCPS
def getImageStreamSha(imageStreamName) {
OpenShiftClient oc = new DefaultOpenShiftClient()
return findTagSha(oc, imageStreamName, getNamespace())
}
// returns the tag sha from an imagestream
// original code came from the fabric8-maven-plugin
@NonCPS
def findTagSha(OpenShiftClient client, String imageStreamName, String namespace) {
def currentImageStream = null
for (int i = 0; i < 15; i++) {
if (i > 0) {
echo("Retrying to find tag on ImageStream ${imageStreamName}")
try {
Thread.sleep(1000)
} catch (InterruptedException e) {
echo("interrupted ${e}")
}
}
currentImageStream = client.imageStreams().withName(imageStreamName).get()
if (currentImageStream == null) {
continue
}
def status = currentImageStream.getStatus()
if (status == null) {
continue
}
def tags = status.getTags()
if (tags == null || tags.isEmpty()) {
continue
}
// latest tag is the first
TAG_EVENT_LIST:
for (def list : tags) {
def items = list.getItems()
if (items == null) {
continue TAG_EVENT_LIST
}
// latest item is the first
for (def item : items) {
def image = item.getImage()
if (image != null && image != '') {
echo("Found tag on ImageStream " + imageStreamName + " tag: " + image)
return image
}
}
}
}
// No image found, even after several retries:
if (currentImageStream == null) {
error("Could not find a current ImageStream with name " + imageStreamName + " in namespace " + namespace)
} else {
error("Could not find a tag in the ImageStream " + imageStreamName)
}
}
@NonCPS
def addAnnotationToBuild(annotation, value) {
def flow = new Fabric8Commands()
if (flow.isOpenShift()) {
def buildName = getValidOpenShiftBuildName()
echo "Adding annotation '${annotation}: ${value}' to Build ${buildName}"
OpenShiftClient oClient = new DefaultOpenShiftClient()
def usersNamespace = getUsersNamespace()
echo "looking for ${buildName} in namespace ${usersNamespace}"
oClient.builds().inNamespace(usersNamespace).withName(buildName).edit().editMetadata().addToAnnotations(annotation, value).endMetadata().done()
} else {
echo "Not running on openshift so skip adding annotation ${annotation}: value"
}
}
@NonCPS
def getSpaceLabelFromBuild(buildName) {
def flow = new Fabric8Commands()
if (flow.isOpenShift()) {
OpenShiftClient oClient = new DefaultOpenShiftClient()
def usersNamespace = getUsersNamespace()
echo "looking for ${buildName} in namespace ${usersNamespace}"
// Search for label named 'space' in currently running Build object
def labels = oClient.builds().inNamespace(usersNamespace).withName(buildName).get().getMetadata().getLabels()
if (labels != null) {
spaceLabel = labels.get("space")
if (spaceLabel != null) {
echo "Found label for space: '${spaceLabel}', in Build ${buildName}"
return spaceLabel
}
}
echo "No space label found in Build ${buildName}"
} else {
echo "Not running on OpenShift, so not looking for 'space' label"
}
return ""
}
@NonCPS
def getUsersNamespace() {
def usersNamespace = getNamespace()
if (usersNamespace.endsWith("-jenkins")) {
usersNamespace = usersNamespace.substring(0, usersNamespace.lastIndexOf("-jenkins"))
}
return usersNamespace
}
def getBranch() {
def branch = env.BRANCH_NAME
if (!branch) {
try {
branch = sh(script: 'git symbolic-ref --short HEAD', returnStdout: true).toString().trim()
} catch (err) {
echo('Unable to get git branch and in a detached HEAD. You may need to select Pipeline additional behaviour and \'Check out to specific local branch\'')
return null
}
}
echo "Using branch ${branch}"
return branch
}
def isCI() {
def branch = getBranch()
if (branch && branch.startsWith('PR-')) {
return true
}
// if we can't get the branch assume we're not in a CI pipeline as that would be a PR branch
return false
}
def isCD() {
def branch = getBranch()
if (!branch || branch.equals('master')) {
return true
}
// if we can't get the branch assume we're not in a CI pipeline as that would be a PR branch
return false
}
def addPipelineAnnotationToBuild(t) {
addAnnotationToBuild('fabric8.io/pipeline.type', t)
}
def getLatestVersionFromTag() {
sh 'git fetch --tags'
sh 'git config versionsort.prereleaseSuffix -RC'
sh 'git config versionsort.prereleaseSuffix -M'
// if the repo has no tags this command will fail
def version = sh(script: 'git tag --sort version:refname | tail -1', returnStdout: true).toString().trim()
if (version == null || version.size() == 0) {
error 'no release tag found'
}
return version.startsWith("v") ? version.substring(1) : version
}
@NonCPS
def isValidBuildName(buildName) {
def flow = new Fabric8Commands()
if (flow.isOpenShift()) {
echo "Looking for matching Build ${buildName}"
OpenShiftClient oClient = new DefaultOpenShiftClient()
def usersNamespace = getUsersNamespace()
def build = oClient.builds().inNamespace(usersNamespace).withName(buildName).get()
if (build) {
return true
}
return false
} else {
error "Not running on openshift so cannot lookup build names"
}
}
@NonCPS
def getValidOpenShiftBuildName() {
def buildName = getOpenShiftBuildName()
if (isValidBuildName(buildName)) {
return buildName
} else {
error "No matching openshift build with name ${buildName} found"
}
}
def replacePackageVersion(packageLocation, pair) {
def property = pair[0]
def version = pair[1]
sh "sed -i -r 's/\"${property}\": \"[0-9][0-9]{0,2}.[0-9][0-9]{0,2}(.[0-9][0-9]{0,2})?(.[0-9][0-9]{0,2})?(-development)?\"/\"${property}\": \"${version}\"/g' ${packageLocation}"
}
def replacePackageVersions(packageLocation, replaceVersions) {
for (int i = 0; i < replaceVersions.size(); i++) {
replacePackageVersion(packageLocation, replaceVersions[i])
}
}
def getExistingPR(project, pair) {
def property = pair[0]
def version = pair[1]
def flow = new Fabric8Commands()
def githubToken = flow.getGitHubToken()
def apiUrl = new URL("https://api.github.com/repos/${project}/pulls")
def rs = restGetURL {
authString = githubToken
url = apiUrl
}
if (rs == null || rs.isEmpty()) {
return null
}
for (int i = 0; i < rs.size(); i++) {
def pr = rs[i]
echo "checking PR ${pr.number}"
if (pr.state == 'open' && pr.title.contains("fix(version): update ${property} to ${version}")) {
echo 'matched'
return pr.number
}
}
return null
}
def getOpenPRs(project) {
def openPRs = []
def flow = new Fabric8Commands()
def githubToken = flow.getGitHubToken()
def apiUrl = new URL("https://api.github.com/repos/${project}/pulls")
def rs = restGetURL {
authString = githubToken
url = apiUrl
}
if (rs == null || rs.isEmpty()) {
return false
}
for (int i = 0; i < rs.size(); i++) {
def pr = rs[i]
if (pr.state == 'open') {
openPRs << String.valueOf(pr.number)
}
}
return openPRs
}
def getDownstreamProjectOverrides(project, id, downstreamProject, botName = '@fabric8cd') {
if (!downstreamProject) {
error 'no downstreamProjects provided'
}
def flow = new Fabric8Commands()
def comments = flow.getIssueComments(project, id)
// start by looking at the most recent commments and work back
Collections.reverse(comments)
for (comment in comments) {
echo "Found PR comment ${comment.body}"
def text = comment.body.trim()
def match = 'CI downstream projects'
if (text.startsWith(botName)) {
if (text.contains(match)) {
def result = text.substring(text.indexOf("[") + 1, text.indexOf("]"))
if (!result) {
echo 'no downstream projects found'
}
def list = result.split(',')
for (repos in list) {
if (!repos.contains('=')) {
error 'no override project found in the form organisation=foo'
}
def overrides = repos.split('=')
if (downstreamProject == overrides[0].trim()) {
"matched and returning ${overrides[1].trim()}"
return overrides[1].trim()
}
}
}
}
}
}
def hasPRComment(project, id, match) {
def flow = new Fabric8Commands()
def comments = flow.getIssueComments(project, id)
// start by looking at the most recent commments and work back
Collections.reverse(comments)
for (comment in comments) {
echo "Found PR comment ${comment.body}"
def text = comment.body.trim()
if (text.equalsIgnoreCase(match)) {
return true
}
}
return false
}
def getDownstreamProjectOverrides(downstreamProject, botName = '@fabric8cd') {
def flow = new Fabric8Commands()
def id = env.CHANGE_ID
if (!id) {
error 'no env.CHANGE_ID / pull request id found'
}
def project = getRepoName()
return getDownstreamProjectOverrides(project, id, downstreamProject, botName = '@fabric8cd')
}
def isSkipCIDeploy(botName = '@fabric8cd') {
def id = env.CHANGE_ID
if (!id) {
error 'no env.CHANGE_ID / pull request id found'
}
def flow = new Fabric8Commands()
def project = getRepoName()
def comments = flow.getIssueComments(project, id)
// start by looking at the most recent commments and work back
Collections.reverse(comments)
for (comment in comments) {
echo comment.body
def text = comment.body.trim()
def skipTrue = 'CI skip deploy=true'
def skipFalse = 'CI skip deploy=false'
if (text.startsWith(botName)) {
if (text.contains(skipTrue)) {
return true
} else if (text.contains(skipFalse)) {
return false
}
}
}
}
// helper to get the repo name from the job name when using org + branch github plugins
def getRepoName() {
def jobName = env.JOB_NAME
// job name from the org plugin
if (jobName.count('/') > 1) {
return jobName.substring(jobName.indexOf('/') + 1, jobName.lastIndexOf('/'))
}
// job name from the branch plugin
if (jobName.count('/') > 0) {
return jobName.substring(0, jobName.lastIndexOf('/'))
}
// normal job name
return jobName
}
// helper to get resource name that can be used as an OpenShift and Kubernetes identifier
def getResourceName() {
def name = env.JOB_NAME
// only lower case alphanumeric. "-" and "." are allowed for Kubernetes name with max lenght of 253
name = name.replaceAll("/", "-").toLowerCase()
name = name.replaceAll(/[^a-z0-9\.-]/, "").take(254)
return name
}
@NonCPS
def getOpenShiftBuildName() {
def activeInstance = Jenkins.getActiveInstance()
def job = (WorkflowJob) activeInstance.getItemByFullName(env.JOB_NAME)
def run = job.getBuildByNumber(Integer.parseInt(env.BUILD_NUMBER))
def flow = new Fabric8Commands()
if (flow.isOpenShift()) {
def clazz = Thread.currentThread().getContextClassLoader().loadClass("io.fabric8.jenkins.openshiftsync.BuildCause")
def cause = run.getCause(clazz)
if (cause != null) {
return cause.name
}
}
return null
}
return this