This repository has been archived by the owner on May 7, 2024. It is now read-only.
/
show_cluster.go
286 lines (232 loc) · 8.52 KB
/
show_cluster.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package commands
import (
"fmt"
"net/http"
"os"
"strings"
"github.com/fatih/color"
"github.com/giantswarm/columnize"
"github.com/giantswarm/gsclientgen/models"
"github.com/giantswarm/microerror"
"github.com/spf13/cobra"
"github.com/giantswarm/gsctl/client/clienterror"
"github.com/giantswarm/gsctl/config"
"github.com/giantswarm/gsctl/util"
)
var (
// ShowClusterCommand performs the "show cluster" function
ShowClusterCommand = &cobra.Command{
Use: "cluster",
Short: "Show cluster details",
Long: `Display details of a cluster
Examples:
gsctl show cluster c7t2o
`,
// PreRun checks a few general things, like authentication.
PreRun: showClusterPreRunOutput,
// Run calls the business function and prints results and errors.
Run: showClusterRunOutput,
}
)
const (
showClusterActivityName = "show-cluster"
)
type showClusterArguments struct {
apiEndpoint string
authToken string
scheme string
clusterID string
verbose bool
}
func defaultShowClusterArguments() showClusterArguments {
endpoint := config.Config.ChooseEndpoint(cmdAPIEndpoint)
token := config.Config.ChooseToken(endpoint, cmdToken)
scheme := config.Config.ChooseScheme(endpoint, cmdToken)
return showClusterArguments{
apiEndpoint: endpoint,
authToken: token,
scheme: scheme,
clusterID: "",
verbose: cmdVerbose,
}
}
func init() {
ShowCommand.AddCommand(ShowClusterCommand)
}
func showClusterPreRunOutput(cmd *cobra.Command, cmdLineArgs []string) {
args := defaultShowClusterArguments()
err := verifyShowClusterPreconditions(args, cmdLineArgs)
if err == nil {
return
}
handleCommonErrors(err)
// handle non-common errors
fmt.Println(color.RedString(err.Error()))
os.Exit(1)
}
func verifyShowClusterPreconditions(args showClusterArguments, cmdLineArgs []string) error {
if config.Config.Token == "" && args.authToken == "" {
return microerror.Mask(notLoggedInError)
}
if len(cmdLineArgs) == 0 {
return microerror.Mask(clusterIDMissingError)
}
return nil
}
// getClusterDetails returns details for one cluster.
func getClusterDetails(clusterID, activityName string) (*models.V4ClusterDetailsResponse, error) {
// perform API call
auxParams := ClientV2.DefaultAuxiliaryParams()
auxParams.ActivityName = activityName
response, err := ClientV2.GetCluster(clusterID, auxParams)
if err != nil {
if clientErr, ok := err.(*clienterror.APIError); ok {
switch clientErr.HTTPStatusCode {
case http.StatusForbidden:
return nil, microerror.Mask(accessForbiddenError)
case http.StatusUnauthorized:
return nil, microerror.Mask(notAuthorizedError)
case http.StatusNotFound:
return nil, microerror.Mask(clusterNotFoundError)
case http.StatusInternalServerError:
return nil, microerror.Mask(internalServerError)
}
}
return nil, microerror.Mask(err)
}
return response.Payload, nil
}
func getOrgCredentials(orgName, credentialID, activityName string) (*models.V4GetCredentialResponse, error) {
auxParams := ClientV2.DefaultAuxiliaryParams()
auxParams.ActivityName = activityName
response, err := ClientV2.GetCredential(orgName, credentialID, auxParams)
if err != nil {
if clientErr, ok := err.(*clienterror.APIError); ok {
switch clientErr.HTTPStatusCode {
case http.StatusForbidden:
return nil, microerror.Mask(accessForbiddenError)
case http.StatusUnauthorized:
return nil, microerror.Mask(notAuthorizedError)
case http.StatusNotFound:
return nil, microerror.Mask(credentialNotFoundError)
case http.StatusInternalServerError:
return nil, microerror.Mask(internalServerError)
}
}
return nil, microerror.Mask(err)
}
return response.Payload, nil
}
// sumWorkerCPUs adds up the worker's CPU cores
func sumWorkerCPUs(workerDetails []*models.V4ClusterDetailsResponseWorkersItems) uint {
sum := uint(0)
for _, item := range workerDetails {
sum = sum + uint(item.CPU.Cores)
}
return sum
}
// sumWorkerStorage adds up the worker's storage
func sumWorkerStorage(workerDetails []*models.V4ClusterDetailsResponseWorkersItems) float64 {
sum := float64(0.0)
for _, item := range workerDetails {
sum = sum + item.Storage.SizeGb
}
return sum
}
// sumWorkerMemory adds up the worker's memory
func sumWorkerMemory(workerDetails []*models.V4ClusterDetailsResponseWorkersItems) float64 {
sum := float64(0.0)
for _, item := range workerDetails {
sum = sum + item.Memory.SizeGb
}
return sum
}
func showClusterRunOutput(cmd *cobra.Command, cmdLineArgs []string) {
args := defaultShowClusterArguments()
args.clusterID = cmdLineArgs[0]
if args.verbose {
fmt.Println(color.WhiteString("Fetching details for cluster %s", args.clusterID))
}
clusterDetails, err := getClusterDetails(args.clusterID, showClusterActivityName)
var credentialDetails *models.V4GetCredentialResponse
if err == nil && clusterDetails.CredentialID != "" {
if args.verbose {
fmt.Println(color.WhiteString("Fetching details for credential %s", clusterDetails.CredentialID))
}
credentialDetails, err = getOrgCredentials(clusterDetails.Owner, clusterDetails.CredentialID, showClusterActivityName)
}
if err != nil {
handleCommonErrors(err)
var headline = ""
var subtext = ""
switch {
case IsClusterNotFoundError(err):
headline = "Cluster not found"
subtext = "The cluster with this ID could not be found. Please use 'gsctl list clusters' to list all available clusters."
case IsCredentialNotFoundError(err):
headline = "Credential not found"
subtext = "Credentials with the given ID could not be found."
case err.Error() == "":
return
default:
headline = err.Error()
}
// Print error output
fmt.Println(color.RedString(headline))
if subtext != "" {
fmt.Println(subtext)
}
os.Exit(1)
}
// print table
output := []string{}
created := util.ParseDate(clusterDetails.CreateDate)
output = append(output, color.YellowString("ID:")+"|"+clusterDetails.ID)
if clusterDetails.Name != "" {
output = append(output, color.YellowString("Name:")+"|"+clusterDetails.Name)
} else {
output = append(output, color.YellowString("Name:")+"|n/a")
}
output = append(output, color.YellowString("Created:")+"|"+util.ShortDate(created))
output = append(output, color.YellowString("Organization:")+"|"+clusterDetails.Owner)
if credentialDetails != nil && credentialDetails.ID != "" {
if credentialDetails.Aws != nil {
parts := strings.Split(credentialDetails.Aws.Roles.Awsoperator, ":")
if len(parts) > 3 {
output = append(output, color.YellowString("AWS account:")+"|"+parts[4])
} else {
output = append(output, color.YellowString("AWS account:")+"|n/a")
}
} else if credentialDetails.Azure != nil {
output = append(output, color.YellowString("Azure subscription:")+"|"+credentialDetails.Azure.Credential.SubscriptionID)
output = append(output, color.YellowString("Azure tenant:")+"|"+credentialDetails.Azure.Credential.TenantID)
}
}
output = append(output, color.YellowString("Kubernetes API endpoint:")+"|"+clusterDetails.APIEndpoint)
if clusterDetails.ReleaseVersion != "" {
output = append(output, color.YellowString("Release version:")+"|"+clusterDetails.ReleaseVersion)
} else {
output = append(output, color.YellowString("Release version:")+"|n/a")
}
output = append(output, color.YellowString("Workers:")+"|"+fmt.Sprintf("%d", len(clusterDetails.Workers)))
// This assumes all nodes use the same instance type.
if len(clusterDetails.Workers) > 0 {
if clusterDetails.Workers[0].Aws != nil && clusterDetails.Workers[0].Aws.InstanceType != "" {
output = append(output, color.YellowString("Worker instance type:")+"|"+clusterDetails.Workers[0].Aws.InstanceType)
}
if clusterDetails.Workers[0].Azure != nil && clusterDetails.Workers[0].Azure.VMSize != "" {
output = append(output, color.YellowString("Worker VM size:")+"|"+clusterDetails.Workers[0].Azure.VMSize)
}
}
output = append(output, color.YellowString("CPU cores in workers:")+"|"+fmt.Sprintf("%d", sumWorkerCPUs(clusterDetails.Workers)))
output = append(output, color.YellowString("RAM in worker nodes (GB):")+"|"+fmt.Sprintf("%.2f", sumWorkerMemory(clusterDetails.Workers)))
if clusterDetails.Kvm != nil {
output = append(output, color.YellowString("Storage in worker nodes (GB):")+"|"+fmt.Sprintf("%.2f", sumWorkerStorage(clusterDetails.Workers)))
}
if clusterDetails.Kvm != nil && len(clusterDetails.Kvm.PortMappings) > 0 {
for _, portMapping := range clusterDetails.Kvm.PortMappings {
output = append(output, color.YellowString(fmt.Sprintf("Ingress port for %s:", portMapping.Protocol))+"|"+fmt.Sprintf("%d", portMapping.Port))
}
}
fmt.Println(columnize.SimpleFormat(output))
}