diff --git a/fixtures/full.yml b/fixtures/full.yml index 477b02f..5c1c42c 100644 --- a/fixtures/full.yml +++ b/fixtures/full.yml @@ -17,4 +17,5 @@ web: privileged: True tty: True working_dir: /var/lib/work - + restart: always + net: host diff --git a/kumpose/kumpose.go b/kumpose/kumpose.go index cdfbe90..d9f2084 100644 --- a/kumpose/kumpose.go +++ b/kumpose/kumpose.go @@ -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{ @@ -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, }, }, } diff --git a/kumpose/pod.go b/kumpose/pod.go new file mode 100644 index 0000000..6325923 --- /dev/null +++ b/kumpose/pod.go @@ -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) + } +} diff --git a/kumpose/pod_test.go b/kumpose/pod_test.go new file mode 100644 index 0000000..cd14312 --- /dev/null +++ b/kumpose/pod_test.go @@ -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) +}