devfile 0.0.12.pre.alpha1-x86_64-linux → 0.0.14.pre.alpha1-x86_64-linux

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/bin/devfile +0 -0
  3. data/ext/devfile.go +25 -74
  4. data/ext/volume.go +118 -0
  5. metadata +2 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1cc3363280657ceecbb55931395fc354e8d13cb814c2a83b11dd679e0a67edd6
4
- data.tar.gz: b8bdad6991ddc7a87cd72c49fc2d684f8fe055245ae8747136e08df8608a7bd0
3
+ metadata.gz: 383ba06875e6f3c5ffa91aff57a88957b18486ac4bc08e18503a7e23e2386c0c
4
+ data.tar.gz: 80a6afb162202a4717312ef2b561c21e906a2753db9e6d530762129e83814306
5
5
  SHA512:
6
- metadata.gz: 928d238a1651d90afb2d1e81e8eafbfbbbb421487a51a21eba8b5262f6649619fcedcbc25ea1bcb4db2632e6e011491eb3abc41bd4736fc3540f2c14fe3b6ef6
7
- data.tar.gz: 2dec36f8b4bd994a41cc3b20c2fb7099b54558dc1864dcd80b13594a036d84c55932d688dc595b38652c0479329e616ee92090f09a8fb0388877b638d1213a47
6
+ metadata.gz: 6d2f882ed4c62d2ed9f22a72e1d5d75cd6832fa847c4aad0059ff9a593fc7b8ac76df9f5f8c0fcb833053e5fc0fb53c9bbf111b706955f7a43cdc0dd186aa2b0
7
+ data.tar.gz: 523d788b7ce4dd99681a14bddd3789d1f854d27fc8ae7fb4cbe0a03ce01adacbae534e096431d73d19112530defc52c7b09983430821683dfaa83edfcbdc52ed
data/bin/devfile CHANGED
Binary file
data/ext/devfile.go CHANGED
@@ -2,8 +2,6 @@ package main
2
2
 
3
3
  import (
4
4
  "bytes"
5
- "fmt"
6
- "sort"
7
5
  "strconv"
8
6
  "text/template"
9
7
 
@@ -24,14 +22,21 @@ type Devfile struct {
24
22
  devfileObj parser.DevfileObj
25
23
  }
26
24
 
27
- type volumeOptions struct {
28
- Info generator.VolumeInfo
29
- Size string
30
- IsEphemeral bool
25
+ type pvcOptions struct {
26
+ Name string
27
+ Size string
31
28
  }
32
29
 
33
30
  func (d Devfile) getDeployment(name, namespace string, labels, annotations map[string]string, replicas int) (*appsv1.Deployment, error) {
34
- containers, initContainers, volumes, _, err := d.getContainersAndVolumes(name)
31
+ containers, err := generator.GetContainers(d.devfileObj, common.DevfileOptions{})
32
+ if err != nil {
33
+ return nil, err
34
+ }
35
+ initContainers, err := generator.GetInitContainers(d.devfileObj)
36
+ if err != nil {
37
+ return nil, err
38
+ }
39
+ volumes, _, err := d.getVolumesAndVolumeMounts(containers, initContainers, name)
35
40
  if err != nil {
36
41
  return nil, err
37
42
  }
@@ -147,24 +152,29 @@ func (d Devfile) getIngress(name, namespace string, labels, annotations map[stri
147
152
  }
148
153
 
149
154
  func (d Devfile) getPVC(name, namespace string, labels, annotations map[string]string) ([]*corev1.PersistentVolumeClaim, error) {
150
- _, _, volumes, volumeNameToVolumeOptions, err := d.getContainersAndVolumes(name)
155
+ containers, err := generator.GetContainers(d.devfileObj, common.DevfileOptions{})
156
+ if err != nil {
157
+ return nil, err
158
+ }
159
+ initContainers, err := generator.GetInitContainers(d.devfileObj)
160
+ if err != nil {
161
+ return nil, err
162
+ }
163
+ _, pvcNameToPvcOptions, err := d.getVolumesAndVolumeMounts(containers, initContainers, name)
151
164
  if err != nil {
152
165
  return nil, err
153
166
  }
167
+
154
168
  pvcs := make([]*corev1.PersistentVolumeClaim, 0)
155
- for _, volume := range volumes {
156
- volumeOptions := volumeNameToVolumeOptions[volume.Name]
157
- if volumeOptions.IsEphemeral {
158
- continue
159
- }
160
- quantity, err := resource.ParseQuantity(volumeOptions.Size)
169
+ for _, options := range pvcNameToPvcOptions {
170
+ quantity, err := resource.ParseQuantity(options.Size)
161
171
  if err != nil {
162
172
  return nil, err
163
173
  }
164
174
  pvcParams := generator.PVCParams{
165
175
  TypeMeta: generator.GetTypeMeta("PersistentVolumeClaim", "v1"),
166
176
  ObjectMeta: metav1.ObjectMeta{
167
- Name: volumeOptions.Info.PVCName,
177
+ Name: options.Name,
168
178
  Namespace: namespace,
169
179
  Labels: labels,
170
180
  Annotations: annotations,
@@ -212,65 +222,6 @@ func (d Devfile) getAll(name, namespace string, labels, annotations map[string]s
212
222
  return result, nil
213
223
  }
214
224
 
215
- func (d Devfile) getContainersAndVolumes(name string) ([]corev1.Container, []corev1.Container, []corev1.Volume, map[string]volumeOptions, error) {
216
- containers, err := generator.GetContainers(d.devfileObj, common.DevfileOptions{})
217
- if err != nil {
218
- return nil, nil, nil, nil, err
219
- }
220
- initContainers, err := generator.GetInitContainers(d.devfileObj)
221
- if err != nil {
222
- return nil, nil, nil, nil, err
223
- }
224
- allContainers := append(containers, initContainers...)
225
-
226
- volumeComponents, err := d.devfileObj.Data.GetDevfileVolumeComponents(common.DevfileOptions{})
227
- if err != nil {
228
- return nil, nil, nil, nil, err
229
- }
230
- volumeNameToVolumeOptions := map[string]volumeOptions{}
231
- volumeNameToVolumeInfo := map[string]generator.VolumeInfo{}
232
- for _, volumeComponent := range volumeComponents {
233
- info := generator.VolumeInfo{
234
- PVCName: fmt.Sprintf("%s-%s", name, volumeComponent.Name),
235
- VolumeName: volumeComponent.Name,
236
- }
237
- volumeNameToVolumeInfo[volumeComponent.Name] = info
238
- volumeNameToVolumeOptions[volumeComponent.Name] = volumeOptions{
239
- Info: info,
240
- Size: volumeComponent.Volume.Size,
241
- IsEphemeral: *volumeComponent.Volume.Ephemeral,
242
- }
243
- }
244
-
245
- volumeParams := generator.VolumeParams{
246
- Containers: allContainers,
247
- VolumeNameToVolumeInfo: volumeNameToVolumeInfo,
248
- }
249
- options := common.DevfileOptions{}
250
- // "containers" and "initContainers" are updated in place with the volume mounts parameters
251
- // after the following function is called
252
- volumes, err := generator.GetVolumesAndVolumeMounts(d.devfileObj, volumeParams, options)
253
- if err != nil {
254
- return nil, nil, nil, nil, err
255
- }
256
- // sort all volumes and volume mounts in the containers and initContainers
257
- // to keep the array order deterministic
258
- sort.SliceStable(volumes, func(i, j int) bool {
259
- return volumes[i].Name < volumes[j].Name
260
- })
261
- for _, container := range containers {
262
- sort.SliceStable(container.VolumeMounts, func(i, j int) bool {
263
- return container.VolumeMounts[i].Name < container.VolumeMounts[j].Name
264
- })
265
- }
266
- for _, initContainer := range initContainers {
267
- sort.SliceStable(initContainer.VolumeMounts, func(i, j int) bool {
268
- return initContainer.VolumeMounts[i].Name < initContainer.VolumeMounts[j].Name
269
- })
270
- }
271
- return containers, initContainers, volumes, volumeNameToVolumeOptions, nil
272
- }
273
-
274
225
  func (d Devfile) hasContainerComponents() (bool, error) {
275
226
  containers, err := generator.GetContainers(d.devfileObj, common.DevfileOptions{})
276
227
  if err != nil {
data/ext/volume.go ADDED
@@ -0,0 +1,118 @@
1
+ package main
2
+
3
+ import (
4
+ "fmt"
5
+ "github.com/devfile/library/v2/pkg/devfile/generator"
6
+ "github.com/devfile/library/v2/pkg/devfile/parser/data/v2/common"
7
+ corev1 "k8s.io/api/core/v1"
8
+ "k8s.io/apimachinery/pkg/api/resource"
9
+ "strings"
10
+ )
11
+
12
+ func (d Devfile) getVolumesAndVolumeMounts(containers []corev1.Container, initContainers []corev1.Container, volumeNamePrefix string) ([]corev1.Volume, map[string]pvcOptions, error) {
13
+ containerComponents, err := d.devfileObj.Data.GetDevfileContainerComponents(common.DevfileOptions{})
14
+ if err != nil {
15
+ return nil, nil, err
16
+ }
17
+ volumeComponents, err := d.devfileObj.Data.GetDevfileVolumeComponents(common.DevfileOptions{})
18
+ if err != nil {
19
+ return nil, nil, err
20
+ }
21
+
22
+ var volumes []corev1.Volume
23
+ pvcNameToPvcOptions := map[string]pvcOptions{}
24
+ for _, volumeComponent := range volumeComponents {
25
+ volName := volumeComponent.Name
26
+ if bool(*volumeComponent.Volume.Ephemeral) == true {
27
+ emptyDir, err := getEmptyDir(volName, volumeComponent.Volume.Size)
28
+ if err != nil {
29
+ return nil, nil, err
30
+ }
31
+ volumes = append(volumes, emptyDir)
32
+ } else {
33
+ pvcName := fmt.Sprintf("%s-%s", volumeNamePrefix, volumeComponent.Name)
34
+ pvcNameToPvcOptions[volumeComponent.Name] = pvcOptions{
35
+ Name: pvcName,
36
+ Size: volumeComponent.Volume.Size,
37
+ }
38
+ volumes = append(volumes, getPersistentVolumeClaim(volName, pvcName))
39
+ }
40
+ // containerNameToMountPaths is a map of the Devfile container name to their Devfile Volume Mount Paths for a given Volume Name
41
+ containerNameToMountPaths := make(map[string][]string)
42
+ for _, containerComp := range containerComponents {
43
+ for _, volumeMount := range containerComp.Container.VolumeMounts {
44
+ if volName == volumeMount.Name {
45
+ containerNameToMountPaths[containerComp.Name] = append(containerNameToMountPaths[containerComp.Name], generator.GetVolumeMountPath(volumeMount))
46
+ }
47
+ }
48
+ }
49
+
50
+ addVolumeMountToContainers(containers, initContainers, volumeComponent.Name, containerNameToMountPaths)
51
+ }
52
+
53
+ return volumes, pvcNameToPvcOptions, nil
54
+ }
55
+
56
+ // addVolumeMountToContainers adds the Volume Mounts in containerNameToMountPaths to the containers for a given pvc and volumeName
57
+ // containerNameToMountPaths is a map of a container name to an array of its Mount Paths.
58
+ // To be moved to devfile/library.
59
+ func addVolumeMountToContainers(containers []corev1.Container, initContainers []corev1.Container, volumeName string, containerNameToMountPaths map[string][]string) {
60
+
61
+ for containerName, mountPaths := range containerNameToMountPaths {
62
+ for i := range containers {
63
+ if containers[i].Name == containerName {
64
+ for _, mountPath := range mountPaths {
65
+ containers[i].VolumeMounts = append(containers[i].VolumeMounts, corev1.VolumeMount{
66
+ Name: volumeName,
67
+ MountPath: mountPath,
68
+ SubPath: "",
69
+ },
70
+ )
71
+ }
72
+ }
73
+ }
74
+ for i := range initContainers {
75
+ if strings.HasPrefix(initContainers[i].Name, containerName) {
76
+ for _, mountPath := range mountPaths {
77
+ initContainers[i].VolumeMounts = append(initContainers[i].VolumeMounts, corev1.VolumeMount{
78
+ Name: volumeName,
79
+ MountPath: mountPath,
80
+ SubPath: "",
81
+ },
82
+ )
83
+ }
84
+ }
85
+ }
86
+ }
87
+ }
88
+
89
+ // getPersistentVolumeClaim gets a pvc type volume with the given volume name and pvc name.
90
+ func getPersistentVolumeClaim(volumeName, pvcName string) corev1.Volume {
91
+
92
+ return corev1.Volume{
93
+ Name: volumeName,
94
+ VolumeSource: corev1.VolumeSource{
95
+ PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
96
+ ClaimName: pvcName,
97
+ },
98
+ },
99
+ }
100
+ }
101
+
102
+ // getEmptyDir gets an emptyDir type volume with the given volume name and size.
103
+ // size should be parseable as a Kubernetes `Quantity` or an error will be returned
104
+ func getEmptyDir(volumeName string, size string) (corev1.Volume, error) {
105
+
106
+ emptyDir := &corev1.EmptyDirVolumeSource{}
107
+ qty, err := resource.ParseQuantity(size)
108
+ if err != nil {
109
+ return corev1.Volume{}, err
110
+ }
111
+ emptyDir.SizeLimit = &qty
112
+ return corev1.Volume{
113
+ Name: volumeName,
114
+ VolumeSource: corev1.VolumeSource{
115
+ EmptyDir: emptyDir,
116
+ },
117
+ }, nil
118
+ }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: devfile
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.12.pre.alpha1
4
+ version: 0.0.14.pre.alpha1
5
5
  platform: x86_64-linux
6
6
  authors:
7
7
  - GitLab
@@ -24,6 +24,7 @@ files:
24
24
  - ext/go.mod
25
25
  - ext/go.sum
26
26
  - ext/main.go
27
+ - ext/volume.go
27
28
  - lib/devfile.rb
28
29
  homepage: https://gitlab.com
29
30
  licenses: