Skip to content

Commit

Permalink
Merge pull request #16 from cllunsford/feature/restart-policy
Browse files Browse the repository at this point in the history
Adding support for restart policies
  • Loading branch information
cllunsford authored Jul 7, 2016
2 parents f5e5061 + 900831d commit 10d197c
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 7 deletions.
3 changes: 2 additions & 1 deletion fixtures/full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ web:
privileged: True
tty: True
working_dir: /var/lib/work

restart: always
net: host
10 changes: 4 additions & 6 deletions kumpose/kumpose.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ func CompToDeployment(proj *project.Project) ([]byte, error) {
for _, name := range proj.ServiceConfigs.Keys() {
s, _ := proj.ServiceConfigs.Get(name)

podSpec, _ := kubePodSpec(name, s)

con, _ := kubeContainer(name, s)
podSpec.Containers = []api.Container{con}

dep := &extensions.Deployment{
TypeMeta: unversioned.TypeMeta{
Expand All @@ -35,12 +38,7 @@ func CompToDeployment(proj *project.Project) ([]byte, error) {
ObjectMeta: api.ObjectMeta{
Labels: map[string]string{"service": name},
},
Spec: api.PodSpec{
Containers: []api.Container{
con,
},
Hostname: s.Hostname,
},
Spec: podSpec,
},
},
}
Expand Down
55 changes: 55 additions & 0 deletions kumpose/pod.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package kumpose

import (
"fmt"
"strings"

"github.com/docker/libcompose/config"
"k8s.io/kubernetes/pkg/api"
)

func kubePodSpec(name string, sc *config.ServiceConfig) (api.PodSpec, error) {
ps := api.PodSpec{
Hostname: sc.Hostname,
}

if sc.Restart != "" {
if rp, err := kubeRestartPolicy(sc.Restart); err == nil {
ps.RestartPolicy = rp
}
}

if psc, err := kubePodSecurityContext(sc); err == nil {
ps.SecurityContext = &psc
}

return ps, nil
}

func kubePodSecurityContext(sc *config.ServiceConfig) (api.PodSecurityContext, error) {
ctx := api.PodSecurityContext{}

if sc.NetworkMode != "" {
switch sc.NetworkMode {
case "host":
ctx.HostNetwork = true
default:
return ctx, fmt.Errorf("NetworkMode not implemented: %s", sc.NetworkMode)
}
}

return ctx, nil
}

func kubeRestartPolicy(r string) (api.RestartPolicy, error) {
switch strings.Split(r, ":")[0] {
case "always":
return api.RestartPolicyAlways, nil
case "on-failure":
return api.RestartPolicyOnFailure, nil
case "no":
return api.RestartPolicyNever, nil
default:
return api.RestartPolicyNever, fmt.Errorf("Restart policy not implemented: %s", r)
}
}
58 changes: 58 additions & 0 deletions kumpose/pod_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package kumpose

import (
"testing"

"github.com/docker/libcompose/config"
"github.com/stretchr/testify/assert"
"k8s.io/kubernetes/pkg/api"
)

func TestKubePodSpec(t *testing.T) {
assert := assert.New(t)

sc := &config.ServiceConfig{
Restart: "always",
}

ps, err := kubePodSpec("name", sc)
assert.NoError(err)
assert.Equal(api.RestartPolicyAlways, ps.RestartPolicy)
}

func TestKubePodSecurityContext(t *testing.T) {
assert := assert.New(t)

sc := &config.ServiceConfig{
NetworkMode: "host",
}

psc, err := kubePodSecurityContext(sc)
assert.NoError(err)
assert.True(psc.HostNetwork)

sc.NetworkMode = "bridge"
psc, err = kubePodSecurityContext(sc)
assert.NotNil(err)
assert.False(psc.HostNetwork)
}

func TestKubeRestartPolicy(t *testing.T) {
assert := assert.New(t)

r, err := kubeRestartPolicy("always")
assert.NoError(err)
assert.Equal(api.RestartPolicyAlways, r)

r, err = kubeRestartPolicy("on-failure")
assert.NoError(err)
assert.Equal(api.RestartPolicyOnFailure, r)

r, err = kubeRestartPolicy("no")
assert.NoError(err)
assert.Equal(api.RestartPolicyNever, r)

r, err = kubeRestartPolicy("foo")
assert.NotNil(err)
assert.Equal(api.RestartPolicyNever, r)
}

0 comments on commit 10d197c

Please sign in to comment.