diff --git a/cmd/daemon/containerd.go b/cmd/daemon/containerd.go new file mode 100644 index 0000000..5cf1d20 --- /dev/null +++ b/cmd/daemon/containerd.go @@ -0,0 +1,13 @@ +// +build !linux + +package daemon + +import ( + "os" + + "github.com/sirupsen/logrus" +) + +func Containerd() { + logrus.Fatalf("%s only supported on Linux", os.Args[0]) +} diff --git a/cmd/daemon/containerd_linux.go b/cmd/daemon/containerd_linux.go new file mode 100644 index 0000000..d14d0c1 --- /dev/null +++ b/cmd/daemon/containerd_linux.go @@ -0,0 +1,23 @@ +package daemon + +import ( + "fmt" + "os" + + "github.com/urfave/cli" + "k8s.io/klog" +) + +func Containerd() { + app := newApp() + app.Name = "containerd" + app.HelpName = app.Name + app.Before = func(clx *cli.Context) error { + klog.InitFlags(nil) + return nil + } + if err := app.Run(os.Args); err != nil { + fmt.Fprintf(os.Stderr, "%s: %s\n", app.Name, err) + os.Exit(1) + } +} diff --git a/cmd/daemon/daemon_linux.go b/cmd/daemon/daemon_linux.go index d85a3e8..78cbf6e 100644 --- a/cmd/daemon/daemon_linux.go +++ b/cmd/daemon/daemon_linux.go @@ -20,7 +20,7 @@ import ( "k8s.io/klog" ) -func Command() *cliv2.Command { +func newApp() *cliv1.App { app := command.App() app.Name = "k3c daemon" app.Usage = "containerd++ (cri, buildkit and k3c)" @@ -145,7 +145,8 @@ the backend to support the Docker work-alike frontend of k3c.` Destination: &cri.Config.SandboxImage, }, }...) - app.Before = func(before cliv1.BeforeFunc) cliv1.BeforeFunc { + + app.Action = func(action interface{}) cliv1.ActionFunc { return func(clx *cliv1.Context) error { // setup env for i := range clx.App.Flags { @@ -212,13 +213,15 @@ the backend to support the Docker work-alike frontend of k3c.` if err := os.Setenv("PATH", fmt.Sprintf("%s%c%s", os.Getenv("PATH"), os.PathListSeparator, filepath.Join(root, "bin", "aux"))); err != nil { return err } - if before != nil { - return before(clx) - } - return nil + return cliv1.HandleAction(action, clx) } - }(app.Before) + }(app.Action) + return app +} + +func Command() *cliv2.Command { + app := newApp() return &cliv2.Command{ Name: "daemon", Usage: "Run the container daemon", diff --git a/main.go b/main.go index 5226559..9d9d94f 100644 --- a/main.go +++ b/main.go @@ -2,16 +2,24 @@ package main import ( "os" + "path/filepath" "github.com/containerd/containerd/pkg/seed" + "github.com/rancher/k3c/cmd/daemon" "github.com/rancher/k3c/pkg/cli/app" "github.com/sirupsen/logrus" ) func main() { seed.WithTimeAndRand() - app := app.New() - if err := app.Run(os.Args); err != nil { - logrus.Fatal(err) + self := filepath.Base(os.Args[0]) + switch self { + case "containerd": + daemon.Containerd() + default: + app := app.New() + if err := app.Run(os.Args); err != nil { + logrus.Fatal(err) + } } }