forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
etcdhelper.go
197 lines (173 loc) · 5.05 KB
/
etcdhelper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package main
import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"flag"
"fmt"
"os"
"time"
jsonserializer "k8s.io/apimachinery/pkg/runtime/serializer/json"
"k8s.io/kubernetes/pkg/kubectl/scheme"
"github.com/coreos/etcd/clientv3"
"github.com/coreos/etcd/pkg/transport"
// install all APIs
install "github.com/openshift/origin/pkg/api/install"
"github.com/openshift/origin/pkg/api/legacy"
)
func init() {
install.InstallInternalOpenShift(scheme.Scheme)
install.InstallInternalKube(scheme.Scheme)
legacy.InstallInternalLegacyAll(scheme.Scheme)
}
func main() {
var endpoint, keyFile, certFile, caFile string
flag.StringVar(&endpoint, "endpoint", "https://127.0.0.1:2379", "etcd endpoint.")
flag.StringVar(&keyFile, "key", "", "TLS client key.")
flag.StringVar(&certFile, "cert", "", "TLS client certificate.")
flag.StringVar(&caFile, "cacert", "", "Server TLS CA certificate.")
flag.Parse()
if flag.NArg() == 0 {
fmt.Fprint(os.Stderr, "ERROR: you need to specify action: dump or ls [<key>] or get <key>\n")
os.Exit(1)
}
if flag.Arg(0) == "get" && flag.NArg() == 1 {
fmt.Fprint(os.Stderr, "ERROR: you need to specify <key> for get operation\n")
os.Exit(1)
}
if flag.Arg(0) == "dump" && flag.NArg() != 1 {
fmt.Fprint(os.Stderr, "ERROR: you cannot specify positional arguments with dump\n")
os.Exit(1)
}
action := flag.Arg(0)
key := ""
if flag.NArg() > 1 {
key = flag.Arg(1)
}
var tlsConfig *tls.Config
if len(certFile) != 0 || len(keyFile) != 0 || len(caFile) != 0 {
tlsInfo := transport.TLSInfo{
CertFile: certFile,
KeyFile: keyFile,
CAFile: caFile,
}
var err error
tlsConfig, err = tlsInfo.ClientConfig()
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: unable to create client config: %v\n", err)
os.Exit(1)
}
}
config := clientv3.Config{
Endpoints: []string{endpoint},
TLS: tlsConfig,
DialTimeout: 5 * time.Second,
}
client, err := clientv3.New(config)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: unable to connect to etcd: %v\n", err)
os.Exit(1)
}
defer client.Close()
switch action {
case "ls":
err = listKeys(client, key)
case "get":
err = getKey(client, key)
case "dump":
err = dump(client)
default:
fmt.Fprintf(os.Stderr, "ERROR: invalid action: %s\n", action)
os.Exit(1)
}
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %s-ing %s: %v\n", action, key, err)
os.Exit(1)
}
}
func listKeys(client *clientv3.Client, key string) error {
var resp *clientv3.GetResponse
var err error
if len(key) == 0 {
resp, err = clientv3.NewKV(client).Get(context.Background(), "/", clientv3.WithFromKey(), clientv3.WithKeysOnly())
} else {
resp, err = clientv3.NewKV(client).Get(context.Background(), key, clientv3.WithPrefix(), clientv3.WithKeysOnly())
}
if err != nil {
return err
}
for _, kv := range resp.Kvs {
fmt.Println(string(kv.Key))
}
return nil
}
func getKey(client *clientv3.Client, key string) error {
resp, err := clientv3.NewKV(client).Get(context.Background(), key)
if err != nil {
return err
}
decoder := scheme.Codecs.UniversalDeserializer()
encoder := jsonserializer.NewSerializer(jsonserializer.DefaultMetaFactory, scheme.Scheme, scheme.Scheme, true)
for _, kv := range resp.Kvs {
obj, gvk, err := decoder.Decode(kv.Value, nil, nil)
if err != nil {
fmt.Fprintf(os.Stderr, "WARN: unable to decode %s: %v\n", kv.Key, err)
continue
}
fmt.Println(gvk)
err = encoder.Encode(obj, os.Stdout)
if err != nil {
fmt.Fprintf(os.Stderr, "WARN: unable to encode %s: %v\n", kv.Key, err)
continue
}
}
return nil
}
func dump(client *clientv3.Client) error {
response, err := clientv3.NewKV(client).Get(context.Background(), "/", clientv3.WithPrefix(), clientv3.WithSort(clientv3.SortByKey, clientv3.SortDescend))
if err != nil {
return err
}
kvData := []etcd3kv{}
decoder := scheme.Codecs.UniversalDeserializer()
encoder := jsonserializer.NewSerializer(jsonserializer.DefaultMetaFactory, scheme.Scheme, scheme.Scheme, false)
objJSON := &bytes.Buffer{}
for _, kv := range response.Kvs {
obj, _, err := decoder.Decode(kv.Value, nil, nil)
if err != nil {
fmt.Fprintf(os.Stderr, "WARN: error decoding value %q: %v\n", string(kv.Value), err)
continue
}
objJSON.Reset()
if err := encoder.Encode(obj, objJSON); err != nil {
fmt.Fprintf(os.Stderr, "WARN: error encoding object %#v as JSON: %v", obj, err)
continue
}
kvData = append(
kvData,
etcd3kv{
Key: string(kv.Key),
Value: string(objJSON.Bytes()),
CreateRevision: kv.CreateRevision,
ModRevision: kv.ModRevision,
Version: kv.Version,
Lease: kv.Lease,
},
)
}
jsonData, err := json.MarshalIndent(kvData, "", " ")
if err != nil {
return err
}
fmt.Println(string(jsonData))
return nil
}
type etcd3kv struct {
Key string `json:"key,omitempty"`
Value string `json:"value,omitempty"`
CreateRevision int64 `json:"create_revision,omitempty"`
ModRevision int64 `json:"mod_revision,omitempty"`
Version int64 `json:"version,omitempty"`
Lease int64 `json:"lease,omitempty"`
}