|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/tls" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/coreos/etcd/clientv3" |
| 11 | + "github.com/coreos/etcd/pkg/transport" |
| 12 | + "golang.org/x/net/context" |
| 13 | + "k8s.io/kubernetes/pkg/api" |
| 14 | + "k8s.io/kubernetes/pkg/runtime/serializer/json" |
| 15 | + "k8s.io/kubernetes/pkg/runtime/serializer/protobuf" |
| 16 | + |
| 17 | + // install all APIs |
| 18 | + _ "github.com/openshift/origin/pkg/api/install" |
| 19 | + _ "github.com/openshift/origin/pkg/quota/api/install" |
| 20 | + _ "k8s.io/kubernetes/pkg/api/install" |
| 21 | +) |
| 22 | + |
| 23 | +func main() { |
| 24 | + var endpoint, keyFile, certFile, caFile string |
| 25 | + flag.StringVar(&endpoint, "endpoint", "https://127.0.0.1:4001", "Etcd endpoint.") |
| 26 | + flag.StringVar(&keyFile, "key-file", "", "TLS client key.") |
| 27 | + flag.StringVar(&certFile, "cert-file", "", "TLS client certificate.") |
| 28 | + flag.StringVar(&caFile, "ca-file", "", "Server TLS CA certificate.") |
| 29 | + flag.Parse() |
| 30 | + |
| 31 | + if flag.NArg() == 0 { |
| 32 | + fmt.Fprintf(os.Stderr, "ERROR: you need to specify action ls [<key>] or get <key>\n") |
| 33 | + os.Exit(1) |
| 34 | + } |
| 35 | + if flag.Arg(0) == "get" && flag.NArg() == 1 { |
| 36 | + fmt.Fprintf(os.Stderr, "ERROR: you need to specify <key> for get operation\n") |
| 37 | + os.Exit(1) |
| 38 | + } |
| 39 | + action := flag.Arg(0) |
| 40 | + key := "" |
| 41 | + if flag.NArg() > 1 { |
| 42 | + key = flag.Arg(1) |
| 43 | + } |
| 44 | + |
| 45 | + var tlsConfig *tls.Config |
| 46 | + if len(certFile) != 0 || len(keyFile) != 0 || len(caFile) != 0 { |
| 47 | + tlsInfo := transport.TLSInfo{ |
| 48 | + CertFile: certFile, |
| 49 | + KeyFile: keyFile, |
| 50 | + CAFile: caFile, |
| 51 | + } |
| 52 | + var err error |
| 53 | + tlsConfig, err = tlsInfo.ClientConfig() |
| 54 | + if err != nil { |
| 55 | + fmt.Fprintf(os.Stderr, "ERROR: unable to create client config: %v\n", err) |
| 56 | + os.Exit(1) |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + config := clientv3.Config{ |
| 61 | + Endpoints: []string{endpoint}, |
| 62 | + TLS: tlsConfig, |
| 63 | + DialTimeout: 5 * time.Second, |
| 64 | + } |
| 65 | + client, err := clientv3.New(config) |
| 66 | + if err != nil { |
| 67 | + fmt.Fprintf(os.Stderr, "ERROR: unable to connect to etcd: %v\n", err) |
| 68 | + os.Exit(1) |
| 69 | + } |
| 70 | + defer client.Close() |
| 71 | + |
| 72 | + switch action { |
| 73 | + case "ls": |
| 74 | + err = listKeys(client, key) |
| 75 | + case "get": |
| 76 | + err = getKey(client, key) |
| 77 | + } |
| 78 | + if err != nil { |
| 79 | + fmt.Fprintf(os.Stderr, "ERROR: %s-ing %s: %v\n", action, key, err) |
| 80 | + os.Exit(1) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func listKeys(client *clientv3.Client, key string) error { |
| 85 | + var resp *clientv3.GetResponse |
| 86 | + var err error |
| 87 | + if len(key) == 0 { |
| 88 | + resp, err = client.Get(context.TODO(), "/", clientv3.WithFromKey(), clientv3.WithKeysOnly()) |
| 89 | + } else { |
| 90 | + resp, err = client.Get(context.TODO(), key, clientv3.WithPrefix(), clientv3.WithKeysOnly()) |
| 91 | + } |
| 92 | + if err != nil { |
| 93 | + return err |
| 94 | + } |
| 95 | + for _, ev := range resp.Kvs { |
| 96 | + fmt.Printf("%s\n", ev.Key) |
| 97 | + } |
| 98 | + return nil |
| 99 | +} |
| 100 | + |
| 101 | +func getKey(client *clientv3.Client, key string) error { |
| 102 | + resp, err := client.Get(context.TODO(), key) |
| 103 | + if err != nil { |
| 104 | + return err |
| 105 | + } |
| 106 | + ps := protobuf.NewSerializer(api.Scheme, api.Scheme, "application/vnd.kubernetes.protobuf") |
| 107 | + js := json.NewSerializer(json.DefaultMetaFactory, api.Scheme, api.Scheme, true) |
| 108 | + for _, ev := range resp.Kvs { |
| 109 | + obj, gvk, err := ps.Decode(ev.Value, nil, nil) |
| 110 | + if err != nil { |
| 111 | + fmt.Fprintf(os.Stderr, "ERROR: unable to decode %s: %v\n", ev.Key, err) |
| 112 | + continue |
| 113 | + } |
| 114 | + fmt.Println(gvk) |
| 115 | + err = js.Encode(obj, os.Stdout) |
| 116 | + if err != nil { |
| 117 | + fmt.Fprintf(os.Stderr, "ERROR: unable to decode %s: %v\n", ev.Key, err) |
| 118 | + continue |
| 119 | + } |
| 120 | + } |
| 121 | + return nil |
| 122 | +} |
0 commit comments