Skip to content

Commit

Permalink
Merge pull request #16067 from tyler92/fix-directory-or-create
Browse files Browse the repository at this point in the history
Create a full path to a directory when DirectoryOrCreate is used
  • Loading branch information
openshift-merge-robot authored Oct 6, 2022
2 parents eb85012 + 8b189c0 commit ab3d3f8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
6 changes: 2 additions & 4 deletions pkg/specgen/generate/kube/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,8 @@ func VolumeFromHostPath(hostPath *v1.HostPathVolumeSource) (*KubeVolume, error)
if hostPath.Type != nil {
switch *hostPath.Type {
case v1.HostPathDirectoryOrCreate:
if _, err := os.Stat(hostPath.Path); os.IsNotExist(err) {
if err := os.Mkdir(hostPath.Path, kubeDirectoryPermission); err != nil {
return nil, err
}
if err := os.MkdirAll(hostPath.Path, kubeDirectoryPermission); err != nil {
return nil, err
}
// Label a newly created volume
if err := libpod.LabelVolumePath(hostPath.Path); err != nil {
Expand Down
30 changes: 30 additions & 0 deletions test/e2e/play_kube_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2604,6 +2604,36 @@ spec:
Expect(st.Mode().IsDir()).To(Equal(true))
})

It("podman play kube test with DirectoryOrCreate HostPath type volume and non-existent directory path", func() {
hostPathLocation := filepath.Join(filepath.Join(tempdir, "dir1"), "dir2")

pod := getPod(withVolume(getHostPathVolume("DirectoryOrCreate", hostPathLocation)))
err := generateKubeYaml("pod", pod, kubeYaml)
Expect(err).To(BeNil())

kube := podmanTest.Podman([]string{"play", "kube", kubeYaml})
kube.WaitWithDefaultTimeout()
Expect(kube).Should(Exit(0))

// the full path should have been created
st, err := os.Stat(hostPathLocation)
Expect(err).To(BeNil())
Expect(st.Mode().IsDir()).To(Equal(true))
})

It("podman play kube test with DirectoryOrCreate HostPath type volume and existent directory path", func() {
hostPathLocation := filepath.Join(filepath.Join(tempdir, "dir1"), "dir2")
Expect(os.MkdirAll(hostPathLocation, os.ModePerm)).To(BeNil())

pod := getPod(withVolume(getHostPathVolume("DirectoryOrCreate", hostPathLocation)))
err := generateKubeYaml("pod", pod, kubeYaml)
Expect(err).To(BeNil())

kube := podmanTest.Podman([]string{"play", "kube", kubeYaml})
kube.WaitWithDefaultTimeout()
Expect(kube).Should(Exit(0))
})

It("podman play kube test with Socket HostPath type volume should fail if not socket", func() {
hostPathLocation := filepath.Join(tempdir, "file")
f, err := os.Create(hostPathLocation)
Expand Down

0 comments on commit ab3d3f8

Please sign in to comment.