-
Notifications
You must be signed in to change notification settings - Fork 6
/
show.go
137 lines (121 loc) · 4.15 KB
/
show.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package kubernetes
import (
"fmt"
"strings"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/completion"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/format"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/labels"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/output"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/resolver"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/ui"
"github.com/jedib0t/go-pretty/v6/text"
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request"
)
// ShowCommand creates the "kubernetes show" command
func ShowCommand() commands.Command {
return &showCommand{
BaseCommand: commands.New(
"show",
"Show Kubernetes cluster details",
"upctl kubernetes show 55199a44-4751-4e27-9394-7c7661910be3",
"upctl kubernetes show my-cluster",
),
}
}
type showCommand struct {
*commands.BaseCommand
resolver.CachingKubernetes
completion.Kubernetes
}
// Execute implements commands.MultipleArgumentCommand
func (s *showCommand) Execute(exec commands.Executor, uuid string) (output.Output, error) {
svc := exec.All()
cluster, err := svc.GetKubernetesCluster(exec.Context(), &request.GetKubernetesClusterRequest{UUID: uuid})
if err != nil {
return nil, err
}
var networkName string
if network, err := svc.GetNetworkDetails(exec.Context(), &request.GetNetworkDetailsRequest{UUID: cluster.Network}); err != nil {
networkName = ""
} else {
networkName = network.Name
}
nodeGroupRows := []output.TableRow{}
for _, nodeGroup := range cluster.NodeGroups {
nodeGroupRows = append(nodeGroupRows, output.TableRow{
nodeGroup.Name,
nodeGroup.Count,
nodeGroup.Plan,
nodeGroup.AntiAffinity,
nodeGroup.UtilityNetworkAccess,
nodeGroup.State,
})
}
nodeGroupColumns := []output.TableColumn{
{Key: "name", Header: "Name"},
{Key: "count", Header: "Count"},
{Key: "plan", Header: "Plan"},
{Key: "anti_affinity", Header: "Anti affinity", Format: format.Boolean},
{Key: "utility_network_access", Header: "Utility network access", Format: format.Boolean},
{Key: "state", Header: "State", Format: format.KubernetesNodeGroupState},
}
// For JSON and YAML output, passthrough API response
return output.MarshaledWithHumanOutput{
Value: cluster,
Output: output.Combined{
output.CombinedSection{
Contents: output.Details{
Sections: []output.DetailSection{
{
Title: "Overview:",
Rows: []output.DetailRow{
{Title: "UUID:", Value: cluster.UUID, Colour: ui.DefaultUUUIDColours},
{Title: "Name:", Value: cluster.Name},
{Title: "Version:", Value: cluster.Version},
{Title: "Network UUID:", Value: cluster.Network, Colour: ui.DefaultUUUIDColours},
{Title: "Network name:", Value: networkName, Format: format.PossiblyUnknownString},
{Title: "Network CIDR:", Value: cluster.NetworkCIDR, Colour: ui.DefaultAddressColours},
{Title: "Kubernetes API allowed IPs:", Value: cluster.ControlPlaneIPFilter, Format: formatIPFilter},
{Title: "Private node groups:", Value: cluster.PrivateNodeGroups, Format: format.Boolean},
{Title: "Zone:", Value: cluster.Zone},
{Title: "Operational state:", Value: cluster.State, Format: format.KubernetesClusterState},
},
},
},
},
},
labels.GetLabelsSectionWithResourceType(cluster.Labels, "cluster"),
output.CombinedSection{
Key: "node_groups",
Title: "Node groups:",
Contents: output.Table{
Columns: nodeGroupColumns,
Rows: nodeGroupRows,
EmptyMessage: "No node groups found for this cluster.",
},
},
},
}, nil
}
func formatIPFilter(val interface{}) (text.Colors, string, error) {
addresses, ok := val.([]string)
if !ok {
return nil, "", fmt.Errorf("cannot parse IP addresses from %T, expected []string", val)
}
allowAll := false
var strs []string
for _, ipa := range addresses {
if ipa == "0.0.0.0/0" {
allowAll = true
}
strs = append(strs, ui.DefaultAddressColours.Sprint(ipa))
}
if addresses == nil || allowAll {
return nil, "all", nil
}
if addresses == nil || allowAll {
return nil, text.FgHiBlack.Sprint("none"), nil
}
return nil, strings.Join(strs, ",\n"), nil
}