|
| 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