-
Notifications
You must be signed in to change notification settings - Fork 19
/
sks_update.go
119 lines (91 loc) · 3.11 KB
/
sks_update.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
package cmd
import (
"fmt"
"slices"
"strings"
"github.com/spf13/cobra"
"github.com/exoscale/cli/pkg/globalstate"
"github.com/exoscale/cli/pkg/output"
v3 "github.com/exoscale/egoscale/v3"
)
type sksUpdateCmd struct {
cliCommandSettings `cli-cmd:"-"`
_ bool `cli-cmd:"update"`
Cluster string `cli-arg:"#" cli-usage:"NAME|ID"`
AutoUpgrade bool `cli-usage:"enable automatic upgrading of the SKS cluster control plane Kubernetes version(--auto-upgrade=false to disable again)"`
Description string `cli-usage:"SKS cluster description"`
Labels map[string]string `cli-flag:"label" cli-usage:"SKS cluster label (format: key=value)"`
Name string `cli-usage:"SKS cluster name"`
EnableCSIAddon bool `cli-usage:"enable the Exoscale CSI driver"`
Zone string `cli-short:"z" cli-usage:"SKS cluster zone"`
}
func (c *sksUpdateCmd) cmdAliases() []string { return nil }
func (c *sksUpdateCmd) cmdShort() string { return "Update an SKS cluster" }
func (c *sksUpdateCmd) cmdLong() string {
return fmt.Sprintf(`This command updates an SKS cluster.
Supported output template annotations: %s`,
strings.Join(output.TemplateAnnotations(&sksShowOutput{}), ", "),
)
}
func (c *sksUpdateCmd) cmdPreRun(cmd *cobra.Command, args []string) error {
cmdSetZoneFlagFromDefault(cmd)
return cliCommandDefaultPreRun(c, cmd, args)
}
func (c *sksUpdateCmd) cmdRun(cmd *cobra.Command, _ []string) error {
var updated bool
ctx := gContext
clusters, err := globalstate.EgoscaleV3Client.ListSKSClusters(ctx)
if err != nil {
return err
}
cluster, err := clusters.FindSKSCluster(c.Cluster)
if err != nil {
return err
}
updateReq := v3.UpdateSKSClusterRequest{}
if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.AutoUpgrade)) {
updateReq.AutoUpgrade = &c.AutoUpgrade
updated = true
}
if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.Labels)) {
updateReq.Labels = c.Labels
updated = true
}
if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.Name)) {
updateReq.Name = c.Name
updated = true
}
if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.Description)) {
updateReq.Description = &c.Description
updated = true
}
if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.EnableCSIAddon)) && !slices.Contains(cluster.Addons, sksClusterAddonExoscaleCSI) {
updateReq.Addons = append(cluster.Addons, sksClusterAddonExoscaleCSI) //nolint:gocritic
updated = true
}
if updated {
op, err := globalstate.EgoscaleV3Client.UpdateSKSCluster(ctx, cluster.ID, updateReq)
if err != nil {
return err
}
decorateAsyncOperation(fmt.Sprintf("Updating SKS cluster %q...", c.Cluster), func() {
_, err = globalstate.EgoscaleV3Client.Wait(ctx, op, v3.OperationStateSuccess)
})
if err != nil {
return err
}
}
if !globalstate.Quiet {
return (&sksShowCmd{
cliCommandSettings: c.cliCommandSettings,
Cluster: string(cluster.ID),
Zone: c.Zone,
}).cmdRun(nil, nil)
}
return nil
}
func init() {
cobra.CheckErr(registerCLICommand(sksCmd, &sksUpdateCmd{
cliCommandSettings: defaultCLICmdSettings(),
}))
}