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 3 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
39 changes: 36 additions & 3 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(componentName, appName, "odo-projects"),
feloy marked this conversation as resolved.
Show resolved Hide resolved
},
},
},
{
Name: storage.SharedDataVolumeName,
VolumeSource: corev1.VolumeSource{
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
ClaimName: getVolumeName(componentName, appName, "shared"),
ClaimName: getVolumeName(componentName, appName, "odo-shared"),
feloy marked this conversation as resolved.
Show resolved Hide resolved
},
},
},
}

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),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For cluster, we use a different naming strategy devfileVolume.Name+componentName+appName for volume components of Devfile. Perhaps we should use the same naming strategy for podman as well?

Copy link
Contributor Author

@feloy feloy Nov 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, I think I misunderstood the naming strategy on cluster. I have made chages, to change name for devfile volumes and also for the "shared" volume. WDYT?

feloy marked this conversation as resolved.
Show resolved Hide resolved
},
},
})
err = addVolumeMountToContainer(containers, devfileVolume)
if err != nil {
return nil, nil, err
}
}

// TODO add labels (for GetRunningPodFromSelector)
pod := corev1.Pod{
Spec: corev1.PodSpec{
Expand All @@ -77,7 +97,7 @@ func createPodFromComponent(
}

func getVolumeName(componentName string, appName string, volume string) string {
feloy marked this conversation as resolved.
Show resolved Hide resolved
return "odo-projects-" + componentName + "-" + appName + "-" + volume
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-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
}