pkg/init/cmd/service: plumb containerd namespace

This PR correctly plumbs a single context to propagate the containerd
namespace to the necessary commands. Services launched with containerd
after this change will now be in a default namespace of
`services.linuxkit`.

A top-level flag is added to the service command,
`--containerd-namespace` which can be used to change, if needed.

Signed-off-by: Stephen J Day <stephen.day@docker.com>
This commit is contained in:
Stephen J Day 2017-12-12 12:18:52 -08:00
parent bad114476a
commit fbbab9eafd
No known key found for this signature in database
GPG Key ID: 67B3DED84EDC823F
3 changed files with 25 additions and 22 deletions

View File

@ -1,12 +1,14 @@
package main
import (
"context"
"flag"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/containerd/containerd/namespaces"
log "github.com/sirupsen/logrus"
)
@ -17,6 +19,7 @@ const (
installPath = "/usr/bin/service"
onbootPath = "/containers/onboot"
shutdownPath = "/containers/onshutdown"
defaultContainerdNamespace = "services.linuxkit"
)
var (
@ -48,8 +51,12 @@ func main() {
fmt.Printf("Options:\n")
flag.PrintDefaults()
}
flagQuiet := flag.Bool("q", false, "Quiet execution")
flagVerbose := flag.Bool("v", false, "Verbose execution")
var (
ctx = context.Background()
flagQuiet = flag.Bool("q", false, "Quiet execution")
flagVerbose = flag.Bool("v", false, "Verbose execution")
flagContainerdNamespace = flag.String("containerd-namespace", defaultContainerdNamespace, "containerd namespace to use with services")
)
// Set up logging
log.SetFormatter(new(infoFormatter))
@ -68,6 +75,8 @@ func main() {
log.SetLevel(log.DebugLevel)
}
ctx = namespaces.WithNamespace(ctx, *flagContainerdNamespace)
args := flag.Args()
if len(args) < 1 {
// check if called form startup scripts
@ -78,16 +87,16 @@ func main() {
case strings.Contains(command, "onshutdown"):
os.Exit(runcInit(shutdownPath))
case strings.Contains(command, "containerd"):
systemInitCmd([]string{})
systemInitCmd(ctx, []string{})
os.Exit(0)
}
}
switch args[0] {
case "start":
startCmd(args[1:])
startCmd(ctx, args[1:])
case "system-init":
systemInitCmd(args[1:])
systemInitCmd(ctx, args[1:])
default:
fmt.Printf("%q is not valid command.\n\n", args[0])
flag.Usage()

View File

@ -11,12 +11,11 @@ import (
"github.com/containerd/containerd"
"github.com/containerd/containerd/cio"
"github.com/containerd/containerd/namespaces"
specs "github.com/opencontainers/runtime-spec/specs-go"
log "github.com/sirupsen/logrus"
)
func startCmd(args []string) {
func startCmd(ctx context.Context, args []string) {
invoked := filepath.Base(os.Args[0])
flags := flag.NewFlagSet("start", flag.ExitOnError)
flags.Usage = func() {
@ -48,7 +47,7 @@ func startCmd(args []string) {
"service": service,
})
id, pid, msg, err := start(service, *sock, *path, *dumpSpec)
id, pid, msg, err := start(ctx, service, *sock, *path, *dumpSpec)
if err != nil {
log.WithError(err).Fatal(msg)
}
@ -74,7 +73,7 @@ func (c *logio) Close() error {
return nil
}
func start(service, sock, basePath, dumpSpec string) (string, uint32, string, error) {
func start(ctx context.Context, service, sock, basePath, dumpSpec string) (string, uint32, string, error) {
path := filepath.Join(basePath, service)
runtimeConfig := getRuntimeConfig(path)
@ -90,8 +89,6 @@ func start(service, sock, basePath, dumpSpec string) (string, uint32, string, er
return "", 0, "creating containerd client", err
}
ctx := namespaces.WithNamespace(context.Background(), "default")
var spec *specs.Spec
specf, err := os.Open(filepath.Join(path, "config.json"))
if err != nil {

View File

@ -13,7 +13,6 @@ import (
"github.com/containerd/containerd"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/namespaces"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
@ -52,7 +51,7 @@ func cleanupTask(ctx context.Context, ctr containerd.Container) error {
}
}
func systemInitCmd(args []string) {
func systemInitCmd(ctx context.Context, args []string) {
invoked := filepath.Base(os.Args[0])
flags := flag.NewFlagSet("system-init", flag.ExitOnError)
flags.Usage = func() {
@ -108,8 +107,6 @@ func systemInitCmd(args []string) {
log.WithError(err).Fatal("creating containerd client")
}
ctx := namespaces.WithNamespace(context.Background(), "default")
ctrs, err := client.Containers(ctx)
if err != nil {
log.WithError(err).Fatal("listing containers")
@ -140,7 +137,7 @@ func systemInitCmd(args []string) {
return
}
for _, file := range files {
if id, pid, msg, err := start(file.Name(), *sock, *path, ""); err != nil {
if id, pid, msg, err := start(ctx, file.Name(), *sock, *path, ""); err != nil {
log.WithError(err).Error(msg)
} else {
log.Debugf("Started %s pid %d", id, pid)