feat: support container volume mount subpath (#1628)

* feat: support volumes subpath

Signed-off-by: AhmedGrati <ahmedgrati1999@gmail.com>

* docs: add the kompose.volume.sub-path label

Signed-off-by: AhmedGrati <ahmedgrati1999@gmail.com>

* fix: update e2e tests

Signed-off-by: AhmedGrati <ahmedgrati1999@gmail.com>

---------

Signed-off-by: AhmedGrati <ahmedgrati1999@gmail.com>
This commit is contained in:
AhmedGrati
2023-07-05 10:47:01 +01:00
committed by GitHub
parent d1e32e7d7d
commit 485cd2f163
10 changed files with 267 additions and 2 deletions
+8
View File
@@ -887,6 +887,7 @@ func (k *Kubernetes) ConfigVolumes(name string, service kobject.ServiceConfig) (
var PVCs []*api.PersistentVolumeClaim
var cms []*api.ConfigMap
var volumeName string
var subpathName string
// Set a var based on if the user wants to use empty volumes
// as opposed to persistent volumes and volume claims
@@ -897,6 +898,10 @@ func (k *Kubernetes) ConfigVolumes(name string, service kobject.ServiceConfig) (
useEmptyVolumes = true
}
if subpath, ok := service.Labels["kompose.volume.subpath"]; ok {
subpathName = subpath
}
// Override volume type if specified in service labels.
if vt, ok := service.Labels["kompose.volume.type"]; ok {
if _, okk := ValidVolumeSet[vt]; !okk {
@@ -993,6 +998,9 @@ func (k *Kubernetes) ConfigVolumes(name string, service kobject.ServiceConfig) (
PVCs = append(PVCs, createdPVC)
}
}
if subpathName != "" {
volMount.SubPath = subpathName
}
volumeMounts = append(volumeMounts, volMount)
// create a new volume object using the volsource and add to list
@@ -104,6 +104,13 @@ func newServiceConfigWithExternalTrafficPolicy() kobject.ServiceConfig {
}
}
func newServiceConfigWithServiceVolumeMount(volumeMountSubPathValue string) kobject.ServiceConfig {
return kobject.ServiceConfig{
Name: "app",
VolumeMountSubPath: volumeMountSubPathValue,
}
}
func equalStringSlice(s1, s2 []string) bool {
if len(s1) != len(s2) {
return false
@@ -1023,3 +1030,24 @@ func TestServiceExternalTrafficPolicy(t *testing.T) {
}
}
}
func TestVolumeMountSubPath(t *testing.T) {
groupName := "pod_group"
expectedSubPathValue := "test-subpath"
komposeObject := kobject.KomposeObject{
ServiceConfigs: map[string]kobject.ServiceConfig{"app": newServiceConfigWithServiceVolumeMount(expectedSubPathValue)},
}
k := Kubernetes{}
objs, err := k.Transform(komposeObject, kobject.ConvertOptions{ServiceGroupMode: groupName})
if err != nil {
t.Error(errors.Wrap(err, "k.Transform failed"))
}
for _, obj := range objs {
if deployment, ok := obj.(*appsv1.Deployment); ok {
volMountSubPath := deployment.Spec.Template.Spec.Containers[0].VolumeMounts[0].SubPath
if volMountSubPath != expectedSubPathValue {
t.Errorf("Expected VolumeMount Subpath %v, got %v", expectedSubPathValue, volMountSubPath)
}
}
}
}