forked from redhat-developer/odo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
186 lines (161 loc) · 5.61 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
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
package utils
import (
"strconv"
devfileParser "github.com/devfile/library/pkg/devfile/parser"
corev1 "k8s.io/api/core/v1"
"k8s.io/klog"
adaptersCommon "github.com/redhat-developer/odo/pkg/devfile/adapters/common"
"github.com/redhat-developer/odo/pkg/libdevfile"
"github.com/redhat-developer/odo/pkg/storage"
)
// GetOdoContainerVolumes returns the mandatory Kube volumes for an Odo component
func GetOdoContainerVolumes(sourcePVCName string) []corev1.Volume {
var sourceVolume corev1.Volume
if sourcePVCName != "" {
sourceVolume = corev1.Volume{
Name: storage.OdoSourceVolume,
VolumeSource: corev1.VolumeSource{
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{ClaimName: sourcePVCName},
},
}
} else {
sourceVolume = corev1.Volume{
Name: storage.OdoSourceVolume,
}
}
return []corev1.Volume{
sourceVolume,
{
// Create a volume that will be shared between InitContainer and the applicationContainer
// in order to pass over some files for odo
Name: storage.SharedDataVolumeName,
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{},
},
},
}
}
// isEnvPresent checks if the env variable is present in an array of env variables
func isEnvPresent(EnvVars []corev1.EnvVar, envVarName string) bool {
isPresent := false
for _, envVar := range EnvVars {
if envVar.Name == envVarName {
isPresent = true
}
}
return isPresent
}
// AddOdoProjectVolume adds the odo project volume to the containers
func AddOdoProjectVolume(containers *[]corev1.Container) {
if containers == nil {
return
}
for i, container := range *containers {
for _, env := range container.Env {
if env.Name == adaptersCommon.EnvProjectsRoot {
container.VolumeMounts = append(container.VolumeMounts, corev1.VolumeMount{
Name: storage.OdoSourceVolume,
MountPath: env.Value,
})
(*containers)[i] = container
}
}
}
}
// AddOdoMandatoryVolume adds the odo mandatory volumes to the containers
func AddOdoMandatoryVolume(containers *[]corev1.Container) {
if containers == nil {
return
}
for i, container := range *containers {
klog.V(2).Infof("Updating container %v with mandatory volume mounts", container.Name)
container.VolumeMounts = append(container.VolumeMounts, corev1.VolumeMount{
Name: storage.SharedDataVolumeName,
MountPath: storage.SharedDataMountPath,
})
(*containers)[i] = container
}
}
// UpdateContainersEntrypointsIfNeeded updates the run components entrypoint
// if no entrypoint has been specified for the component in the devfile
func UpdateContainersEntrypointsIfNeeded(
devfileObj devfileParser.DevfileObj,
containers []corev1.Container,
devfileRunCmd string,
devfileDebugCmd string,
) ([]corev1.Container, error) {
runCommand, err := libdevfile.GetRunCommand(devfileObj.Data, devfileRunCmd)
if err != nil {
return nil, err
}
debugCommand, err := libdevfile.GetDebugCommand(devfileObj.Data, devfileDebugCmd)
if err != nil {
return nil, err
}
runContainers, err := libdevfile.GetContainerComponentsForCommand(devfileObj, runCommand)
if err != nil {
return nil, err
}
debugContainers, err := libdevfile.GetContainerComponentsForCommand(devfileObj, debugCommand)
if err != nil {
return nil, err
}
components := append(runContainers, debugContainers...)
for i := range containers {
container := &containers[i]
for _, c := range components {
if container.Name == c {
overrideContainerCommandAndArgsIfNeeded(container)
break
}
}
}
return containers, nil
}
// UpdateContainerEnvVars updates the container environment variables
func UpdateContainerEnvVars(
devfileObj devfileParser.DevfileObj,
containers []corev1.Container,
devfileDebugCmd string,
devfileDebugPort int,
) ([]corev1.Container, error) {
debugCommand, err := libdevfile.GetDebugCommand(devfileObj.Data, devfileDebugCmd)
if err != nil {
return nil, err
}
debugContainers, err := libdevfile.GetContainerComponentsForCommand(devfileObj, debugCommand)
if err != nil {
return nil, err
}
for i := range containers {
container := &containers[i]
// Check if the container belongs to a debug command component
for _, c := range debugContainers {
if container.Name == c && !isEnvPresent(container.Env, adaptersCommon.EnvDebugPort) {
klog.V(2).Infof("Updating container %v env with debug command's debugPort", container.Name)
container.Env = append(container.Env,
corev1.EnvVar{
Name: adaptersCommon.EnvDebugPort,
Value: strconv.Itoa(devfileDebugPort),
})
break
}
}
}
return containers, nil
}
// overrideContainerCommandAndArgsIfNeeded overrides the container's entrypoint
// if the corresponding component does not have any command and/or args in the Devfile.
// This is a workaround until the default Devfile registry exposes stacks with non-terminating containers.
func overrideContainerCommandAndArgsIfNeeded(container *corev1.Container) {
if len(container.Command) != 0 || len(container.Args) != 0 {
return
}
klog.V(2).Infof("No entrypoint defined for the component, setting container %v entrypoint to 'tail -f /dev/null'. You can set a 'command' and/or 'args' for the component to override this default entrypoint.", container.Name)
// #5768: overriding command and args if the container had no Command or Args defined in it.
// This is a workaround for us to quickly switch to running without Supervisord,
// while waiting for the Devfile registry to expose stacks with non-terminating containers.
// TODO(rm3l): Remove this once https://github.com/devfile/registry/pull/102 is merged on the Devfile side
container.Command = []string{"tail"}
container.Args = []string{"-f", "/dev/null"}
}