devfile 0.0.12.pre.alpha1-arm64-darwin → 0.0.14.pre.alpha1-arm64-darwin

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: 51f867c9659b085063e543b19d4aa1b368e0c5586dfb3753657195a0988a3477
4
- data.tar.gz: 6fe5ebe8e2126969d14084a2b94df0056e42f00f7dd2bd66124f1e8e18e72fe3
3
+ metadata.gz: a51dbd46d989075807ea91333a1bcabca598d5bbfb6a6010c0aca3eb82a6cd83
4
+ data.tar.gz: 1f6dde67b986ef69691c6a577209332f6bd88095b50de0e6fdc1a8b1c776f81e
5
5
  SHA512:
6
- metadata.gz: a34eb8c12d90069e173cf18ea080e3a4c15e82d6e6e71ae969e7b1c192300c7f6eb36779faba39a44608522950c2eaeb6f0c948cc870ba1aad7ef5e998371b3f
7
- data.tar.gz: d5cff0dd5b7d067cc42ba9f2d6c3c91c6ab0d3bf8253ab3406611a1791b1c28eb1d62dba3399dcc37d364875469ca5e5b4658f842e2a40d0618d489b05199642
6
+ metadata.gz: 1ca4a2dbdda67b2f53e0a2159564b99297ba2eeb24595a6ece5b45720881484ca826dbccedb20028b5aee0090806d8ff8e4707c6b0da4c21304b0cabb358efe9
7
+ data.tar.gz: dcde2fe0951b7654eed898eb81165d20f8b0e0c2b5e0339b642099ea13709b8795ac9f9dd75a42372e708422cca07e81bc21a333a54791ad7a359b92887b35fc
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: arm64-darwin
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: