diff --git a/pkg/init/cmd/service/main.go b/pkg/init/cmd/service/main.go index c34201ce5..160266682 100644 --- a/pkg/init/cmd/service/main.go +++ b/pkg/init/cmd/service/main.go @@ -1,22 +1,25 @@ package main import ( + "context" "flag" "fmt" "os" "path/filepath" "strings" + "github.com/containerd/containerd/namespaces" log "github.com/sirupsen/logrus" ) const ( - defaultSocket = "/run/containerd/containerd.sock" - defaultPath = "/containers/services" - defaultContainerd = "/usr/bin/containerd" - installPath = "/usr/bin/service" - onbootPath = "/containers/onboot" - shutdownPath = "/containers/onshutdown" + defaultSocket = "/run/containerd/containerd.sock" + defaultPath = "/containers/services" + defaultContainerd = "/usr/bin/containerd" + 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() diff --git a/pkg/init/cmd/service/start.go b/pkg/init/cmd/service/start.go index 187322f47..d4964ba1e 100644 --- a/pkg/init/cmd/service/start.go +++ b/pkg/init/cmd/service/start.go @@ -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 { diff --git a/pkg/init/cmd/service/system_init.go b/pkg/init/cmd/service/system_init.go index e530e54a5..3ca71b50c 100644 --- a/pkg/init/cmd/service/system_init.go +++ b/pkg/init/cmd/service/system_init.go @@ -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)