Skip to content

Commit b4864f5

Browse files
authored
Merge pull request kubernetes-sigs#859 from wzshiming/feat/hack-etcd
[kwokctl] Add hack subcommand for etcd operation
2 parents f813968 + f89a31c commit b4864f5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+2423
-9
lines changed

go.mod

+8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ require (
2222
github.com/wzshiming/cmux v0.3.2
2323
github.com/wzshiming/ctc v1.2.3
2424
github.com/wzshiming/easycel v0.5.0
25+
go.etcd.io/etcd/client/v3 v3.5.10
2526
golang.org/x/sync v0.5.0
2627
golang.org/x/sys v0.15.0
2728
golang.org/x/term v0.15.0
@@ -50,6 +51,8 @@ require (
5051
github.com/beorn7/perks v1.0.1 // indirect
5152
github.com/cespare/xxhash/v2 v2.2.0 // indirect
5253
github.com/containernetworking/cni v1.1.2 // indirect
54+
github.com/coreos/go-semver v0.3.1 // indirect
55+
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
5356
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
5457
github.com/davecgh/go-spew v1.1.1 // indirect
5558
github.com/distribution/distribution/v3 v3.0.0-20230214150026-36d8c594d7aa // indirect
@@ -103,7 +106,11 @@ require (
103106
github.com/wzshiming/trie v0.1.1 // indirect
104107
github.com/wzshiming/winseq v0.0.0-20200112104235-db357dc107ae // indirect
105108
github.com/xlab/treeprint v1.2.0 // indirect
109+
go.etcd.io/etcd/api/v3 v3.5.10 // indirect
110+
go.etcd.io/etcd/client/pkg/v3 v3.5.10 // indirect
106111
go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 // indirect
112+
go.uber.org/multierr v1.11.0 // indirect
113+
go.uber.org/zap v1.25.0 // indirect
107114
golang.org/x/crypto v0.17.0 // indirect
108115
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa // indirect
109116
golang.org/x/mod v0.14.0 // indirect
@@ -113,6 +120,7 @@ require (
113120
golang.org/x/time v0.3.0 // indirect
114121
golang.org/x/tools v0.15.0 // indirect
115122
google.golang.org/appengine v1.6.7 // indirect
123+
google.golang.org/genproto v0.0.0-20230822172742-b8732ec3820d // indirect
116124
google.golang.org/genproto/googleapis/api v0.0.0-20230822172742-b8732ec3820d // indirect
117125
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d // indirect
118126
google.golang.org/grpc v1.59.0 // indirect

go.sum

+17
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230512164433-5d1fd1a340c9 h
88
github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230512164433-5d1fd1a340c9/go.mod h1:pSwJ0fSY5KhvocuWSx4fz3BA8OrA1bQn+K1Eli3BRwM=
99
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
1010
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
11+
github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A=
12+
github.com/benbjohnson/clock v1.3.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
1113
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
1214
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
1315
github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM=
@@ -25,6 +27,10 @@ github.com/containernetworking/cni v1.1.2 h1:wtRGZVv7olUHMOqouPpn3cXJWpJgM6+EUl3
2527
github.com/containernetworking/cni v1.1.2/go.mod h1:sDpYKmGVENF3s6uvMvGgldDWeG8dMxakj/u+i9ht9vw=
2628
github.com/containernetworking/plugins v1.3.0 h1:QVNXMT6XloyMUoO2wUOqWTC1hWFV62Q6mVDp5H1HnjM=
2729
github.com/containernetworking/plugins v1.3.0/go.mod h1:Pc2wcedTQQCVuROOOaLBPPxrEXqqXBFt3cZ+/yVg6l0=
30+
github.com/coreos/go-semver v0.3.1 h1:yi21YpKnrx1gt5R+la8n5WgS0kCrsPp33dmEyHReZr4=
31+
github.com/coreos/go-semver v0.3.1/go.mod h1:irMmmIw/7yzSRPWryHsK7EYSg09caPQL03VsM8rvUec=
32+
github.com/coreos/go-systemd/v22 v22.5.0 h1:RrqgGjYQKalulkV8NGVIfkXQf6YYmOyiJKk8iXXhfZs=
33+
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
2834
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
2935
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
3036
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
@@ -67,6 +73,7 @@ github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEe
6773
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls=
6874
github.com/gobuffalo/flect v1.0.2 h1:eqjPGSo2WmjgY2XlpGwo2NXgL3RucAKo4k4qQMNA5sA=
6975
github.com/gobuffalo/flect v1.0.2/go.mod h1:A5msMlrHtLqh9umBSnvabjsMrCcCpAyzglnDvkbYKHs=
76+
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
7077
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
7178
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
7279
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE=
@@ -242,8 +249,16 @@ github.com/xlab/treeprint v1.2.0/go.mod h1:gj5Gd3gPdKtR1ikdDK6fnFLdmIS0X30kTTuNd
242249
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
243250
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
244251
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
252+
go.etcd.io/etcd/api/v3 v3.5.10 h1:szRajuUUbLyppkhs9K6BRtjY37l66XQQmw7oZRANE4k=
253+
go.etcd.io/etcd/api/v3 v3.5.10/go.mod h1:TidfmT4Uycad3NM/o25fG3J07odo4GBB9hoxaodFCtI=
254+
go.etcd.io/etcd/client/pkg/v3 v3.5.10 h1:kfYIdQftBnbAq8pUWFXfpuuxFSKzlmM5cSn76JByiT0=
255+
go.etcd.io/etcd/client/pkg/v3 v3.5.10/go.mod h1:DYivfIviIuQ8+/lCq4vcxuseg2P2XbHygkKwFo9fc8U=
256+
go.etcd.io/etcd/client/v3 v3.5.10 h1:W9TXNZ+oB3MCd/8UjxHTWK5J9Nquw9fQBLJd5ne5/Ao=
257+
go.etcd.io/etcd/client/v3 v3.5.10/go.mod h1:RVeBnDz2PUEZqTpgqwAtUd8nAPf5kjyFyND7P1VkOKc=
245258
go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 h1:+FNtrFTmVw0YZGpBGX56XDee331t6JAXeK2bcyhLOOc=
246259
go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5/go.mod h1:nmDLcffg48OtT/PSW0Hg7FvpRQsQh5OSqIylirxKC7o=
260+
go.uber.org/goleak v1.2.1 h1:NBol2c7O1ZokfZ0LEU9K6Whx/KnwvepVetCUhtKja4A=
261+
go.uber.org/goleak v1.2.1/go.mod h1:qlT2yGI9QafXHhZZLxlSuNsMw3FFLxBr+tBRlmO1xH4=
247262
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
248263
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
249264
go.uber.org/zap v1.25.0 h1:4Hvk6GtkucQ790dqmj7l1eEnRdKm3k3ZUrUMS2d5+5c=
@@ -339,6 +354,8 @@ gomodules.xyz/jsonpatch/v2 v2.4.0 h1:Ci3iUJyx9UeRx7CeFN8ARgGbkESwJK+KB9lLcWxY/Zw
339354
gomodules.xyz/jsonpatch/v2 v2.4.0/go.mod h1:AH3dM2RI6uoBZxn3LVrfvJ3E0/9dG4cSrbuBJT4moAY=
340355
google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c=
341356
google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
357+
google.golang.org/genproto v0.0.0-20230822172742-b8732ec3820d h1:VBu5YqKPv6XiJ199exd8Br+Aetz+o08F+PLMnwJQHAY=
358+
google.golang.org/genproto v0.0.0-20230822172742-b8732ec3820d/go.mod h1:yZTlhN0tQnXo3h00fuXNCxJdLdIdnVFVBaRJ5LWBbw4=
342359
google.golang.org/genproto/googleapis/api v0.0.0-20230822172742-b8732ec3820d h1:DoPTO70H+bcDXcd39vOqb2viZxgqeBeSGtZ55yZU4/Q=
343360
google.golang.org/genproto/googleapis/api v0.0.0-20230822172742-b8732ec3820d/go.mod h1:KjSP20unUpOx5kyQUFa7k4OJg0qeJ7DEZflGDu2p6Bk=
344361
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d h1:uvYuEyMHKNt+lT4K3bN6fGswmK8qSvcreM3BwjDh+y4=

pkg/kwokctl/cmd/hack/del/del.go

+186
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
/*
2+
Copyright 2023 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Package del defines a command to delete data in etcd
18+
package del
19+
20+
import (
21+
"context"
22+
"fmt"
23+
"os"
24+
25+
"github.com/spf13/cobra"
26+
"k8s.io/apimachinery/pkg/runtime/schema"
27+
28+
"sigs.k8s.io/kwok/pkg/config"
29+
"sigs.k8s.io/kwok/pkg/kwokctl/dryrun"
30+
"sigs.k8s.io/kwok/pkg/kwokctl/etcd"
31+
"sigs.k8s.io/kwok/pkg/kwokctl/runtime"
32+
"sigs.k8s.io/kwok/pkg/log"
33+
"sigs.k8s.io/kwok/pkg/utils/client"
34+
"sigs.k8s.io/kwok/pkg/utils/path"
35+
)
36+
37+
type flagpole struct {
38+
Name string
39+
Namespace string
40+
Prefix string
41+
Output string
42+
}
43+
44+
// NewCommand returns a new cobra.Command for use hack the etcd data.
45+
func NewCommand(ctx context.Context) *cobra.Command {
46+
flags := &flagpole{}
47+
48+
cmd := &cobra.Command{
49+
Args: cobra.RangeArgs(0, 2),
50+
Use: "delete [resource] [name]",
51+
Short: "delete data in etcd",
52+
RunE: func(cmd *cobra.Command, args []string) error {
53+
flags.Name = config.DefaultCluster
54+
err := runE(cmd.Context(), flags, args)
55+
if err != nil {
56+
return fmt.Errorf("%v: %w", args, err)
57+
}
58+
return nil
59+
},
60+
}
61+
62+
cmd.Flags().StringVar(&flags.Prefix, "prefix", "/registry", "prefix of the key")
63+
cmd.Flags().StringVarP(&flags.Output, "output", "o", "key", "output format. One of: (key, none).")
64+
cmd.Flags().StringVarP(&flags.Namespace, "namespace", "n", "", "namespace of resource")
65+
return cmd
66+
}
67+
68+
func runE(ctx context.Context, flags *flagpole, args []string) error {
69+
name := config.ClusterName(flags.Name)
70+
workdir := path.Join(config.ClustersDir, flags.Name)
71+
72+
logger := log.FromContext(ctx)
73+
logger = logger.With("cluster", flags.Name)
74+
ctx = log.NewContext(ctx, logger)
75+
76+
rt, err := runtime.DefaultRegistry.Load(ctx, name, workdir)
77+
if err != nil {
78+
return err
79+
}
80+
81+
if rt.IsDryRun() {
82+
switch len(args) {
83+
case 1:
84+
if flags.Namespace == "" {
85+
dryrun.PrintMessage("kubectl delete %s --all -A", args[0])
86+
} else {
87+
dryrun.PrintMessage("kubectl delete %s --all -n %s", args[0], flags.Namespace)
88+
}
89+
case 2:
90+
if flags.Namespace == "" {
91+
dryrun.PrintMessage("kubectl delete %s %s", args[0], args[1])
92+
} else {
93+
dryrun.PrintMessage("kubectl delete %s %s -n %s", args[0], args[1], flags.Namespace)
94+
}
95+
default:
96+
if flags.Namespace == "" {
97+
dryrun.PrintMessage("kubectl delete all --all -A")
98+
} else {
99+
dryrun.PrintMessage("kubectl delete all -all -n %s", flags.Namespace)
100+
}
101+
}
102+
return nil
103+
}
104+
105+
etcdclient, err := rt.GetEtcdClient(ctx)
106+
if err != nil {
107+
return err
108+
}
109+
110+
var targetGvr schema.GroupVersionResource
111+
var targetName string
112+
var targetNamespace string
113+
if len(args) != 0 {
114+
kubeconfigPath := rt.GetWorkdirPath(runtime.InHostKubeconfigName)
115+
clientset, err := client.NewClientset("", kubeconfigPath)
116+
if err != nil {
117+
return err
118+
}
119+
120+
dc, err := clientset.ToDiscoveryClient()
121+
if err != nil {
122+
return err
123+
}
124+
rl, err := dc.ServerPreferredResources()
125+
if err != nil {
126+
return err
127+
}
128+
129+
resourceName := args[0]
130+
131+
gvr, resource, err := client.MatchShortResourceName(rl, resourceName)
132+
if err != nil {
133+
return err
134+
}
135+
136+
if resource.Namespaced {
137+
if flags.Namespace == "" {
138+
flags.Namespace = "default"
139+
}
140+
} else {
141+
if flags.Namespace != "" {
142+
return fmt.Errorf("resource %s is not namespaced", gvr)
143+
}
144+
}
145+
146+
targetGvr = gvr
147+
targetNamespace = flags.Namespace
148+
if len(args) >= 2 {
149+
targetName = args[1]
150+
}
151+
}
152+
153+
var count int
154+
var response func(kv *etcd.KeyValue) error
155+
if flags.Output == "key" {
156+
response = func(kv *etcd.KeyValue) error {
157+
count++
158+
fmt.Fprintf(os.Stdout, "%s\n", kv.Key)
159+
return nil
160+
}
161+
}
162+
163+
opOpts := []etcd.OpOption{
164+
etcd.WithName(targetName, targetNamespace),
165+
etcd.WithGVR(targetGvr),
166+
}
167+
168+
if response != nil {
169+
opOpts = append(opOpts,
170+
etcd.WithKeysOnly(),
171+
etcd.WithResponse(response),
172+
)
173+
}
174+
175+
err = etcdclient.Delete(ctx, flags.Prefix,
176+
opOpts...,
177+
)
178+
if err != nil {
179+
return err
180+
}
181+
182+
if log.IsTerminal() && flags.Output == "key" {
183+
fmt.Fprintf(os.Stderr, "delete %d keys\n", count)
184+
}
185+
return nil
186+
}

0 commit comments

Comments
 (0)