2016-07-13 21:10:10 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-07-20 01:30:05 +00:00
|
|
|
"encoding/json"
|
2016-07-19 18:37:49 +00:00
|
|
|
"fmt"
|
2016-07-22 20:44:27 +00:00
|
|
|
"net"
|
2016-07-13 21:10:10 +00:00
|
|
|
"os"
|
2017-09-07 18:27:20 +00:00
|
|
|
"strings"
|
2016-07-22 20:44:27 +00:00
|
|
|
"time"
|
2016-07-13 21:10:10 +00:00
|
|
|
|
2017-08-05 11:40:46 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2016-07-13 21:10:10 +00:00
|
|
|
"github.com/urfave/cli"
|
|
|
|
"google.golang.org/grpc"
|
2017-08-04 11:13:19 +00:00
|
|
|
pb "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime"
|
2016-09-15 22:55:39 +00:00
|
|
|
)
|
|
|
|
|
2017-09-07 18:27:20 +00:00
|
|
|
// This is populated by the Makefile from the VERSION file
|
|
|
|
// in the repository
|
|
|
|
var version = ""
|
|
|
|
|
|
|
|
// gitCommit is the commit that the binary is being built from.
|
|
|
|
// It will be populated by the Makefile.
|
|
|
|
var gitCommit = ""
|
|
|
|
|
2016-09-23 07:41:21 +00:00
|
|
|
func getClientConnection(context *cli.Context) (*grpc.ClientConn, error) {
|
2016-10-18 05:50:16 +00:00
|
|
|
conn, err := grpc.Dial(context.GlobalString("connect"), grpc.WithInsecure(), grpc.WithTimeout(context.GlobalDuration("timeout")),
|
2016-07-22 20:44:27 +00:00
|
|
|
grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
|
|
|
|
return net.DialTimeout("unix", addr, timeout)
|
|
|
|
}))
|
|
|
|
if err != nil {
|
2016-09-18 09:08:29 +00:00
|
|
|
return nil, fmt.Errorf("failed to connect: %v", err)
|
2016-07-22 20:44:27 +00:00
|
|
|
}
|
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
|
2016-08-01 17:39:42 +00:00
|
|
|
func openFile(path string) (*os.File, error) {
|
2016-07-20 01:30:05 +00:00
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
2016-08-01 17:39:42 +00:00
|
|
|
return nil, fmt.Errorf("config at %s not found", path)
|
2016-07-20 01:30:05 +00:00
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-08-01 17:39:42 +00:00
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadPodSandboxConfig(path string) (*pb.PodSandboxConfig, error) {
|
|
|
|
f, err := openFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-07-20 01:30:05 +00:00
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
var config pb.PodSandboxConfig
|
|
|
|
if err := json.NewDecoder(f).Decode(&config); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &config, nil
|
|
|
|
}
|
|
|
|
|
2016-08-01 17:39:42 +00:00
|
|
|
func loadContainerConfig(path string) (*pb.ContainerConfig, error) {
|
|
|
|
f, err := openFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
var config pb.ContainerConfig
|
|
|
|
if err := json.NewDecoder(f).Decode(&config); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &config, nil
|
|
|
|
}
|
|
|
|
|
2016-07-13 21:10:10 +00:00
|
|
|
func main() {
|
|
|
|
app := cli.NewApp()
|
2017-09-07 18:27:20 +00:00
|
|
|
var v []string
|
|
|
|
if version != "" {
|
|
|
|
v = append(v, version)
|
|
|
|
}
|
|
|
|
if gitCommit != "" {
|
|
|
|
v = append(v, fmt.Sprintf("commit: %s", gitCommit))
|
|
|
|
}
|
|
|
|
|
2017-05-12 13:36:15 +00:00
|
|
|
app.Name = "crioctl"
|
|
|
|
app.Usage = "client for crio"
|
2017-09-07 18:27:20 +00:00
|
|
|
app.Version = strings.Join(v, "\n")
|
2016-07-15 21:28:52 +00:00
|
|
|
|
|
|
|
app.Commands = []cli.Command{
|
2016-08-29 19:24:45 +00:00
|
|
|
podSandboxCommand,
|
2016-08-29 20:08:07 +00:00
|
|
|
containerCommand,
|
2016-07-15 21:28:52 +00:00
|
|
|
runtimeVersionCommand,
|
2016-09-23 07:50:05 +00:00
|
|
|
imageCommand,
|
2017-09-22 21:56:10 +00:00
|
|
|
infoCommand,
|
2016-07-15 21:28:52 +00:00
|
|
|
}
|
|
|
|
|
2016-09-15 22:55:39 +00:00
|
|
|
app.Flags = []cli.Flag{
|
|
|
|
cli.StringFlag{
|
2016-10-10 09:57:17 +00:00
|
|
|
Name: "connect",
|
2017-10-18 23:33:58 +00:00
|
|
|
Value: "/var/run/crio.sock",
|
2016-09-23 07:41:21 +00:00
|
|
|
Usage: "Socket to connect to",
|
2016-09-15 22:55:39 +00:00
|
|
|
},
|
2016-10-18 05:50:16 +00:00
|
|
|
cli.DurationFlag{
|
|
|
|
Name: "timeout",
|
|
|
|
Value: 10 * time.Second,
|
|
|
|
Usage: "Timeout of connecting to server",
|
|
|
|
},
|
2016-09-15 22:55:39 +00:00
|
|
|
}
|
|
|
|
|
2016-07-15 21:28:52 +00:00
|
|
|
if err := app.Run(os.Args); err != nil {
|
2016-09-23 07:41:21 +00:00
|
|
|
logrus.Fatal(err)
|
2016-07-13 21:10:10 +00:00
|
|
|
}
|
2016-07-15 21:28:52 +00:00
|
|
|
}
|