|
| 1 | +package get |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "github.com/spf13/pflag" |
| 9 | + |
| 10 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 11 | + "k8s.io/apimachinery/pkg/api/meta" |
| 12 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 13 | + "k8s.io/cli-runtime/pkg/genericclioptions" |
| 14 | + "k8s.io/cli-runtime/pkg/genericiooptions" |
| 15 | + |
| 16 | + "github.com/opendatahub-io/odh-cli/pkg/cmd" |
| 17 | + "github.com/opendatahub-io/odh-cli/pkg/resources" |
| 18 | + "github.com/opendatahub-io/odh-cli/pkg/util/client" |
| 19 | + "github.com/opendatahub-io/odh-cli/pkg/util/iostreams" |
| 20 | +) |
| 21 | + |
| 22 | +var _ cmd.Command = (*Command)(nil) |
| 23 | + |
| 24 | +const ( |
| 25 | + outputFormatTable = "table" |
| 26 | + outputFormatJSON = "json" |
| 27 | + outputFormatYAML = "yaml" |
| 28 | + |
| 29 | + msgCRDNotInstalled = "resource type %q is not installed on this cluster\n" |
| 30 | +) |
| 31 | + |
| 32 | +// Command contains the get command configuration. |
| 33 | +type Command struct { |
| 34 | + IO iostreams.Interface |
| 35 | + ConfigFlags *genericclioptions.ConfigFlags |
| 36 | + Client client.Client |
| 37 | + |
| 38 | + // ResourceName is the positional arg identifying the resource type (e.g. "nb"). |
| 39 | + ResourceName string |
| 40 | + // ItemName is the optional positional arg for a specific resource name. |
| 41 | + ItemName string |
| 42 | + |
| 43 | + // Namespace is the resolved target namespace (populated during Complete). |
| 44 | + Namespace string |
| 45 | + // AllNamespaces lists resources across all namespaces when true. |
| 46 | + AllNamespaces bool |
| 47 | + // LabelSelector filters resources by label (e.g. "app=my-model"). |
| 48 | + LabelSelector string |
| 49 | + // OutputFormat controls output rendering: table, json, or yaml. |
| 50 | + OutputFormat string |
| 51 | + |
| 52 | + // ResolvedType is the ResourceType resolved from ResourceName during Complete. |
| 53 | + ResolvedType resources.ResourceType |
| 54 | +} |
| 55 | + |
| 56 | +// NewCommand creates a new get Command with defaults. |
| 57 | +func NewCommand( |
| 58 | + streams genericiooptions.IOStreams, |
| 59 | + configFlags *genericclioptions.ConfigFlags, |
| 60 | +) *Command { |
| 61 | + return &Command{ |
| 62 | + IO: iostreams.NewIOStreams(streams.In, streams.Out, streams.ErrOut), |
| 63 | + ConfigFlags: configFlags, |
| 64 | + OutputFormat: outputFormatTable, |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +// AddFlags registers command-specific flags. |
| 69 | +// Note: -n/--namespace is already registered by ConfigFlags on the root command. |
| 70 | +func (c *Command) AddFlags(fs *pflag.FlagSet) { |
| 71 | + fs.BoolVarP(&c.AllNamespaces, "all-namespaces", "A", false, "List resources across all namespaces") |
| 72 | + fs.StringVarP(&c.LabelSelector, "selector", "l", "", "Label selector to filter resources (e.g. app=my-model)") |
| 73 | + fs.StringVarP(&c.OutputFormat, "output", "o", outputFormatTable, "Output format: table, json, or yaml") |
| 74 | +} |
| 75 | + |
| 76 | +// Complete resolves derived fields after flag parsing. |
| 77 | +func (c *Command) Complete() error { |
| 78 | + rt, err := Resolve(c.ResourceName) |
| 79 | + if err != nil { |
| 80 | + return err |
| 81 | + } |
| 82 | + |
| 83 | + c.ResolvedType = rt |
| 84 | + |
| 85 | + if !c.AllNamespaces { |
| 86 | + ns, _, err := c.ConfigFlags.ToRawKubeConfigLoader().Namespace() |
| 87 | + if err != nil { |
| 88 | + return fmt.Errorf("determining namespace: %w", err) |
| 89 | + } |
| 90 | + |
| 91 | + c.Namespace = ns |
| 92 | + } |
| 93 | + |
| 94 | + k8sClient, err := client.NewClient(c.ConfigFlags) |
| 95 | + if err != nil { |
| 96 | + return fmt.Errorf("creating Kubernetes client: %w", err) |
| 97 | + } |
| 98 | + |
| 99 | + c.Client = k8sClient |
| 100 | + |
| 101 | + return nil |
| 102 | +} |
| 103 | + |
| 104 | +// Validate checks that all options are valid before execution. |
| 105 | +func (c *Command) Validate() error { |
| 106 | + if c.AllNamespaces && c.ConfigFlags.Namespace != nil && *c.ConfigFlags.Namespace != "" { |
| 107 | + return errors.New("--all-namespaces and --namespace are mutually exclusive") |
| 108 | + } |
| 109 | + |
| 110 | + switch c.OutputFormat { |
| 111 | + case outputFormatTable, outputFormatJSON, outputFormatYAML: |
| 112 | + default: |
| 113 | + return fmt.Errorf("invalid output format %q (must be one of: table, json, yaml)", c.OutputFormat) |
| 114 | + } |
| 115 | + |
| 116 | + return nil |
| 117 | +} |
| 118 | + |
| 119 | +// Run executes the get command. |
| 120 | +func (c *Command) Run(ctx context.Context) error { |
| 121 | + if c.ItemName != "" { |
| 122 | + return c.getResource(ctx) |
| 123 | + } |
| 124 | + |
| 125 | + return c.listResources(ctx) |
| 126 | +} |
| 127 | + |
| 128 | +// listResources lists resources of the resolved type. |
| 129 | +func (c *Command) listResources(ctx context.Context) error { |
| 130 | + opts := []client.ListResourcesOption{} |
| 131 | + if c.Namespace != "" { |
| 132 | + opts = append(opts, client.WithNamespace(c.Namespace)) |
| 133 | + } |
| 134 | + |
| 135 | + if c.LabelSelector != "" { |
| 136 | + opts = append(opts, client.WithLabelSelector(c.LabelSelector)) |
| 137 | + } |
| 138 | + |
| 139 | + items, err := c.Client.List(ctx, c.ResolvedType, opts...) |
| 140 | + if err != nil { |
| 141 | + if client.IsResourceTypeNotFound(err) { |
| 142 | + c.IO.Fprintf(msgCRDNotInstalled, c.ResolvedType.Kind) |
| 143 | + |
| 144 | + return nil |
| 145 | + } |
| 146 | + |
| 147 | + if !client.IsPermissionError(err) { |
| 148 | + return fmt.Errorf("listing %s: %w", c.ResolvedType.Kind, err) |
| 149 | + } |
| 150 | + |
| 151 | + c.IO.Errorf("Warning: insufficient permissions to list %s", c.ResolvedType.Kind) |
| 152 | + items = []*unstructured.Unstructured{} |
| 153 | + } |
| 154 | + |
| 155 | + return c.renderOutput(items) |
| 156 | +} |
| 157 | + |
| 158 | +// getResource retrieves a single named resource. |
| 159 | +func (c *Command) getResource(ctx context.Context) error { |
| 160 | + opts := []client.GetOption{} |
| 161 | + if c.Namespace != "" { |
| 162 | + opts = append(opts, client.InNamespace(c.Namespace)) |
| 163 | + } |
| 164 | + |
| 165 | + item, err := c.Client.GetResource(ctx, c.ResolvedType, c.ItemName, opts...) |
| 166 | + if err != nil { |
| 167 | + if meta.IsNoMatchError(err) { |
| 168 | + c.IO.Fprintf(msgCRDNotInstalled, c.ResolvedType.Kind) |
| 169 | + |
| 170 | + return nil |
| 171 | + } |
| 172 | + |
| 173 | + if apierrors.IsNotFound(err) { |
| 174 | + return fmt.Errorf("%s %q not found in namespace %q", c.ResolvedType.Kind, c.ItemName, c.Namespace) |
| 175 | + } |
| 176 | + |
| 177 | + return fmt.Errorf("getting %s %q: %w", c.ResolvedType.Kind, c.ItemName, err) |
| 178 | + } |
| 179 | + |
| 180 | + if item == nil { |
| 181 | + c.IO.Errorf("Warning: insufficient permissions to get %s %q", c.ResolvedType.Kind, c.ItemName) |
| 182 | + |
| 183 | + return nil |
| 184 | + } |
| 185 | + |
| 186 | + return c.renderOutput([]*unstructured.Unstructured{item}) |
| 187 | +} |
| 188 | + |
| 189 | +// renderOutput dispatches to the appropriate output formatter. |
| 190 | +func (c *Command) renderOutput(items []*unstructured.Unstructured) error { |
| 191 | + switch c.OutputFormat { |
| 192 | + case outputFormatJSON: |
| 193 | + return outputJSON(c.IO.Out(), items) |
| 194 | + case outputFormatYAML: |
| 195 | + return outputYAML(c.IO.Out(), items) |
| 196 | + default: |
| 197 | + return outputTable(c.IO.Out(), items, c.ResolvedType, c.AllNamespaces) |
| 198 | + } |
| 199 | +} |
0 commit comments