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

Conversation

surajnarwade
Copy link
Contributor

fixes #436

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Mar 10, 2017
@surajnarwade surajnarwade force-pushed the add_tmpfs_support branch 2 times, most recently from 5b08fe1 to db527f1 Compare March 10, 2017 12:01
@surajnarwade
Copy link
Contributor Author

cc @cdrage @kadel @surajssd @containscafeine

@cdrage
Copy link
Member

cdrage commented Mar 10, 2017

Missing tests! Will do a review now however.

@cdrage
Copy link
Member

cdrage commented Mar 10, 2017

@surajnarwade

This doesn't work..

redis:                                                                                                                                                                                                                                                                            
  image: redis:3.0                                                                                                                                                                                                                                                                
  ports:                                                                                                                                                                                                                                                                          
    - "6379"                                                                                                                                                                                                                                                                      
  tmpfs: /run
github.com/kubernetes-incubator/kompose  pr_484 ✗                                                                                                                                                                                                                       4h29m ◒  
▶ ./kompose convert --stdout
ERRO Could not parse config for project kompose : Unsupported config option for redis service: 'tmpfs' 
FATA Failed to load compose file: Unsupported config option for redis service: 'tmpfs' 

@@ -315,6 +315,19 @@ func (k *Kubernetes) UpdateKubernetesObjects(name string, service kobject.Servic

// Configure the container volumes.
volumesMount, volumes, pvc := k.ConfigVolumes(name, service)
// Configure Tmpfs
if len(service.TmpFs) > 0 {
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this check if service.TmpFs != nil?

Copy link
Member

Choose a reason for hiding this comment

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

I think this is better than just !=nil

if you test only for !=nil than it would be false if service.Tmpfs is empty array.

https://play.golang.org/p/9W5KUrr9jY

@@ -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

@@ -369,7 +369,7 @@ func (c *Compose) LoadFile(files []string) kobject.KomposeObject {
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.

👍

func (k *Kubernetes) ConfigTmpfs(name string, service kobject.ServiceConfig) ([]api.VolumeMount, []api.Volume) {
volumeMounts := []api.VolumeMount{}
volumes := []api.Volume{}
var count int
Copy link
Member

Choose a reason for hiding this comment

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

no need for count

for _, volume := range service.TmpFs {

volumeName = fmt.Sprintf("%s-tmpfs%d", name, count)
count++
Copy link
Member

Choose a reason for hiding this comment

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

no need for count, you can use the range loop for the index value..

for index, volume := range service.TmpFs {

}
volumeMounts = append(volumeMounts, volmount)

var volsource *api.VolumeSource
Copy link
Member

Choose a reason for hiding this comment

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

no need, use implicit, much cleaner


var volsource *api.VolumeSource
//create tmpfs specific empty volumes
volsource = k.ConfigEmptyVolumeSource("tmpfs")
Copy link
Member

Choose a reason for hiding this comment

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

volSource := ...

// create a new volume object using the volsource and add to list
vol := api.Volume{
Name: volumeName,
VolumeSource: *volsource,
Copy link
Member

Choose a reason for hiding this comment

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

volSource for value

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no it has to passed like as it is mentioned

Copy link
Member

Choose a reason for hiding this comment

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

@surajnarwade what I mean is that it's suppose to be Go conventions for naming, so volSource instead of 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.

@cdrage oh got it

@@ -368,7 +400,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

@@ -389,10 +421,19 @@ 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 {
func (k *Kubernetes) ConfigEmptyVolumeSource(key string) *api.VolumeSource {
if key == "tmpfs" {
Copy link
Member

Choose a reason for hiding this comment

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

add comments

@cdrage
Copy link
Member

cdrage commented Mar 10, 2017

Hiya @surajnarwade

So a few things!

Great code, but there's a lot of missing comments, explaining what's happening in a few sentences would really help with some parts of the code.

The commit description is lacking, please type up what you do, why it's necessary and link the issue (like you already did), you can modify it with git commit --amend

@cdrage
Copy link
Member

cdrage commented Mar 10, 2017

A good example for a descriptive git message is @containscafeine 's one he used on a recent PR:

This commit refactors the code to remove more or less
all occurences of logrus.Fatalf() from the code under
pkg/ except for app.go where all the errors are being
handled currently.

This is being done since random logrus.Fatalf() calls
all around the code was making handling the errors,
unit testing and troubleshooting a bit more painful.

logrus.Fatalf() calls are either replaced by
return errors.New("new error")
or
return errors.Wrap(err, "annonate error")
calls, and the function signatures are accordingly
changed to accomodate the new return values.

The unit tests which previously used to check
if logrus.Fatalf() calls worked fine have also
been fixed to only check for errors now.

Fixes #416

@kadel
Copy link
Member

kadel commented Mar 10, 2017

This doesn't work..

redis:
image: redis:3.0
ports:
- "6379"
tmpfs: /run
github.com/kubernetes-incubator/kompose pr_484 ✗ 4h29m ◒
▶ ./kompose convert --stdout
ERRO Could not parse config for project kompose : Unsupported config option for redis service: 'tmpfs'
FATA Failed to load compose file: Unsupported config option for redis service: 'tmpfs'

tmpfs is not in docker-compose from v2 and up ;-)

@cdrage
Copy link
Member

cdrage commented Mar 10, 2017

There we go, my mistake!

github.com/kubernetes-incubator/kompose  pr_484 ✗                                                                                                                                                                                                                      4h53m ◒  ⍉
▶ cat docker-compose.yml 
version: '2'

services:

  redis:
    image: redis:3.0
    tmpfs: /foobar

github.com/kubernetes-incubator/kompose  pr_484 ✗                                                                                                                                                                                                                       4h54m ◒  
▶ ./kompose convert --stdout
apiVersion: v1
items:
- apiVersion: v1
  kind: Service
  metadata:
    creationTimestamp: null
    labels:
      service: redis
    name: redis
  spec:
    clusterIP: None
    ports:
    - name: headless
      port: 55555
      targetPort: 0
    selector:
      service: redis
  status:
    loadBalancer: {}
- apiVersion: extensions/v1beta1
  kind: Deployment
  metadata:
    creationTimestamp: null
    name: redis
  spec:
    replicas: 1
    strategy: {}
    template:
      metadata:
        creationTimestamp: null
        labels:
          service: redis
      spec:
        containers:
        - image: redis:3.0
          name: redis
          resources: {}
          volumeMounts:
          - mountPath: /foobar
            name: redis-tmpfs0
        restartPolicy: Always
        volumes:
        - emptyDir:
            medium: Memory
          name: redis-tmpfs0
  status: {}
kind: List
metadata: {}

@cdrage
Copy link
Member

cdrage commented Mar 10, 2017

And I can confirm it works with a list:

github.com/kubernetes-incubator/kompose  pr_484 ✗                                                                                                                                                                                                                      4h54m ◒  ⍉
▶ cat docker-compose.yml 
version: '2'

services:

  redis:
    image: redis:3.0
    tmpfs: 
      - /foobar
      - /foobar2

github.com/kubernetes-incubator/kompose  pr_484 ✗                                                                                                                                                                                                                       4h54m ◒  
▶ ./kompose convert --stdout
apiVersion: v1
items:
- apiVersion: v1
  kind: Service
  metadata:
    creationTimestamp: null
    labels:
      service: redis
    name: redis
  spec:
    clusterIP: None
    ports:
    - name: headless
      port: 55555
      targetPort: 0
    selector:
      service: redis
  status:
    loadBalancer: {}
- apiVersion: extensions/v1beta1
  kind: Deployment
  metadata:
    creationTimestamp: null
    name: redis
  spec:
    replicas: 1
    strategy: {}
    template:
      metadata:
        creationTimestamp: null
        labels:
          service: redis
      spec:
        containers:
        - image: redis:3.0
          name: redis
          resources: {}
          volumeMounts:
          - mountPath: /foobar
            name: redis-tmpfs0
          - mountPath: /foobar2
            name: redis-tmpfs1
        restartPolicy: Always
        volumes:
        - emptyDir:
            medium: Memory
          name: redis-tmpfs0
        - emptyDir:
            medium: Memory
          name: redis-tmpfs1
  status: {}
kind: List
metadata: {}

@surajnarwade
Copy link
Contributor Author

cc @kadel @cdrage

@cdrage
Copy link
Member

cdrage commented Mar 14, 2017

Saw the new comments added to the code, thanks!

This LGTM.

@@ -511,3 +512,14 @@ func TestInitPodSpec(t *testing.T) {
t.Fatalf("Pod object not found")
}
}

func TestKubernetes_ConfigTmpfs(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

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

One last change here ;-)
Can you please rename this? Its in Kubernetes package so it doesn't have to to have Kubernetes in name and _ is not GOlangy :-)

just TestConfigTmpfs says everything about what it is doing

Copy link
Contributor Author

@surajnarwade surajnarwade Mar 15, 2017

Choose a reason for hiding this comment

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

Sure, I will make it GOlangy ;-)

@surajnarwade
Copy link
Contributor Author

@kadel , done with correction.

@cdrage
Copy link
Member

cdrage commented Mar 15, 2017

@surajnarwade

Almost there! Just need to resolve the conflicts from me merging in #416

After that, I think it's ready to be merged! Cause as of right now, the code LGTM 👍

Copy link
Member

@kadel kadel left a comment

Choose a reason for hiding this comment

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

This should be also last thing from me ;-)

k := Kubernetes{}
resultVolumeMount, resultVolume := k.ConfigTmpfs(name, newServiceConfig())

if resultVolumeMount[0].Name != "foo-tmpfs0" && resultVolume[0].EmptyDir.Medium != "Memory" {
Copy link
Member

@kadel kadel Mar 16, 2017

Choose a reason for hiding this comment

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

One last thing. Should it be || instead of &&?

What if only one of them is wrong?

fixes kubernetes#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.
@surajnarwade
Copy link
Contributor Author

@kadel , last thing updated ;-)

@kadel kadel merged commit 752b203 into kubernetes:master Mar 16, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for: tmpfs
4 participants