This repository has been archived by the owner on Nov 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.gradle.kts
241 lines (200 loc) · 6.07 KB
/
build.gradle.kts
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
import de.undercouch.gradle.tasks.download.Download
import org.apache.tools.ant.taskdefs.condition.Os.*
import java.io.ByteArrayOutputStream
plugins {
id("de.undercouch.download") version "5.0.4"
}
val registryName = "local-registry"
val clusterName = "eshop-cluster"
var kindPath: String = project.file("build/download/kind").absolutePath
var ctlPath: String = project.file("build/download/kubectl").absolutePath
/**
* Lädt die "Kind" application runter
*/
val downloadKind = tasks.register<Download>("downloadKind") {
group = "internal"
val type = if (isFamily(FAMILY_MAC)) {
"darwin"
} else if (isFamily(FAMILY_UNIX)) {
"linux"
} else {
kindPath = "$kindPath.exe"
"windows"
}
src("https://kind.sigs.k8s.io/dl/v0.13.0/kind-$type-amd64")
dest(kindPath)
doLast {
if (type != "windows") {
exec {
executable = "chmod"
args("+x", kindPath)
}
}
}
onlyIf {
!project.file(kindPath).exists()
}
}
/**
* Lädt das CLI-Program für Kubernetes herunter
*/
val downloadKubectl = tasks.register<Download>("downloadKubectl") {
group = "internal"
val type = if (isFamily(FAMILY_MAC)) {
"darwin"
} else if (isFamily(FAMILY_UNIX)) {
"linux"
} else {
ctlPath = "$ctlPath.exe"
"windows"
}
if (type == "windows") {
src("https://dl.k8s.io/release/v1.24.0/bin/windows/amd64/kubectl.exe")
} else {
src("https://dl.k8s.io/release/v1.24.0/bin/$type/amd64/kubectl")
}
dest(ctlPath)
doLast {
if (type != "windows") {
exec {
executable = "chmod"
args("+x", ctlPath)
}
}
}
onlyIf {
!project.file(ctlPath).exists()
}
}
/**
* Konfiguriert die Ingress Implementiertung "NGINX"
*/
val configureNGINX = tasks.register<Exec>("configureNGINX") {
group = "internal"
dependsOn(createKindCluster, downloadKubectl)
executable = ctlPath
args("apply", "-f", project.file("kubernetes/deploy.yaml").absolutePath)
}
/**
* Erstellt eine lokale Docker-Registry, in dem unsere Images landen
*/
val createRegistry = tasks.register<Exec>("createRegistry") {
group = "internal"
executable = "docker"
args("run", "-d", "-p", "127.0.0.1:5001:5000", "--restart=always", "--name", registryName, "registry:2")
//Checks if a registry is already registered
onlyIf {
val out = ByteArrayOutputStream()
exec {
executable = "docker"
args("container", "ls")
standardOutput = out
}
!String(out.toByteArray()).contains(registryName)
}
}
/**
* Erstellt den Kubernetes Cluster via Kind und lädt grundlegende Konfigurationen
*/
val createKindCluster = tasks.register<Exec>("createKindCluster") {
dependsOn(downloadKind, downloadKubectl, createRegistry)
group = "internal"
val configPath = project.file("kubernetes/kind_config.yaml").absolutePath
executable = kindPath
args("create", "cluster", "--name", clusterName, "--config", configPath)
doLast {
exec {
executable = ctlPath
args("apply", "-f", project.file("kubernetes/kind_configmap.yaml").absolutePath)
}
exec {
executable = ctlPath
args("label", "nodes", "eshop-cluster-worker", "ingress-ready=true")
}
}
}
/**
* Verbindet das Netzwerk des Clusters mit dem Netzwerk unserer lokalen Registry
*/
val kindNetwork = tasks.register<Exec>("connectKindToKindNetwork") {
dependsOn(createKindCluster)
group = "internal"
executable = "docker"
args("network", "connect", "kind", registryName)
}
/**
* Erstellt den kompletten Kubernetes Cluster
*/
tasks.register("createCluster") {
dependsOn(pushImages, kindNetwork, configureNGINX, downloadKubectl)
group = "deployment"
doLast {
exec {
executable = ctlPath
args("apply", "-f", project.file("kubernetes/postgres.yaml").absolutePath)
}
exec {
executable = ctlPath
args("rollout", "status", "deployment/postgres")
}
exec {
executable = ctlPath
args("apply", "-f", project.file("kubernetes/price.yaml").absolutePath)
}
exec {
executable = ctlPath
args("apply", "-f", project.file("kubernetes/products.yaml").absolutePath)
}
exec {
executable = ctlPath
args("apply", "-f", project.file("kubernetes/frontend.yaml").absolutePath)
}
try {
exec {
executable = ctlPath
args("apply", "-f", project.file("kubernetes/ingress.yaml").absolutePath)
}
}catch(ignored :Throwable){
exec {
executable = ctlPath
args("delete", "-A", "ValidatingWebhookConfiguration", "ingress-nginx-admission")
}
//If you at first don't succeed, just try again
//this legit works, sometimes the validation Webhook just decides it doesn't want to work today
exec {
executable = ctlPath
args("apply", "-f", project.file("kubernetes/ingress.yaml").absolutePath)
}
}
}
}
/**
* Entfernt die lokale Registry
*/
val deleteRegistry = tasks.register<Exec>("deleteRegistry") {
group = "internal"
executable = "docker"
args("stop", registryName)
doLast {
exec {
executable = "docker"
args("rm", registryName)
}
}
}
/**
* Entfernt den Kubernetes Cluster
*/
val deleteCluster = tasks.register<Exec>("deleteCluster") {
dependsOn(deleteRegistry, downloadKind)
group = "deployment"
executable = kindPath
args("delete", "cluster", "--name", clusterName)
}
/**
* Pusht alle Docker-Container in die lokale Registry (Sammeltask)
*/
val pushImages = tasks.register("pushImages") {
dependsOn(":products:publishContainer", ":prices:publishContainer", ":frontend:dockerPush")
group = "internal"
}