-
Notifications
You must be signed in to change notification settings - Fork 20
/
sks_show.go
168 lines (147 loc) · 4.66 KB
/
sks_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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package cmd
import (
"errors"
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
"github.com/exoscale/cli/pkg/account"
"github.com/exoscale/cli/pkg/globalstate"
"github.com/exoscale/cli/pkg/output"
"github.com/exoscale/cli/table"
"github.com/exoscale/cli/utils"
exoapi "github.com/exoscale/egoscale/v2/api"
)
type sksShowOutput struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
CreationDate string `json:"creation_date"`
Zone string `json:"zone"`
Endpoint string `json:"endpoint"`
Version string `json:"version"`
ServiceLevel string `json:"service_level"`
CNI string `json:"cni"`
AddOns []string `json:"addons"`
State string `json:"state"`
Labels map[string]string `json:"labels"`
Nodepools []sksNodepoolShowOutput `json:"nodepools"`
}
func (o *sksShowOutput) ToJSON() { output.JSON(o) }
func (o *sksShowOutput) ToText() { output.Text(o) }
func (o *sksShowOutput) ToTable() {
t := table.NewTable(os.Stdout)
t.SetHeader([]string{"SKS Cluster"})
defer t.Render()
t.Append([]string{"ID", o.ID})
t.Append([]string{"Name", o.Name})
t.Append([]string{"Description", o.Description})
t.Append([]string{"Zone", o.Zone})
t.Append([]string{"Creation Date", o.CreationDate})
t.Append([]string{"Endpoint", o.Endpoint})
t.Append([]string{"Version", o.Version})
t.Append([]string{"Service Level", o.ServiceLevel})
t.Append([]string{"CNI", o.CNI})
t.Append([]string{"Add-Ons", strings.Join(o.AddOns, "\n")})
t.Append([]string{"State", o.State})
t.Append([]string{"Labels", func() string {
if len(o.Labels) > 0 {
return strings.Join(
func() []string {
labels := make([]string, 0)
for k, v := range o.Labels {
labels = append(labels, fmt.Sprintf("%s:%s", k, v))
}
return labels
}(),
"\n")
}
return "n/a"
}()})
t.Append([]string{"Nodepools", func() string {
if len(o.Nodepools) > 0 {
return strings.Join(
func() []string {
services := make([]string, len(o.Nodepools))
for i := range o.Nodepools {
services[i] = fmt.Sprintf("%s | %s",
o.Nodepools[i].ID,
o.Nodepools[i].Name)
}
return services
}(),
"\n")
}
return "n/a"
}()})
}
type sksShowCmd struct {
cliCommandSettings `cli-cmd:"-"`
_ bool `cli-cmd:"show"`
Cluster string `cli-arg:"#" cli-usage:"NAME|ID"`
Zone string `cli-short:"z" cli-usage:"SKS cluster zone"`
}
func (c *sksShowCmd) cmdAliases() []string { return gShowAlias }
func (c *sksShowCmd) cmdShort() string { return "Show an SKS cluster details" }
func (c *sksShowCmd) cmdLong() string {
return fmt.Sprintf(`This command shows an SKS cluster details.
Supported output template annotations: %s`,
strings.Join(output.TemplateAnnotations(&sksShowOutput{}), ", "))
}
func (c *sksShowCmd) cmdPreRun(cmd *cobra.Command, args []string) error {
cmdSetZoneFlagFromDefault(cmd)
return cliCommandDefaultPreRun(c, cmd, args)
}
func (c *sksShowCmd) cmdRun(_ *cobra.Command, _ []string) error {
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(account.CurrentAccount.Environment, c.Zone))
cluster, err := globalstate.EgoscaleClient.FindSKSCluster(ctx, c.Zone, c.Cluster)
if err != nil {
if errors.Is(err, exoapi.ErrNotFound) {
return fmt.Errorf("resource not found in zone %q", c.Zone)
}
return err
}
sksNodepools := make([]sksNodepoolShowOutput, 0)
for _, np := range cluster.Nodepools {
sksNodepools = append(sksNodepools, sksNodepoolShowOutput{
ID: *np.ID,
Name: *np.Name,
})
}
return c.outputFunc(
&sksShowOutput{
AddOns: func() (v []string) {
if cluster.AddOns != nil {
v = *cluster.AddOns
}
return
}(),
CNI: utils.DefaultString(cluster.CNI, "-"),
CreationDate: cluster.CreatedAt.String(),
Description: utils.DefaultString(cluster.Description, ""),
Endpoint: *cluster.Endpoint,
ID: *cluster.ID,
Labels: func() (v map[string]string) {
if cluster.Labels != nil {
v = *cluster.Labels
}
return
}(),
Name: *cluster.Name,
Nodepools: sksNodepools,
ServiceLevel: *cluster.ServiceLevel,
State: *cluster.State,
Version: *cluster.Version,
Zone: c.Zone,
},
nil)
}
func init() {
cobra.CheckErr(registerCLICommand(sksCmd, &sksShowCmd{
cliCommandSettings: defaultCLICmdSettings(),
}))
// FIXME: remove this someday.
cobra.CheckErr(registerCLICommand(deprecatedSKSCmd, &sksShowCmd{
cliCommandSettings: defaultCLICmdSettings(),
}))
}