summaryrefslogtreecommitdiff
path: root/libpod/kube.go
diff options
context:
space:
mode:
authorPeter Hunt <pehunt@redhat.com>2019-07-02 11:40:14 -0400
committerPeter Hunt <pehunt@redhat.com>2019-07-02 15:18:44 -0400
commitaeabc45ccef10fc858677765675bd86944a93db6 (patch)
tree11629dba0ec30e90dd5c8a3aa728aa06e03fb650 /libpod/kube.go
parent341f5e22e5426e83e20174913fa274809a1f47ca (diff)
downloadpodman-aeabc45ccef10fc858677765675bd86944a93db6.tar.gz
podman-aeabc45ccef10fc858677765675bd86944a93db6.tar.bz2
podman-aeabc45ccef10fc858677765675bd86944a93db6.zip
Improve parsing of mounts
Specifically, we were needlessly doing a double lookup to find which config mounts were user volumes. Improve this by refactoring a bit of code from inspect Signed-off-by: Peter Hunt <pehunt@redhat.com>
Diffstat (limited to 'libpod/kube.go')
-rw-r--r--libpod/kube.go95
1 files changed, 48 insertions, 47 deletions
diff --git a/libpod/kube.go b/libpod/kube.go
index 6a52ff80b..08ab7a4a4 100644
--- a/libpod/kube.go
+++ b/libpod/kube.go
@@ -340,40 +340,58 @@ func libpodMaxAndMinToResourceList(c *Container) (v1.ResourceList, v1.ResourceLi
return maxResources, minResources
}
-func generateKubeVolumeMount(hostSourcePath string, mounts []specs.Mount) (v1.VolumeMount, v1.Volume, error) {
+// libpodMountsToKubeVolumeMounts converts the containers mounts to a struct kube understands
+func libpodMountsToKubeVolumeMounts(c *Container) ([]v1.VolumeMount, []v1.Volume, error) {
+ var vms []v1.VolumeMount
+ var vos []v1.Volume
+
+ // TjDO when named volumes are supported in play kube, also parse named volumes here
+ _, mounts := c.sortUserVolumes(c.config.Spec)
+ for _, m := range mounts {
+ vm, vo, err := generateKubeVolumeMount(m)
+ if err != nil {
+ return vms, vos, err
+ }
+ vms = append(vms, vm)
+ vos = append(vos, vo)
+ }
+ return vms, vos, nil
+}
+
+// generateKubeVolumeMount takes a user specfied mount and returns
+// a kubernetes VolumeMount (to be added to the container) and a kubernetes Volume
+// (to be added to the pod)
+func generateKubeVolumeMount(m specs.Mount) (v1.VolumeMount, v1.Volume, error) {
vm := v1.VolumeMount{}
vo := v1.Volume{}
- for _, m := range mounts {
- if m.Destination == hostSourcePath {
- name, err := convertVolumePathToName(m.Source)
- if err != nil {
- return vm, vo, err
- }
- vm.Name = name
- vm.MountPath = m.Destination
- if util.StringInSlice("ro", m.Options) {
- vm.ReadOnly = true
- }
- vo.Name = name
- vo.HostPath = &v1.HostPathVolumeSource{}
- vo.HostPath.Path = m.Source
- isDir, err := isHostPathDirectory(m.Source)
- // neither a directory or a file lives here, default to creating a directory
- // TODO should this be an error instead?
- var hostPathType v1.HostPathType
- if err != nil {
- hostPathType = v1.HostPathDirectoryOrCreate
- } else if isDir {
- hostPathType = v1.HostPathDirectory
- } else {
- hostPathType = v1.HostPathFile
- }
- vo.HostPath.Type = &hostPathType
- return vm, vo, nil
- }
+ name, err := convertVolumePathToName(m.Source)
+ if err != nil {
+ return vm, vo, err
+ }
+ vm.Name = name
+ vm.MountPath = m.Destination
+ if util.StringInSlice("ro", m.Options) {
+ vm.ReadOnly = true
+ }
+
+ vo.Name = name
+ vo.HostPath = &v1.HostPathVolumeSource{}
+ vo.HostPath.Path = m.Source
+ isDir, err := isHostPathDirectory(m.Source)
+ // neither a directory or a file lives here, default to creating a directory
+ // TODO should this be an error instead?
+ var hostPathType v1.HostPathType
+ if err != nil {
+ hostPathType = v1.HostPathDirectoryOrCreate
+ } else if isDir {
+ hostPathType = v1.HostPathDirectory
+ } else {
+ hostPathType = v1.HostPathFile
}
- return vm, vo, errors.New("unable to find mount source")
+ vo.HostPath.Type = &hostPathType
+
+ return vm, vo, nil
}
func isHostPathDirectory(hostPathSource string) (bool, error) {
@@ -400,23 +418,6 @@ func convertVolumePathToName(hostSourcePath string) (string, error) {
return strings.Replace(strings.Trim(hostSourcePath, "/"), "/", "-", -1), nil
}
-// libpodMountsToKubeVolumeMounts converts the containers mounts to a struct kube understands
-func libpodMountsToKubeVolumeMounts(c *Container) ([]v1.VolumeMount, []v1.Volume, error) {
- // At this point, I dont think we can distinguish between the default
- // volume mounts and user added ones. For now, we pass them all.
- var vms []v1.VolumeMount
- var vos []v1.Volume
- for _, hostSourcePath := range c.config.UserVolumes {
- vm, vo, err := generateKubeVolumeMount(hostSourcePath, c.config.Spec.Mounts)
- if err != nil {
- return vms, vos, err
- }
- vms = append(vms, vm)
- vos = append(vos, vo)
- }
- return vms, vos, nil
-}
-
func determineCapAddDropFromCapabilities(defaultCaps, containerCaps []string) *v1.Capabilities {
var (
drop []v1.Capability