Skip to content

Commit 702ea20

Browse files
committed
Don't add projects volumeMount if container mounts it manually
This avoids an issue where a container can mount the projects volume directly and have `mountSources: true`, which results in a duplicate mount and invalid deployment. Related to issue devfile/api#290 and how projects volume should be handled with `mountSources`. Signed-off-by: Angel Misevski <[email protected]>
1 parent 3a0ac94 commit 702ea20

File tree

3 files changed

+65
-10
lines changed

3 files changed

+65
-10
lines changed

pkg/library/container/container_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"path/filepath"
1919
"testing"
2020

21+
"github.com/google/go-cmp/cmp"
2122
corev1 "k8s.io/api/core/v1"
2223

2324
devworkspace "github.com/devfile/api/pkg/apis/workspaces/v1alpha2"
@@ -68,6 +69,7 @@ func TestGetKubeContainersFromDevfile(t *testing.T) {
6869
loadTestCaseOrPanic(t, "handles-mountSources.yaml"),
6970
loadTestCaseOrPanic(t, "handles-resources.yaml"),
7071
loadTestCaseOrPanic(t, "handles-endpoints-with-common-port.yaml"),
72+
loadTestCaseOrPanic(t, "handles-container-that-mounts-projects-directly.yaml"),
7173
loadTestCaseOrPanic(t, "ignores-non-container-components.yaml"),
7274
loadTestCaseOrPanic(t, "converts-all-fields.yaml"),
7375
loadTestCaseOrPanic(t, "error-has-parent.yaml"),
@@ -87,7 +89,8 @@ func TestGetKubeContainersFromDevfile(t *testing.T) {
8789
if !assert.NoError(t, err, "Should not return error") {
8890
return
8991
}
90-
assert.Equal(t, tt.Output.PodAdditions, gotPodAdditions, "PodAdditions should match expected output")
92+
assert.True(t, cmp.Equal(tt.Output.PodAdditions, gotPodAdditions),
93+
"PodAdditions should match expected output: \n%s", cmp.Diff(tt.Output.PodAdditions, gotPodAdditions))
9194
}
9295
})
9396
}

pkg/library/container/mountSources.go

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,23 @@ func handleMountSources(k8sContainer *corev1.Container, devfileContainer *devwor
4040
if !HasMountSources(devfileContainer) {
4141
return
4242
}
43-
sourceMapping := devfileContainer.SourceMapping
44-
if sourceMapping == "" {
45-
// Sanity check -- this value should be defaulted to `/projects` but may not be
46-
// if struct was not processed by k8s
47-
sourceMapping = config.DefaultProjectsSourcesRoot
43+
var sourceMapping string
44+
if vm := getProjectsVolumeMount(k8sContainer); vm != nil {
45+
// Container already mounts projects volume; need to set env vars according to mountPath
46+
// TODO: see issue https://github.com/devfile/api/issues/290
47+
sourceMapping = vm.MountPath
48+
} else {
49+
sourceMapping = devfileContainer.SourceMapping
50+
if sourceMapping == "" {
51+
// Sanity check -- this value should be defaulted to `/projects` but may not be
52+
// if struct was not processed by k8s
53+
sourceMapping = config.DefaultProjectsSourcesRoot
54+
}
55+
k8sContainer.VolumeMounts = append(k8sContainer.VolumeMounts, corev1.VolumeMount{
56+
Name: constants.ProjectsVolumeName,
57+
MountPath: sourceMapping,
58+
})
4859
}
49-
k8sContainer.VolumeMounts = append(k8sContainer.VolumeMounts, corev1.VolumeMount{
50-
Name: constants.ProjectsVolumeName,
51-
MountPath: sourceMapping,
52-
})
5360
k8sContainer.Env = append(k8sContainer.Env, corev1.EnvVar{
5461
Name: constants.ProjectsRootEnvVar,
5562
Value: sourceMapping,
@@ -58,3 +65,14 @@ func handleMountSources(k8sContainer *corev1.Container, devfileContainer *devwor
5865
Value: sourceMapping, // TODO: Unclear how this should be handled in case of multiple projects
5966
})
6067
}
68+
69+
// getProjectsVolumeMount returns the projects volumeMount in a container, if it is defined; if it does not exist,
70+
// returns nil.
71+
func getProjectsVolumeMount(k8sContainer *corev1.Container) *corev1.VolumeMount {
72+
for _, vm := range k8sContainer.VolumeMounts {
73+
if vm.Name == constants.ProjectsVolumeName {
74+
return &vm
75+
}
76+
}
77+
return nil
78+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# This test is due to an inconsistency in devfile/api semantics; see
2+
# issue https://github.com/devfile/api/issues/290
3+
4+
name: "Handles container with mountSources and explicit projects volume"
5+
6+
input:
7+
components:
8+
- name: testing-container-1
9+
container:
10+
image: testing-image-1
11+
memoryLimit: 999Mi # isolate test to not include memoryLimit
12+
sourceMapping: "/testdir1"
13+
mountSources: true
14+
volumeMounts:
15+
- name: "projects"
16+
path: "/not-source-mapping"
17+
18+
output:
19+
podAdditions:
20+
containers:
21+
- name: testing-container-1
22+
image: testing-image-1
23+
imagePullPolicy: Always
24+
resources:
25+
limits:
26+
memory: "999Mi"
27+
volumeMounts:
28+
- name: "projects"
29+
mountPath: "/not-source-mapping"
30+
env:
31+
- name: "PROJECTS_ROOT"
32+
value: "/not-source-mapping"
33+
- name: "PROJECTS_SOURCE"
34+
value: "/not-source-mapping" # Temp value until projects is figured out

0 commit comments

Comments
 (0)