|
| 1 | +/* |
| 2 | +Copyright 2024 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 command |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "os" |
| 23 | + |
| 24 | + "github.com/etcd-io/auger/pkg/client" |
| 25 | + "github.com/spf13/cobra" |
| 26 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 27 | +) |
| 28 | + |
| 29 | +type getFlagpole struct { |
| 30 | + Namespace string |
| 31 | + Output string |
| 32 | + ChunkSize int64 |
| 33 | + Prefix string |
| 34 | +} |
| 35 | + |
| 36 | +var ( |
| 37 | + getExample = ` |
| 38 | + # List a single service with namespace "default" and name "kubernetes" |
| 39 | + augerctl get services -n default kubernetes |
| 40 | + # Nearly equivalent |
| 41 | + kubectl get services -n default kubernetes -o yaml |
| 42 | +
|
| 43 | + # List a single resource of type "priorityclasses" and name "system-node-critical" without namespaced |
| 44 | + augerctl get priorityclasses system-node-critical |
| 45 | + # Nearly equivalent |
| 46 | + kubectl get priorityclasses system-node-critical -A -o yaml |
| 47 | +
|
| 48 | + # List all leases with namespace "kube-system" |
| 49 | + augerctl get leases -n kube-system |
| 50 | + # Nearly equivalent |
| 51 | + kubectl get leases -n kube-system -o yaml |
| 52 | +
|
| 53 | + # List a single resource of type "apiservices.apiregistration.k8s.io" and name "v1.apps" |
| 54 | + augerctl get apiservices.apiregistration.k8s.io v1.apps |
| 55 | + # Nearly equivalent |
| 56 | + kubectl get apiservices.apiregistration.k8s.io v1.apps -o yaml |
| 57 | +
|
| 58 | + # List all resources |
| 59 | + augerctl get |
| 60 | + # Nearly equivalent |
| 61 | + kubectl get $(kubectl api-resources --verbs=list --output=name | paste -s -d, - ) -A -o yaml |
| 62 | +` |
| 63 | +) |
| 64 | + |
| 65 | +func newCtlGetCommand(f *flagpole) *cobra.Command { |
| 66 | + flags := &getFlagpole{} |
| 67 | + |
| 68 | + cmd := &cobra.Command{ |
| 69 | + Args: cobra.RangeArgs(0, 2), |
| 70 | + Use: "get [resource] [name]", |
| 71 | + Short: "Gets the resource of Kubernetes in etcd", |
| 72 | + Example: getExample, |
| 73 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 74 | + etcdclient, err := clientFromCmd(f) |
| 75 | + if err != nil { |
| 76 | + return err |
| 77 | + } |
| 78 | + err = getCommand(cmd.Context(), etcdclient, flags, args) |
| 79 | + |
| 80 | + if err != nil { |
| 81 | + return fmt.Errorf("%v: %w", args, err) |
| 82 | + } |
| 83 | + return nil |
| 84 | + }, |
| 85 | + } |
| 86 | + |
| 87 | + cmd.Flags().StringVarP(&flags.Output, "output", "o", "yaml", "output format. One of: (yaml).") |
| 88 | + cmd.Flags().StringVarP(&flags.Namespace, "namespace", "n", "", "namespace of resource") |
| 89 | + cmd.Flags().Int64Var(&flags.ChunkSize, "chunk-size", 500, "chunk size of the list pager") |
| 90 | + cmd.Flags().StringVar(&flags.Prefix, "prefix", "/registry", "prefix to prepend to the resource") |
| 91 | + |
| 92 | + return cmd |
| 93 | +} |
| 94 | + |
| 95 | +func getCommand(ctx context.Context, etcdclient client.Client, flags *getFlagpole, args []string) error { |
| 96 | + var targetGr schema.GroupResource |
| 97 | + var targetName string |
| 98 | + var targetNamespace string |
| 99 | + if len(args) != 0 { |
| 100 | + // TODO: Support get information from CRD and scheme.Codecs |
| 101 | + // Support short name |
| 102 | + // Check for namespaced |
| 103 | + |
| 104 | + gr := schema.ParseGroupResource(args[0]) |
| 105 | + if gr.Empty() { |
| 106 | + return fmt.Errorf("invalid resource %q", args[0]) |
| 107 | + } |
| 108 | + targetGr = gr |
| 109 | + targetNamespace = flags.Namespace |
| 110 | + if len(args) >= 2 { |
| 111 | + targetName = args[1] |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + printer := NewPrinter(os.Stdout, flags.Output) |
| 116 | + if printer == nil { |
| 117 | + return fmt.Errorf("invalid output format: %q", flags.Output) |
| 118 | + } |
| 119 | + |
| 120 | + opOpts := []client.OpOption{ |
| 121 | + client.WithName(targetName, targetNamespace), |
| 122 | + client.WithGroupResource(targetGr), |
| 123 | + client.WithChunkSize(flags.ChunkSize), |
| 124 | + client.WithResponse(printer.Print), |
| 125 | + } |
| 126 | + |
| 127 | + // TODO: Support watch |
| 128 | + |
| 129 | + _, err := etcdclient.Get(ctx, flags.Prefix, |
| 130 | + opOpts..., |
| 131 | + ) |
| 132 | + if err != nil { |
| 133 | + return err |
| 134 | + } |
| 135 | + |
| 136 | + return nil |
| 137 | +} |
0 commit comments