From 793ac4fb8945b9c4f118c7f9fa1af16c6f8f4919 Mon Sep 17 00:00:00 2001 From: Darren Shepherd Date: Thu, 7 Feb 2019 21:08:10 -0700 Subject: [PATCH] Add crictl --- cmd/ctr/main.go | 50 ++++++++++++++++++++++++++++++++++++++++ cmd/k3s/main.go | 28 +++++++++++++--------- cmd/server/main.go | 4 ++++ main.go | 2 ++ pkg/cli/cmds/crictl.go | 15 ++++++++++++ pkg/cli/cmds/ctr.go | 15 ++++++++++++ pkg/cli/crictl/crictl.go | 11 +++++++++ scripts/build | 9 +++++++- scripts/package-cli | 3 ++- 9 files changed, 124 insertions(+), 13 deletions(-) create mode 100644 cmd/ctr/main.go create mode 100644 pkg/cli/cmds/crictl.go create mode 100644 pkg/cli/cmds/ctr.go create mode 100644 pkg/cli/crictl/crictl.go diff --git a/cmd/ctr/main.go b/cmd/ctr/main.go new file mode 100644 index 0000000000..7361aee43d --- /dev/null +++ b/cmd/ctr/main.go @@ -0,0 +1,50 @@ +/* + Copyright The containerd Authors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package main + +import ( + "fmt" + "os" + + "github.com/containerd/containerd/cmd/ctr/app" + "github.com/containerd/containerd/pkg/seed" + "github.com/urfave/cli" +) + +func init() { + seed.WithTimeAndRand() +} + +func main() { + app := app.New() + for i, flag := range app.Flags { + if sFlag, ok := flag.(cli.StringFlag); ok { + if sFlag.Name == "address, a" { + sFlag.Value = "/run/k3s/containerd/containerd.sock" + app.Flags[i] = sFlag + } else if sFlag.Name == "namespace, n" { + sFlag.Value = "k8s.io" + app.Flags[i] = sFlag + } + } + } + + if err := app.Run(os.Args); err != nil { + fmt.Fprintf(os.Stderr, "ctr: %s\n", err) + os.Exit(1) + } +} diff --git a/cmd/k3s/main.go b/cmd/k3s/main.go index 3572c56518..919778f5a6 100644 --- a/cmd/k3s/main.go +++ b/cmd/k3s/main.go @@ -18,7 +18,7 @@ import ( ) func main() { - if runKubectl() { + if runCLIs() { return } @@ -26,7 +26,9 @@ func main() { app.Commands = []cli.Command{ cmds.NewServerCommand(wrap("k3s-server", os.Args)), cmds.NewAgentCommand(wrap("k3s-agent", os.Args)), - cmds.NewKubectlCommand(kubectlCLI), + cmds.NewKubectlCommand(externalCLIAction("kubectl")), + //cmds.NewCtrCommand(externalCLIAction("ctr")), + cmds.NewCRICTL(externalCLIAction("crictl")), } err := app.Run(os.Args) @@ -35,26 +37,30 @@ func main() { } } -func runKubectl() bool { - if filepath.Base(os.Args[0]) == "kubectl" { - if err := kubectl("", os.Args[1:]); err != nil { - logrus.Fatal(err) +func runCLIs() bool { + for _, cmd := range []string{"kubectl", "ctr", "crictl"} { + if filepath.Base(os.Args[0]) == cmd { + if err := externalCLI(cmd, "", os.Args[1:]); err != nil { + logrus.Fatal(err) + } + return true } - return true } return false } -func kubectlCLI(cli *cli.Context) error { - return kubectl(cli.String("data-dir"), cli.Args()) +func externalCLIAction(cmd string) func(cli *cli.Context) error { + return func(cli *cli.Context) error { + return externalCLI(cmd, cli.String("data-dir"), cli.Args()) + } } -func kubectl(dataDir string, args []string) error { +func externalCLI(cli, dataDir string, args []string) error { dataDir, err := datadir.Resolve(dataDir) if err != nil { return err } - return stageAndRun(dataDir, "kubectl", append([]string{"kubectl"}, args...)) + return stageAndRun(dataDir, cli, append([]string{cli}, args...)) } func wrap(cmd string, args []string) func(ctx *cli.Context) error { diff --git a/cmd/server/main.go b/cmd/server/main.go index 1694e821fe..1557e5d008 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -5,8 +5,10 @@ import ( "path/filepath" "github.com/docker/docker/pkg/reexec" + crictl2 "github.com/kubernetes-sigs/cri-tools/cmd/crictl" "github.com/rancher/k3s/pkg/cli/agent" "github.com/rancher/k3s/pkg/cli/cmds" + "github.com/rancher/k3s/pkg/cli/crictl" "github.com/rancher/k3s/pkg/cli/kubectl" "github.com/rancher/k3s/pkg/cli/server" "github.com/rancher/k3s/pkg/containerd" @@ -18,6 +20,7 @@ import ( func init() { reexec.Register("containerd", containerd.Main) reexec.Register("kubectl", kubectl2.Main) + reexec.Register("crictl", crictl2.Main) } func main() { @@ -33,6 +36,7 @@ func main() { cmds.NewServerCommand(server.Run), cmds.NewAgentCommand(agent.Run), cmds.NewKubectlCommand(kubectl.Run), + cmds.NewCRICTL(crictl.Run), } err := app.Run(os.Args) diff --git a/main.go b/main.go index 96e00f3da6..dc9211b98b 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,7 @@ import ( "github.com/rancher/k3s/pkg/cli/agent" "github.com/rancher/k3s/pkg/cli/cmds" + "github.com/rancher/k3s/pkg/cli/crictl" "github.com/rancher/k3s/pkg/cli/kubectl" "github.com/rancher/k3s/pkg/cli/server" "github.com/sirupsen/logrus" @@ -22,6 +23,7 @@ func main() { cmds.NewServerCommand(server.Run), cmds.NewAgentCommand(agent.Run), cmds.NewKubectlCommand(kubectl.Run), + cmds.NewCRICTL(crictl.Run), } if err := app.Run(os.Args); err != nil { diff --git a/pkg/cli/cmds/crictl.go b/pkg/cli/cmds/crictl.go new file mode 100644 index 0000000000..ff1bbbbfd0 --- /dev/null +++ b/pkg/cli/cmds/crictl.go @@ -0,0 +1,15 @@ +package cmds + +import ( + "github.com/urfave/cli" +) + +func NewCRICTL(action func(*cli.Context) error) cli.Command { + return cli.Command{ + Name: "crictl", + Usage: "Run crictl", + SkipFlagParsing: true, + SkipArgReorder: true, + Action: action, + } +} diff --git a/pkg/cli/cmds/ctr.go b/pkg/cli/cmds/ctr.go new file mode 100644 index 0000000000..c6a5e30ef7 --- /dev/null +++ b/pkg/cli/cmds/ctr.go @@ -0,0 +1,15 @@ +package cmds + +import ( + "github.com/urfave/cli" +) + +func NewCtrCommand(action func(*cli.Context) error) cli.Command { + return cli.Command{ + Name: "ctr", + Usage: "Run ctr", + SkipFlagParsing: true, + SkipArgReorder: true, + Action: action, + } +} diff --git a/pkg/cli/crictl/crictl.go b/pkg/cli/crictl/crictl.go new file mode 100644 index 0000000000..03b3b5b661 --- /dev/null +++ b/pkg/cli/crictl/crictl.go @@ -0,0 +1,11 @@ +package crictl + +import ( + "github.com/kubernetes-sigs/cri-tools/cmd/crictl" + "github.com/urfave/cli" +) + +func Run(ctx *cli.Context) error { + crictl.Main() + return nil +} diff --git a/scripts/build b/scripts/build index b45045ef89..5a2c5da92d 100755 --- a/scripts/build +++ b/scripts/build @@ -19,7 +19,11 @@ fi mkdir -p bin -rm -f bin/k3s-agent bin/hyperkube bin/containerd bin/cni ./bin/runc bin/containerd-shim bin/k3s-server bin/kubectl +if [ -z "$GOARM" ] && [ "arm" = "$(go env GOARCH)" ]; then + GOARM=7 +fi + +rm -f bin/k3s-agent bin/hyperkube bin/containerd bin/cni ./bin/runc bin/containerd-shim bin/k3s-server bin/kubectl bin/crictl # echo Building agent # CGO_ENABLED=1 go build -tags "$TAGS" -ldflags "$LDFLAGS $STATIC" -o bin/k3s-agent ./cmd/agent/main.go echo Building server @@ -27,8 +31,11 @@ CGO_ENABLED=1 go build -tags "$TAGS" -ldflags "$LDFLAGS $STATIC_SQLITE" -o bin/c ln -s containerd ./bin/k3s-agent ln -s containerd ./bin/k3s-server ln -s containerd ./bin/kubectl +ln -s containerd ./bin/crictl echo Building hyperkube CGO_ENABLED=1 go build -tags "$TAGS" -ldflags "$LDFLAGS $STATIC_SQLITE" -o bin/hyperkube ./vendor/k8s.io/kubernetes/cmd/hyperkube/ +#echo Building ctr +#CGO_ENABLED=1 go build -tags "$TAGS" -ldflags "$LDFLAGS $STATIC_SQLITE" -o bin/ctr ./cmd/ctr/main.go # echo Building containerd # CGO_ENABLED=0 go build -tags "$TAGS" -ldflags "$LDFLAGS $STATIC" -o bin/containerd ./cmd/containerd/ echo Building cni diff --git a/scripts/package-cli b/scripts/package-cli index 14ba3ab48f..83d0040f74 100755 --- a/scripts/package-cli +++ b/scripts/package-cli @@ -7,10 +7,11 @@ cd $(dirname $0)/.. ./scripts/download -rm -rf bin/kubectl bin/k3s-agent bin/k3s-server bin/kubectl bin/k3s build/data +rm -rf bin/crictl bin/kubectl bin/k3s-agent bin/k3s-server bin/kubectl bin/k3s build/data ln -s containerd bin/k3s-agent ln -s containerd bin/k3s-server ln -s containerd bin/kubectl +ln -s containerd bin/crictl for i in bridge flannel host-local loopback portmap; do if [ -e ./bin/$i ]; then rm -f ./bin/$i