-
Notifications
You must be signed in to change notification settings - Fork 20
/
sks_upgrade.go
134 lines (105 loc) · 3.64 KB
/
sks_upgrade.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
package cmd
import (
"errors"
"fmt"
"strings"
"github.com/spf13/cobra"
"github.com/exoscale/cli/pkg/account"
"github.com/exoscale/cli/pkg/globalstate"
"github.com/exoscale/cli/utils"
v2 "github.com/exoscale/egoscale/v2"
exoapi "github.com/exoscale/egoscale/v2/api"
)
type sksUpgradeCmd struct {
cliCommandSettings `cli-cmd:"-"`
_ bool `cli-cmd:"upgrade"`
Cluster string `cli-arg:"#" cli-usage:"NAME|ID"`
Version string `cli-arg:"#"`
Force bool `cli-short:"f" cli-usage:"don't prompt for confirmation"`
Zone string `cli-short:"z" cli-usage:"SKS cluster zone"`
}
func (c *sksUpgradeCmd) cmdAliases() []string { return nil }
func (c *sksUpgradeCmd) cmdShort() string { return "Upgrade an SKS cluster Kubernetes version" }
func (c *sksUpgradeCmd) cmdLong() string { return "" }
func (c *sksUpgradeCmd) cmdPreRun(cmd *cobra.Command, args []string) error {
cmdSetZoneFlagFromDefault(cmd)
return cliCommandDefaultPreRun(c, cmd, args)
}
func (c *sksUpgradeCmd) 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
}
if !c.Force {
if utils.VersionIsNewer(c.Version, *cluster.Version) {
deprecatedResources, err := globalstate.EgoscaleClient.ListSKSClusterDeprecatedResources(
ctx,
c.Zone,
cluster,
)
if err != nil {
return fmt.Errorf("error retrieving deprecated resources: %w", err)
}
removedDeprecatedResources := []*v2.SKSClusterDeprecatedResource{}
for _, resource := range deprecatedResources {
if utils.VersionsAreEquivalent(*resource.RemovedRelease, *cluster.Version) {
removedDeprecatedResources = append(removedDeprecatedResources, resource)
}
}
if len(removedDeprecatedResources) > 0 {
fmt.Println("Some resources in your cluster are using deprecated APIs:")
for _, t := range removedDeprecatedResources {
fmt.Println("- " + formatDeprecatedResource(t))
}
}
}
if !askQuestion(fmt.Sprintf(
"Are you sure you want to upgrade the cluster %q to version %s?",
c.Cluster,
c.Version,
)) {
return nil
}
}
decorateAsyncOperation(fmt.Sprintf("Upgrading SKS cluster %q...", c.Cluster), func() {
err = globalstate.EgoscaleClient.UpgradeSKSCluster(ctx, c.Zone, cluster, c.Version)
})
if err != nil {
return err
}
if !globalstate.Quiet {
return (&sksShowCmd{
cliCommandSettings: c.cliCommandSettings,
Cluster: *cluster.ID,
Zone: c.Zone,
}).cmdRun(nil, nil)
}
return nil
}
func formatDeprecatedResource(deprecatedResource *v2.SKSClusterDeprecatedResource) string {
var version string
var resource string
if !utils.IsEmptyStringPtr(deprecatedResource.Group) && !utils.IsEmptyStringPtr(deprecatedResource.Version) {
version = *deprecatedResource.Group + "/" + *deprecatedResource.Version
}
if !utils.IsEmptyStringPtr(deprecatedResource.Resource) {
resource = *deprecatedResource.Resource
if !utils.IsEmptyStringPtr(deprecatedResource.SubResource) {
resource += " (" + *deprecatedResource.SubResource + " subresource)"
}
}
deprecationNotice := strings.Join([]string{version, resource}, " ")
if !utils.IsEmptyStringPtr(deprecatedResource.RemovedRelease) {
return "Removed in Kubernetes v" + *deprecatedResource.RemovedRelease + ": " + deprecationNotice
}
return deprecationNotice
}
func init() {
cobra.CheckErr(registerCLICommand(sksCmd, &sksUpgradeCmd{
cliCommandSettings: defaultCLICmdSettings(),
}))
}