forked from devfile/library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerators.go
448 lines (371 loc) · 14.5 KB
/
generators.go
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
package generator
import (
"fmt"
v1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/devfile/library/pkg/devfile/parser"
"github.com/devfile/library/pkg/devfile/parser/data/v2/common"
"github.com/devfile/library/pkg/util"
buildv1 "github.com/openshift/api/build/v1"
imagev1 "github.com/openshift/api/image/v1"
routev1 "github.com/openshift/api/route/v1"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
extensionsv1 "k8s.io/api/extensions/v1beta1"
networkingv1 "k8s.io/api/networking/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
// DevfileSourceVolumeMount is the default directory to mount the volume in the container
DevfileSourceVolumeMount = "/projects"
// EnvProjectsRoot is the env defined for project mount in a component container when component's mountSources=true
EnvProjectsRoot = "PROJECTS_ROOT"
// EnvProjectsSrc is the env defined for path to the project source in a component container
EnvProjectsSrc = "PROJECT_SOURCE"
deploymentKind = "Deployment"
deploymentAPIVersion = "apps/v1"
containerNameMaxLen = 55
)
// GetTypeMeta gets a type meta of the specified kind and version
func GetTypeMeta(kind string, APIVersion string) metav1.TypeMeta {
return metav1.TypeMeta{
Kind: kind,
APIVersion: APIVersion,
}
}
// GetObjectMeta gets an object meta with the parameters
func GetObjectMeta(name, namespace string, labels, annotations map[string]string) metav1.ObjectMeta {
objectMeta := metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: labels,
Annotations: annotations,
}
return objectMeta
}
// GetContainers iterates through all container components, filters out init containers and returns corresponding containers
func GetContainers(devfileObj parser.DevfileObj, options common.DevfileOptions) ([]corev1.Container, error) {
allContainers, err := getAllContainers(devfileObj, options)
if err != nil {
return nil, err
}
// filter out containers for preStart and postStop events
preStartEvents := devfileObj.Data.GetEvents().PreStart
postStopEvents := devfileObj.Data.GetEvents().PostStop
if len(preStartEvents) > 0 || len(postStopEvents) > 0 {
var eventCommands []string
commands, err := devfileObj.Data.GetCommands(common.DevfileOptions{})
if err != nil {
return nil, err
}
commandsMap := common.GetCommandsMap(commands)
for _, event := range preStartEvents {
eventSubCommands := common.GetCommandsFromEvent(commandsMap, event)
eventCommands = append(eventCommands, eventSubCommands...)
}
for _, event := range postStopEvents {
eventSubCommands := common.GetCommandsFromEvent(commandsMap, event)
eventCommands = append(eventCommands, eventSubCommands...)
}
for _, commandName := range eventCommands {
command, _ := commandsMap[commandName]
component := common.GetApplyComponent(command)
// Get the container info for the given component
for i, container := range allContainers {
if container.Name == component {
allContainers = append(allContainers[:i], allContainers[i+1:]...)
}
}
}
}
return allContainers, nil
}
// GetInitContainers gets the init container for every preStart devfile event
func GetInitContainers(devfileObj parser.DevfileObj) ([]corev1.Container, error) {
containers, err := getAllContainers(devfileObj, common.DevfileOptions{})
if err != nil {
return nil, err
}
preStartEvents := devfileObj.Data.GetEvents().PreStart
var initContainers []corev1.Container
if len(preStartEvents) > 0 {
var eventCommands []string
commands, err := devfileObj.Data.GetCommands(common.DevfileOptions{})
if err != nil {
return nil, err
}
commandsMap := common.GetCommandsMap(commands)
for _, event := range preStartEvents {
eventSubCommands := common.GetCommandsFromEvent(commandsMap, event)
eventCommands = append(eventCommands, eventSubCommands...)
}
for i, commandName := range eventCommands {
command, _ := commandsMap[commandName]
component := common.GetApplyComponent(command)
// Get the container info for the given component
for _, container := range containers {
if container.Name == component {
// Override the init container name since there cannot be two containers with the same
// name in a pod. This applies to pod containers and pod init containers. The convention
// for init container name here is, containername-eventname-<position of command in prestart events>
// If there are two events referencing the same devfile component, then we will have
// tools-event1-1 & tools-event2-3, for example. And if in the edge case, the same command is
// executed twice by preStart events, then we will have tools-event1-1 & tools-event1-2
initContainerName := fmt.Sprintf("%s-%s", container.Name, commandName)
initContainerName = util.TruncateString(initContainerName, containerNameMaxLen)
initContainerName = fmt.Sprintf("%s-%d", initContainerName, i+1)
container.Name = initContainerName
initContainers = append(initContainers, container)
}
}
}
}
return initContainers, nil
}
// DeploymentParams is a struct that contains the required data to create a deployment object
type DeploymentParams struct {
TypeMeta metav1.TypeMeta
ObjectMeta metav1.ObjectMeta
InitContainers []corev1.Container
Containers []corev1.Container
Volumes []corev1.Volume
PodSelectorLabels map[string]string
Replicas *int32
}
// GetDeployment gets a deployment object
func GetDeployment(devfileObj parser.DevfileObj, deployParams DeploymentParams) (*appsv1.Deployment, error) {
podTemplateSpecParams := podTemplateSpecParams{
ObjectMeta: deployParams.ObjectMeta,
InitContainers: deployParams.InitContainers,
Containers: deployParams.Containers,
Volumes: deployParams.Volumes,
}
deploySpecParams := deploymentSpecParams{
PodTemplateSpec: *getPodTemplateSpec(podTemplateSpecParams),
PodSelectorLabels: deployParams.PodSelectorLabels,
Replicas: deployParams.Replicas,
}
containerAnnotations, err := getContainerAnnotations(devfileObj, common.DevfileOptions{})
if err != nil {
return nil, err
}
deployParams.ObjectMeta.Annotations = mergeMaps(deployParams.ObjectMeta.Annotations, containerAnnotations.Deployment)
deployment := &appsv1.Deployment{
TypeMeta: deployParams.TypeMeta,
ObjectMeta: deployParams.ObjectMeta,
Spec: *getDeploymentSpec(deploySpecParams),
}
return deployment, nil
}
// PVCParams is a struct to create PVC
type PVCParams struct {
TypeMeta metav1.TypeMeta
ObjectMeta metav1.ObjectMeta
Quantity resource.Quantity
}
// GetPVC returns a PVC
func GetPVC(pvcParams PVCParams) *corev1.PersistentVolumeClaim {
pvcSpec := getPVCSpec(pvcParams.Quantity)
pvc := &corev1.PersistentVolumeClaim{
TypeMeta: pvcParams.TypeMeta,
ObjectMeta: pvcParams.ObjectMeta,
Spec: *pvcSpec,
}
return pvc
}
// ServiceParams is a struct that contains the required data to create a service object
type ServiceParams struct {
TypeMeta metav1.TypeMeta
ObjectMeta metav1.ObjectMeta
SelectorLabels map[string]string
}
// GetService gets the service
func GetService(devfileObj parser.DevfileObj, serviceParams ServiceParams, options common.DevfileOptions) (*corev1.Service, error) {
serviceSpec, err := getServiceSpec(devfileObj, serviceParams.SelectorLabels, options)
if err != nil {
return nil, err
}
containerAnnotations, err := getContainerAnnotations(devfileObj, options)
if err != nil {
return nil, err
}
serviceParams.ObjectMeta.Annotations = mergeMaps(serviceParams.ObjectMeta.Annotations, containerAnnotations.Service)
service := &corev1.Service{
TypeMeta: serviceParams.TypeMeta,
ObjectMeta: serviceParams.ObjectMeta,
Spec: *serviceSpec,
}
return service, nil
}
// IngressParams is a struct that contains the required data to create an ingress object
type IngressParams struct {
TypeMeta metav1.TypeMeta
ObjectMeta metav1.ObjectMeta
IngressSpecParams IngressSpecParams
}
// GetIngress gets an ingress
func GetIngress(endpoint v1.Endpoint, ingressParams IngressParams) *extensionsv1.Ingress {
ingressSpec := getIngressSpec(ingressParams.IngressSpecParams)
ingressParams.ObjectMeta.Annotations = mergeMaps(ingressParams.ObjectMeta.Annotations, endpoint.Annotations)
ingress := &extensionsv1.Ingress{
TypeMeta: ingressParams.TypeMeta,
ObjectMeta: ingressParams.ObjectMeta,
Spec: *ingressSpec,
}
return ingress
}
// GetNetworkingV1Ingress gets a networking v1 ingress
func GetNetworkingV1Ingress(endpoint v1.Endpoint, ingressParams IngressParams) *networkingv1.Ingress {
ingressSpec := getNetworkingV1IngressSpec(ingressParams.IngressSpecParams)
ingressParams.ObjectMeta.Annotations = mergeMaps(ingressParams.ObjectMeta.Annotations, endpoint.Annotations)
ingress := &networkingv1.Ingress{
TypeMeta: ingressParams.TypeMeta,
ObjectMeta: ingressParams.ObjectMeta,
Spec: *ingressSpec,
}
return ingress
}
// RouteParams is a struct that contains the required data to create a route object
type RouteParams struct {
TypeMeta metav1.TypeMeta
ObjectMeta metav1.ObjectMeta
RouteSpecParams RouteSpecParams
}
// GetRoute gets a route
func GetRoute(endpoint v1.Endpoint, routeParams RouteParams) *routev1.Route {
routeSpec := getRouteSpec(routeParams.RouteSpecParams)
routeParams.ObjectMeta.Annotations = mergeMaps(routeParams.ObjectMeta.Annotations, endpoint.Annotations)
route := &routev1.Route{
TypeMeta: routeParams.TypeMeta,
ObjectMeta: routeParams.ObjectMeta,
Spec: *routeSpec,
}
return route
}
// GetOwnerReference generates an ownerReference from the deployment which can then be set as
// owner for various Kubernetes objects and ensure that when the owner object is deleted from the
// cluster, all other objects are automatically removed by Kubernetes garbage collector
func GetOwnerReference(deployment *appsv1.Deployment) metav1.OwnerReference {
ownerReference := metav1.OwnerReference{
APIVersion: deploymentAPIVersion,
Kind: deploymentKind,
Name: deployment.Name,
UID: deployment.UID,
}
return ownerReference
}
// BuildConfigParams is a struct that contains the required data to create a build config object
type BuildConfigParams struct {
TypeMeta metav1.TypeMeta
ObjectMeta metav1.ObjectMeta
BuildConfigSpecParams BuildConfigSpecParams
}
// GetBuildConfig gets a build config
func GetBuildConfig(buildConfigParams BuildConfigParams) *buildv1.BuildConfig {
buildConfigSpec := getBuildConfigSpec(buildConfigParams.BuildConfigSpecParams)
buildConfig := &buildv1.BuildConfig{
TypeMeta: buildConfigParams.TypeMeta,
ObjectMeta: buildConfigParams.ObjectMeta,
Spec: *buildConfigSpec,
}
return buildConfig
}
// GetSourceBuildStrategy gets the source build strategy
func GetSourceBuildStrategy(imageName, imageNamespace string) buildv1.BuildStrategy {
return buildv1.BuildStrategy{
SourceStrategy: &buildv1.SourceBuildStrategy{
From: corev1.ObjectReference{
Kind: "ImageStreamTag",
Name: imageName,
Namespace: imageNamespace,
},
},
}
}
// GetDockerBuildStrategy gets the docker build strategy
func GetDockerBuildStrategy(dockerfilePath string, env []corev1.EnvVar) buildv1.BuildStrategy {
return buildv1.BuildStrategy{
Type: buildv1.DockerBuildStrategyType,
DockerStrategy: &buildv1.DockerBuildStrategy{
DockerfilePath: dockerfilePath,
Env: env,
},
}
}
// ImageStreamParams is a struct that contains the required data to create an image stream object
type ImageStreamParams struct {
TypeMeta metav1.TypeMeta
ObjectMeta metav1.ObjectMeta
}
// GetImageStream is a function to return the image stream
func GetImageStream(imageStreamParams ImageStreamParams) imagev1.ImageStream {
imageStream := imagev1.ImageStream{
TypeMeta: imageStreamParams.TypeMeta,
ObjectMeta: imageStreamParams.ObjectMeta,
}
return imageStream
}
// VolumeInfo is a struct to hold the pvc name and the volume name to create a volume.
type VolumeInfo struct {
PVCName string
VolumeName string
}
// VolumeParams is a struct that contains the required data to create Kubernetes Volumes and mount Volumes in Containers
type VolumeParams struct {
// Containers is a list of containers that needs to be updated for the volume mounts
Containers []corev1.Container
// VolumeNameToVolumeInfo is a map of the devfile volume name to the volume info containing the pvc name and the volume name.
VolumeNameToVolumeInfo map[string]VolumeInfo
}
// GetVolumesAndVolumeMounts gets the PVC volumes and updates the containers with the volume mounts.
func GetVolumesAndVolumeMounts(devfileObj parser.DevfileObj, volumeParams VolumeParams, options common.DevfileOptions) ([]corev1.Volume, error) {
options.ComponentOptions = common.ComponentOptions{
ComponentType: v1.ContainerComponentType,
}
containerComponents, err := devfileObj.Data.GetComponents(options)
if err != nil {
return nil, err
}
options.ComponentOptions = common.ComponentOptions{
ComponentType: v1.VolumeComponentType,
}
volumeComponent, err := devfileObj.Data.GetComponents(options)
if err != nil {
return nil, err
}
var pvcVols []corev1.Volume
for volName, volInfo := range volumeParams.VolumeNameToVolumeInfo {
emptyDirVolume := false
for _, volumeComp := range volumeComponent {
if volumeComp.Name == volName && *volumeComp.Volume.Ephemeral {
emptyDirVolume = true
break
}
}
// if `ephemeral=true`, a volume with emptyDir should be created
if emptyDirVolume {
pvcVols = append(pvcVols, getEmptyDirVol(volInfo.VolumeName))
} else {
pvcVols = append(pvcVols, getPVC(volInfo.VolumeName, volInfo.PVCName))
}
// containerNameToMountPaths is a map of the Devfile container name to their Devfile Volume Mount Paths for a given Volume Name
containerNameToMountPaths := make(map[string][]string)
for _, containerComp := range containerComponents {
for _, volumeMount := range containerComp.Container.VolumeMounts {
if volName == volumeMount.Name {
containerNameToMountPaths[containerComp.Name] = append(containerNameToMountPaths[containerComp.Name], GetVolumeMountPath(volumeMount))
}
}
}
addVolumeMountToContainers(volumeParams.Containers, volInfo.VolumeName, containerNameToMountPaths)
}
return pvcVols, nil
}
// GetVolumeMountPath gets the volume mount's path.
func GetVolumeMountPath(volumeMount v1.VolumeMount) string {
// if there is no volume mount path, default to volume mount name as per devfile schema
if volumeMount.Path == "" {
volumeMount.Path = "/" + volumeMount.Name
}
return volumeMount.Path
}