forked from redhat-developer/odo
/
utils.go
79 lines (71 loc) · 2.69 KB
/
utils.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
package utils
import (
"github.com/openshift/odo/pkg/devfile"
adaptersCommon "github.com/openshift/odo/pkg/devfile/adapters/common"
"github.com/openshift/odo/pkg/devfile/versions/common"
"github.com/openshift/odo/pkg/kclient"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
)
const (
volumeSize = "5Gi"
)
// ComponentExists checks whether a deployment by the given name exists
func ComponentExists(client kclient.Client, name string) bool {
_, err := client.GetDeploymentByName(name)
return err == nil
}
// ConvertEnvs converts environment variables from the devfile structure to kubernetes structure
func ConvertEnvs(vars []common.DockerimageEnv) []corev1.EnvVar {
kVars := []corev1.EnvVar{}
for _, env := range vars {
kVars = append(kVars, corev1.EnvVar{
Name: *env.Name,
Value: *env.Value,
})
}
return kVars
}
// GetContainers iterates through the components in the devfile and returns a slice of the corresponding containers
func GetContainers(devfileObj devfile.DevfileObj) []corev1.Container {
var containers []corev1.Container
for _, comp := range adaptersCommon.GetSupportedComponents(devfileObj.Data) {
envVars := ConvertEnvs(comp.Env)
resourceReqs := GetResourceReqs(comp)
container := kclient.GenerateContainer(*comp.Alias, *comp.Image, false, comp.Command, comp.Args, envVars, resourceReqs)
containers = append(containers, *container)
}
return containers
}
// GetVolumes iterates through the components in the devfile and returns a map of component alias to the devfile volumes
func GetVolumes(devfileObj devfile.DevfileObj) map[string][]adaptersCommon.DevfileVolume {
// componentAliasToVolumes is a map of the Devfile Component Alias to the Devfile Component Volumes
componentAliasToVolumes := make(map[string][]adaptersCommon.DevfileVolume)
size := volumeSize
for _, comp := range adaptersCommon.GetSupportedComponents(devfileObj.Data) {
if comp.Volumes != nil {
for _, volume := range comp.Volumes {
vol := adaptersCommon.DevfileVolume{
Name: volume.Name,
ContainerPath: volume.ContainerPath,
Size: &size,
}
componentAliasToVolumes[*comp.Alias] = append(componentAliasToVolumes[*comp.Alias], vol)
}
}
}
return componentAliasToVolumes
}
// GetResourceReqs creates a kubernetes ResourceRequirements object based on resource requirements set in the devfile
func GetResourceReqs(comp common.DevfileComponent) corev1.ResourceRequirements {
reqs := corev1.ResourceRequirements{}
limits := make(corev1.ResourceList)
if comp.MemoryLimit != nil {
memoryLimit, err := resource.ParseQuantity(*comp.MemoryLimit)
if err == nil {
limits[corev1.ResourceMemory] = memoryLimit
}
reqs.Limits = limits
}
return reqs
}