-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
upgrade.go
309 lines (257 loc) · 12.1 KB
/
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
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
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package client
import (
"context"
"strings"
"time"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/cluster"
)
const upgradeItemProviderNameError = "invalid provider name %q. Provider name should be in the form namespace/provider:version or provider:version"
// PlanUpgradeOptions carries the options supported by upgrade plan.
type PlanUpgradeOptions struct {
// Kubeconfig defines the kubeconfig to use for accessing the management cluster. If empty, default discovery rules apply.
Kubeconfig Kubeconfig
}
func (c *clusterctlClient) PlanCertManagerUpgrade(ctx context.Context, options PlanUpgradeOptions) (CertManagerUpgradePlan, error) {
// Get the client for interacting with the management cluster.
cluster, err := c.clusterClientFactory(ClusterClientFactoryInput{Kubeconfig: options.Kubeconfig})
if err != nil {
return CertManagerUpgradePlan{}, err
}
certManager := cluster.CertManager()
plan, err := certManager.PlanUpgrade(ctx)
return CertManagerUpgradePlan(plan), err
}
func (c *clusterctlClient) PlanUpgrade(ctx context.Context, options PlanUpgradeOptions) ([]UpgradePlan, error) {
// Get the client for interacting with the management cluster.
clusterClient, err := c.clusterClientFactory(ClusterClientFactoryInput{Kubeconfig: options.Kubeconfig})
if err != nil {
return nil, err
}
// Ensure this command only runs against management clusters with the current Cluster API contract.
if err := clusterClient.ProviderInventory().CheckCAPIContract(ctx); err != nil {
return nil, err
}
// Ensures the custom resource definitions required by clusterctl are in place.
if err := clusterClient.ProviderInventory().EnsureCustomResourceDefinitions(ctx); err != nil {
return nil, err
}
upgradePlans, err := clusterClient.ProviderUpgrader().Plan(ctx)
if err != nil {
return nil, err
}
// UpgradePlan is an alias for cluster.UpgradePlan; this makes the conversion
aliasUpgradePlan := make([]UpgradePlan, len(upgradePlans))
for i, plan := range upgradePlans {
aliasUpgradePlan[i] = UpgradePlan{
Contract: plan.Contract,
Providers: plan.Providers,
}
}
return aliasUpgradePlan, nil
}
// ApplyUpgradeOptions carries the options supported by upgrade apply.
type ApplyUpgradeOptions struct {
// Kubeconfig to use for accessing the management cluster. If empty, default discovery rules apply.
Kubeconfig Kubeconfig
// Contract defines the API Version of Cluster API (contract e.g. v1alpha4) the management cluster should upgrade to.
// When upgrading by contract, the latest versions available will be used for all the providers; if you want
// a more granular control on upgrade, use CoreProvider, BootstrapProviders, ControlPlaneProviders, InfrastructureProviders.
Contract string
// CoreProvider instance and version (e.g. [capi-system/]cluster-api:v1.1.5) to upgrade to. This field can be used as alternative to Contract.
// Specifying a namespace is now optional and in the future it will be deprecated.
CoreProvider string
// BootstrapProviders instance and versions (e.g. [capi-kubeadm-bootstrap-system/]kubeadm:v1.1.5) to upgrade to. This field can be used as alternative to Contract.
// Specifying a namespace is now optional and in the future it will be deprecated.
BootstrapProviders []string
// ControlPlaneProviders instance and versions (e.g. [capi-kubeadm-control-plane-system/]kubeadm:v1.1.5) to upgrade to. This field can be used as alternative to Contract.
// Specifying a namespace is now optional and in the future it will be deprecated.
ControlPlaneProviders []string
// InfrastructureProviders instance and versions (e.g. [capa-system/]aws:v0.5.0) to upgrade to. This field can be used as alternative to Contract.
// Specifying a namespace is now optional and in the future it will be deprecated.
InfrastructureProviders []string
// IPAMProviders instance and versions (e.g. ipam-system/infoblox:v0.0.1) to upgrade to. This field can be used as alternative to Contract.
IPAMProviders []string
// RuntimeExtensionProviders instance and versions (e.g. runtime-extension-system/test:v0.0.1) to upgrade to. This field can be used as alternative to Contract.
RuntimeExtensionProviders []string
// AddonProviders instance and versions (e.g. caaph-system/helm:v0.1.0) to upgrade to. This field can be used as alternative to Contract.
AddonProviders []string
// WaitProviders instructs the upgrade apply command to wait till the providers are successfully upgraded.
WaitProviders bool
// WaitProviderTimeout sets the timeout per provider upgrade.
WaitProviderTimeout time.Duration
}
func (c *clusterctlClient) ApplyUpgrade(ctx context.Context, options ApplyUpgradeOptions) error {
if options.Contract != "" && options.Contract != clusterv1.GroupVersion.Version {
return errors.Errorf("current version of clusterctl could only upgrade to %s contract, requested %s", clusterv1.GroupVersion.Version, options.Contract)
}
// Default WaitProviderTimeout as we cannot rely on defaulting in the CLI
// when clusterctl is used as a library.
if options.WaitProviderTimeout.Nanoseconds() == 0 {
options.WaitProviderTimeout = time.Duration(5*60) * time.Second
}
// Get the client for interacting with the management cluster.
clusterClient, err := c.clusterClientFactory(ClusterClientFactoryInput{Kubeconfig: options.Kubeconfig})
if err != nil {
return err
}
// Ensure this command only runs against management clusters with the current Cluster API contract.
if err := clusterClient.ProviderInventory().CheckCAPIContract(ctx); err != nil {
return err
}
// Ensures the custom resource definitions required by clusterctl are in place.
if err := clusterClient.ProviderInventory().EnsureCustomResourceDefinitions(ctx); err != nil {
return err
}
// Ensures the latest version of cert-manager.
// NOTE: it is safe to upgrade to latest version of cert-manager given that it provides
// conversion web-hooks around Issuer/Certificate kinds, so installing an older versions of providers
// should continue to work with the latest cert-manager.
certManager := clusterClient.CertManager()
if err := certManager.EnsureLatestVersion(ctx); err != nil {
return err
}
// Check if the user want a custom upgrade
isCustomUpgrade := options.CoreProvider != "" ||
len(options.BootstrapProviders) > 0 ||
len(options.ControlPlaneProviders) > 0 ||
len(options.InfrastructureProviders) > 0 ||
len(options.IPAMProviders) > 0 ||
len(options.RuntimeExtensionProviders) > 0 ||
len(options.AddonProviders) > 0
opts := cluster.UpgradeOptions{
WaitProviders: options.WaitProviders,
WaitProviderTimeout: options.WaitProviderTimeout,
}
// If we are upgrading a specific set of providers only, process the providers and call ApplyCustomPlan.
if isCustomUpgrade {
// Converts upgrade references back into an UpgradeItem.
upgradeItems := []cluster.UpgradeItem{}
if options.CoreProvider != "" {
upgradeItems, err = addUpgradeItems(ctx, clusterClient, upgradeItems, clusterctlv1.CoreProviderType, options.CoreProvider)
if err != nil {
return err
}
}
upgradeItems, err = addUpgradeItems(ctx, clusterClient, upgradeItems, clusterctlv1.BootstrapProviderType, options.BootstrapProviders...)
if err != nil {
return err
}
upgradeItems, err = addUpgradeItems(ctx, clusterClient, upgradeItems, clusterctlv1.ControlPlaneProviderType, options.ControlPlaneProviders...)
if err != nil {
return err
}
upgradeItems, err = addUpgradeItems(ctx, clusterClient, upgradeItems, clusterctlv1.InfrastructureProviderType, options.InfrastructureProviders...)
if err != nil {
return err
}
upgradeItems, err = addUpgradeItems(ctx, clusterClient, upgradeItems, clusterctlv1.IPAMProviderType, options.IPAMProviders...)
if err != nil {
return err
}
upgradeItems, err = addUpgradeItems(ctx, clusterClient, upgradeItems, clusterctlv1.RuntimeExtensionProviderType, options.RuntimeExtensionProviders...)
if err != nil {
return err
}
upgradeItems, err = addUpgradeItems(ctx, clusterClient, upgradeItems, clusterctlv1.AddonProviderType, options.AddonProviders...)
if err != nil {
return err
}
// Execute the upgrade using the custom upgrade items
return clusterClient.ProviderUpgrader().ApplyCustomPlan(ctx, opts, upgradeItems...)
}
// Otherwise we are upgrading a whole management cluster according to a clusterctl generated upgrade plan.
return clusterClient.ProviderUpgrader().ApplyPlan(ctx, opts, options.Contract)
}
func addUpgradeItems(ctx context.Context, clusterClient cluster.Client, upgradeItems []cluster.UpgradeItem, providerType clusterctlv1.ProviderType, providers ...string) ([]cluster.UpgradeItem, error) {
for _, upgradeReference := range providers {
providerUpgradeItem, err := parseUpgradeItem(ctx, clusterClient, upgradeReference, providerType)
if err != nil {
return nil, err
}
if providerUpgradeItem.NextVersion == "" {
return nil, errors.Errorf("invalid provider name %q. Provider name should be in the form namespace/name:version and version cannot be empty", upgradeReference)
}
upgradeItems = append(upgradeItems, *providerUpgradeItem)
}
return upgradeItems, nil
}
func parseUpgradeItem(ctx context.Context, clusterClient cluster.Client, ref string, providerType clusterctlv1.ProviderType) (*cluster.UpgradeItem, error) {
// TODO(oscr) Remove when explicit namespaces for providers is removed
// ref format is old format: namespace/provider:version
if strings.Contains(ref, "/") {
return parseUpgradeItemWithNamespace(ref, providerType)
}
// ref format is: provider:version
return parseUpgradeItemWithoutNamespace(ctx, clusterClient, ref, providerType)
}
func parseUpgradeItemWithNamespace(ref string, providerType clusterctlv1.ProviderType) (*cluster.UpgradeItem, error) {
refSplit := strings.Split(strings.ToLower(ref), "/")
if len(refSplit) != 2 {
return nil, errors.Errorf(upgradeItemProviderNameError, ref)
}
if refSplit[0] == "" {
return nil, errors.Errorf(upgradeItemProviderNameError, ref)
}
namespace := refSplit[0]
name, version, err := parseProviderName(refSplit[1])
if err != nil {
return nil, errors.Wrapf(err, upgradeItemProviderNameError, ref)
}
return &cluster.UpgradeItem{
Provider: clusterctlv1.Provider{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: clusterctlv1.ManifestLabel(name, providerType),
},
ProviderName: name,
Type: string(providerType),
// The value for the following fields will be retrieved while
// creating the custom upgrade plan.
WatchedNamespace: "",
},
NextVersion: version,
}, nil
}
func parseUpgradeItemWithoutNamespace(ctx context.Context, clusterClient cluster.Client, ref string, providerType clusterctlv1.ProviderType) (*cluster.UpgradeItem, error) {
if !strings.Contains(ref, ":") {
return nil, errors.Errorf(upgradeItemProviderNameError, ref)
}
name, version, err := parseProviderName(ref)
if err != nil {
return nil, errors.Wrapf(err, upgradeItemProviderNameError, ref)
}
namespace, err := clusterClient.ProviderInventory().GetProviderNamespace(ctx, name, providerType)
if err != nil {
return nil, errors.Errorf("unable to find default namespace for provider %q", ref)
}
return &cluster.UpgradeItem{
Provider: clusterctlv1.Provider{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: clusterctlv1.ManifestLabel(name, providerType),
},
ProviderName: name,
Type: string(providerType),
// The value for the following fields will be retrieved while
// creating the custom upgrade plan.
WatchedNamespace: "",
},
NextVersion: version,
}, nil
}