/
k8s_node.go
449 lines (380 loc) · 20 KB
/
k8s_node.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
package commands
import (
"context"
"errors"
"fmt"
"github.com/ionos-cloud/ionosctl/v6/commands/cloudapi-v6/completer"
"github.com/ionos-cloud/ionosctl/v6/commands/cloudapi-v6/query"
"github.com/ionos-cloud/ionosctl/v6/commands/cloudapi-v6/waiter"
"github.com/ionos-cloud/ionosctl/v6/internal/constants"
"github.com/ionos-cloud/ionosctl/v6/internal/core"
"github.com/ionos-cloud/ionosctl/v6/internal/printer/json2table/jsonpaths"
"github.com/ionos-cloud/ionosctl/v6/internal/printer/jsontabwriter"
"github.com/ionos-cloud/ionosctl/v6/internal/printer/tabheaders"
"github.com/ionos-cloud/ionosctl/v6/internal/request"
"github.com/ionos-cloud/ionosctl/v6/internal/waitfor"
"github.com/ionos-cloud/ionosctl/v6/pkg/confirm"
cloudapiv6 "github.com/ionos-cloud/ionosctl/v6/services/cloudapi-v6"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
defaultK8sNodeCols = []string{"NodeId", "Name", "K8sVersion", "PublicIP", "PrivateIP", "State"}
)
func K8sNodeCmd() *core.Command {
ctx := context.TODO()
k8sCmd := &core.Command{
Command: &cobra.Command{
Use: "node",
Aliases: []string{"n"},
Short: "Kubernetes Node Operations",
Long: "The sub-commands of `ionosctl k8s node` allow you to list, get, recreate, delete Kubernetes Nodes.",
TraverseChildren: true,
},
}
globalFlags := k8sCmd.GlobalFlags()
globalFlags.StringSliceP(constants.ArgCols, "", defaultK8sNodeCols, tabheaders.ColsMessage(defaultK8sNodeCols))
_ = viper.BindPFlag(core.GetFlagName(k8sCmd.Name(), constants.ArgCols), globalFlags.Lookup(constants.ArgCols))
_ = k8sCmd.Command.RegisterFlagCompletionFunc(constants.ArgCols, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return defaultK8sNodeCols, cobra.ShellCompDirectiveNoFileComp
})
/*
List Command
*/
list := core.NewCommand(ctx, k8sCmd, core.CommandBuilder{
Namespace: "k8s",
Resource: "node",
Verb: "list",
Aliases: []string{"l", "ls"},
ShortDesc: "List Kubernetes Nodes",
LongDesc: "Use this command to get a list of existing Kubernetes Nodes.\n\nYou can filter the results using `--filters` option. Use the following format to set filters: `--filters KEY1=VALUE1,KEY2=VALUE2`.\n" + completer.K8sNodesFiltersUsage() + "\n\nRequired values to run command:\n\n* K8s Cluster Id\n* K8s NodePool Id",
Example: listK8sNodesExample,
PreCmdRun: PreRunK8sNodesList,
CmdRun: RunK8sNodeList,
InitClient: true,
})
list.AddUUIDFlag(constants.FlagClusterId, "", "", cloudapiv6.K8sClusterId, core.RequiredFlagOption())
_ = list.Command.RegisterFlagCompletionFunc(constants.FlagClusterId, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return completer.K8sClustersIds(), cobra.ShellCompDirectiveNoFileComp
})
list.AddUUIDFlag(constants.FlagNodepoolId, "", "", cloudapiv6.K8sNodePoolId, core.RequiredFlagOption())
_ = list.Command.RegisterFlagCompletionFunc(constants.FlagNodepoolId, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return completer.K8sNodePoolsIds(viper.GetString(core.GetFlagName(list.NS, constants.FlagClusterId))), cobra.ShellCompDirectiveNoFileComp
})
list.AddInt32Flag(constants.FlagMaxResults, constants.FlagMaxResultsShort, cloudapiv6.DefaultMaxResults, constants.DescMaxResults)
list.AddInt32Flag(cloudapiv6.ArgDepth, cloudapiv6.ArgDepthShort, cloudapiv6.DefaultListDepth, cloudapiv6.ArgDepthDescription)
list.AddStringFlag(cloudapiv6.ArgOrderBy, "", "", cloudapiv6.ArgOrderByDescription)
_ = list.Command.RegisterFlagCompletionFunc(cloudapiv6.ArgOrderBy, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return completer.K8sNodesFilters(), cobra.ShellCompDirectiveNoFileComp
})
list.AddStringSliceFlag(cloudapiv6.ArgFilters, cloudapiv6.ArgFiltersShort, []string{""}, cloudapiv6.ArgFiltersDescription)
_ = list.Command.RegisterFlagCompletionFunc(cloudapiv6.ArgFilters, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return completer.K8sNodesFilters(), cobra.ShellCompDirectiveNoFileComp
})
/*
Get Command
*/
get := core.NewCommand(ctx, k8sCmd, core.CommandBuilder{
Namespace: "k8s",
Resource: "node",
Verb: "get",
Aliases: []string{"g"},
ShortDesc: "Get a Kubernetes Node",
LongDesc: "Use this command to retrieve details about a specific Kubernetes Node.You can wait for the Node to be in \"ACTIVE\" state using `--wait-for-state` flag together with `--timeout` option.\n\nRequired values to run command:\n\n* K8s Cluster Id\n* K8s NodePool Id\n* K8s Node Id",
Example: getK8sNodeExample,
PreCmdRun: PreRunK8sClusterNodesIds,
CmdRun: RunK8sNodeGet,
InitClient: true,
})
get.AddUUIDFlag(constants.FlagClusterId, "", "", cloudapiv6.K8sClusterId, core.RequiredFlagOption())
_ = get.Command.RegisterFlagCompletionFunc(constants.FlagClusterId, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return completer.K8sClustersIds(), cobra.ShellCompDirectiveNoFileComp
})
get.AddUUIDFlag(constants.FlagNodepoolId, "", "", cloudapiv6.K8sNodePoolId, core.RequiredFlagOption())
_ = get.Command.RegisterFlagCompletionFunc(constants.FlagNodepoolId, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return completer.K8sNodePoolsIds(viper.GetString(core.GetFlagName(get.NS, constants.FlagClusterId))), cobra.ShellCompDirectiveNoFileComp
})
get.AddUUIDFlag(cloudapiv6.ArgK8sNodeId, cloudapiv6.ArgIdShort, "", cloudapiv6.K8sNodeId, core.RequiredFlagOption())
_ = get.Command.RegisterFlagCompletionFunc(cloudapiv6.ArgK8sNodeId, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return completer.K8sNodesIds(viper.GetString(core.GetFlagName(get.NS, constants.FlagClusterId)),
viper.GetString(core.GetFlagName(get.NS, constants.FlagNodepoolId)),
), cobra.ShellCompDirectiveNoFileComp
})
get.AddBoolFlag(constants.ArgWaitForState, constants.ArgWaitForStateShort, constants.DefaultWait, "Wait for specified Node to be in ACTIVE state")
get.AddIntFlag(constants.ArgTimeout, constants.ArgTimeoutShort, cloudapiv6.K8sTimeoutSeconds, "Timeout option for waiting for Node to be in ACTIVE state [seconds]")
get.AddInt32Flag(cloudapiv6.ArgDepth, cloudapiv6.ArgDepthShort, cloudapiv6.DefaultGetDepth, cloudapiv6.ArgDepthDescription)
/*
Recreate Command
*/
recreate := core.NewCommand(ctx, k8sCmd, core.CommandBuilder{
Namespace: "k8s",
Resource: "node",
Verb: "recreate",
Aliases: []string{"r"},
ShortDesc: "Recreate a Kubernetes Node",
LongDesc: `You can recreate a single Kubernetes Node.
Managed Kubernetes starts a process which based on the NodePool's template creates & configures a new Node, waits for status "ACTIVE", and migrates all the Pods from the faulty Node, deleting it once empty. While this operation occurs, the NodePool will have an extra billable "ACTIVE" Node.
Required values to run command:
* K8s Cluster Id
* K8s NodePool Id
* K8s Node Id`,
Example: recreateK8sNodeExample,
PreCmdRun: PreRunK8sClusterNodesIds,
CmdRun: RunK8sNodeRecreate,
InitClient: true,
})
recreate.AddUUIDFlag(constants.FlagClusterId, "", "", cloudapiv6.K8sClusterId, core.RequiredFlagOption())
_ = recreate.Command.RegisterFlagCompletionFunc(constants.FlagClusterId, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return completer.K8sClustersIds(), cobra.ShellCompDirectiveNoFileComp
})
recreate.AddUUIDFlag(constants.FlagNodepoolId, "", "", cloudapiv6.K8sNodePoolId, core.RequiredFlagOption())
_ = recreate.Command.RegisterFlagCompletionFunc(constants.FlagNodepoolId, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return completer.K8sNodePoolsIds(viper.GetString(core.GetFlagName(recreate.NS, constants.FlagClusterId))), cobra.ShellCompDirectiveNoFileComp
})
recreate.AddUUIDFlag(cloudapiv6.ArgK8sNodeId, cloudapiv6.ArgIdShort, "", cloudapiv6.K8sNodeId, core.RequiredFlagOption())
_ = recreate.Command.RegisterFlagCompletionFunc(cloudapiv6.ArgK8sNodeId, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return completer.K8sNodesIds(viper.GetString(core.GetFlagName(recreate.NS, constants.FlagClusterId)),
viper.GetString(core.GetFlagName(recreate.NS, constants.FlagNodepoolId)),
), cobra.ShellCompDirectiveNoFileComp
})
recreate.AddInt32Flag(cloudapiv6.ArgDepth, cloudapiv6.ArgDepthShort, cloudapiv6.DefaultCreateDepth, cloudapiv6.ArgDepthDescription)
/*
Delete Command
*/
deleteCmd := core.NewCommand(ctx, k8sCmd, core.CommandBuilder{
Namespace: "k8s",
Resource: "node",
Verb: "delete",
Aliases: []string{"d"},
ShortDesc: "Delete a Kubernetes Node",
LongDesc: `This command deletes a Kubernetes Node within an existing Kubernetes NodePool in a Cluster.
Required values to run command:
* K8s Cluster Id
* K8s NodePool Id
* K8s Node Id`,
Example: deleteK8sNodeExample,
PreCmdRun: PreRunK8sClusterNodesIdsAll,
CmdRun: RunK8sNodeDelete,
InitClient: true,
})
deleteCmd.AddUUIDFlag(constants.FlagClusterId, "", "", cloudapiv6.K8sClusterId, core.RequiredFlagOption())
_ = deleteCmd.Command.RegisterFlagCompletionFunc(constants.FlagClusterId, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return completer.K8sClustersIds(), cobra.ShellCompDirectiveNoFileComp
})
deleteCmd.AddUUIDFlag(constants.FlagNodepoolId, "", "", cloudapiv6.K8sNodePoolId, core.RequiredFlagOption())
_ = deleteCmd.Command.RegisterFlagCompletionFunc(constants.FlagNodepoolId, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return completer.K8sNodePoolsIds(viper.GetString(core.GetFlagName(deleteCmd.NS, constants.FlagClusterId))), cobra.ShellCompDirectiveNoFileComp
})
deleteCmd.AddUUIDFlag(cloudapiv6.ArgK8sNodeId, "", "", cloudapiv6.K8sNodeId, core.RequiredFlagOption())
_ = deleteCmd.Command.RegisterFlagCompletionFunc(cloudapiv6.ArgK8sNodeId, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return completer.K8sNodesIds(viper.GetString(core.GetFlagName(deleteCmd.NS, constants.FlagClusterId)),
viper.GetString(core.GetFlagName(deleteCmd.NS, constants.FlagNodepoolId)),
), cobra.ShellCompDirectiveNoFileComp
})
deleteCmd.AddBoolFlag(cloudapiv6.ArgAll, cloudapiv6.ArgAllShort, false, "Delete all the Kubernetes Nodes within an existing Kubernetes NodePool in a Cluster.")
deleteCmd.AddInt32Flag(cloudapiv6.ArgDepth, cloudapiv6.ArgDepthShort, cloudapiv6.DefaultDeleteDepth, cloudapiv6.ArgDepthDescription)
return k8sCmd
}
func PreRunK8sNodesList(c *core.PreCommandConfig) error {
if err := core.CheckRequiredFlags(c.Command, c.NS, constants.FlagClusterId, constants.FlagNodepoolId); err != nil {
return err
}
if viper.IsSet(core.GetFlagName(c.NS, cloudapiv6.ArgFilters)) {
return query.ValidateFilters(c, completer.K8sNodesFilters(), completer.K8sNodesFiltersUsage())
}
return nil
}
func PreRunK8sClusterNodesIds(c *core.PreCommandConfig) error {
return core.CheckRequiredFlags(c.Command, c.NS, constants.FlagClusterId, constants.FlagNodepoolId, cloudapiv6.ArgK8sNodeId)
}
func PreRunK8sClusterNodesIdsAll(c *core.PreCommandConfig) error {
return core.CheckRequiredFlagsSets(c.Command, c.NS,
[]string{constants.FlagClusterId, constants.FlagNodepoolId, cloudapiv6.ArgAll},
[]string{constants.FlagClusterId, constants.FlagNodepoolId, cloudapiv6.ArgK8sNodeId},
)
}
func RunK8sNodeList(c *core.CommandConfig) error {
fmt.Fprintf(c.Command.Command.ErrOrStderr(), jsontabwriter.GenerateVerboseOutput("Listing Nodes from K8s NodePool ID: %v from K8s Cluster ID: %v",
viper.GetString(core.GetFlagName(c.NS, constants.FlagNodepoolId)),
viper.GetString(core.GetFlagName(c.NS, constants.FlagClusterId))))
// Add Query Parameters for GET Requests
listQueryParams, err := query.GetListQueryParams(c)
if err != nil {
return err
}
k8ss, resp, err := c.CloudApiV6Services.K8s().ListNodes(
viper.GetString(core.GetFlagName(c.NS, constants.FlagClusterId)),
viper.GetString(core.GetFlagName(c.NS, constants.FlagNodepoolId)),
listQueryParams,
)
if resp != nil {
fmt.Fprintf(c.Command.Command.ErrOrStderr(), jsontabwriter.GenerateVerboseOutput(constants.MessageRequestTime, resp.RequestTime))
}
if err != nil {
return err
}
cols := viper.GetStringSlice(core.GetFlagName(c.Resource, constants.ArgCols))
out, err := jsontabwriter.GenerateOutput("items", jsonpaths.K8sNode, k8ss.KubernetesNodes,
tabheaders.GetHeadersAllDefault(defaultK8sNodeCols, cols))
if err != nil {
return err
}
fmt.Fprintf(c.Command.Command.OutOrStdout(), out)
return nil
}
func RunK8sNodeGet(c *core.CommandConfig) error {
listQueryParams, err := query.GetListQueryParams(c)
if err != nil {
return err
}
queryParams := listQueryParams.QueryParams
if err := waitfor.WaitForState(c, waiter.K8sNodeStateInterrogator, viper.GetString(core.GetFlagName(c.NS, cloudapiv6.ArgK8sNodeId))); err != nil {
return err
}
fmt.Fprintf(c.Command.Command.ErrOrStderr(), jsontabwriter.GenerateVerboseOutput("Getting K8s Node with ID: %v from K8s NodePool ID: %v from K8s Cluster ID: %v......",
viper.GetString(core.GetFlagName(c.NS, cloudapiv6.ArgK8sNodeId)),
viper.GetString(core.GetFlagName(c.NS, constants.FlagNodepoolId)),
viper.GetString(core.GetFlagName(c.NS, constants.FlagClusterId))))
u, resp, err := c.CloudApiV6Services.K8s().GetNode(
viper.GetString(core.GetFlagName(c.NS, constants.FlagClusterId)),
viper.GetString(core.GetFlagName(c.NS, constants.FlagNodepoolId)),
viper.GetString(core.GetFlagName(c.NS, cloudapiv6.ArgK8sNodeId)),
queryParams,
)
if resp != nil {
fmt.Fprintf(c.Command.Command.ErrOrStderr(), jsontabwriter.GenerateVerboseOutput(constants.MessageRequestTime, resp.RequestTime))
}
if err != nil {
return err
}
cols := viper.GetStringSlice(core.GetFlagName(c.Resource, constants.ArgCols))
out, err := jsontabwriter.GenerateOutput("", jsonpaths.K8sNode, u.KubernetesNode,
tabheaders.GetHeadersAllDefault(defaultK8sNodeCols, cols))
if err != nil {
return err
}
fmt.Fprintf(c.Command.Command.OutOrStdout(), out)
return nil
}
func RunK8sNodeRecreate(c *core.CommandConfig) error {
listQueryParams, err := query.GetListQueryParams(c)
if err != nil {
return err
}
queryParams := listQueryParams.QueryParams
if !confirm.FAsk(c.Command.Command.InOrStdin(), "recreate k8s node", viper.GetBool(constants.ArgForce)) {
return fmt.Errorf(confirm.UserDenied)
}
k8sClusterId := viper.GetString(core.GetFlagName(c.NS, constants.FlagClusterId))
k8sNodePoolId := viper.GetString(core.GetFlagName(c.NS, constants.FlagNodepoolId))
k8sNodeId := viper.GetString(core.GetFlagName(c.NS, cloudapiv6.ArgK8sNodeId))
fmt.Fprintf(c.Command.Command.ErrOrStderr(), jsontabwriter.GenerateVerboseOutput("K8sClusterId: %v, K8sNodePoolId: %v, K8sNodeId: %v",
k8sClusterId, k8sNodePoolId, k8sNodeId))
fmt.Fprintf(c.Command.Command.ErrOrStderr(), jsontabwriter.GenerateVerboseOutput("Recreating Node..."))
resp, err := c.CloudApiV6Services.K8s().RecreateNode(k8sClusterId, k8sNodePoolId, k8sNodeId, queryParams)
if resp != nil && request.GetId(resp) != "" {
fmt.Fprintf(c.Command.Command.ErrOrStderr(), jsontabwriter.GenerateVerboseOutput(constants.MessageRequestInfo, request.GetId(resp), resp.RequestTime))
}
if err != nil {
return err
}
fmt.Fprintf(c.Command.Command.ErrOrStderr(), jsontabwriter.GenerateLogOutput("Status: Command node recreate has been successfully executed"))
return nil
}
func RunK8sNodeDelete(c *core.CommandConfig) error {
listQueryParams, err := query.GetListQueryParams(c)
if err != nil {
return err
}
queryParams := listQueryParams.QueryParams
clusterId := viper.GetString(core.GetFlagName(c.NS, constants.FlagClusterId))
nodepoolId := viper.GetString(core.GetFlagName(c.NS, constants.FlagNodepoolId))
nodeId := viper.GetString(core.GetFlagName(c.NS, cloudapiv6.ArgK8sNodeId))
if viper.GetBool(core.GetFlagName(c.NS, cloudapiv6.ArgAll)) {
if err := DeleteAllK8sNodes(c); err != nil {
return err
}
return nil
}
if !confirm.FAsk(c.Command.Command.InOrStdin(), "delete k8s node", viper.GetBool(constants.ArgForce)) {
return fmt.Errorf(confirm.UserDenied)
}
fmt.Fprintf(c.Command.Command.ErrOrStderr(), jsontabwriter.GenerateVerboseOutput(
"Starting deleting Node with ID: %v from K8s NodePool ID: %v from K8s Cluster ID: %v...", nodeId, nodepoolId, clusterId))
resp, err := c.CloudApiV6Services.K8s().DeleteNode(clusterId, nodepoolId, nodeId, queryParams)
if resp != nil && request.GetId(resp) != "" {
fmt.Fprintf(c.Command.Command.ErrOrStderr(), jsontabwriter.GenerateVerboseOutput(constants.MessageRequestInfo, request.GetId(resp), resp.RequestTime))
}
if err != nil {
return err
}
fmt.Fprintf(c.Command.Command.OutOrStdout(), jsontabwriter.GenerateLogOutput("Kubernetes Node successfully deleted"))
return nil
}
func DeleteAllK8sNodes(c *core.CommandConfig) error {
listQueryParams, err := query.GetListQueryParams(c)
if err != nil {
return err
}
queryParams := listQueryParams.QueryParams
clusterId := viper.GetString(core.GetFlagName(c.NS, constants.FlagClusterId))
nodepoolId := viper.GetString(core.GetFlagName(c.NS, constants.FlagNodepoolId))
fmt.Fprintf(c.Command.Command.ErrOrStderr(), jsontabwriter.GenerateVerboseOutput("K8sCluster ID: %v", clusterId))
fmt.Fprintf(c.Command.Command.ErrOrStderr(), jsontabwriter.GenerateVerboseOutput("K8sNodePool ID: %v", nodepoolId))
fmt.Fprintf(c.Command.Command.ErrOrStderr(), jsontabwriter.GenerateVerboseOutput("Getting K8sNodes..."))
k8sNodes, resp, err := c.CloudApiV6Services.K8s().ListNodes(clusterId, nodepoolId, cloudapiv6.ParentResourceListQueryParams)
if err != nil {
return err
}
k8sNodesItems, ok := k8sNodes.GetItemsOk()
if !ok || k8sNodesItems == nil {
return fmt.Errorf("could not get items of Kubernetes Nodes")
}
if len(*k8sNodesItems) <= 0 {
return fmt.Errorf("no Kubernetes Nodes found")
}
fmt.Fprintf(c.Command.Command.ErrOrStderr(), jsontabwriter.GenerateLogOutput("Kubernetes Nodes to be deleted:"))
for _, dc := range *k8sNodesItems {
delIdAndName := ""
if id, ok := dc.GetIdOk(); ok && id != nil {
delIdAndName += "K8sNodes Id: " + *id
}
if properties, ok := dc.GetPropertiesOk(); ok && properties != nil {
if name, ok := properties.GetNameOk(); ok && name != nil {
delIdAndName += " K8sNodes Name: " + *name
}
}
fmt.Fprintf(c.Command.Command.ErrOrStderr(), jsontabwriter.GenerateLogOutput(delIdAndName))
}
if !confirm.FAsk(c.Command.Command.InOrStdin(), "delete all the K8sNodes", viper.GetBool(constants.ArgForce)) {
return fmt.Errorf(confirm.UserDenied)
}
fmt.Fprintf(c.Command.Command.ErrOrStderr(), jsontabwriter.GenerateVerboseOutput("Deleting all the K8sNodes..."))
var multiErr error
for _, dc := range *k8sNodesItems {
id, ok := dc.GetIdOk()
if !ok || id == nil {
continue
}
fmt.Fprintf(c.Command.Command.ErrOrStderr(), jsontabwriter.GenerateVerboseOutput(
"Staring deleting Node with ID: %v from K8s NodePool ID: %v from K8s Cluster ID: %v...", *id, nodepoolId, clusterId))
resp, err = c.CloudApiV6Services.K8s().DeleteNode(clusterId, nodepoolId, *id, queryParams)
if resp != nil && request.GetId(resp) != "" {
fmt.Fprintf(c.Command.Command.ErrOrStderr(), jsontabwriter.GenerateVerboseOutput(constants.MessageRequestInfo, request.GetId(resp), resp.RequestTime))
}
if err != nil {
multiErr = errors.Join(multiErr, fmt.Errorf(constants.ErrDeleteAll, c.Resource, *id, err))
continue
}
fmt.Fprintf(c.Command.Command.ErrOrStderr(), jsontabwriter.GenerateLogOutput(constants.MessageDeletingAll, c.Resource, *id))
if err = waitfor.WaitForRequest(c, waiter.RequestInterrogator, request.GetId(resp)); err != nil {
multiErr = errors.Join(multiErr, fmt.Errorf(constants.ErrWaitDeleteAll, c.Resource, *id, err))
continue
}
}
if multiErr != nil {
return multiErr
}
fmt.Fprintf(c.Command.Command.OutOrStdout(), jsontabwriter.GenerateLogOutput("Kubernetes Nodes successfully deleted"))
return nil
}