Added support for tmpfs

fixes #436
This commit will add support for tmpfs, configEmptyVolumeSource
function is being modified as it have to work in two ways now.
(For emptyvols and tmpfs)
Added unit test for tmpfs too.
This commit is contained in:
Suraj Narwade 2017-03-10 16:08:00 +05:30
parent 54f133958f
commit 494133484c
5 changed files with 69 additions and 3 deletions

View File

@ -83,6 +83,7 @@ type ServiceConfig struct {
Stdin bool `compose:"stdin_open" bundle:""`
Tty bool `compose:"tty" bundle:""`
MemLimit yaml.MemStringorInt `compose:"mem_limit" bundle:""`
TmpFs []string `compose:"tmpfs" bundle:""`
}
// EnvVar holds the environment variable struct of a container

View File

@ -375,7 +375,7 @@ func (c *Compose) LoadFile(files []string) (kobject.KomposeObject, error) {
serviceConfig.Stdin = composeServiceConfig.StdinOpen
serviceConfig.Tty = composeServiceConfig.Tty
serviceConfig.MemLimit = composeServiceConfig.MemLimit
serviceConfig.TmpFs = composeServiceConfig.Tmpfs
komposeObject.ServiceConfigs[name] = serviceConfig
}

View File

@ -336,6 +336,18 @@ func (k *Kubernetes) UpdateKubernetesObjects(name string, service kobject.Servic
if err != nil {
return errors.Wrap(err, "k.ConfigVolumes failed")
}
// Configure Tmpfs
if len(service.TmpFs) > 0 {
TmpVolumesMount, TmpVolumes := k.ConfigTmpfs(name, service)
for _, volume := range TmpVolumes {
volumes = append(volumes, volume)
}
for _, vMount := range TmpVolumesMount {
volumesMount = append(volumesMount, vMount)
}
}
if pvc != nil {
// Looping on the slice pvc instead of `*objects = append(*objects, pvc...)`

View File

@ -324,6 +324,36 @@ func (k *Kubernetes) ConfigServicePorts(name string, service kobject.ServiceConf
return servicePorts
}
// ConfigTmpfs configure the tmpfs.
func (k *Kubernetes) ConfigTmpfs(name string, service kobject.ServiceConfig) ([]api.VolumeMount, []api.Volume) {
//initializing volumemounts and volumes
volumeMounts := []api.VolumeMount{}
volumes := []api.Volume{}
for index, volume := range service.TmpFs {
//naming volumes if multiple tmpfs are provided
volumeName := fmt.Sprintf("%s-tmpfs%d", name, index)
// create a new volume mount object and append to list
volMount := api.VolumeMount{
Name: volumeName,
MountPath: volume,
}
volumeMounts = append(volumeMounts, volMount)
//create tmpfs specific empty volumes
volSource := k.ConfigEmptyVolumeSource("tmpfs")
// create a new volume object using the volsource and add to list
vol := api.Volume{
Name: volumeName,
VolumeSource: *volSource,
}
volumes = append(volumes, vol)
}
return volumeMounts, volumes
}
// ConfigVolumes configure the container volumes.
func (k *Kubernetes) ConfigVolumes(name string, service kobject.ServiceConfig) ([]api.VolumeMount, []api.Volume, []*api.PersistentVolumeClaim, error) {
volumeMounts := []api.VolumeMount{}
@ -369,7 +399,7 @@ func (k *Kubernetes) ConfigVolumes(name string, service kobject.ServiceConfig) (
// For PVC we will also create a PVC object and add to list
var volsource *api.VolumeSource
if useEmptyVolumes {
volsource = k.ConfigEmptyVolumeSource()
volsource = k.ConfigEmptyVolumeSource("volume")
} else {
volsource = k.ConfigPVCVolumeSource(volumeName, readonly)
@ -396,10 +426,21 @@ func (k *Kubernetes) ConfigVolumes(name string, service kobject.ServiceConfig) (
}
// ConfigEmptyVolumeSource is helper function to create an EmptyDir api.VolumeSource
func (k *Kubernetes) ConfigEmptyVolumeSource() *api.VolumeSource {
//either for Tmpfs or for emptyvolumes
func (k *Kubernetes) ConfigEmptyVolumeSource(key string) *api.VolumeSource {
//if key is tmpfs
if key == "tmpfs" {
return &api.VolumeSource{
EmptyDir: &api.EmptyDirVolumeSource{Medium: api.StorageMediumMemory},
}
}
//if key is volume
return &api.VolumeSource{
EmptyDir: &api.EmptyDirVolumeSource{},
}
}
// ConfigPVCVolumeSource is helper function to create an api.VolumeSource with a PVC

View File

@ -55,6 +55,7 @@ func newServiceConfig() kobject.ServiceConfig {
Restart: "always",
Stdin: true,
Tty: true,
TmpFs: []string{"/tmp"},
}
}
@ -499,3 +500,14 @@ func TestInitPodSpec(t *testing.T) {
t.Fatalf("Pod object not found")
}
}
func TestConfigTmpfs(t *testing.T) {
name := "foo"
k := Kubernetes{}
resultVolumeMount, resultVolume := k.ConfigTmpfs(name, newServiceConfig())
if resultVolumeMount[0].Name != "foo-tmpfs0" || resultVolume[0].EmptyDir.Medium != "Memory" {
t.Fatalf("Tmpfs not found")
}
}