Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for tmpfs #484

Merged
merged 1 commit into from
Mar 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:""`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind! I confirmed that both work regardless of string slice or normal string.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is ok

[]string is hidden under yaml.Stringorslice ;-)

#types_yaml.go
type Stringorslice strslice.StrSlice

#strslice.go
type StrSlice []string

}

// 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again, can be implied, using volSource := ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can't implied here, because volsource is used in if and else and outside of that too

} 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")
}

}