|
| 1 | +package nodegroup |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/UpCloudLtd/upcloud-cli/v2/internal/commands" |
| 5 | + "github.com/UpCloudLtd/upcloud-cli/v2/internal/completion" |
| 6 | + "github.com/UpCloudLtd/upcloud-cli/v2/internal/format" |
| 7 | + "github.com/UpCloudLtd/upcloud-cli/v2/internal/labels" |
| 8 | + "github.com/UpCloudLtd/upcloud-cli/v2/internal/output" |
| 9 | + "github.com/UpCloudLtd/upcloud-cli/v2/internal/resolver" |
| 10 | + "github.com/UpCloudLtd/upcloud-cli/v2/internal/ui" |
| 11 | + "github.com/spf13/pflag" |
| 12 | + |
| 13 | + "github.com/UpCloudLtd/upcloud-go-api/v6/upcloud/request" |
| 14 | +) |
| 15 | + |
| 16 | +// ShowCommand creates the "kubernetes nodegroup show" command |
| 17 | +func ShowCommand() commands.Command { |
| 18 | + return &showCommand{ |
| 19 | + BaseCommand: commands.New( |
| 20 | + "show", |
| 21 | + "Show node group details", |
| 22 | + "upctl kubernetes nodegroup show 55199a44-4751-4e27-9394-7c7661910be3 --name default", |
| 23 | + ), |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +type showCommand struct { |
| 28 | + *commands.BaseCommand |
| 29 | + name string |
| 30 | + resolver.CachingKubernetes |
| 31 | + completion.Kubernetes |
| 32 | +} |
| 33 | + |
| 34 | +// InitCommand implements Command.InitCommand |
| 35 | +func (s *showCommand) InitCommand() { |
| 36 | + flagSet := &pflag.FlagSet{} |
| 37 | + flagSet.StringVar(&s.name, "name", "", "Node group name") |
| 38 | + s.AddFlags(flagSet) |
| 39 | + |
| 40 | + _ = s.Cobra().MarkFlagRequired("name") |
| 41 | +} |
| 42 | + |
| 43 | +// ExecuteSingleArgument implements commands.SingleArgumentCommand |
| 44 | +func (s *showCommand) ExecuteSingleArgument(exec commands.Executor, uuid string) (output.Output, error) { |
| 45 | + svc := exec.All() |
| 46 | + nodeGroup, err := svc.GetKubernetesNodeGroup(exec.Context(), &request.GetKubernetesNodeGroupRequest{ClusterUUID: uuid, Name: s.name}) |
| 47 | + if err != nil { |
| 48 | + return nil, err |
| 49 | + } |
| 50 | + |
| 51 | + taintColumns := []output.TableColumn{ |
| 52 | + {Key: "key", Header: "Key"}, |
| 53 | + {Key: "value", Header: "Value"}, |
| 54 | + {Key: "effect", Header: "Effect"}, |
| 55 | + } |
| 56 | + |
| 57 | + taintRows := []output.TableRow{} |
| 58 | + for _, taint := range nodeGroup.Taints { |
| 59 | + taintRows = append(taintRows, output.TableRow{ |
| 60 | + taint.Key, |
| 61 | + taint.Value, |
| 62 | + taint.Effect, |
| 63 | + }) |
| 64 | + } |
| 65 | + |
| 66 | + kubeletArgColumns := taintColumns[0:2] |
| 67 | + |
| 68 | + kubeletArgRows := []output.TableRow{} |
| 69 | + for _, kubeletArg := range nodeGroup.KubeletArgs { |
| 70 | + kubeletArgRows = append(kubeletArgRows, output.TableRow{ |
| 71 | + kubeletArg.Key, |
| 72 | + kubeletArg.Value, |
| 73 | + }) |
| 74 | + } |
| 75 | + |
| 76 | + nodeRows := []output.TableRow{} |
| 77 | + for _, node := range nodeGroup.Nodes { |
| 78 | + nodeRows = append(nodeRows, output.TableRow{ |
| 79 | + node.UUID, |
| 80 | + node.Name, |
| 81 | + node.State, |
| 82 | + }) |
| 83 | + } |
| 84 | + |
| 85 | + nodeColumns := []output.TableColumn{ |
| 86 | + {Key: "uuid", Header: "UUID", Colour: ui.DefaultUUUIDColours}, |
| 87 | + {Key: "name", Header: "Name"}, |
| 88 | + {Key: "state", Header: "State", Format: format.KubernetesNodeState}, |
| 89 | + } |
| 90 | + |
| 91 | + var storageName string |
| 92 | + if storage, err := svc.GetStorageDetails(exec.Context(), &request.GetStorageDetailsRequest{UUID: nodeGroup.Storage}); err != nil { |
| 93 | + storageName = "" |
| 94 | + } else { |
| 95 | + storageName = storage.Title |
| 96 | + } |
| 97 | + |
| 98 | + // For JSON and YAML output, passthrough API response |
| 99 | + return output.MarshaledWithHumanOutput{ |
| 100 | + Value: nodeGroup, |
| 101 | + Output: output.Combined{ |
| 102 | + output.CombinedSection{ |
| 103 | + Contents: output.Details{ |
| 104 | + Sections: []output.DetailSection{ |
| 105 | + { |
| 106 | + Title: "Overview", |
| 107 | + Rows: []output.DetailRow{ |
| 108 | + {Title: "Name:", Value: nodeGroup.Name}, |
| 109 | + {Title: "Count:", Value: nodeGroup.Count}, |
| 110 | + {Title: "Plan:", Value: nodeGroup.Plan}, |
| 111 | + {Title: "State:", Value: nodeGroup.State, Format: format.KubernetesNodeGroupState}, |
| 112 | + {Title: "Storage UUID:", Value: nodeGroup.Storage, Colour: ui.DefaultUUUIDColours}, |
| 113 | + {Title: "Storage name:", Value: storageName, Format: format.PossiblyUnknownString}, |
| 114 | + {Title: "Anti-affinity:", Value: nodeGroup.AntiAffinity, Format: format.Boolean}, |
| 115 | + {Title: "Utility network access:", Value: nodeGroup.UtilityNetworkAccess, Format: format.Boolean}, |
| 116 | + }, |
| 117 | + }, |
| 118 | + }, |
| 119 | + }, |
| 120 | + }, |
| 121 | + labels.GetLabelsSectionWithResourceType(nodeGroup.Labels, "node group"), |
| 122 | + output.CombinedSection{ |
| 123 | + Key: "taints", |
| 124 | + Title: "Taints:", |
| 125 | + Contents: output.Table{ |
| 126 | + Columns: taintColumns, |
| 127 | + Rows: taintRows, |
| 128 | + EmptyMessage: "No taints defined for this node group.", |
| 129 | + }, |
| 130 | + }, |
| 131 | + output.CombinedSection{ |
| 132 | + Key: "kubelet_args", |
| 133 | + Title: "Kubelet arguments:", |
| 134 | + Contents: output.Table{ |
| 135 | + Columns: kubeletArgColumns, |
| 136 | + Rows: kubeletArgRows, |
| 137 | + EmptyMessage: "No kubelet arguments defined for this node group.", |
| 138 | + }, |
| 139 | + }, |
| 140 | + output.CombinedSection{ |
| 141 | + Key: "nodes", |
| 142 | + Title: "Nodes:", |
| 143 | + Contents: output.Table{ |
| 144 | + Columns: nodeColumns, |
| 145 | + Rows: nodeRows, |
| 146 | + EmptyMessage: "No nodes found for this node group.", |
| 147 | + }, |
| 148 | + }, |
| 149 | + }, |
| 150 | + }, nil |
| 151 | +} |
0 commit comments