-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
upgrade_apply.go
114 lines (95 loc) · 4.89 KB
/
upgrade_apply.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
/*
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 cmd
import (
"time"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"sigs.k8s.io/cluster-api/cmd/clusterctl/client"
)
type upgradeApplyOptions struct {
kubeconfig string
kubeconfigContext string
contract string
coreProvider string
bootstrapProviders []string
controlPlaneProviders []string
infrastructureProviders []string
waitProviders bool
waitProviderTimeout int
}
var ua = &upgradeApplyOptions{}
var upgradeApplyCmd = &cobra.Command{
Use: "apply",
Short: "Apply new versions of Cluster API core and providers in a management cluster",
Long: LongDesc(`
The upgrade apply command applies new versions of Cluster API providers as defined by clusterctl upgrade plan.
New version should be applied ensuring all the providers uses the same cluster API version
in order to guarantee the proper functioning of the management cluster.`),
Example: Examples(`
# Upgrades all the providers in the management cluster to the latest version available which is compliant
# to the v1alpha4 API Version of Cluster API (contract).
clusterctl upgrade apply --contract v1alpha4
# Upgrades only the capa-system/aws provider to the v0.5.0 version.
clusterctl upgrade apply --infrastructure capa-system/aws:v0.5.0`),
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return runUpgradeApply()
},
}
func init() {
upgradeApplyCmd.Flags().StringVar(&ua.kubeconfig, "kubeconfig", "",
"Path to the kubeconfig file to use for accessing the management cluster. If unspecified, default discovery rules apply.")
upgradeApplyCmd.Flags().StringVar(&ua.kubeconfigContext, "kubeconfig-context", "",
"Context to be used within the kubeconfig file. If empty, current context will be used.")
upgradeApplyCmd.Flags().StringVar(&ua.contract, "contract", "",
"The API Version of Cluster API (contract, e.g. v1alpha4) the management cluster should upgrade to")
upgradeApplyCmd.Flags().StringVar(&ua.coreProvider, "core", "",
"Core provider instance version (e.g. capi-system/cluster-api:v0.3.0) to upgrade to. This flag can be used as alternative to --contract.")
upgradeApplyCmd.Flags().StringSliceVarP(&ua.infrastructureProviders, "infrastructure", "i", nil,
"Infrastructure providers instance and versions (e.g. capa-system/aws:v0.5.0) to upgrade to. This flag can be used as alternative to --contract.")
upgradeApplyCmd.Flags().StringSliceVarP(&ua.bootstrapProviders, "bootstrap", "b", nil,
"Bootstrap providers instance and versions (e.g. capi-kubeadm-bootstrap-system/kubeadm:v0.3.0) to upgrade to. This flag can be used as alternative to --contract.")
upgradeApplyCmd.Flags().StringSliceVarP(&ua.controlPlaneProviders, "control-plane", "c", nil,
"ControlPlane providers instance and versions (e.g. capi-kubeadm-control-plane-system/kubeadm:v0.3.0) to upgrade to. This flag can be used as alternative to --contract.")
upgradeApplyCmd.Flags().BoolVar(&ua.waitProviders, "wait-providers", false,
"Wait for providers to be upgraded.")
upgradeApplyCmd.Flags().IntVar(&ua.waitProviderTimeout, "wait-provider-timeout", 5*60,
"Wait timeout per provider upgrade in seconds. This value is ignored if --wait-providers is false")
}
func runUpgradeApply() error {
c, err := client.New(cfgFile)
if err != nil {
return err
}
hasProviderNames := (ua.coreProvider != "") ||
(len(ua.bootstrapProviders) > 0) ||
(len(ua.controlPlaneProviders) > 0) ||
(len(ua.infrastructureProviders) > 0)
if ua.contract == "" && !hasProviderNames {
return errors.New("Either the --contract flag or at least one of the following flags has to be set: --core, --bootstrap, --control-plane, --infrastructure")
}
if ua.contract != "" && hasProviderNames {
return errors.New("The --contract flag can't be used in combination with --core, --bootstrap, --control-plane, --infrastructure")
}
return c.ApplyUpgrade(client.ApplyUpgradeOptions{
Kubeconfig: client.Kubeconfig{Path: ua.kubeconfig, Context: ua.kubeconfigContext},
Contract: ua.contract,
CoreProvider: ua.coreProvider,
BootstrapProviders: ua.bootstrapProviders,
ControlPlaneProviders: ua.controlPlaneProviders,
InfrastructureProviders: ua.infrastructureProviders,
WaitProviders: ua.waitProviders,
WaitProviderTimeout: time.Duration(ua.waitProviderTimeout) * time.Second,
})
}