Replace ctrcri with ctr cri.

Signed-off-by: Lantao Liu <lantaol@google.com>
This commit is contained in:
Lantao Liu
2018-03-15 23:22:00 +00:00
parent 62d1f13217
commit 7e67d96b9b
8 changed files with 35 additions and 248 deletions

35
cmd/ctr/main.go Normal file
View File

@@ -0,0 +1,35 @@
/*
Copyright 2018 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/command"
cricli "github.com/containerd/cri/cli"
)
func main() {
app := command.App()
app.Commands = append(app.Commands, cricli.Command)
if err := app.Run(os.Args); err != nil {
fmt.Fprintf(os.Stderr, "ctr: %s\n", err)
os.Exit(1)
}
}

View File

@@ -1,82 +0,0 @@
/*
Copyright 2018 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 (
"os"
"time"
"github.com/containerd/containerd/defaults"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/containerd/cri/pkg/version"
)
const (
// Add \u200B to avoid the space trimming.
desc = "\u200B" + ` __ _
_____/ /________________(_)
/ ___/ __/ ___/ ___/ ___/ /
/ /__/ /_/ / / /__/ / / /
\___/\__/_/ \___/_/ /_/
containerd CRI plugin CLI
`
command = "ctrcri"
)
var cmd = &cobra.Command{
Use: command,
Short: "A CLI for containerd CRI plugin.",
Long: desc,
}
var (
// address is the address for containerd's GRPC server.
address string
// timeout is the timeout for containerd grpc connection.
timeout time.Duration
// defaultTimeout is the default timeout for containerd grpc connection.
defaultTimeout = 10 * time.Second
)
func addGlobalFlags(fs *pflag.FlagSet) {
fs.StringVar(&address, "address", defaults.DefaultAddress, "address for containerd's GRPC server.")
fs.DurationVar(&timeout, "timeout", defaultTimeout, "timeout for containerd grpc connection.")
}
func versionCommand() *cobra.Command {
return &cobra.Command{
Use: "version",
Short: "Print " + command + " version information.",
Run: func(cmd *cobra.Command, args []string) {
version.PrintVersion()
},
}
}
func main() {
addGlobalFlags(cmd.PersistentFlags())
cmd.AddCommand(versionCommand())
cmd.AddCommand(loadImageCommand())
if err := cmd.Execute(); err != nil {
// Error should have been reported.
os.Exit(1)
}
}

View File

@@ -1,88 +0,0 @@
/*
Copyright 2017 The Kubernetes 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"
"path/filepath"
"strings"
dedentutil "github.com/renstrom/dedent"
"github.com/spf13/cobra"
"golang.org/x/net/context"
api "github.com/containerd/cri/pkg/api/v1"
"github.com/containerd/cri/pkg/client"
)
func dedent(s string) string {
return strings.TrimLeft(dedentutil.Dedent(s), "\n")
}
var (
loadLong = dedent(`
Help for "load TAR" command
TAR - The path to a tar archive containing a container image.
Requirement:
Containerd and its cri plugin must already be up and running before the load
command is used.
Description:
Running as a client, ` + command + ` implements the load command by sending the
load request to the already running containerd daemon/server, which in
turn loads the image into containerd's image storage.`)
loadExample = dedent(`
- use docker to pull the latest busybox image and save it as a tar archive:
$ docker pull busybox:latest
$ docker save busybox:latest -o busybox.tar
- use ` + command + ` to load the image into containerd's image storage:
$ ` + command + ` load busybox.tar`)
)
func loadImageCommand() *cobra.Command {
c := &cobra.Command{
Use: "load TAR",
Long: loadLong,
Short: "Load an image from a tar archive.",
Args: cobra.ExactArgs(1),
Example: loadExample,
}
c.SetUsageTemplate(strings.Replace(c.UsageTemplate(), "Examples:", "Example:", 1))
c.RunE = func(cmd *cobra.Command, args []string) error {
cl, err := client.NewCRIContainerdClient(address, timeout)
if err != nil {
return fmt.Errorf("failed to create grpc client: %v", err)
}
path, err := filepath.Abs(args[0])
if err != nil {
return fmt.Errorf("failed to get absolute path: %v", err)
}
res, err := cl.LoadImage(context.Background(), &api.LoadImageRequest{FilePath: path})
if err != nil {
return fmt.Errorf("failed to load image: %v", err)
}
images := res.GetImages()
for _, image := range images {
fmt.Println("Loaded image:", image)
}
return nil
}
return c
}