-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
cluster.go
201 lines (162 loc) · 6.41 KB
/
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
package upgrade
import (
"fmt"
"time"
"github.com/kris-nova/logger"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5"
"github.com/weaveworks/eksctl/pkg/ctl/cmdutils"
"github.com/weaveworks/eksctl/pkg/printers"
"github.com/weaveworks/eksctl/pkg/utils"
)
func upgradeCluster(cmd *cmdutils.Cmd) {
upgradeClusterWithRunFunc(cmd, DoUpgradeCluster)
}
func upgradeClusterWithRunFunc(cmd *cmdutils.Cmd, runFunc func(cmd *cmdutils.Cmd) error) {
cfg := api.NewClusterConfig()
// Reset version
cfg.Metadata.Version = ""
cmd.ClusterConfig = cfg
cmd.SetDescription("cluster", "Upgrade control plane to the next version",
"Upgrade control plane to the next Kubernetes version if available. Will also perform any updates needed in the cluster stack if resources are missing.")
cmdutils.AddCommonFlagsForAWS(cmd.FlagSetGroup, &cmd.ProviderConfig, false)
cmd.FlagSetGroup.InFlagSet("General", func(fs *pflag.FlagSet) {
fs.StringVarP(&cfg.Metadata.Name, "name", "n", "", "EKS cluster name")
cmdutils.AddRegionFlag(fs, &cmd.ProviderConfig)
cmdutils.AddVersionFlag(fs, cfg.Metadata, "")
cmdutils.AddConfigFileFlag(fs, &cmd.ClusterConfigFile)
// cmdutils.AddVersionFlag(fs, cfg.Metadata, `"next" and "latest" can be used to automatically increment version by one, or force latest`)
cmdutils.AddApproveFlag(fs, cmd)
// updating from 1.15 to 1.16 has been observed to take longer than the default value of 25 minutes
cmdutils.AddTimeoutFlagWithValue(fs, &cmd.ProviderConfig.WaitTimeout, 35*time.Minute)
})
cmd.CobraCommand.RunE = func(_ *cobra.Command, args []string) error {
cmd.NameArg = cmdutils.GetNameArg(args)
if err := cmdutils.NewMetadataLoader(cmd).Load(); err != nil {
return err
}
return runFunc(cmd)
}
}
// DoUpgradeCluster made public so that it can be shared with update/cluster.go until this is deprecated
// TODO Once `eksctl update cluster` is officially deprecated this can be made package private again
func DoUpgradeCluster(cmd *cmdutils.Cmd) error {
cfg := cmd.ClusterConfig
meta := cmd.ClusterConfig.Metadata
printer := printers.NewJSONPrinter()
ctl, err := cmd.NewCtl()
if err != nil {
return err
}
cmdutils.LogRegionAndVersionInfo(meta)
if err := ctl.CheckAuth(); err != nil {
return err
}
if ok, err := ctl.CanUpdate(cfg); !ok {
return err
}
if cmd.ClusterConfigFile != "" {
logger.Warning("NOTE: cluster VPC (subnets, routing & NAT Gateway) configuration changes are not yet implemented")
}
currentVersion := ctl.ControlPlaneVersion()
versionUpdateRequired, err := requiresVersionUpgrade(cfg.Metadata, currentVersion)
if err != nil {
return err
}
if err := ctl.LoadClusterVPC(cfg); err != nil {
return errors.Wrapf(err, "getting VPC configuration for cluster %q", cfg.Metadata.Name)
}
if err := printer.LogObj(logger.Debug, "cfg.json = \\\n%s\n", cfg); err != nil {
return err
}
stackManager := ctl.NewStackManager(cfg)
if versionUpdateRequired {
msgNodeGroupsAndAddons := "you will need to follow the upgrade procedure for all of nodegroups and add-ons"
cmdutils.LogIntendedAction(cmd.Plan, "upgrade cluster %q control plane from current version %q to %q", cfg.Metadata.Name, currentVersion, cfg.Metadata.Version)
if !cmd.Plan {
if err := ctl.UpdateClusterVersionBlocking(cfg); err != nil {
return err
}
logger.Success("cluster %q control plane has been upgraded to version %q", cfg.Metadata.Name, cfg.Metadata.Version)
logger.Info(msgNodeGroupsAndAddons)
}
}
if err := ctl.RefreshClusterStatus(cfg); err != nil {
return err
}
supportsManagedNodes, err := ctl.SupportsManagedNodes(cfg)
if err != nil {
return err
}
stackUpdateRequired, err := stackManager.AppendNewClusterStackResource(cmd.Plan, supportsManagedNodes)
if err != nil {
return err
}
if err := ctl.ValidateExistingNodeGroupsForCompatibility(cfg, stackManager); err != nil {
logger.Critical("failed checking nodegroups", err.Error())
}
cmdutils.LogPlanModeWarning(cmd.Plan && (stackUpdateRequired || versionUpdateRequired))
return nil
}
func requiresVersionUpgrade(clusterMeta *api.ClusterMeta, currentEKSVersion string) (bool, error) {
nextVersion, err := getNextVersion(currentEKSVersion)
if err != nil {
return false, err
}
// If the version was not specified default to the next Kubernetes version and assume the user intended to upgrade if possible
// also support "auto" as version (see #2461)
if clusterMeta.Version == "" || clusterMeta.Version == "auto" {
if api.IsSupportedVersion(nextVersion) {
clusterMeta.Version = nextVersion
return true, nil
}
// There is no new version, stay in the current one
clusterMeta.Version = currentEKSVersion
return false, nil
}
if c, err := utils.CompareVersions(clusterMeta.Version, currentEKSVersion); err != nil {
return false, errors.Wrap(err, "couldn't compare versions for upgrade")
} else if c < 0 {
return false, fmt.Errorf("cannot upgrade to a lower version. Found given target version %q, current cluster version %q", clusterMeta.Version, currentEKSVersion)
}
if api.IsDeprecatedVersion(clusterMeta.Version) {
return false, fmt.Errorf("control plane version %q has been deprecated", clusterMeta.Version)
}
if !api.IsSupportedVersion(clusterMeta.Version) {
return false, fmt.Errorf("control plane version %q is not known to this version of eksctl, try to upgrade eksctl first", clusterMeta.Version)
}
if clusterMeta.Version == currentEKSVersion {
return false, nil
}
if clusterMeta.Version == nextVersion {
return true, nil
}
return false, fmt.Errorf(
"upgrading more than one version at a time is not supported. Found upgrade from %q to %q. Please upgrade to %q first",
currentEKSVersion,
clusterMeta.Version,
nextVersion)
}
func getNextVersion(currentVersion string) (string, error) {
switch currentVersion {
case "":
return "", errors.New("unable to get control plane version")
case api.Version1_12:
return api.Version1_13, nil
case api.Version1_13:
return api.Version1_14, nil
case api.Version1_14:
return api.Version1_15, nil
case api.Version1_15:
return api.Version1_16, nil
case api.Version1_16:
return api.Version1_17, nil
case api.Version1_17:
return api.Version1_18, nil
default:
// version of control plane is not known to us, maybe we are just too old...
return "", fmt.Errorf("control plane version %q is not known to this version of eksctl, try to upgrade eksctl first", currentVersion)
}
}