From 87b3c0290ce4b1f679f890db1c8275b16395b74a Mon Sep 17 00:00:00 2001 From: Philippe Martin Date: Tue, 22 Nov 2022 09:47:29 +0100 Subject: [PATCH 1/4] Add support for devfile volume --- pkg/dev/podmandev/pod.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/pkg/dev/podmandev/pod.go b/pkg/dev/podmandev/pod.go index dd8334dad2b..fc678929ab9 100644 --- a/pkg/dev/podmandev/pod.go +++ b/pkg/dev/podmandev/pod.go @@ -58,6 +58,26 @@ func createPodFromComponent( }, } + devfileVolumes, err := storage.ListStorage(devfileObj) + if err != nil { + return nil, nil, err + } + + for _, devfileVolume := range devfileVolumes { + volumes = append(volumes, corev1.Volume{ + Name: devfileVolume.Name, + VolumeSource: corev1.VolumeSource{ + PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{ + ClaimName: getVolumeName(componentName, appName, devfileVolume.Name), + }, + }, + }) + err = addVolumeMountToContainer(containers, devfileVolume) + if err != nil { + return nil, nil, err + } + } + // TODO add labels (for GetRunningPodFromSelector) pod := corev1.Pod{ Spec: corev1.PodSpec{ @@ -97,3 +117,16 @@ func addHostPorts(containers []corev1.Container) []api.ForwardedPort { } return result } + +func addVolumeMountToContainer(containers []corev1.Container, devfileVolume storage.LocalStorage) error { + for i := range containers { + if containers[i].Name == devfileVolume.Container { + containers[i].VolumeMounts = append(containers[i].VolumeMounts, corev1.VolumeMount{ + Name: devfileVolume.Name, + MountPath: devfileVolume.Path, + }) + return nil + } + } + return fmt.Errorf("container %q not found", devfileVolume.Container) +} From 4adb74e9f9e221d06df71867672f257298b9f550 Mon Sep 17 00:00:00 2001 From: Philippe Martin Date: Tue, 22 Nov 2022 11:22:25 +0100 Subject: [PATCH 2/4] Add unit test --- pkg/dev/podmandev/pod_test.go | 43 +++++++++++++++++++++++++++ pkg/libdevfile/generator/component.go | 14 +++++++++ 2 files changed, 57 insertions(+) diff --git a/pkg/dev/podmandev/pod_test.go b/pkg/dev/podmandev/pod_test.go index f78d0967204..1cf896770a0 100644 --- a/pkg/dev/podmandev/pod_test.go +++ b/pkg/dev/podmandev/pod_test.go @@ -37,6 +37,10 @@ var ( }, }) + volume = generator.GetVolumeComponent(generator.VolumeComponentParams{ + Name: "myvolume", + }) + basePod = &corev1.Pod{ TypeMeta: v1.TypeMeta{ APIVersion: "v1", @@ -273,6 +277,45 @@ func Test_createPodFromComponent(t *testing.T) { }, }, }, + { + name: "basic component with volume mount", + args: args{ + devfileObj: func() parser.DevfileObj { + data, _ := data.NewDevfileData(string(data.APISchemaVersion200)) + _ = data.AddCommands([]v1alpha2.Command{command}) + _ = data.AddComponents([]v1alpha2.Component{baseComponent, volume}) + _ = data.AddVolumeMounts(baseComponent.Name, []v1alpha2.VolumeMount{ + { + Name: volume.Name, + Path: "/path/to/mount", + }, + }) + + return parser.DevfileObj{ + Data: data, + } + }, + componentName: devfileName, + appName: appName, + }, + wantPod: func() *corev1.Pod { + pod := basePod.DeepCopy() + pod.Spec.Volumes = append(pod.Spec.Volumes, corev1.Volume{ + Name: volume.Name, + VolumeSource: corev1.VolumeSource{ + PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{ + ClaimName: "odo-projects-" + devfileName + "-" + appName + "-" + volume.Name, + }, + }, + }) + pod.Spec.Containers[0].VolumeMounts = append(pod.Spec.Containers[0].VolumeMounts, corev1.VolumeMount{ + Name: volume.Name, + MountPath: "/path/to/mount", + }) + return pod + }, + }, + // TODO: Add test cases. } for _, tt := range tests { diff --git a/pkg/libdevfile/generator/component.go b/pkg/libdevfile/generator/component.go index 1644e4c5e13..cec387633ec 100644 --- a/pkg/libdevfile/generator/component.go +++ b/pkg/libdevfile/generator/component.go @@ -90,3 +90,17 @@ func GetOpenshiftComponent(params OpenshiftComponentParams) v1alpha2.Component { } return cmp } + +type VolumeComponentParams struct { + Name string +} + +func GetVolumeComponent(params VolumeComponentParams) v1alpha2.Component { + cmp := v1alpha2.Component{ + Name: params.Name, + ComponentUnion: v1alpha2.ComponentUnion{ + Volume: &v1alpha2.VolumeComponent{}, + }, + } + return cmp +} From 2c0af4d543443537f203301dc3e1fb33fadc0828 Mon Sep 17 00:00:00 2001 From: Philippe Martin Date: Wed, 23 Nov 2022 11:29:54 +0100 Subject: [PATCH 3/4] Use same naming strategy as for cluster --- pkg/dev/podmandev/pod.go | 6 +++--- pkg/dev/podmandev/pod_test.go | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/pkg/dev/podmandev/pod.go b/pkg/dev/podmandev/pod.go index fc678929ab9..e97a2462a98 100644 --- a/pkg/dev/podmandev/pod.go +++ b/pkg/dev/podmandev/pod.go @@ -44,7 +44,7 @@ func createPodFromComponent( Name: storage.OdoSourceVolume, VolumeSource: corev1.VolumeSource{ PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{ - ClaimName: getVolumeName(componentName, appName, "source"), + ClaimName: getVolumeName(componentName, appName, "odo-projects"), }, }, }, @@ -52,7 +52,7 @@ func createPodFromComponent( Name: storage.SharedDataVolumeName, VolumeSource: corev1.VolumeSource{ PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{ - ClaimName: getVolumeName(componentName, appName, "shared"), + ClaimName: getVolumeName(componentName, appName, "odo-shared"), }, }, }, @@ -97,7 +97,7 @@ func createPodFromComponent( } func getVolumeName(componentName string, appName string, volume string) string { - return "odo-projects-" + componentName + "-" + appName + "-" + volume + return volume + "-" + componentName + "-" + appName } func addHostPorts(containers []corev1.Container) []api.ForwardedPort { diff --git a/pkg/dev/podmandev/pod_test.go b/pkg/dev/podmandev/pod_test.go index 1cf896770a0..ab3748a95d6 100644 --- a/pkg/dev/podmandev/pod_test.go +++ b/pkg/dev/podmandev/pod_test.go @@ -84,7 +84,7 @@ var ( Name: "odo-projects", VolumeSource: corev1.VolumeSource{ PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{ - ClaimName: "odo-projects-mycmp-app-source", + ClaimName: "odo-projects-mycmp-app", }, }, }, @@ -92,7 +92,7 @@ var ( Name: "odo-shared-data", VolumeSource: corev1.VolumeSource{ PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{ - ClaimName: "odo-projects-mycmp-app-shared", + ClaimName: "odo-shared-mycmp-app", }, }, }, @@ -304,7 +304,7 @@ func Test_createPodFromComponent(t *testing.T) { Name: volume.Name, VolumeSource: corev1.VolumeSource{ PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{ - ClaimName: "odo-projects-" + devfileName + "-" + appName + "-" + volume.Name, + ClaimName: volume.Name + "-" + devfileName + "-" + appName, }, }, }) From bfd25e2f13ac0fb8cc8a5ed1d114e09ab4bbc879 Mon Sep 17 00:00:00 2001 From: Philippe Martin Date: Wed, 23 Nov 2022 12:59:21 +0100 Subject: [PATCH 4/4] Apply suggestions from code review Co-authored-by: Parthvi Vala --- pkg/dev/podmandev/pod.go | 8 ++++---- pkg/dev/podmandev/pod_test.go | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pkg/dev/podmandev/pod.go b/pkg/dev/podmandev/pod.go index e97a2462a98..d2bc2fce2dc 100644 --- a/pkg/dev/podmandev/pod.go +++ b/pkg/dev/podmandev/pod.go @@ -44,7 +44,7 @@ func createPodFromComponent( Name: storage.OdoSourceVolume, VolumeSource: corev1.VolumeSource{ PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{ - ClaimName: getVolumeName(componentName, appName, "odo-projects"), + ClaimName: getVolumeName(storage.OdoSourceVolume, componentName, appName), }, }, }, @@ -52,7 +52,7 @@ func createPodFromComponent( Name: storage.SharedDataVolumeName, VolumeSource: corev1.VolumeSource{ PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{ - ClaimName: getVolumeName(componentName, appName, "odo-shared"), + ClaimName: getVolumeName(storage.SharedDataVolumeName, componentName, appName), }, }, }, @@ -68,7 +68,7 @@ func createPodFromComponent( Name: devfileVolume.Name, VolumeSource: corev1.VolumeSource{ PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{ - ClaimName: getVolumeName(componentName, appName, devfileVolume.Name), + ClaimName: getVolumeName(devfileVolume.Name, componentName, appName), }, }, }) @@ -96,7 +96,7 @@ func createPodFromComponent( return &pod, fwPorts, nil } -func getVolumeName(componentName string, appName string, volume string) string { +func getVolumeName(volume string, componentName string, appName string) string { return volume + "-" + componentName + "-" + appName } diff --git a/pkg/dev/podmandev/pod_test.go b/pkg/dev/podmandev/pod_test.go index ab3748a95d6..02b1b3225c7 100644 --- a/pkg/dev/podmandev/pod_test.go +++ b/pkg/dev/podmandev/pod_test.go @@ -92,7 +92,7 @@ var ( Name: "odo-shared-data", VolumeSource: corev1.VolumeSource{ PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{ - ClaimName: "odo-shared-mycmp-app", + ClaimName: "odo-shared-data-mycmp-app", }, }, },