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

Use containerd restart manager to monitor services #3168

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/yaml.md
Expand Up @@ -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:
```
Expand Down
13 changes: 13 additions & 0 deletions pkg/init/cmd/service/cmd.go
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
1 change: 1 addition & 0 deletions pkg/init/cmd/service/prepare.go
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/cmd/linuxkit/moby/config.go
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -605,6 +607,7 @@ func assignRuntime(v1, v2 *Runtime) Runtime {
Uts: assignStringPtr(v1.BindNS.Uts, v2.BindNS.Uts),
},
Namespace: &runtimeNamespace,
NoRestart: &runtimeNoRestart,
}
return runtime
}
Expand Down
3 changes: 2 additions & 1 deletion src/cmd/linuxkit/moby/schema.go
Expand Up @@ -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": {
Expand Down