|
| 1 | +package kubectl |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + |
| 8 | + "github.com/spf13/cobra" |
| 9 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 10 | + "k8s.io/cli-runtime/pkg/printers" |
| 11 | + "k8s.io/client-go/dynamic" |
| 12 | + "k8s.io/client-go/kubernetes/scheme" |
| 13 | + |
| 14 | + api "sigs.k8s.io/hierarchical-namespaces/api/v1alpha2" |
| 15 | +) |
| 16 | + |
| 17 | +var ( |
| 18 | + watch bool |
| 19 | + parent string |
| 20 | + allNamespaces bool |
| 21 | +) |
| 22 | + |
| 23 | +var getCmd = &cobra.Command{ |
| 24 | + Use: "get", |
| 25 | + Short: "list resources", |
| 26 | + RunE: func(c *cobra.Command, args []string) error { |
| 27 | + if len(args) == 0 { |
| 28 | + return fmt.Errorf("not enough arguments to 'get'") |
| 29 | + } |
| 30 | + if len(args) > 1 { |
| 31 | + return fmt.Errorf("too many arguments to 'get'") |
| 32 | + } |
| 33 | + d := newDoer(args[0]) |
| 34 | + rv, err := d.get(args[0]) |
| 35 | + if err != nil { |
| 36 | + return err |
| 37 | + } |
| 38 | + if watch { |
| 39 | + return d.watch(args[0], rv) |
| 40 | + } |
| 41 | + return nil |
| 42 | + }, |
| 43 | +} |
| 44 | + |
| 45 | +func newGetCmd() *cobra.Command { |
| 46 | + return getCmd |
| 47 | +} |
| 48 | + |
| 49 | +func init() { |
| 50 | + getCmd.Flags().BoolVarP(&watch, "watch", "w", false, "watch for changes") |
| 51 | + getCmd.PersistentFlags().StringVarP(&parent, "namespace", "n", "default", "parent namespace to scope request to") |
| 52 | + getCmd.PersistentFlags().BoolVarP(&allNamespaces, "all-namespaces", "A", false, "list across all namespaces (equivalent to kubectl get <resource> -A)") |
| 53 | +} |
| 54 | + |
| 55 | +type doer struct { |
| 56 | + client dynamic.ResourceInterface |
| 57 | + printer printers.ResourcePrinter |
| 58 | +} |
| 59 | + |
| 60 | +func newDoer(resource string) doer { |
| 61 | + client := getClient(resource) |
| 62 | + // See https://iximiuz.com/en/posts/kubernetes-api-go-cli/#pretty-print-kubernetes-object-as-yamljsontables for information on k8s printers |
| 63 | + printOpts := printers.PrintOptions{WithNamespace: true} |
| 64 | + return doer{ |
| 65 | + client: client, |
| 66 | + printer: printers.NewTypeSetter(scheme.Scheme).ToPrinter(printers.NewTablePrinter(printOpts)), |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func getClient(resource string) dynamic.ResourceInterface { |
| 71 | + gvr := api.ResourcesGroupVersion.WithResource(resource) |
| 72 | + gvr, _ = mapper.ResourceFor(gvr) |
| 73 | + resourceClient := dynamicClient.Resource(gvr) |
| 74 | + if allNamespaces { |
| 75 | + return resourceClient |
| 76 | + } |
| 77 | + return resourceClient.Namespace(parent) |
| 78 | +} |
| 79 | + |
| 80 | +func (d doer) get(resource string) (string, error) { |
| 81 | + unst, err := d.client.List(context.TODO(), metav1.ListOptions{}) |
| 82 | + if err != nil { |
| 83 | + return "", err |
| 84 | + } |
| 85 | + rv := unst.GetResourceVersion() |
| 86 | + if err = d.printer.PrintObj(unst, os.Stdout); err != nil { |
| 87 | + return rv, err |
| 88 | + } |
| 89 | + return rv, nil |
| 90 | +} |
| 91 | + |
| 92 | +func (d doer) watch(resource, rv string) error { |
| 93 | + if rv == "" { |
| 94 | + rv = "0" |
| 95 | + } |
| 96 | + opts := metav1.ListOptions{ |
| 97 | + ResourceVersion: rv, |
| 98 | + } |
| 99 | + watcher, err := d.client.Watch(context.TODO(), opts) |
| 100 | + if err != nil { |
| 101 | + return err |
| 102 | + } |
| 103 | + for event := range watcher.ResultChan() { |
| 104 | + if err = d.printer.PrintObj(event.Object, os.Stdout); err != nil { |
| 105 | + return err |
| 106 | + } |
| 107 | + } |
| 108 | + return nil |
| 109 | +} |
0 commit comments