Skip to content

Commit

Permalink
Add crictl
Browse files Browse the repository at this point in the history
  • Loading branch information
ibuildthecloud committed Feb 8, 2019
1 parent 76a1918 commit 793ac4f
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 13 deletions.
50 changes: 50 additions & 0 deletions cmd/ctr/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
28 changes: 17 additions & 11 deletions cmd/k3s/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@ import (
)

func main() {
if runKubectl() {
if runCLIs() {
return
}

app := cmds.NewApp()
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)
Expand All @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -18,6 +20,7 @@ import (
func init() {
reexec.Register("containerd", containerd.Main)
reexec.Register("kubectl", kubectl2.Main)
reexec.Register("crictl", crictl2.Main)
}

func main() {
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down
15 changes: 15 additions & 0 deletions pkg/cli/cmds/crictl.go
Original file line number Diff line number Diff line change
@@ -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,
}
}
15 changes: 15 additions & 0 deletions pkg/cli/cmds/ctr.go
Original file line number Diff line number Diff line change
@@ -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,
}
}
11 changes: 11 additions & 0 deletions pkg/cli/crictl/crictl.go
Original file line number Diff line number Diff line change
@@ -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
}
9 changes: 8 additions & 1 deletion scripts/build
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,23 @@ 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
CGO_ENABLED=1 go build -tags "$TAGS" -ldflags "$LDFLAGS $STATIC_SQLITE" -o bin/containerd ./cmd/server/main.go
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
Expand Down
3 changes: 2 additions & 1 deletion scripts/package-cli
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 793ac4f

Please sign in to comment.