Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

odo dev on podman: Add support for devfile volume #6328

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 37 additions & 4 deletions pkg/dev/podmandev/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,40 @@ func createPodFromComponent(
Name: storage.OdoSourceVolume,
VolumeSource: corev1.VolumeSource{
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
ClaimName: getVolumeName(componentName, appName, "source"),
ClaimName: getVolumeName(storage.OdoSourceVolume, componentName, appName),
},
},
},
{
Name: storage.SharedDataVolumeName,
VolumeSource: corev1.VolumeSource{
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
ClaimName: getVolumeName(componentName, appName, "shared"),
ClaimName: getVolumeName(storage.SharedDataVolumeName, componentName, appName),
},
},
},
}

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(devfileVolume.Name, componentName, appName),
},
},
})
err = addVolumeMountToContainer(containers, devfileVolume)
if err != nil {
return nil, nil, err
}
}

// TODO add labels (for GetRunningPodFromSelector)
pod := corev1.Pod{
Spec: corev1.PodSpec{
Expand All @@ -76,8 +96,8 @@ func createPodFromComponent(
return &pod, fwPorts, nil
}

func getVolumeName(componentName string, appName string, volume string) string {
return "odo-projects-" + componentName + "-" + appName + "-" + volume
func getVolumeName(volume string, componentName string, appName string) string {
return volume + "-" + componentName + "-" + appName
}

func addHostPorts(containers []corev1.Container) []api.ForwardedPort {
Expand All @@ -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)
}
47 changes: 45 additions & 2 deletions pkg/dev/podmandev/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ var (
},
})

volume = generator.GetVolumeComponent(generator.VolumeComponentParams{
Name: "myvolume",
})

basePod = &corev1.Pod{
TypeMeta: v1.TypeMeta{
APIVersion: "v1",
Expand Down Expand Up @@ -80,15 +84,15 @@ var (
Name: "odo-projects",
VolumeSource: corev1.VolumeSource{
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
ClaimName: "odo-projects-mycmp-app-source",
ClaimName: "odo-projects-mycmp-app",
},
},
},
{
Name: "odo-shared-data",
VolumeSource: corev1.VolumeSource{
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
ClaimName: "odo-projects-mycmp-app-shared",
ClaimName: "odo-shared-data-mycmp-app",
},
},
},
Expand Down Expand Up @@ -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: volume.Name + "-" + devfileName + "-" + appName,
},
},
})
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 {
Expand Down
14 changes: 14 additions & 0 deletions pkg/libdevfile/generator/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}