-
Notifications
You must be signed in to change notification settings - Fork 0
/
kubernetes.groovy
112 lines (93 loc) · 3.25 KB
/
kubernetes.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
class Kubernetes {
private pipeline;
private example
def Kubernetes(pipeline, example) {
this.pipeline = pipeline;
this.example = example
}
def runTest(def assertion) {
if (! hasKubernetes()) {
return
}
try {
pipeline.echo "==========================================================="
pipeline.echo "START TESTING KUBERNETES EXAMPLE $example"
kustomize()
kubernetesApply()
kubernetesForwardPort()
waitUntilIvyIsRunning()
def consoleLog = getIvyConsoleLog()
assertion.call(consoleLog)
assertNoErrorOrWarnInIvyLog(consoleLog)
} catch (ex) {
pipeline.echo ex.getMessage()
pipeline.currentBuild.result = 'UNSTABLE'
def log = "warn-kubernetes-${example}.log"
pipeline.sh "echo SAMPLE ${example} FAILED >> ${log}"
pipeline.sh "echo =========================================================== >> ${log}"
pipeline.sh "echo \"Error Message: ${ex.getMessage()}\" >> ${log}"
pipeline.sh "echo =========================================================== >> ${log}"
pipeline.sh "echo KUBERNETES IVY-ENGINE POD LOG: >> ${log}"
pipeline.sh "kubectl logs -l app=ivy-engine --tail=-1 >> ${log}"
throw ex
} finally {
pipeline.echo getIvyConsoleLog()
kubernetesDelete()
pipeline.echo "==========================================================="
}
}
def hasKubernetes() {
def exitCode = pipeline.sh script: "test -f $example/base/kustomization.yaml", returnStatus: true
return exitCode == 0;
}
def kustomize() {
pipeline.sh "kubectl kustomize --load-restrictor=\"LoadRestrictionsNone\" $example/base > $example/kubernetes.yaml"
}
def kubernetesApply() {
pipeline.sh "kubectl apply -f $example/kubernetes.yaml"
}
def kubernetesDelete() {
pipeline.sh "kubectl delete -f $example/kubernetes.yaml"
}
def kubernetesForwardPort() {
waitUntilPodIsRunning()
waitUntilPortIsBound()
pipeline.sh "kubectl port-forward deployment/ivy-engine 8080:8080 &"
}
def waitUntilPodIsRunning() {
pipeline.timeout(2) {
pipeline.waitUntil {
def stdOut = pipeline.sh script: "kubectl get pods -l app=ivy-engine", returnStdout: true
return stdOut.contains("Running")
}
}
}
def waitUntilPortIsBound() {
pipeline.timeout(2) {
pipeline.waitUntil {
def log = getIvyConsoleLog()
return log.contains("Web Server")
}
}
}
def waitUntilIvyIsRunning() {
pipeline.timeout(2) {
pipeline.waitUntil {
def exitCode = pipeline.sh script: "wget -t 1 -q http://localhost:8080/ -O /dev/null", returnStatus: true
return exitCode == 0
}
}
}
def getIvyConsoleLog() {
return pipeline.sh (script: "kubectl logs -l app=ivy-engine --tail=-1", returnStdout: true)
}
def assertNoErrorOrWarnInIvyLog(log) {
if (log.contains("WARN") || log.contains("ERROR")) {
throw new Exception("console log of ivy contains WARN/ERROR messages");
}
}
}
def Kubernetes newKubernetes(example) {
return new Kubernetes(this, example)
}
return this