Skip to content

Commit b58250b

Browse files
Merge pull request #25664 from victortoso/add-kube-subpath
Add volume SubPath in generate kube
2 parents ffcad3c + 321634d commit b58250b

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

libpod/kube.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,6 +1195,7 @@ func generateKubePersistentVolumeClaim(v *ContainerNamedVolume) (v1.VolumeMount,
11951195
vm.Name = name
11961196
vm.MountPath = v.Dest
11971197
vm.ReadOnly = ro
1198+
vm.SubPath = v.SubPath
11981199

11991200
pvc := v1.PersistentVolumeClaimVolumeSource{ClaimName: vName, ReadOnly: ro}
12001201
vs := v1.VolumeSource{}

test/e2e/generate_kube_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,57 @@ var _ = Describe("Podman kube generate", func() {
849849
Expect(inspect.OutputToString()).To(ContainSubstring(vol1))
850850
})
851851

852+
It("with subpath volume", func() {
853+
// We want to verify that generating Volume's subPath is working properly
854+
// https://kubernetes.io/docs/concepts/storage/volumes/#using-subpath
855+
// by creating a volume with two directories and mounthing them separately
856+
volName := "vol-test1"
857+
pvcName := fmt.Sprintf("%s-pvc", volName)
858+
podmanTest.PodmanExitCleanly("volume", "create", volName)
859+
860+
mountPoint := "/mnt"
861+
862+
etcFile := "etcfile"
863+
etcDirPath := filepath.Join(mountPoint, "etc")
864+
etcSubPath := filepath.Join("/etc", etcFile)
865+
etcMountSubPath := filepath.Join(mountPoint, etcSubPath)
866+
populateEtcCmd := fmt.Sprintf("mkdir -p %s; touch %s", etcDirPath, etcMountSubPath)
867+
868+
varFile := "varfile"
869+
varDirPath := filepath.Join(mountPoint, "var")
870+
varSubPath := filepath.Join("/var", varFile)
871+
varMountSubPath := filepath.Join(mountPoint, varSubPath)
872+
populateVarCmd := fmt.Sprintf("mkdir -p %s; touch %s", varDirPath, varMountSubPath)
873+
874+
cmd := fmt.Sprintf("%s; %s", populateEtcCmd, populateVarCmd)
875+
vol := fmt.Sprintf("%s:%s", volName, mountPoint)
876+
podmanTest.PodmanExitCleanly("run", "-v", vol, ALPINE, "sh", "-c", cmd)
877+
878+
etcTargetPath := filepath.Join(mountPoint, etcFile)
879+
varTargetPath := filepath.Join(mountPoint, varFile)
880+
mountTemplate := "type=volume,source=%s,volume-subpath=%s,target=%s"
881+
podmanTest.PodmanExitCleanly("run", "-d", "--pod", "new:test1", "--name", "test-ctr",
882+
"--mount", fmt.Sprintf(mountTemplate, volName, etcSubPath, etcTargetPath),
883+
"--mount", fmt.Sprintf(mountTemplate, volName, varSubPath, varTargetPath),
884+
CITEST_IMAGE, "top")
885+
886+
kube := podmanTest.PodmanExitCleanly("kube", "generate", "test1")
887+
888+
pod := new(v1.Pod)
889+
err = yaml.Unmarshal(kube.Out.Contents(), pod)
890+
Expect(err).ToNot(HaveOccurred())
891+
Expect(pod.Spec.Containers[0].VolumeMounts).To(ContainElements(v1.VolumeMount{
892+
Name: pvcName,
893+
MountPath: etcTargetPath,
894+
SubPath: etcSubPath,
895+
}, v1.VolumeMount{
896+
Name: pvcName,
897+
MountPath: varTargetPath,
898+
SubPath: varSubPath,
899+
}),
900+
)
901+
})
902+
852903
It("when bind-mounting '/' and '/root' at the same time ", func() {
853904
// Fixes https://github.com/containers/podman/issues/9764
854905

0 commit comments

Comments
 (0)