From df09fd5193061ab706dbe687883ed965dd52cec4 Mon Sep 17 00:00:00 2001 From: Dusty Mabe Date: Tue, 25 Oct 2016 22:34:21 -0400 Subject: [PATCH] ConfigVolumes: enable creating EmptyDir volumes again Will enable the user to specify which type of volume they want in a subsequent commit. --- pkg/transformer/kubernetes/kubernetes.go | 55 ++++++++++++++++++------ 1 file changed, 43 insertions(+), 12 deletions(-) diff --git a/pkg/transformer/kubernetes/kubernetes.go b/pkg/transformer/kubernetes/kubernetes.go index c275dd67..7f3bd814 100644 --- a/pkg/transformer/kubernetes/kubernetes.go +++ b/pkg/transformer/kubernetes/kubernetes.go @@ -223,6 +223,8 @@ func (k *Kubernetes) ConfigVolumes(name string, service kobject.ServiceConfig) ( volumes := []api.Volume{} var PVCs []*api.PersistentVolumeClaim + useEmptyVolumes := true + var count int for _, volume := range service.Volumes { volumeName, host, container, mode, err := transformer.ParseVolume(volume) @@ -230,13 +232,20 @@ func (k *Kubernetes) ConfigVolumes(name string, service kobject.ServiceConfig) ( logrus.Warningf("Failed to configure container volume: %v", err) continue } - if volumeName == "" { - volumeName = fmt.Sprintf("%s-claim%d", name, count) - count++ - } + // check if ro/rw mode is defined, default rw readonly := len(mode) > 0 && mode == "ro" + if volumeName == "" { + if useEmptyVolumes { + volumeName = fmt.Sprintf("%s-empty%d", name, count) + } else { + volumeName = fmt.Sprintf("%s-claim%d", name, count) + } + count++ + } + + // create a new volume mount object and append to list volmount := api.VolumeMount{ Name: volumeName, ReadOnly: readonly, @@ -244,25 +253,47 @@ func (k *Kubernetes) ConfigVolumes(name string, service kobject.ServiceConfig) ( } volumeMounts = append(volumeMounts, volmount) + // Get a volume source based on the type of volume we are using + // For PVC we will also create a PVC object and add to list + var volsource *api.VolumeSource + if useEmptyVolumes { + volsource = k.ConfigEmptyVolumeSource() + } else { + volsource = k.ConfigPVCVolumeSource(volumeName, readonly) + PVCs = append(PVCs, k.CreatePVC(volumeName, mode)) + } + + // create a new volume object using the volsource and add to list vol := api.Volume{ - Name: volumeName, - VolumeSource: api.VolumeSource{ - PersistentVolumeClaim: &api.PersistentVolumeClaimVolumeSource{ - ClaimName: volumeName, - ReadOnly: readonly, - }, - }, + Name: volumeName, + VolumeSource: *volsource, } volumes = append(volumes, vol) if len(host) > 0 { logrus.Warningf("Volume mount on the host %q isn't supported - ignoring path on the host", host) } - PVCs = append(PVCs, k.CreatePVC(volumeName, mode)) } return volumeMounts, volumes, PVCs } +// helper function to create an EmptyDir api.VolumeSource +func (k *Kubernetes) ConfigEmptyVolumeSource() *api.VolumeSource { + return &api.VolumeSource{ + EmptyDir: &api.EmptyDirVolumeSource{}, + } +} + +// helper function to create an api.VolumeSource with a PVC +func (k *Kubernetes) ConfigPVCVolumeSource(name string, readonly bool) *api.VolumeSource { + return &api.VolumeSource{ + PersistentVolumeClaim: &api.PersistentVolumeClaimVolumeSource{ + ClaimName: name, + ReadOnly: readonly, + }, + } +} + // Configure the environment variables. func (k *Kubernetes) ConfigEnvs(name string, service kobject.ServiceConfig) []api.EnvVar { envs := []api.EnvVar{}