From 1307e96d5e209ce139527c01a56da57d75b66fd9 Mon Sep 17 00:00:00 2001 From: Peter Hunt Date: Mon, 1 Jul 2019 16:07:25 -0400 Subject: Configure container volumes for generate kube Signed-off-by: Peter Hunt --- libpod/kube.go | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) (limited to 'libpod') diff --git a/libpod/kube.go b/libpod/kube.go index 1622246d5..d22df1ee7 100644 --- a/libpod/kube.go +++ b/libpod/kube.go @@ -328,9 +328,14 @@ func libpodMaxAndMinToResourceList(c *Container) (v1.ResourceList, v1.ResourceLi func generateKubeVolumeMount(hostSourcePath string, mounts []specs.Mount) (v1.VolumeMount, error) { vm := v1.VolumeMount{} for _, m := range mounts { - if m.Source == hostSourcePath { + fmt.Println(m.Destination, hostSourcePath) + if m.Destination == hostSourcePath { // TODO Name is not provided and is required by Kube; therefore, this is disabled earlier - //vm.Name = + name, err := convertVolumePathToName(m.Source) + if err != nil { + return vm, err + } + vm.Name = name vm.MountPath = m.Source vm.SubPath = m.Destination if util.StringInSlice("ro", m.Options) { @@ -342,6 +347,22 @@ func generateKubeVolumeMount(hostSourcePath string, mounts []specs.Mount) (v1.Vo return vm, errors.New("unable to find mount source") } +func convertVolumePathToName(hostSourcePath string) (string, error) { + if len(hostSourcePath) == 0 { + return "", errors.Errorf("hostSourcePath must be specified to generate volume name") + } + if len(hostSourcePath) == 1 { + if hostSourcePath != "/" { + return "", errors.Errorf("hostSourcePath malformatted: %s", hostSourcePath) + } + // add special case name + return "root", nil + } + // First, trim trailing slashes, then replace slashes with dashes. + // Thus, /mnt/data/ will become mnt-data + return strings.Replace(strings.Trim(hostSourcePath, "/"), "/", "-", -1), nil +} + // libpodMountsToKubeVolumeMounts converts the containers mounts to a struct kube understands func libpodMountsToKubeVolumeMounts(c *Container) ([]v1.VolumeMount, error) { // At this point, I dont think we can distinguish between the default @@ -350,7 +371,7 @@ func libpodMountsToKubeVolumeMounts(c *Container) ([]v1.VolumeMount, error) { for _, hostSourcePath := range c.config.UserVolumes { vm, err := generateKubeVolumeMount(hostSourcePath, c.config.Spec.Mounts) if err != nil { - continue + return vms, err } vms = append(vms, vm) } -- cgit v1.2.3-54-g00ecf