-
Notifications
You must be signed in to change notification settings - Fork 9.8k
/
etcdctl.go
702 lines (638 loc) · 19.7 KB
/
etcdctl.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
// Copyright 2022 The etcd 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 e2e
import (
"context"
"encoding/json"
"fmt"
"io"
"strconv"
"strings"
"time"
"google.golang.org/grpc"
"go.etcd.io/etcd/api/v3/authpb"
"go.etcd.io/etcd/api/v3/etcdserverpb"
clientv3 "go.etcd.io/etcd/client/v3"
"go.etcd.io/etcd/tests/v3/framework/config"
)
type EtcdctlV3 struct {
cfg *EtcdProcessClusterConfig
endpoints []string
authConfig clientv3.AuthConfig
}
func NewEtcdctl(cfg *EtcdProcessClusterConfig, endpoints []string, opts ...config.ClientOption) (*EtcdctlV3, error) {
ctl := &EtcdctlV3{
cfg: cfg,
endpoints: endpoints,
}
for _, opt := range opts {
opt(ctl)
}
if !ctl.authConfig.Empty() {
client, err := clientv3.New(clientv3.Config{
Endpoints: ctl.endpoints,
DialTimeout: 5 * time.Second,
DialOptions: []grpc.DialOption{grpc.WithBlock()},
Username: ctl.authConfig.Username,
Password: ctl.authConfig.Password,
})
if err != nil {
return nil, err
}
client.Close()
}
return ctl, nil
}
func WithAuth(userName, password string) config.ClientOption {
return func(c any) {
ctl := c.(*EtcdctlV3)
ctl.authConfig.Username = userName
ctl.authConfig.Password = password
}
}
func (ctl *EtcdctlV3) DowngradeEnable(ctx context.Context, version string) error {
_, err := SpawnWithExpectLines(ctx, ctl.cmdArgs("downgrade", "enable", version), nil, "Downgrade enable success")
return err
}
func (ctl *EtcdctlV3) Get(ctx context.Context, key string, o config.GetOptions) (*clientv3.GetResponse, error) {
resp := clientv3.GetResponse{}
var args []string
if o.Timeout != 0 {
args = append(args, fmt.Sprintf("--command-timeout=%s", o.Timeout))
}
if o.Serializable {
args = append(args, "--consistency", "s")
}
args = append(args, "get", key, "-w", "json")
if o.End != "" {
args = append(args, o.End)
}
if o.Revision != 0 {
args = append(args, fmt.Sprintf("--rev=%d", o.Revision))
}
if o.Prefix {
args = append(args, "--prefix")
}
if o.Limit != 0 {
args = append(args, fmt.Sprintf("--limit=%d", o.Limit))
}
if o.FromKey {
args = append(args, "--from-key")
}
if o.CountOnly {
args = append(args, "-w", "fields", "--count-only")
} else {
args = append(args, "-w", "json")
}
switch o.SortBy {
case clientv3.SortByCreateRevision:
args = append(args, "--sort-by=CREATE")
case clientv3.SortByModRevision:
args = append(args, "--sort-by=MODIFY")
case clientv3.SortByValue:
args = append(args, "--sort-by=VALUE")
case clientv3.SortByVersion:
args = append(args, "--sort-by=VERSION")
case clientv3.SortByKey:
// nothing
default:
return nil, fmt.Errorf("bad sort target %v", o.SortBy)
}
switch o.Order {
case clientv3.SortAscend:
args = append(args, "--order=ASCEND")
case clientv3.SortDescend:
args = append(args, "--order=DESCEND")
case clientv3.SortNone:
// nothing
default:
return nil, fmt.Errorf("bad sort order %v", o.Order)
}
if o.CountOnly {
cmd, err := SpawnCmd(ctl.cmdArgs(args...), nil)
if err != nil {
return nil, err
}
defer cmd.Close()
_, err = cmd.ExpectWithContext(ctx, "Count")
return &resp, err
}
err := ctl.spawnJsonCmd(ctx, &resp, args...)
return &resp, err
}
func (ctl *EtcdctlV3) Put(ctx context.Context, key, value string, opts config.PutOptions) error {
args := ctl.cmdArgs()
args = append(args, "put", key, value)
if opts.LeaseID != 0 {
args = append(args, "--lease", strconv.FormatInt(int64(opts.LeaseID), 16))
}
_, err := SpawnWithExpectLines(ctx, args, nil, "OK")
return err
}
func (ctl *EtcdctlV3) Delete(ctx context.Context, key string, o config.DeleteOptions) (*clientv3.DeleteResponse, error) {
args := []string{"del", key}
if o.End != "" {
args = append(args, o.End)
}
if o.Prefix {
args = append(args, "--prefix")
}
if o.FromKey {
args = append(args, "--from-key")
}
var resp clientv3.DeleteResponse
err := ctl.spawnJsonCmd(ctx, &resp, args...)
return &resp, err
}
func (ctl *EtcdctlV3) Txn(ctx context.Context, compares, ifSucess, ifFail []string, o config.TxnOptions) (*clientv3.TxnResponse, error) {
args := ctl.cmdArgs()
args = append(args, "txn")
if o.Interactive {
args = append(args, "--interactive")
}
args = append(args, "-w", "json", "--hex=true")
cmd, err := SpawnCmd(args, nil)
if err != nil {
return nil, err
}
defer cmd.Close()
_, err = cmd.ExpectWithContext(ctx, "compares:")
if err != nil {
return nil, err
}
for _, cmp := range compares {
if err := cmd.Send(cmp + "\r"); err != nil {
return nil, err
}
}
if err := cmd.Send("\r"); err != nil {
return nil, err
}
_, err = cmd.ExpectWithContext(ctx, "success requests (get, put, del):")
if err != nil {
return nil, err
}
for _, req := range ifSucess {
if err = cmd.Send(req + "\r"); err != nil {
return nil, err
}
}
if err = cmd.Send("\r"); err != nil {
return nil, err
}
_, err = cmd.ExpectWithContext(ctx, "failure requests (get, put, del):")
if err != nil {
return nil, err
}
for _, req := range ifFail {
if err = cmd.Send(req + "\r"); err != nil {
return nil, err
}
}
if err = cmd.Send("\r"); err != nil {
return nil, err
}
var line string
line, err = cmd.ExpectWithContext(ctx, "header")
if err != nil {
return nil, err
}
var resp clientv3.TxnResponse
AddTxnResponse(&resp, line)
err = json.Unmarshal([]byte(line), &resp)
return &resp, err
}
// AddTxnResponse looks for ResponseOp json tags and adds the objects for json decoding
func AddTxnResponse(resp *clientv3.TxnResponse, jsonData string) {
if resp == nil {
return
}
if resp.Responses == nil {
resp.Responses = []*etcdserverpb.ResponseOp{}
}
jd := json.NewDecoder(strings.NewReader(jsonData))
for {
t, e := jd.Token()
if e == io.EOF {
break
}
if t == "response_range" {
resp.Responses = append(resp.Responses, &etcdserverpb.ResponseOp{
Response: &etcdserverpb.ResponseOp_ResponseRange{},
})
}
if t == "response_put" {
resp.Responses = append(resp.Responses, &etcdserverpb.ResponseOp{
Response: &etcdserverpb.ResponseOp_ResponsePut{},
})
}
if t == "response_delete_range" {
resp.Responses = append(resp.Responses, &etcdserverpb.ResponseOp{
Response: &etcdserverpb.ResponseOp_ResponseDeleteRange{},
})
}
if t == "response_txn" {
resp.Responses = append(resp.Responses, &etcdserverpb.ResponseOp{
Response: &etcdserverpb.ResponseOp_ResponseTxn{},
})
}
}
}
func (ctl *EtcdctlV3) MemberList(ctx context.Context) (*clientv3.MemberListResponse, error) {
var resp clientv3.MemberListResponse
err := ctl.spawnJsonCmd(ctx, &resp, "member", "list")
return &resp, err
}
func (ctl *EtcdctlV3) MemberAdd(ctx context.Context, name string, peerAddrs []string) (*clientv3.MemberAddResponse, error) {
var resp clientv3.MemberAddResponse
err := ctl.spawnJsonCmd(ctx, &resp, "member", "add", name, "--peer-urls", strings.Join(peerAddrs, ","))
return &resp, err
}
func (ctl *EtcdctlV3) MemberAddAsLearner(ctx context.Context, name string, peerAddrs []string) (*clientv3.MemberAddResponse, error) {
var resp clientv3.MemberAddResponse
err := ctl.spawnJsonCmd(ctx, &resp, "member", "add", name, "--learner", "--peer-urls", strings.Join(peerAddrs, ","))
return &resp, err
}
func (ctl *EtcdctlV3) MemberRemove(ctx context.Context, id uint64) (*clientv3.MemberRemoveResponse, error) {
var resp clientv3.MemberRemoveResponse
err := ctl.spawnJsonCmd(ctx, &resp, "member", "remove", fmt.Sprintf("%x", id))
return &resp, err
}
func (ctl *EtcdctlV3) cmdArgs(args ...string) []string {
cmdArgs := []string{BinPath.Etcdctl}
for k, v := range ctl.flags() {
cmdArgs = append(cmdArgs, fmt.Sprintf("--%s=%s", k, v))
}
return append(cmdArgs, args...)
}
func (ctl *EtcdctlV3) flags() map[string]string {
fmap := make(map[string]string)
if ctl.cfg.ClientTLS == ClientTLS {
if ctl.cfg.IsClientAutoTLS {
fmap["insecure-transport"] = "false"
fmap["insecure-skip-tls-verify"] = "true"
} else if ctl.cfg.IsClientCRL {
fmap["cacert"] = CaPath
fmap["cert"] = RevokedCertPath
fmap["key"] = RevokedPrivateKeyPath
} else {
fmap["cacert"] = CaPath
fmap["cert"] = CertPath
fmap["key"] = PrivateKeyPath
}
}
fmap["endpoints"] = strings.Join(ctl.endpoints, ",")
if !ctl.authConfig.Empty() {
fmap["user"] = ctl.authConfig.Username + ":" + ctl.authConfig.Password
}
return fmap
}
func (ctl *EtcdctlV3) Compact(ctx context.Context, rev int64, o config.CompactOption) (*clientv3.CompactResponse, error) {
args := ctl.cmdArgs("compact", fmt.Sprint(rev))
if o.Timeout != 0 {
args = append(args, fmt.Sprintf("--command-timeout=%s", o.Timeout))
}
if o.Physical {
args = append(args, "--physical")
}
_, err := SpawnWithExpectLines(ctx, args, nil, fmt.Sprintf("compacted revision %v", rev))
return nil, err
}
func (ctl *EtcdctlV3) Status(ctx context.Context) ([]*clientv3.StatusResponse, error) {
var epStatus []*struct {
Endpoint string
Status *clientv3.StatusResponse
}
err := ctl.spawnJsonCmd(ctx, &epStatus, "endpoint", "status")
if err != nil {
return nil, err
}
resp := make([]*clientv3.StatusResponse, len(epStatus))
for i, e := range epStatus {
resp[i] = e.Status
}
return resp, err
}
func (ctl *EtcdctlV3) HashKV(ctx context.Context, rev int64) ([]*clientv3.HashKVResponse, error) {
var epHashKVs []*struct {
Endpoint string
HashKV *clientv3.HashKVResponse
}
err := ctl.spawnJsonCmd(ctx, &epHashKVs, "endpoint", "hashkv", "--rev", fmt.Sprint(rev))
if err != nil {
return nil, err
}
resp := make([]*clientv3.HashKVResponse, len(epHashKVs))
for i, e := range epHashKVs {
resp[i] = e.HashKV
}
return resp, err
}
func (ctl *EtcdctlV3) Health(ctx context.Context) error {
args := ctl.cmdArgs()
args = append(args, "endpoint", "health")
lines := make([]string, len(ctl.endpoints))
for i := range lines {
lines[i] = "is healthy"
}
_, err := SpawnWithExpectLines(ctx, args, nil, lines...)
return err
}
func (ctl *EtcdctlV3) Grant(ctx context.Context, ttl int64) (*clientv3.LeaseGrantResponse, error) {
args := ctl.cmdArgs()
args = append(args, "lease", "grant", strconv.FormatInt(ttl, 10), "-w", "json")
cmd, err := SpawnCmd(args, nil)
if err != nil {
return nil, err
}
defer cmd.Close()
var resp clientv3.LeaseGrantResponse
line, err := cmd.ExpectWithContext(ctx, "ID")
if err != nil {
return nil, err
}
err = json.Unmarshal([]byte(line), &resp)
return &resp, err
}
func (ctl *EtcdctlV3) TimeToLive(ctx context.Context, id clientv3.LeaseID, o config.LeaseOption) (*clientv3.LeaseTimeToLiveResponse, error) {
args := ctl.cmdArgs()
args = append(args, "lease", "timetolive", strconv.FormatInt(int64(id), 16), "-w", "json")
if o.WithAttachedKeys {
args = append(args, "--keys")
}
cmd, err := SpawnCmd(args, nil)
if err != nil {
return nil, err
}
defer cmd.Close()
var resp clientv3.LeaseTimeToLiveResponse
line, err := cmd.ExpectWithContext(ctx, "id")
if err != nil {
return nil, err
}
err = json.Unmarshal([]byte(line), &resp)
return &resp, err
}
func (ctl *EtcdctlV3) Defragment(ctx context.Context, o config.DefragOption) error {
args := append(ctl.cmdArgs(), "defrag")
if o.Timeout != 0 {
args = append(args, fmt.Sprintf("--command-timeout=%s", o.Timeout))
}
lines := make([]string, len(ctl.endpoints))
for i := range lines {
lines[i] = "Finished defragmenting etcd member"
}
_, err := SpawnWithExpectLines(ctx, args, map[string]string{}, lines...)
return err
}
func (ctl *EtcdctlV3) Leases(ctx context.Context) (*clientv3.LeaseLeasesResponse, error) {
args := ctl.cmdArgs("lease", "list", "-w", "json")
cmd, err := SpawnCmd(args, nil)
if err != nil {
return nil, err
}
defer cmd.Close()
var resp clientv3.LeaseLeasesResponse
line, err := cmd.ExpectWithContext(ctx, "id")
if err != nil {
return nil, err
}
err = json.Unmarshal([]byte(line), &resp)
return &resp, err
}
func (ctl *EtcdctlV3) KeepAliveOnce(ctx context.Context, id clientv3.LeaseID) (*clientv3.LeaseKeepAliveResponse, error) {
args := ctl.cmdArgs("lease", "keep-alive", strconv.FormatInt(int64(id), 16), "--once", "-w", "json")
cmd, err := SpawnCmd(args, nil)
if err != nil {
return nil, err
}
defer cmd.Close()
var resp clientv3.LeaseKeepAliveResponse
line, err := cmd.ExpectWithContext(ctx, "ID")
if err != nil {
return nil, err
}
err = json.Unmarshal([]byte(line), &resp)
return &resp, err
}
func (ctl *EtcdctlV3) Revoke(ctx context.Context, id clientv3.LeaseID) (*clientv3.LeaseRevokeResponse, error) {
var resp clientv3.LeaseRevokeResponse
err := ctl.spawnJsonCmd(ctx, &resp, "lease", "revoke", strconv.FormatInt(int64(id), 16))
return &resp, err
}
func (ctl *EtcdctlV3) AlarmList(ctx context.Context) (*clientv3.AlarmResponse, error) {
var resp clientv3.AlarmResponse
err := ctl.spawnJsonCmd(ctx, &resp, "alarm", "list")
return &resp, err
}
func (ctl *EtcdctlV3) AlarmDisarm(ctx context.Context, _ *clientv3.AlarmMember) (*clientv3.AlarmResponse, error) {
args := ctl.cmdArgs()
args = append(args, "alarm", "disarm", "-w", "json")
ep, err := SpawnCmd(args, nil)
if err != nil {
return nil, err
}
defer ep.Close()
var resp clientv3.AlarmResponse
line, err := ep.ExpectWithContext(ctx, "alarm")
if err != nil {
return nil, err
}
err = json.Unmarshal([]byte(line), &resp)
return &resp, err
}
func (ctl *EtcdctlV3) AuthEnable(ctx context.Context) error {
args := []string{"auth", "enable"}
cmd, err := SpawnCmd(ctl.cmdArgs(args...), nil)
if err != nil {
return err
}
defer cmd.Close()
_, err = cmd.ExpectWithContext(ctx, "Authentication Enabled")
return err
}
func (ctl *EtcdctlV3) AuthDisable(ctx context.Context) error {
args := []string{"auth", "disable"}
cmd, err := SpawnCmd(ctl.cmdArgs(args...), nil)
if err != nil {
return err
}
defer cmd.Close()
_, err = cmd.ExpectWithContext(ctx, "Authentication Disabled")
return err
}
func (ctl *EtcdctlV3) AuthStatus(ctx context.Context) (*clientv3.AuthStatusResponse, error) {
var resp clientv3.AuthStatusResponse
err := ctl.spawnJsonCmd(ctx, &resp, "auth", "status")
return &resp, err
}
func (ctl *EtcdctlV3) UserAdd(ctx context.Context, name, password string, opts config.UserAddOptions) (*clientv3.AuthUserAddResponse, error) {
args := ctl.cmdArgs()
args = append(args, "user", "add")
if password == "" {
args = append(args, name)
} else {
args = append(args, fmt.Sprintf("%s:%s", name, password))
}
if opts.NoPassword {
args = append(args, "--no-password")
}
args = append(args, "--interactive=false", "-w", "json")
cmd, err := SpawnCmd(args, nil)
if err != nil {
return nil, err
}
defer cmd.Close()
// If no password is provided, and NoPassword isn't set, the CLI will always
// wait for a password, send an enter in this case for an "empty" password.
if !opts.NoPassword && password == "" {
err := cmd.Send("\n")
if err != nil {
return nil, err
}
}
var resp clientv3.AuthUserAddResponse
line, err := cmd.ExpectWithContext(ctx, "header")
if err != nil {
return nil, err
}
err = json.Unmarshal([]byte(line), &resp)
return &resp, err
}
func (ctl *EtcdctlV3) UserGet(ctx context.Context, name string) (*clientv3.AuthUserGetResponse, error) {
var resp clientv3.AuthUserGetResponse
err := ctl.spawnJsonCmd(ctx, &resp, "user", "get", name)
return &resp, err
}
func (ctl *EtcdctlV3) UserList(ctx context.Context) (*clientv3.AuthUserListResponse, error) {
var resp clientv3.AuthUserListResponse
err := ctl.spawnJsonCmd(ctx, &resp, "user", "list")
return &resp, err
}
func (ctl *EtcdctlV3) UserDelete(ctx context.Context, name string) (*clientv3.AuthUserDeleteResponse, error) {
var resp clientv3.AuthUserDeleteResponse
err := ctl.spawnJsonCmd(ctx, &resp, "user", "delete", name)
return &resp, err
}
func (ctl *EtcdctlV3) UserChangePass(ctx context.Context, user, newPass string) error {
args := ctl.cmdArgs()
args = append(args, "user", "passwd", user, "--interactive=false")
cmd, err := SpawnCmd(args, nil)
if err != nil {
return err
}
defer cmd.Close()
err = cmd.Send(newPass + "\n")
if err != nil {
return err
}
_, err = cmd.ExpectWithContext(ctx, "Password updated")
return err
}
func (ctl *EtcdctlV3) UserGrantRole(ctx context.Context, user string, role string) (*clientv3.AuthUserGrantRoleResponse, error) {
var resp clientv3.AuthUserGrantRoleResponse
err := ctl.spawnJsonCmd(ctx, &resp, "user", "grant-role", user, role)
return &resp, err
}
func (ctl *EtcdctlV3) UserRevokeRole(ctx context.Context, user string, role string) (*clientv3.AuthUserRevokeRoleResponse, error) {
var resp clientv3.AuthUserRevokeRoleResponse
err := ctl.spawnJsonCmd(ctx, &resp, "user", "revoke-role", user, role)
return &resp, err
}
func (ctl *EtcdctlV3) RoleAdd(ctx context.Context, name string) (*clientv3.AuthRoleAddResponse, error) {
var resp clientv3.AuthRoleAddResponse
err := ctl.spawnJsonCmd(ctx, &resp, "role", "add", name)
return &resp, err
}
func (ctl *EtcdctlV3) RoleGrantPermission(ctx context.Context, name string, key, rangeEnd string, permType clientv3.PermissionType) (*clientv3.AuthRoleGrantPermissionResponse, error) {
permissionType := authpb.Permission_Type_name[int32(permType)]
var resp clientv3.AuthRoleGrantPermissionResponse
err := ctl.spawnJsonCmd(ctx, &resp, "role", "grant-permission", name, permissionType, key, rangeEnd)
return &resp, err
}
func (ctl *EtcdctlV3) RoleGet(ctx context.Context, role string) (*clientv3.AuthRoleGetResponse, error) {
var resp clientv3.AuthRoleGetResponse
err := ctl.spawnJsonCmd(ctx, &resp, "role", "get", role)
return &resp, err
}
func (ctl *EtcdctlV3) RoleList(ctx context.Context) (*clientv3.AuthRoleListResponse, error) {
var resp clientv3.AuthRoleListResponse
err := ctl.spawnJsonCmd(ctx, &resp, "role", "list")
return &resp, err
}
func (ctl *EtcdctlV3) RoleRevokePermission(ctx context.Context, role string, key, rangeEnd string) (*clientv3.AuthRoleRevokePermissionResponse, error) {
var resp clientv3.AuthRoleRevokePermissionResponse
err := ctl.spawnJsonCmd(ctx, &resp, "role", "revoke-permission", role, key, rangeEnd)
return &resp, err
}
func (ctl *EtcdctlV3) RoleDelete(ctx context.Context, role string) (*clientv3.AuthRoleDeleteResponse, error) {
var resp clientv3.AuthRoleDeleteResponse
err := ctl.spawnJsonCmd(ctx, &resp, "role", "delete", role)
return &resp, err
}
func (ctl *EtcdctlV3) spawnJsonCmd(ctx context.Context, output interface{}, args ...string) error {
args = append(args, "-w", "json")
cmd, err := SpawnCmd(append(ctl.cmdArgs(), args...), nil)
if err != nil {
return err
}
defer cmd.Close()
line, err := cmd.ExpectWithContext(ctx, "header")
if err != nil {
return err
}
return json.Unmarshal([]byte(line), output)
}
func (ctl *EtcdctlV3) Watch(ctx context.Context, key string, opts config.WatchOptions) clientv3.WatchChan {
args := ctl.cmdArgs()
args = append(args, "watch", key)
if opts.RangeEnd != "" {
args = append(args, opts.RangeEnd)
}
args = append(args, "-w", "json")
if opts.Prefix {
args = append(args, "--prefix")
}
if opts.Revision != 0 {
args = append(args, "--rev", fmt.Sprint(opts.Revision))
}
proc, err := SpawnCmd(args, nil)
if err != nil {
return nil
}
ch := make(chan clientv3.WatchResponse)
go func() {
defer proc.Stop()
for {
select {
case <-ctx.Done():
close(ch)
return
default:
if line := proc.ReadLine(); line != "" {
var resp clientv3.WatchResponse
json.Unmarshal([]byte(line), &resp)
if resp.Canceled {
close(ch)
return
}
if len(resp.Events) > 0 {
ch <- resp
}
}
}
}
}()
return ch
}