Skip to content

Commit

Permalink
Merge pull request #484 from surajnarwade/add_tmpfs_support
Browse files Browse the repository at this point in the history
Added support for tmpfs
  • Loading branch information
kadel authored Mar 16, 2017
2 parents 54f1339 + 4941334 commit 752b203
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 3 deletions.
1 change: 1 addition & 0 deletions pkg/kobject/kobject.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/loader/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
12 changes: 12 additions & 0 deletions pkg/transformer/kubernetes/k8sutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)`
Expand Down
45 changes: 43 additions & 2 deletions pkg/transformer/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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)

Expand All @@ -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
Expand Down
12 changes: 12 additions & 0 deletions pkg/transformer/kubernetes/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func newServiceConfig() kobject.ServiceConfig {
Restart: "always",
Stdin: true,
Tty: true,
TmpFs: []string{"/tmp"},
}
}

Expand Down Expand Up @@ -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")
}

}

0 comments on commit 752b203

Please sign in to comment.