mirror of
https://github.com/containers/podman.git
synced 2025-06-24 19:42:56 +08:00
Merge pull request #7494 from haircommander/play-kube-socket
play kube: handle Socket HostPath type
This commit is contained in:
@ -250,13 +250,22 @@ func (ic *ContainerEngine) playKubePod(ctx context.Context, podName string, podY
|
|||||||
if err := libpod.LabelVolumePath(hostPath.Path); err != nil {
|
if err := libpod.LabelVolumePath(hostPath.Path); err != nil {
|
||||||
return nil, errors.Wrapf(err, "Error giving %s a label", hostPath.Path)
|
return nil, errors.Wrapf(err, "Error giving %s a label", hostPath.Path)
|
||||||
}
|
}
|
||||||
|
case v1.HostPathSocket:
|
||||||
|
st, err := os.Stat(hostPath.Path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrapf(err, "Error checking HostPathSocket")
|
||||||
|
}
|
||||||
|
if st.Mode()&os.ModeSocket != os.ModeSocket {
|
||||||
|
return nil, errors.Errorf("Error checking HostPathSocket: path %s is not a socket", hostPath.Path)
|
||||||
|
}
|
||||||
|
|
||||||
case v1.HostPathDirectory:
|
case v1.HostPathDirectory:
|
||||||
case v1.HostPathFile:
|
case v1.HostPathFile:
|
||||||
case v1.HostPathUnset:
|
case v1.HostPathUnset:
|
||||||
// do nothing here because we will verify the path exists in validateVolumeHostDir
|
// do nothing here because we will verify the path exists in validateVolumeHostDir
|
||||||
break
|
break
|
||||||
default:
|
default:
|
||||||
return nil, errors.Errorf("Directories are the only supported HostPath type")
|
return nil, errors.Errorf("Invalid HostPath type %v", hostPath.Type)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,6 +94,15 @@ spec:
|
|||||||
{{ end }}
|
{{ end }}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
{{ with .Volumes }}
|
||||||
|
volumes:
|
||||||
|
{{ range . }}
|
||||||
|
- name: {{ .Name }}
|
||||||
|
hostPath:
|
||||||
|
path: {{ .Path }}
|
||||||
|
type: {{ .Type }}
|
||||||
|
{{ end }}
|
||||||
|
{{ end }}
|
||||||
status: {}
|
status: {}
|
||||||
`
|
`
|
||||||
|
|
||||||
@ -186,6 +195,7 @@ var (
|
|||||||
defaultCtrArg = []string{"-d", "1.5"}
|
defaultCtrArg = []string{"-d", "1.5"}
|
||||||
defaultCtrImage = ALPINE
|
defaultCtrImage = ALPINE
|
||||||
defaultPodName = "testPod"
|
defaultPodName = "testPod"
|
||||||
|
defaultVolName = "testVol"
|
||||||
defaultDeploymentName = "testDeployment"
|
defaultDeploymentName = "testDeployment"
|
||||||
seccompPwdEPERM = []byte(`{"defaultAction":"SCMP_ACT_ALLOW","syscalls":[{"name":"getcwd","action":"SCMP_ACT_ERRNO"}]}`)
|
seccompPwdEPERM = []byte(`{"defaultAction":"SCMP_ACT_ALLOW","syscalls":[{"name":"getcwd","action":"SCMP_ACT_ERRNO"}]}`)
|
||||||
)
|
)
|
||||||
@ -240,6 +250,7 @@ type Pod struct {
|
|||||||
Name string
|
Name string
|
||||||
Hostname string
|
Hostname string
|
||||||
Ctrs []*Ctr
|
Ctrs []*Ctr
|
||||||
|
Volumes []*Volume
|
||||||
Annotations map[string]string
|
Annotations map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -247,7 +258,7 @@ type Pod struct {
|
|||||||
// and the configured options
|
// and the configured options
|
||||||
// if no containers are added, it will add the default container
|
// if no containers are added, it will add the default container
|
||||||
func getPod(options ...podOption) *Pod {
|
func getPod(options ...podOption) *Pod {
|
||||||
p := Pod{defaultPodName, "", make([]*Ctr, 0), make(map[string]string)}
|
p := Pod{defaultPodName, "", make([]*Ctr, 0), make([]*Volume, 0), make(map[string]string)}
|
||||||
for _, option := range options {
|
for _, option := range options {
|
||||||
option(&p)
|
option(&p)
|
||||||
}
|
}
|
||||||
@ -277,6 +288,12 @@ func withAnnotation(k, v string) podOption {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func withVolume(v *Volume) podOption {
|
||||||
|
return func(pod *Pod) {
|
||||||
|
pod.Volumes = append(pod.Volumes, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Deployment describes the options a kube yaml can be configured at deployment level
|
// Deployment describes the options a kube yaml can be configured at deployment level
|
||||||
type Deployment struct {
|
type Deployment struct {
|
||||||
Name string
|
Name string
|
||||||
@ -412,6 +429,22 @@ func getCtrNameInPod(pod *Pod) string {
|
|||||||
return fmt.Sprintf("%s-%s", pod.Name, defaultCtrName)
|
return fmt.Sprintf("%s-%s", pod.Name, defaultCtrName)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Volume struct {
|
||||||
|
Name string
|
||||||
|
Path string
|
||||||
|
Type string
|
||||||
|
}
|
||||||
|
|
||||||
|
// getVolume takes a type and a location for a volume
|
||||||
|
// giving it a default name of volName
|
||||||
|
func getVolume(vType, vPath string) *Volume {
|
||||||
|
return &Volume{
|
||||||
|
Name: defaultVolName,
|
||||||
|
Path: vPath,
|
||||||
|
Type: vType,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var _ = Describe("Podman generate kube", func() {
|
var _ = Describe("Podman generate kube", func() {
|
||||||
var (
|
var (
|
||||||
tempdir string
|
tempdir string
|
||||||
@ -853,4 +886,106 @@ spec:
|
|||||||
Expect(inspect.ExitCode()).To(Equal(0))
|
Expect(inspect.ExitCode()).To(Equal(0))
|
||||||
Expect(inspect.OutputToString()).To(Equal("5000/tcp -> 127.0.0.100:5000"))
|
Expect(inspect.OutputToString()).To(Equal("5000/tcp -> 127.0.0.100:5000"))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
It("podman play kube test with non-existent empty HostPath type volume", func() {
|
||||||
|
hostPathLocation := filepath.Join(tempdir, "file")
|
||||||
|
|
||||||
|
pod := getPod(withVolume(getVolume(`""`, hostPathLocation)))
|
||||||
|
err := generatePodKubeYaml(pod, kubeYaml)
|
||||||
|
Expect(err).To(BeNil())
|
||||||
|
|
||||||
|
kube := podmanTest.Podman([]string{"play", "kube", kubeYaml})
|
||||||
|
kube.WaitWithDefaultTimeout()
|
||||||
|
Expect(kube.ExitCode()).NotTo(Equal(0))
|
||||||
|
})
|
||||||
|
|
||||||
|
It("podman play kube test with empty HostPath type volume", func() {
|
||||||
|
hostPathLocation := filepath.Join(tempdir, "file")
|
||||||
|
f, err := os.Create(hostPathLocation)
|
||||||
|
Expect(err).To(BeNil())
|
||||||
|
f.Close()
|
||||||
|
|
||||||
|
pod := getPod(withVolume(getVolume(`""`, hostPathLocation)))
|
||||||
|
err = generatePodKubeYaml(pod, kubeYaml)
|
||||||
|
Expect(err).To(BeNil())
|
||||||
|
|
||||||
|
kube := podmanTest.Podman([]string{"play", "kube", kubeYaml})
|
||||||
|
kube.WaitWithDefaultTimeout()
|
||||||
|
Expect(kube.ExitCode()).To(Equal(0))
|
||||||
|
})
|
||||||
|
|
||||||
|
It("podman play kube test with non-existent File HostPath type volume", func() {
|
||||||
|
hostPathLocation := filepath.Join(tempdir, "file")
|
||||||
|
|
||||||
|
pod := getPod(withVolume(getVolume("File", hostPathLocation)))
|
||||||
|
err := generatePodKubeYaml(pod, kubeYaml)
|
||||||
|
Expect(err).To(BeNil())
|
||||||
|
|
||||||
|
kube := podmanTest.Podman([]string{"play", "kube", kubeYaml})
|
||||||
|
kube.WaitWithDefaultTimeout()
|
||||||
|
Expect(kube.ExitCode()).NotTo(Equal(0))
|
||||||
|
})
|
||||||
|
|
||||||
|
It("podman play kube test with File HostPath type volume", func() {
|
||||||
|
hostPathLocation := filepath.Join(tempdir, "file")
|
||||||
|
f, err := os.Create(hostPathLocation)
|
||||||
|
Expect(err).To(BeNil())
|
||||||
|
f.Close()
|
||||||
|
|
||||||
|
pod := getPod(withVolume(getVolume("File", hostPathLocation)))
|
||||||
|
err = generatePodKubeYaml(pod, kubeYaml)
|
||||||
|
Expect(err).To(BeNil())
|
||||||
|
|
||||||
|
kube := podmanTest.Podman([]string{"play", "kube", kubeYaml})
|
||||||
|
kube.WaitWithDefaultTimeout()
|
||||||
|
Expect(kube.ExitCode()).To(Equal(0))
|
||||||
|
})
|
||||||
|
|
||||||
|
It("podman play kube test with FileOrCreate HostPath type volume", func() {
|
||||||
|
hostPathLocation := filepath.Join(tempdir, "file")
|
||||||
|
|
||||||
|
pod := getPod(withVolume(getVolume("FileOrCreate", hostPathLocation)))
|
||||||
|
err := generatePodKubeYaml(pod, kubeYaml)
|
||||||
|
Expect(err).To(BeNil())
|
||||||
|
|
||||||
|
kube := podmanTest.Podman([]string{"play", "kube", kubeYaml})
|
||||||
|
kube.WaitWithDefaultTimeout()
|
||||||
|
Expect(kube.ExitCode()).To(Equal(0))
|
||||||
|
|
||||||
|
// the file should have been created
|
||||||
|
_, err = os.Stat(hostPathLocation)
|
||||||
|
Expect(err).To(BeNil())
|
||||||
|
})
|
||||||
|
|
||||||
|
It("podman play kube test with DirectoryOrCreate HostPath type volume", func() {
|
||||||
|
hostPathLocation := filepath.Join(tempdir, "file")
|
||||||
|
|
||||||
|
pod := getPod(withVolume(getVolume("DirectoryOrCreate", hostPathLocation)))
|
||||||
|
err := generatePodKubeYaml(pod, kubeYaml)
|
||||||
|
Expect(err).To(BeNil())
|
||||||
|
|
||||||
|
kube := podmanTest.Podman([]string{"play", "kube", kubeYaml})
|
||||||
|
kube.WaitWithDefaultTimeout()
|
||||||
|
Expect(kube.ExitCode()).To(Equal(0))
|
||||||
|
|
||||||
|
// the file 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 Socket HostPath type volume should fail if not socket", func() {
|
||||||
|
hostPathLocation := filepath.Join(tempdir, "file")
|
||||||
|
f, err := os.Create(hostPathLocation)
|
||||||
|
Expect(err).To(BeNil())
|
||||||
|
f.Close()
|
||||||
|
|
||||||
|
pod := getPod(withVolume(getVolume("Socket", hostPathLocation)))
|
||||||
|
err = generatePodKubeYaml(pod, kubeYaml)
|
||||||
|
Expect(err).To(BeNil())
|
||||||
|
|
||||||
|
kube := podmanTest.Podman([]string{"play", "kube", kubeYaml})
|
||||||
|
kube.WaitWithDefaultTimeout()
|
||||||
|
Expect(kube.ExitCode()).NotTo(Equal(0))
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user