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"
|
2016-07-22 20:44:27 +00:00
|
|
|
"time"
|
2016-07-13 21:10:10 +00:00
|
|
|
|
2016-09-23 07:41:21 +00:00
|
|
|
"github.com/Sirupsen/logrus"
|
2016-07-13 21:10:10 +00:00
|
|
|
"github.com/urfave/cli"
|
|
|
|
"google.golang.org/grpc"
|
2016-09-23 07:41:21 +00:00
|
|
|
pb "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
|
2016-09-15 22:55:39 +00:00
|
|
|
)
|
|
|
|
|
2016-07-13 21:10:10 +00:00
|
|
|
const (
|
2016-07-22 20:44:27 +00:00
|
|
|
// TODO: Make configurable
|
|
|
|
timeout = 10 * time.Second
|
2016-07-13 21:10:10 +00:00
|
|
|
)
|
|
|
|
|
2016-09-23 07:41:21 +00:00
|
|
|
func getClientConnection(context *cli.Context) (*grpc.ClientConn, error) {
|
2016-10-10 09:57:17 +00:00
|
|
|
conn, err := grpc.Dial(context.GlobalString("connect"), grpc.WithInsecure(), grpc.WithTimeout(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()
|
2016-07-15 17:48:53 +00:00
|
|
|
app.Name = "ocic"
|
|
|
|
app.Usage = "client for ocid"
|
2016-08-29 19:24:45 +00:00
|
|
|
app.Version = "0.0.1"
|
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,
|
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",
|
2016-09-23 07:41:21 +00:00
|
|
|
Value: "/var/run/ocid.sock",
|
|
|
|
Usage: "Socket to connect to",
|
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
|
|
|
}
|