From 1d1c8fcee99d58781b90bc646398046d22e286ef Mon Sep 17 00:00:00 2001 From: Omar Ramadan Date: Tue, 27 Nov 2018 10:04:26 -0800 Subject: [PATCH] Add containerd restart plugin over moby services Signed-off-by: Omar Ramadan --- docs/yaml.md | 1 + pkg/init/cmd/service/cmd.go | 13 +++++++++++++ pkg/init/cmd/service/prepare.go | 1 + src/cmd/linuxkit/moby/config.go | 3 +++ src/cmd/linuxkit/moby/schema.go | 3 ++- 5 files changed, 20 insertions(+), 1 deletion(-) diff --git a/docs/yaml.md b/docs/yaml.md index c04fabfc59..9932fe550d 100644 --- a/docs/yaml.md +++ b/docs/yaml.md @@ -208,6 +208,7 @@ which specifies some actions to take place when the container is being started. - `bindNS` specifies a namespace type and a path where the namespace from the container being created will be bound. This allows a namespace to be set up in an `onboot` container, and then using `net: path` for a `service` container to use that network namespace later. - `namespace` overrides the LinuxKit default containerd namespace to put the container in; only applicable to services. +- `noRestart` By default, long running services in the `service` section are restarted upon exit. This boolean allows to disabling service restart. An example of using the `runtime` config to configure a network namespace with `wireguard` and then run `nginx` in that namespace is shown below: ``` diff --git a/pkg/init/cmd/service/cmd.go b/pkg/init/cmd/service/cmd.go index cff32f2fe5..c5aa7ede0e 100644 --- a/pkg/init/cmd/service/cmd.go +++ b/pkg/init/cmd/service/cmd.go @@ -11,6 +11,7 @@ import ( "github.com/containerd/containerd" "github.com/containerd/containerd/cio" "github.com/containerd/containerd/namespaces" + "github.com/containerd/containerd/runtime/restart" specs "github.com/opencontainers/runtime-spec/specs-go" log "github.com/sirupsen/logrus" ) @@ -118,6 +119,11 @@ func stop(ctx context.Context, service, sock, basePath string) (string, uint32, return "", 0, "loading container", err } + err = ctr.Update(ctx, restart.WithNoRestarts) + if err != nil { + return "", 0, "failed to clear restart monitor", err + } + task, err := ctr.Task(ctx, nil) if err != nil { return "", 0, "fetching task", err @@ -227,5 +233,12 @@ func start(ctx context.Context, service, sock, basePath, dumpSpec string) (strin return "", 0, "failed to start task", err } + if runtimeConfig.NoRestart == nil || *runtimeConfig.NoRestart == false { + err = ctr.Update(ctx, restart.WithStatus(containerd.Running)) + if err != nil { + return "", 0, "failed to add restart monitor", err + } + } + return ctr.ID(), task.Pid(), "", nil } diff --git a/pkg/init/cmd/service/prepare.go b/pkg/init/cmd/service/prepare.go index 9df68349aa..92a6791e6e 100644 --- a/pkg/init/cmd/service/prepare.go +++ b/pkg/init/cmd/service/prepare.go @@ -24,6 +24,7 @@ type Runtime struct { Interfaces []Interface `yaml:"interfaces" json:"interfaces,omitempty"` BindNS Namespaces `yaml:"bindNS" json:"bindNS,omitempty"` Namespace string `yaml:"namespace,omitempty" json:"namespace,omitempty"` + NoRestart *bool `yaml:"noRestart,omitempty" json:"noRestart,omitempty"` } // Namespaces is the type for configuring paths to bind namespaces diff --git a/src/cmd/linuxkit/moby/config.go b/src/cmd/linuxkit/moby/config.go index d41677034a..4a4c29359f 100644 --- a/src/cmd/linuxkit/moby/config.go +++ b/src/cmd/linuxkit/moby/config.go @@ -116,6 +116,7 @@ type Runtime struct { Interfaces *[]Interface `yaml:"interfaces,omitempty,omitempty" json:"interfaces,omitempty"` BindNS Namespaces `yaml:"bindNS,omitempty" json:"bindNS,omitempty"` Namespace *string `yaml:"namespace,omitempty" json:"namespace,omitempty"` + NoRestart *bool `yaml:"noRestart,omitempty" json:"noRestart,omitempty"` } // Namespaces is the type for configuring paths to bind namespaces @@ -590,6 +591,7 @@ func assignRuntime(v1, v2 *Runtime) Runtime { runtimeMkdir := assignStrings(v1.Mkdir, v2.Mkdir) runtimeInterfaces := assignRuntimeInterfaceArray(v1.Interfaces, v2.Interfaces) runtimeNamespace := assignString(v1.Namespace, v2.Namespace) + runtimeNoRestart := assignBool(v1.NoRestart, v2.NoRestart) runtime := Runtime{ Cgroups: &runtimeCgroups, Mounts: &runtimeMounts, @@ -605,6 +607,7 @@ func assignRuntime(v1, v2 *Runtime) Runtime { Uts: assignStringPtr(v1.BindNS.Uts, v2.BindNS.Uts), }, Namespace: &runtimeNamespace, + NoRestart: &runtimeNoRestart, } return runtime } diff --git a/src/cmd/linuxkit/moby/schema.go b/src/cmd/linuxkit/moby/schema.go index 9379098f41..b858910758 100644 --- a/src/cmd/linuxkit/moby/schema.go +++ b/src/cmd/linuxkit/moby/schema.go @@ -249,7 +249,8 @@ var schema = string(` "mkdir": {"$ref": "#/definitions/strings"}, "interfaces": {"$ref": "#/definitions/interfaces"}, "bindNS": {"$ref": "#/definitions/namespaces"}, - "namespace": {"type": "string"} + "namespace": {"type": "string"}, + "noRestart": {"type": "boolean"} } }, "image": {