Merge pull request #248 from dustymabe/dusty-empty-vols

Adding support for choosing empty volumes
This commit is contained in:
Tomas Kral 2016-10-30 17:49:50 +02:00 committed by GitHub
commit d9c5de661a
4 changed files with 69 additions and 16 deletions

View File

@ -139,6 +139,7 @@ func Convert(c *cli.Context) {
CreateDS: c.BoolT("daemonset"), CreateDS: c.BoolT("daemonset"),
CreateRC: c.BoolT("replicationcontroller"), CreateRC: c.BoolT("replicationcontroller"),
CreateDeploymentConfig: c.BoolT("deploymentconfig"), CreateDeploymentConfig: c.BoolT("deploymentconfig"),
EmptyVols: c.BoolT("emptyvols"),
} }
validateFlags(c, &opt) validateFlags(c, &opt)
@ -171,6 +172,7 @@ func Up(c *cli.Context) {
InputFile: c.GlobalString("file"), InputFile: c.GlobalString("file"),
Replicas: 1, Replicas: 1,
Provider: strings.ToLower(c.GlobalString("provider")), Provider: strings.ToLower(c.GlobalString("provider")),
EmptyVols: c.BoolT("emptyvols"),
} }
validateFlags(c, &opt) validateFlags(c, &opt)
validateControllers(&opt) validateControllers(&opt)
@ -202,6 +204,7 @@ func Down(c *cli.Context) {
InputFile: c.GlobalString("file"), InputFile: c.GlobalString("file"),
Replicas: 1, Replicas: 1,
Provider: strings.ToLower(c.GlobalString("provider")), Provider: strings.ToLower(c.GlobalString("provider")),
EmptyVols: c.BoolT("emptyvols"),
} }
validateFlags(c, &opt) validateFlags(c, &opt)
validateControllers(&opt) validateControllers(&opt)

View File

@ -145,6 +145,10 @@ func commonConvertFlags() []cli.Flag {
Name: "stdout", Name: "stdout",
Usage: "Print converted objects to stdout", Usage: "Print converted objects to stdout",
}, },
cli.BoolFlag{
Name: "emptyvols",
Usage: "Use Empty Volumes. Don't generate PVCs",
},
} }
} }
@ -156,6 +160,12 @@ func UpCommand() cli.Command {
Action: func(c *cli.Context) { Action: func(c *cli.Context) {
app.Up(c) app.Up(c)
}, },
Flags: []cli.Flag{
cli.BoolFlag{
Name: "emptyvols",
Usage: "Use Empty Volumes. Don't generate PVCs",
},
},
} }
} }
@ -167,6 +177,12 @@ func DownCommand() cli.Command {
Action: func(c *cli.Context) { Action: func(c *cli.Context) {
app.Down(c) app.Down(c)
}, },
Flags: []cli.Flag{
cli.BoolFlag{
Name: "emptyvols",
Usage: "Use Empty Volumes. Don't generate PVCs",
},
},
} }
} }

View File

@ -113,6 +113,7 @@ type ConvertOptions struct {
CreateDeploymentConfig bool CreateDeploymentConfig bool
CreateChart bool CreateChart bool
GenerateYaml bool GenerateYaml bool
EmptyVols bool
Replicas int Replicas int
InputFile string InputFile string
OutFile string OutFile string

View File

@ -219,9 +219,13 @@ func (k *Kubernetes) ConfigServicePorts(name string, service kobject.ServiceConf
// Configure the container volumes. // Configure the container volumes.
func (k *Kubernetes) ConfigVolumes(name string, service kobject.ServiceConfig) ([]api.VolumeMount, []api.Volume, []*api.PersistentVolumeClaim) { func (k *Kubernetes) ConfigVolumes(name string, service kobject.ServiceConfig) ([]api.VolumeMount, []api.Volume, []*api.PersistentVolumeClaim) {
volumesMount := []api.VolumeMount{} volumeMounts := []api.VolumeMount{}
volumes := []api.Volume{} volumes := []api.Volume{}
var pvc []*api.PersistentVolumeClaim var PVCs []*api.PersistentVolumeClaim
// Set a var based on if the user wants to use emtpy volumes
// as opposed to persistent volumes and volume claims
useEmptyVolumes := k.Opt.EmptyVols
var count int var count int
for _, volume := range service.Volumes { for _, volume := range service.Volumes {
@ -230,37 +234,66 @@ func (k *Kubernetes) ConfigVolumes(name string, service kobject.ServiceConfig) (
logrus.Warningf("Failed to configure container volume: %v", err) logrus.Warningf("Failed to configure container volume: %v", err)
continue continue
} }
if volumeName == "" {
volumeName = fmt.Sprintf("%s-claim%d", name, count)
count++
}
// check if ro/rw mode is defined, default rw // check if ro/rw mode is defined, default rw
readonly := len(mode) > 0 && mode == "ro" 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{ volmount := api.VolumeMount{
Name: volumeName, Name: volumeName,
ReadOnly: readonly, ReadOnly: readonly,
MountPath: container, MountPath: container,
} }
volumesMount = append(volumesMount, volmount) 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{ vol := api.Volume{
Name: volumeName, Name: volumeName,
VolumeSource: api.VolumeSource{ VolumeSource: *volsource,
PersistentVolumeClaim: &api.PersistentVolumeClaimVolumeSource{
ClaimName: volumeName,
ReadOnly: readonly,
},
},
} }
volumes = append(volumes, vol) volumes = append(volumes, vol)
if len(host) > 0 { if len(host) > 0 {
logrus.Warningf("Volume mount on the host %q isn't supported - ignoring path on the host", host) logrus.Warningf("Volume mount on the host %q isn't supported - ignoring path on the host", host)
} }
pvc = append(pvc, k.CreatePVC(volumeName, mode))
} }
return volumesMount, volumes, pvc 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. // Configure the environment variables.