mirror of
https://github.com/linuxkit/linuxkit.git
synced 2025-07-23 02:51:55 +00:00
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:
parent
bad114476a
commit
fbbab9eafd
@ -1,22 +1,25 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/containerd/containerd/namespaces"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
defaultSocket = "/run/containerd/containerd.sock"
|
defaultSocket = "/run/containerd/containerd.sock"
|
||||||
defaultPath = "/containers/services"
|
defaultPath = "/containers/services"
|
||||||
defaultContainerd = "/usr/bin/containerd"
|
defaultContainerd = "/usr/bin/containerd"
|
||||||
installPath = "/usr/bin/service"
|
installPath = "/usr/bin/service"
|
||||||
onbootPath = "/containers/onboot"
|
onbootPath = "/containers/onboot"
|
||||||
shutdownPath = "/containers/onshutdown"
|
shutdownPath = "/containers/onshutdown"
|
||||||
|
defaultContainerdNamespace = "services.linuxkit"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -48,8 +51,12 @@ func main() {
|
|||||||
fmt.Printf("Options:\n")
|
fmt.Printf("Options:\n")
|
||||||
flag.PrintDefaults()
|
flag.PrintDefaults()
|
||||||
}
|
}
|
||||||
flagQuiet := flag.Bool("q", false, "Quiet execution")
|
var (
|
||||||
flagVerbose := flag.Bool("v", false, "Verbose execution")
|
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
|
// Set up logging
|
||||||
log.SetFormatter(new(infoFormatter))
|
log.SetFormatter(new(infoFormatter))
|
||||||
@ -68,6 +75,8 @@ func main() {
|
|||||||
log.SetLevel(log.DebugLevel)
|
log.SetLevel(log.DebugLevel)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ctx = namespaces.WithNamespace(ctx, *flagContainerdNamespace)
|
||||||
|
|
||||||
args := flag.Args()
|
args := flag.Args()
|
||||||
if len(args) < 1 {
|
if len(args) < 1 {
|
||||||
// check if called form startup scripts
|
// check if called form startup scripts
|
||||||
@ -78,16 +87,16 @@ func main() {
|
|||||||
case strings.Contains(command, "onshutdown"):
|
case strings.Contains(command, "onshutdown"):
|
||||||
os.Exit(runcInit(shutdownPath))
|
os.Exit(runcInit(shutdownPath))
|
||||||
case strings.Contains(command, "containerd"):
|
case strings.Contains(command, "containerd"):
|
||||||
systemInitCmd([]string{})
|
systemInitCmd(ctx, []string{})
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
switch args[0] {
|
switch args[0] {
|
||||||
case "start":
|
case "start":
|
||||||
startCmd(args[1:])
|
startCmd(ctx, args[1:])
|
||||||
case "system-init":
|
case "system-init":
|
||||||
systemInitCmd(args[1:])
|
systemInitCmd(ctx, args[1:])
|
||||||
default:
|
default:
|
||||||
fmt.Printf("%q is not valid command.\n\n", args[0])
|
fmt.Printf("%q is not valid command.\n\n", args[0])
|
||||||
flag.Usage()
|
flag.Usage()
|
||||||
|
@ -11,12 +11,11 @@ import (
|
|||||||
|
|
||||||
"github.com/containerd/containerd"
|
"github.com/containerd/containerd"
|
||||||
"github.com/containerd/containerd/cio"
|
"github.com/containerd/containerd/cio"
|
||||||
"github.com/containerd/containerd/namespaces"
|
|
||||||
specs "github.com/opencontainers/runtime-spec/specs-go"
|
specs "github.com/opencontainers/runtime-spec/specs-go"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
func startCmd(args []string) {
|
func startCmd(ctx context.Context, args []string) {
|
||||||
invoked := filepath.Base(os.Args[0])
|
invoked := filepath.Base(os.Args[0])
|
||||||
flags := flag.NewFlagSet("start", flag.ExitOnError)
|
flags := flag.NewFlagSet("start", flag.ExitOnError)
|
||||||
flags.Usage = func() {
|
flags.Usage = func() {
|
||||||
@ -48,7 +47,7 @@ func startCmd(args []string) {
|
|||||||
"service": service,
|
"service": service,
|
||||||
})
|
})
|
||||||
|
|
||||||
id, pid, msg, err := start(service, *sock, *path, *dumpSpec)
|
id, pid, msg, err := start(ctx, service, *sock, *path, *dumpSpec)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithError(err).Fatal(msg)
|
log.WithError(err).Fatal(msg)
|
||||||
}
|
}
|
||||||
@ -74,7 +73,7 @@ func (c *logio) Close() error {
|
|||||||
return nil
|
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)
|
path := filepath.Join(basePath, service)
|
||||||
|
|
||||||
runtimeConfig := getRuntimeConfig(path)
|
runtimeConfig := getRuntimeConfig(path)
|
||||||
@ -90,8 +89,6 @@ func start(service, sock, basePath, dumpSpec string) (string, uint32, string, er
|
|||||||
return "", 0, "creating containerd client", err
|
return "", 0, "creating containerd client", err
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx := namespaces.WithNamespace(context.Background(), "default")
|
|
||||||
|
|
||||||
var spec *specs.Spec
|
var spec *specs.Spec
|
||||||
specf, err := os.Open(filepath.Join(path, "config.json"))
|
specf, err := os.Open(filepath.Join(path, "config.json"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -13,7 +13,6 @@ import (
|
|||||||
|
|
||||||
"github.com/containerd/containerd"
|
"github.com/containerd/containerd"
|
||||||
"github.com/containerd/containerd/errdefs"
|
"github.com/containerd/containerd/errdefs"
|
||||||
"github.com/containerd/containerd/namespaces"
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
log "github.com/sirupsen/logrus"
|
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])
|
invoked := filepath.Base(os.Args[0])
|
||||||
flags := flag.NewFlagSet("system-init", flag.ExitOnError)
|
flags := flag.NewFlagSet("system-init", flag.ExitOnError)
|
||||||
flags.Usage = func() {
|
flags.Usage = func() {
|
||||||
@ -108,8 +107,6 @@ func systemInitCmd(args []string) {
|
|||||||
log.WithError(err).Fatal("creating containerd client")
|
log.WithError(err).Fatal("creating containerd client")
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx := namespaces.WithNamespace(context.Background(), "default")
|
|
||||||
|
|
||||||
ctrs, err := client.Containers(ctx)
|
ctrs, err := client.Containers(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithError(err).Fatal("listing containers")
|
log.WithError(err).Fatal("listing containers")
|
||||||
@ -140,7 +137,7 @@ func systemInitCmd(args []string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
for _, file := range files {
|
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)
|
log.WithError(err).Error(msg)
|
||||||
} else {
|
} else {
|
||||||
log.Debugf("Started %s pid %d", id, pid)
|
log.Debugf("Started %s pid %d", id, pid)
|
||||||
|
Loading…
Reference in New Issue
Block a user