This repository was archived by the owner on Apr 1, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 245
Expand file tree
/
Copy pathutils.go
More file actions
156 lines (139 loc) · 5 KB
/
utils.go
File metadata and controls
156 lines (139 loc) · 5 KB
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
package utils
import (
"github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
devfileParser "github.com/devfile/library/v2/pkg/devfile/parser"
corev1 "k8s.io/api/core/v1"
"k8s.io/klog/v2"
"github.com/redhat-developer/odo/pkg/libdevfile"
"github.com/redhat-developer/odo/pkg/storage"
)
const (
// _envProjectsRoot is the env defined for project mount in a component container when component's mountSources=true
_envProjectsRoot = "PROJECTS_ROOT"
)
// GetOdoContainerVolumes returns the mandatory Kube volumes for an Odo component
func GetOdoContainerVolumes(sourcePVCName string) []corev1.Volume {
var sourceVolume corev1.Volume
if sourcePVCName != "" {
// Define a Persistent volume using the found PVC volume source
sourceVolume = corev1.Volume{
Name: storage.OdoSourceVolume,
VolumeSource: corev1.VolumeSource{
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{ClaimName: sourcePVCName},
},
}
} else {
// Define an Ephemeral volume using an EmptyDir volume source
sourceVolume = corev1.Volume{
Name: storage.OdoSourceVolume,
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{},
},
}
}
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{},
},
},
}
}
// AddOdoProjectVolume adds the odo project volume to the containers
func AddOdoProjectVolume(containers []corev1.Container) {
if containers == nil {
return
}
for i := range containers {
for _, env := range containers[i].Env {
if env.Name == _envProjectsRoot {
containers[i].VolumeMounts = append(containers[i].VolumeMounts, corev1.VolumeMount{
Name: storage.OdoSourceVolume,
MountPath: env.Value,
})
}
}
}
}
// AddOdoMandatoryVolume adds the odo mandatory volumes to the containers
func AddOdoMandatoryVolume(containers []corev1.Container) {
if containers == nil {
return
}
for i := range containers {
klog.V(2).Infof("Updating container %v with mandatory volume mounts", containers[i].Name)
containers[i].VolumeMounts = append(containers[i].VolumeMounts, corev1.VolumeMount{
Name: storage.SharedDataVolumeName,
MountPath: storage.SharedDataMountPath,
})
}
}
// 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,
devfileBuildCmd string,
devfileRunCmd string,
devfileDebugCmd string,
) ([]corev1.Container, error) {
buildCommand, hasBuildCmd, err := libdevfile.GetCommand(devfileObj, devfileBuildCmd, v1alpha2.BuildCommandGroupKind)
if err != nil {
return nil, err
}
runCommand, hasRunCmd, err := libdevfile.GetCommand(devfileObj, devfileRunCmd, v1alpha2.RunCommandGroupKind)
if err != nil {
return nil, err
}
debugCommand, hasDebugCmd, err := libdevfile.GetCommand(devfileObj, devfileDebugCmd, v1alpha2.DebugCommandGroupKind)
if err != nil {
return nil, err
}
var cmdsToHandle []v1alpha2.Command
if hasBuildCmd {
cmdsToHandle = append(cmdsToHandle, buildCommand)
}
if hasRunCmd {
cmdsToHandle = append(cmdsToHandle, runCommand)
}
if hasDebugCmd {
cmdsToHandle = append(cmdsToHandle, debugCommand)
}
var components []string
var containerComps []string
for _, cmd := range cmdsToHandle {
containerComps, err = libdevfile.GetContainerComponentsForCommand(devfileObj, cmd)
if err != nil {
return nil, err
}
components = append(components, containerComps...)
}
for _, c := range components {
for i := range containers {
container := &containers[i]
if container.Name == c {
overrideContainerCommandAndArgsIfNeeded(container)
}
}
}
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"}
}