-
Notifications
You must be signed in to change notification settings - Fork 210
/
getDeploymentResources.groovy
223 lines (210 loc) · 5.47 KB
/
getDeploymentResources.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
#!/usr/bin/groovy
import io.fabric8.Fabric8Commands
import io.fabric8.Utils
def call(body) {
// evaluate the body block, and collect configuration into the object
def config = [:]
body.resolveStrategy = Closure.DELEGATE_FIRST
body.delegate = config
body()
def flow = new Fabric8Commands()
def utils = new Utils()
def resourceName = utils.getResourceName()
def expose = config.exposeApp ?: 'true'
def requestCPU = config.resourceRequestCPU ?: '0'
def requestMemory = config.resourceRequestMemory ?: '0'
def limitCPU = config.resourceLimitMemory ?: '0'
def limitMemory = config.resourceLimitMemory ?: '0'
def yaml
def isSha = ''
if (flow.isOpenShift()) {
isSha = utils.getImageStreamSha(resourceName)
}
def fabric8Registry = ''
if (env.FABRIC8_DOCKER_REGISTRY_SERVICE_HOST) {
fabric8Registry = env.FABRIC8_DOCKER_REGISTRY_SERVICE_HOST + ':' + env.FABRIC8_DOCKER_REGISTRY_SERVICE_PORT + '/'
}
def sha
def list = """
---
apiVersion: v1
kind: List
items:
"""
def service = """
- apiVersion: v1
kind: Service
metadata:
annotations:
fabric8.io/iconUrl: ${config.icon}
labels:
provider: fabric8
project: ${resourceName}
expose: '${expose}'
version: ${config.version}
group: quickstart
name: ${resourceName}
spec:
ports:
- port: 80
protocol: TCP
targetPort: ${config.port}
selector:
project: ${resourceName}
provider: fabric8
group: quickstart
"""
def deployment = """
- apiVersion: extensions/v1beta1
kind: Deployment
metadata:
annotations:
fabric8.io/iconUrl: ${config.icon}
labels:
provider: fabric8
project: ${resourceName}
version: ${config.version}
group: quickstart
name: ${resourceName}
spec:
replicas: 1
selector:
matchLabels:
provider: fabric8
project: ${resourceName}
group: quickstart
template:
metadata:
labels:
provider: fabric8
project: ${resourceName}
version: ${config.version}
group: quickstart
spec:
containers:
- env:
- name: KUBERNETES_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
image: ${fabric8Registry}${env.KUBERNETES_NAMESPACE}/${env.JOB_NAME}:${config.version}
imagePullPolicy: IfNotPresent
name: ${resourceName}
ports:
- containerPort: ${config.port}
name: http
resources:
limits:
cpu: ${requestCPU}
memory: ${requestMemory}
requests:
cpu: ${limitCPU}
memory: ${limitMemory}
readinessProbe:
httpGet:
path: "/"
port: ${config.port}
initialDelaySeconds: 1
timeoutSeconds: 5
failureThreshold: 5
livenessProbe:
httpGet:
path: "/"
port: ${config.port}
initialDelaySeconds: 180
timeoutSeconds: 5
failureThreshold: 5
terminationGracePeriodSeconds: 2
"""
def deploymentConfig = """
- apiVersion: v1
kind: DeploymentConfig
metadata:
annotations:
fabric8.io/iconUrl: ${config.icon}
labels:
provider: fabric8
project: ${resourceName}
version: ${config.version}
group: quickstart
name: ${resourceName}
spec:
replicas: 1
selector:
provider: fabric8
project: ${resourceName}
group: quickstart
template:
metadata:
labels:
provider: fabric8
project: ${resourceName}
version: ${config.version}
group: quickstart
spec:
containers:
- env:
- name: KUBERNETES_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
image: ${resourceName}:${config.version}
imagePullPolicy: IfNotPresent
name: ${resourceName}
ports:
- containerPort: ${config.port}
name: http
resources:
limits:
cpu: ${requestCPU}
memory: ${requestMemory}
requests:
cpu: ${limitCPU}
memory: ${limitMemory}
readinessProbe:
httpGet:
path: "/"
port: ${config.port}
initialDelaySeconds: 1
timeoutSeconds: 5
failureThreshold: 5
livenessProbe:
httpGet:
path: "/"
port: ${config.port}
initialDelaySeconds: 180
timeoutSeconds: 5
failureThreshold: 5
terminationGracePeriodSeconds: 2
triggers:
- type: ConfigChange
- imageChangeParams:
automatic: true
containerNames:
- ${resourceName}
from:
kind: ImageStreamTag
name: ${resourceName}:${config.version}
type: ImageChange
"""
def is = """
- apiVersion: v1
kind: ImageStream
metadata:
name: ${resourceName}
spec:
tags:
- from:
kind: ImageStreamImage
name: ${resourceName}@${isSha}
namespace: ${utils.getNamespace()}
name: ${config.version}
"""
if (flow.isOpenShift()) {
yaml = list + service + is + deploymentConfig
} else {
yaml = list + service + deployment
}
echo 'using resources:\n' + yaml
return yaml
}