Skip to content

Commit

Permalink
Merge bfd25e2 into 8c24424
Browse files Browse the repository at this point in the history
  • Loading branch information
feloy authored Nov 23, 2022
2 parents 8c24424 + bfd25e2 commit df3e03d
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 6 deletions.
41 changes: 37 additions & 4 deletions pkg/dev/podmandev/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,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 @@ -81,8 +101,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 @@ -102,3 +122,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 @@ -39,6 +39,10 @@ var (
},
})

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

basePod = &corev1.Pod{
TypeMeta: v1.TypeMeta{
APIVersion: "v1",
Expand Down Expand Up @@ -91,15 +95,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 @@ -284,6 +288,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
}

0 comments on commit df3e03d

Please sign in to comment.