From 723bd0f9d8f93745f31171adb38b83215bb23adf Mon Sep 17 00:00:00 2001 From: Hang Yan Date: Wed, 7 Mar 2018 13:35:36 +0800 Subject: [PATCH] Support old restart policy in compose v3 --- docs/user-guide.md | 1 + pkg/loader/compose/v3.go | 8 +- pkg/transformer/kubernetes/k8sutils.go | 2 +- script/test/cmd/tests.sh | 10 +- .../docker-compose-restart-unless-stopped.yml | 8 ++ .../output-k8s-restart-unless-stopped.json | 82 +++++++++++ .../output-os-restart-unless-stopped.json | 134 ++++++++++++++++++ 7 files changed, 241 insertions(+), 4 deletions(-) create mode 100644 script/test/fixtures/restart-options/docker-compose-restart-unless-stopped.yml create mode 100644 script/test/fixtures/restart-options/output-k8s-restart-unless-stopped.json create mode 100644 script/test/fixtures/restart-options/output-os-restart-unless-stopped.json diff --git a/docs/user-guide.md b/docs/user-guide.md index ba6c8a37e..b53a67727 100644 --- a/docs/user-guide.md +++ b/docs/user-guide.md @@ -373,6 +373,7 @@ If you want to create normal pods without controllers you can use `restart` cons |----------------------------|-------------------|---------------------| | `""` | controller object | `Always` | | `always` | controller object | `Always` | +| `unless-stopped` | controller object | `Always` | | `on-failure` | Pod | `OnFailure` | | `no` | Pod | `Never` | diff --git a/pkg/loader/compose/v3.go b/pkg/loader/compose/v3.go index 4c17f8513..a619d0aa1 100644 --- a/pkg/loader/compose/v3.go +++ b/pkg/loader/compose/v3.go @@ -285,10 +285,16 @@ func dockerComposeToKomposeMapping(composeObject *types.Config) (kobject.Kompose } - // restart-policy: + // restart-policy: deploy.restart_policy.condition will rewrite restart option + // see: https://docs.docker.com/compose/compose-file/#restart_policy + serviceConfig.Restart = composeServiceConfig.Restart if composeServiceConfig.Deploy.RestartPolicy != nil { serviceConfig.Restart = composeServiceConfig.Deploy.RestartPolicy.Condition } + if serviceConfig.Restart == "unless-stopped" { + log.Warnf("Restart policy 'unless-stopped' in service %s is not supported, convert it to 'always'", name) + serviceConfig.Restart = "always" + } // replicas: if composeServiceConfig.Deploy.Replicas != nil { diff --git a/pkg/transformer/kubernetes/k8sutils.go b/pkg/transformer/kubernetes/k8sutils.go index bac178ebd..c488a417a 100644 --- a/pkg/transformer/kubernetes/k8sutils.go +++ b/pkg/transformer/kubernetes/k8sutils.go @@ -512,7 +512,7 @@ func (k *Kubernetes) UpdateKubernetesObjects(name string, service kobject.Servic case "on-failure": template.Spec.RestartPolicy = api.RestartPolicyOnFailure default: - return errors.New("Unknown restart policy " + service.Restart + " for service" + name) + return errors.New("Unknown restart policy " + service.Restart + " for service " + name) } return nil } diff --git a/script/test/cmd/tests.sh b/script/test/cmd/tests.sh index df85a42b5..d748d58af 100755 --- a/script/test/cmd/tests.sh +++ b/script/test/cmd/tests.sh @@ -223,13 +223,19 @@ convert::expect_success_and_warning "kompose --provider=openshift -f $KOMPOSE_RO # kubernetes test convert::expect_success "kompose -f $KOMPOSE_ROOT/script/test/fixtures/restart-options/docker-compose-restart-no.yml convert --stdout -j" "$KOMPOSE_ROOT/script/test/fixtures/restart-options/output-k8s-restart-no.json" convert::expect_success "kompose -f $KOMPOSE_ROOT/script/test/fixtures/restart-options/docker-compose-restart-onfail.yml convert --stdout -j" "$KOMPOSE_ROOT/script/test/fixtures/restart-options/output-k8s-restart-onfail.json" +cmd="kompose -f $KOMPOSE_ROOT/script/test/fixtures/restart-options/docker-compose-restart-unless-stopped.yml convert --stdout -j" +sed -e "s;%VERSION%;$version;g" -e "s;%CMD%;$cmd;g" "$KOMPOSE_ROOT/script/test/fixtures/restart-options/output-k8s-restart-unless-stopped.json" > /tmp/output-k8s.json +convert::expect_success_and_warning "kompose -f $KOMPOSE_ROOT/script/test/fixtures/restart-options/docker-compose-restart-unless-stopped.yml convert --stdout -j" /tmp/output-k8s.json + # openshift test convert::expect_success "kompose -f $KOMPOSE_ROOT/script/test/fixtures/restart-options/docker-compose-restart-no.yml --provider openshift convert --stdout -j" "$KOMPOSE_ROOT/script/test/fixtures/restart-options/output-os-restart-no.json" convert::expect_success "kompose -f $KOMPOSE_ROOT/script/test/fixtures/restart-options/docker-compose-restart-onfail.yml --provider openshift convert --stdout -j" "$KOMPOSE_ROOT/script/test/fixtures/restart-options/output-os-restart-onfail.json" - +cmd="kompose -f $KOMPOSE_ROOT/script/test/fixtures/restart-options/docker-compose-restart-unless-stopped.yml --provider openshift convert --stdout -j" +sed -e "s;%VERSION%;$version;g" -e "s;%CMD%;$cmd;g" "$KOMPOSE_ROOT/script/test/fixtures/restart-options/output-os-restart-unless-stopped.json" > /tmp/output-os.json +convert::expect_success_and_warning "kompose -f $KOMPOSE_ROOT/script/test/fixtures/restart-options/docker-compose-restart-unless-stopped.yml --provider openshift convert --stdout -j" /tmp/output-os.json ###### -# Test key-only envrionment variable +# Test key-only environment variable export $(cat $KOMPOSE_ROOT/script/test/fixtures/keyonly-envs/envs) cmd="kompose --file $KOMPOSE_ROOT/script/test/fixtures/keyonly-envs/env.yml convert --stdout -j" sed -e "s;%VERSION%;$version;g" -e "s;%CMD%;$cmd;g" $KOMPOSE_ROOT/script/test/fixtures/keyonly-envs/output-k8s-template.json > /tmp/output-k8s.json diff --git a/script/test/fixtures/restart-options/docker-compose-restart-unless-stopped.yml b/script/test/fixtures/restart-options/docker-compose-restart-unless-stopped.yml new file mode 100644 index 000000000..80e0dd39f --- /dev/null +++ b/script/test/fixtures/restart-options/docker-compose-restart-unless-stopped.yml @@ -0,0 +1,8 @@ +version: "3" + +services: + foo: + image: "foobar" + restart: "unless-stopped" + environment: + GITHUB: surajssd diff --git a/script/test/fixtures/restart-options/output-k8s-restart-unless-stopped.json b/script/test/fixtures/restart-options/output-k8s-restart-unless-stopped.json new file mode 100644 index 000000000..c9b3e9548 --- /dev/null +++ b/script/test/fixtures/restart-options/output-k8s-restart-unless-stopped.json @@ -0,0 +1,82 @@ +{ + "kind": "List", + "apiVersion": "v1", + "metadata": {}, + "items": [ + { + "kind": "Service", + "apiVersion": "v1", + "metadata": { + "name": "foo", + "creationTimestamp": null, + "labels": { + "io.kompose.service": "foo" + }, + "annotations": { + "kompose.cmd": "%CMD%", + "kompose.version": "%VERSION%" + } + }, + "spec": { + "ports": [ + { + "name": "headless", + "port": 55555, + "targetPort": 0 + } + ], + "selector": { + "io.kompose.service": "foo" + }, + "clusterIP": "None" + }, + "status": { + "loadBalancer": {} + } + }, + { + "kind": "Deployment", + "apiVersion": "extensions/v1beta1", + "metadata": { + "name": "foo", + "creationTimestamp": null, + "labels": { + "io.kompose.service": "foo" + }, + "annotations": { + "kompose.cmd": "%CMD%", + "kompose.version": "%VERSION%" + } + }, + "spec": { + "replicas": 1, + "template": { + "metadata": { + "creationTimestamp": null, + "labels": { + "io.kompose.service": "foo" + } + }, + "spec": { + "containers": [ + { + "name": "foo", + "image": "foobar", + "env": [ + { + "name": "GITHUB", + "value": "surajssd" + } + ], + "resources": {} + } + ], + "restartPolicy": "Always" + } + }, + "strategy": {} + }, + "status": {} + } + ] +} \ No newline at end of file diff --git a/script/test/fixtures/restart-options/output-os-restart-unless-stopped.json b/script/test/fixtures/restart-options/output-os-restart-unless-stopped.json new file mode 100644 index 000000000..32613c759 --- /dev/null +++ b/script/test/fixtures/restart-options/output-os-restart-unless-stopped.json @@ -0,0 +1,134 @@ +{ + "kind": "List", + "apiVersion": "v1", + "metadata": {}, + "items": [ + { + "kind": "Service", + "apiVersion": "v1", + "metadata": { + "name": "foo", + "creationTimestamp": null, + "labels": { + "io.kompose.service": "foo" + }, + "annotations": { + "kompose.cmd": "%CMD%", + "kompose.version": "%VERSION%" + } + }, + "spec": { + "ports": [ + { + "name": "headless", + "port": 55555, + "targetPort": 0 + } + ], + "selector": { + "io.kompose.service": "foo" + }, + "clusterIP": "None" + }, + "status": { + "loadBalancer": {} + } + }, + { + "kind": "DeploymentConfig", + "apiVersion": "v1", + "metadata": { + "name": "foo", + "creationTimestamp": null, + "labels": { + "io.kompose.service": "foo" + }, + "annotations": { + "kompose.cmd": "%CMD%", + "kompose.version": "%VERSION%" + } + }, + "spec": { + "strategy": { + "resources": {} + }, + "triggers": [ + { + "type": "ConfigChange" + }, + { + "type": "ImageChange", + "imageChangeParams": { + "automatic": true, + "containerNames": [ + "foo" + ], + "from": { + "kind": "ImageStreamTag", + "name": "foo:latest" + } + } + } + ], + "replicas": 1, + "test": false, + "selector": { + "io.kompose.service": "foo" + }, + "template": { + "metadata": { + "creationTimestamp": null, + "labels": { + "io.kompose.service": "foo" + } + }, + "spec": { + "containers": [ + { + "name": "foo", + "image": " ", + "env": [ + { + "name": "GITHUB", + "value": "surajssd" + } + ], + "resources": {} + } + ], + "restartPolicy": "Always" + } + } + }, + "status": {} + }, + { + "kind": "ImageStream", + "apiVersion": "v1", + "metadata": { + "name": "foo", + "creationTimestamp": null, + "labels": { + "io.kompose.service": "foo" + } + }, + "spec": { + "tags": [ + { + "name": "latest", + "annotations": null, + "from": { + "kind": "DockerImage", + "name": "foobar" + }, + "generation": null, + "importPolicy": {} + } + ] + }, + "status": { + "dockerImageRepository": "" + } + } + ] +} \ No newline at end of file