-
Notifications
You must be signed in to change notification settings - Fork 20
/
sks_delete.go
102 lines (80 loc) · 2.62 KB
/
sks_delete.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
package cmd
import (
"errors"
"fmt"
exoapi "github.com/exoscale/egoscale/v2/api"
"github.com/spf13/cobra"
)
type sksDeleteCmd struct {
cliCommandSettings `cli-cmd:"-"`
_ bool `cli-cmd:"delete"`
Cluster string `cli-arg:"#" cli-usage:"NAME|ID"`
Force bool `cli-short:"f" cli-usage:"don't prompt for confirmation"`
DeleteNodepools bool `cli-flag:"nodepools" cli-short:"n" cli-usage:"delete existing Nodepools before deleting the SKS cluster"`
Zone string `cli-short:"z" cli-usage:"SKS cluster zone"`
}
func (c *sksDeleteCmd) cmdAliases() []string { return gRemoveAlias }
func (c *sksDeleteCmd) cmdShort() string { return "Delete an SKS cluster" }
func (c *sksDeleteCmd) cmdLong() string { return "" }
func (c *sksDeleteCmd) cmdPreRun(cmd *cobra.Command, args []string) error {
cmdSetZoneFlagFromDefault(cmd)
return cliCommandDefaultPreRun(c, cmd, args)
}
func (c *sksDeleteCmd) cmdRun(_ *cobra.Command, _ []string) error {
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(gCurrentAccount.Environment, c.Zone))
cluster, err := cs.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
}
if len(cluster.Nodepools) > 0 {
nodepoolsRemaining := len(cluster.Nodepools)
if c.DeleteNodepools {
for _, nodepool := range cluster.Nodepools {
nodepool := nodepool
if !c.Force {
if !askQuestion(fmt.Sprintf(
"Are you sure you want to delete Nodepool %q?",
*nodepool.Name),
) {
continue
}
}
decorateAsyncOperation(fmt.Sprintf("Deleting Nodepool %q...", *nodepool.Name), func() {
err = cs.DeleteSKSNodepool(ctx, c.Zone, cluster, nodepool)
})
if err != nil {
return err
}
nodepoolsRemaining--
}
}
// It's not possible to delete an SKS cluster that still has Nodepools, no need to go further.
if nodepoolsRemaining > 0 {
return errors.New("impossible to delete the SKS cluster: Nodepools still present")
}
}
if !c.Force {
if !askQuestion(fmt.Sprintf("Are you sure you want to delete SKS cluster %q?", *cluster.Name)) {
return nil
}
}
decorateAsyncOperation(fmt.Sprintf("Deleting SKS cluster %q...", *cluster.Name), func() {
err = cs.DeleteSKSCluster(ctx, c.Zone, cluster)
})
if err != nil {
return err
}
return nil
}
func init() {
cobra.CheckErr(registerCLICommand(sksCmd, &sksDeleteCmd{
cliCommandSettings: defaultCLICmdSettings(),
}))
// FIXME: remove this someday.
cobra.CheckErr(registerCLICommand(deprecatedSKSCmd, &sksDeleteCmd{
cliCommandSettings: defaultCLICmdSettings(),
}))
}