Skip to content

Commit 08d0522

Browse files
authored
Merge pull request kubernetes#73844 from rojkov/kubeadm-restructure-upgradeVariables
kubeadm: restructure upgradeVariables
2 parents 120bcd7 + 6ac76f9 commit 08d0522

File tree

3 files changed

+29
-42
lines changed

3 files changed

+29
-42
lines changed

cmd/kubeadm/app/cmd/upgrade/apply.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -151,37 +151,37 @@ func runApply(flags *applyFlags) error {
151151
// Start with the basics, verify that the cluster is healthy and get the configuration from the cluster (using the ConfigMap)
152152
klog.V(1).Infof("[upgrade/apply] verifying health of cluster")
153153
klog.V(1).Infof("[upgrade/apply] retrieving configuration from cluster")
154-
upgradeVars, err := enforceRequirements(flags.applyPlanFlags, flags.dryRun, flags.newK8sVersionStr)
154+
client, versionGetter, cfg, err := enforceRequirements(flags.applyPlanFlags, flags.dryRun, flags.newK8sVersionStr)
155155
if err != nil {
156156
return err
157157
}
158158

159159
if len(flags.criSocket) != 0 {
160160
fmt.Println("[upgrade/apply] Respecting the --cri-socket flag that is set with higher priority than the config file.")
161-
upgradeVars.cfg.NodeRegistration.CRISocket = flags.criSocket
161+
cfg.NodeRegistration.CRISocket = flags.criSocket
162162
}
163163

164164
// Validate requested and validate actual version
165165
klog.V(1).Infof("[upgrade/apply] validating requested and actual version")
166-
if err := configutil.NormalizeKubernetesVersion(&upgradeVars.cfg.ClusterConfiguration); err != nil {
166+
if err := configutil.NormalizeKubernetesVersion(&cfg.ClusterConfiguration); err != nil {
167167
return err
168168
}
169169

170170
// Use normalized version string in all following code.
171-
flags.newK8sVersionStr = upgradeVars.cfg.KubernetesVersion
171+
flags.newK8sVersionStr = cfg.KubernetesVersion
172172
k8sVer, err := version.ParseSemantic(flags.newK8sVersionStr)
173173
if err != nil {
174174
return errors.Errorf("unable to parse normalized version %q as a semantic version", flags.newK8sVersionStr)
175175
}
176176
flags.newK8sVersion = k8sVer
177177

178-
if err := features.ValidateVersion(features.InitFeatureGates, upgradeVars.cfg.FeatureGates, upgradeVars.cfg.KubernetesVersion); err != nil {
178+
if err := features.ValidateVersion(features.InitFeatureGates, cfg.FeatureGates, cfg.KubernetesVersion); err != nil {
179179
return err
180180
}
181181

182182
// Enforce the version skew policies
183183
klog.V(1).Infof("[upgrade/version] enforcing version skew policies")
184-
if err := EnforceVersionPolicies(flags, upgradeVars.versionGetter); err != nil {
184+
if err := EnforceVersionPolicies(flags, versionGetter); err != nil {
185185
return errors.Wrap(err, "[upgrade/version] FATAL")
186186
}
187187

@@ -192,12 +192,14 @@ func runApply(flags *applyFlags) error {
192192
}
193193
}
194194

195+
waiter := getWaiter(flags.dryRun, client)
196+
195197
// Use a prepuller implementation based on creating DaemonSets
196198
// and block until all DaemonSets are ready; then we know for sure that all control plane images are cached locally
197199
klog.V(1).Infof("[upgrade/apply] creating prepuller")
198-
prepuller := upgrade.NewDaemonSetPrepuller(upgradeVars.client, upgradeVars.waiter, &upgradeVars.cfg.ClusterConfiguration)
200+
prepuller := upgrade.NewDaemonSetPrepuller(client, waiter, &cfg.ClusterConfiguration)
199201
componentsToPrepull := constants.MasterComponents
200-
if upgradeVars.cfg.Etcd.External == nil && flags.etcdUpgrade {
202+
if cfg.Etcd.External == nil && flags.etcdUpgrade {
201203
componentsToPrepull = append(componentsToPrepull, constants.Etcd)
202204
}
203205
if err := upgrade.PrepullImagesInParallel(prepuller, flags.imagePullTimeout, componentsToPrepull); err != nil {
@@ -206,13 +208,13 @@ func runApply(flags *applyFlags) error {
206208

207209
// Now; perform the upgrade procedure
208210
klog.V(1).Infof("[upgrade/apply] performing upgrade")
209-
if err := PerformControlPlaneUpgrade(flags, upgradeVars.client, upgradeVars.waiter, upgradeVars.cfg); err != nil {
211+
if err := PerformControlPlaneUpgrade(flags, client, waiter, cfg); err != nil {
210212
return errors.Wrap(err, "[upgrade/apply] FATAL")
211213
}
212214

213215
// Upgrade RBAC rules and addons.
214216
klog.V(1).Infof("[upgrade/postupgrade] upgrading RBAC rules and addons")
215-
if err := upgrade.PerformPostUpgradeTasks(upgradeVars.client, upgradeVars.cfg, flags.newK8sVersion, flags.dryRun); err != nil {
217+
if err := upgrade.PerformPostUpgradeTasks(client, cfg, flags.newK8sVersion, flags.dryRun); err != nil {
216218
return errors.Wrap(err, "[upgrade/postupgrade] FATAL post-upgrade error")
217219
}
218220

cmd/kubeadm/app/cmd/upgrade/common.go

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -42,31 +42,22 @@ import (
4242
kubeconfigutil "k8s.io/kubernetes/cmd/kubeadm/app/util/kubeconfig"
4343
)
4444

45-
// upgradeVariables holds variables needed for performing an upgrade or planning to do so
46-
// TODO - Restructure or rename upgradeVariables
47-
type upgradeVariables struct {
48-
client clientset.Interface
49-
cfg *kubeadmapi.InitConfiguration
50-
versionGetter upgrade.VersionGetter
51-
waiter apiclient.Waiter
52-
}
53-
5445
// enforceRequirements verifies that it's okay to upgrade and then returns the variables needed for the rest of the procedure
55-
func enforceRequirements(flags *applyPlanFlags, dryRun bool, newK8sVersion string) (*upgradeVariables, error) {
46+
func enforceRequirements(flags *applyPlanFlags, dryRun bool, newK8sVersion string) (clientset.Interface, upgrade.VersionGetter, *kubeadmapi.InitConfiguration, error) {
5647

5748
client, err := getClient(flags.kubeConfigPath, dryRun)
5849
if err != nil {
59-
return nil, errors.Wrapf(err, "couldn't create a Kubernetes client from file %q", flags.kubeConfigPath)
50+
return nil, nil, nil, errors.Wrapf(err, "couldn't create a Kubernetes client from file %q", flags.kubeConfigPath)
6051
}
6152

6253
// Check if the cluster is self-hosted
6354
if upgrade.IsControlPlaneSelfHosted(client) {
64-
return nil, errors.Errorf("cannot upgrade a self-hosted control plane")
55+
return nil, nil, nil, errors.Errorf("cannot upgrade a self-hosted control plane")
6556
}
6657

6758
// Run healthchecks against the cluster
6859
if err := upgrade.CheckClusterHealth(client, flags.ignorePreflightErrorsSet); err != nil {
69-
return nil, errors.Wrap(err, "[upgrade/health] FATAL")
60+
return nil, nil, nil, errors.Wrap(err, "[upgrade/health] FATAL")
7061
}
7162

7263
// Fetch the configuration from a file or ConfigMap and validate it
@@ -91,7 +82,7 @@ func enforceRequirements(flags *applyPlanFlags, dryRun bool, newK8sVersion strin
9182
fmt.Println("")
9283
err = errors.Errorf("the ConfigMap %q in the %s namespace used for getting configuration information was not found", constants.KubeadmConfigConfigMap, metav1.NamespaceSystem)
9384
}
94-
return nil, errors.Wrap(err, "[upgrade/config] FATAL")
85+
return nil, nil, nil, errors.Wrap(err, "[upgrade/config] FATAL")
9586
}
9687

9788
// If a new k8s version should be set, apply the change before printing the config
@@ -103,7 +94,7 @@ func enforceRequirements(flags *applyPlanFlags, dryRun bool, newK8sVersion strin
10394
if flags.featureGatesString != "" {
10495
cfg.FeatureGates, err = features.NewFeatureGate(&features.InitFeatureGates, flags.featureGatesString)
10596
if err != nil {
106-
return nil, errors.Wrap(err, "[upgrade/config] FATAL")
97+
return nil, nil, nil, errors.Wrap(err, "[upgrade/config] FATAL")
10798
}
10899
}
109100

@@ -112,22 +103,16 @@ func enforceRequirements(flags *applyPlanFlags, dryRun bool, newK8sVersion strin
112103
for _, m := range msg {
113104
fmt.Printf("[upgrade/config] %s\n", m)
114105
}
115-
return nil, errors.New("[upgrade/config] FATAL. Unable to upgrade a cluster using deprecated feature-gate flags. Please see the release notes")
106+
return nil, nil, nil, errors.New("[upgrade/config] FATAL. Unable to upgrade a cluster using deprecated feature-gate flags. Please see the release notes")
116107
}
117108

118109
// If the user told us to print this information out; do it!
119110
if flags.printConfig {
120111
printConfiguration(&cfg.ClusterConfiguration, os.Stdout)
121112
}
122113

123-
return &upgradeVariables{
124-
client: client,
125-
cfg: cfg,
126-
// Use a real version getter interface that queries the API server, the kubeadm client and the Kubernetes CI system for latest versions
127-
versionGetter: upgrade.NewOfflineVersionGetter(upgrade.NewKubeVersionGetter(client, os.Stdout), newK8sVersion),
128-
// Use the waiter conditionally based on the dryrunning variable
129-
waiter: getWaiter(dryRun, client),
130-
}, nil
114+
// Use a real version getter interface that queries the API server, the kubeadm client and the Kubernetes CI system for latest versions
115+
return client, upgrade.NewOfflineVersionGetter(upgrade.NewKubeVersionGetter(client, os.Stdout), cfg.KubernetesVersion), cfg, nil
131116
}
132117

133118
// printConfiguration prints the external version of the API to yaml

cmd/kubeadm/app/cmd/upgrade/plan.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func runPlan(flags *planFlags) error {
8888
// Start with the basics, verify that the cluster is healthy, build a client and a versionGetter. Never dry-run when planning.
8989
klog.V(1).Infof("[upgrade/plan] verifying health of cluster")
9090
klog.V(1).Infof("[upgrade/plan] retrieving configuration from cluster")
91-
upgradeVars, err := enforceRequirements(flags.applyPlanFlags, false, flags.newK8sVersionStr)
91+
client, versionGetter, cfg, err := enforceRequirements(flags.applyPlanFlags, false, flags.newK8sVersionStr)
9292
if err != nil {
9393
return err
9494
}
@@ -97,20 +97,20 @@ func runPlan(flags *planFlags) error {
9797

9898
// Currently this is the only method we have for distinguishing
9999
// external etcd vs static pod etcd
100-
isExternalEtcd := upgradeVars.cfg.Etcd.External != nil
100+
isExternalEtcd := cfg.Etcd.External != nil
101101
if isExternalEtcd {
102102
client, err := etcdutil.New(
103-
upgradeVars.cfg.Etcd.External.Endpoints,
104-
upgradeVars.cfg.Etcd.External.CAFile,
105-
upgradeVars.cfg.Etcd.External.CertFile,
106-
upgradeVars.cfg.Etcd.External.KeyFile)
103+
cfg.Etcd.External.Endpoints,
104+
cfg.Etcd.External.CAFile,
105+
cfg.Etcd.External.CertFile,
106+
cfg.Etcd.External.KeyFile)
107107
if err != nil {
108108
return err
109109
}
110110
etcdClient = client
111111
} else {
112112
// Connects to local/stacked etcd existing in the cluster
113-
client, err := etcdutil.NewFromCluster(upgradeVars.client, upgradeVars.cfg.CertificatesDir)
113+
client, err := etcdutil.NewFromCluster(client, cfg.CertificatesDir)
114114
if err != nil {
115115
return err
116116
}
@@ -119,7 +119,7 @@ func runPlan(flags *planFlags) error {
119119

120120
// Compute which upgrade possibilities there are
121121
klog.V(1).Infof("[upgrade/plan] computing upgrade possibilities")
122-
availUpgrades, err := upgrade.GetAvailableUpgrades(upgradeVars.versionGetter, flags.allowExperimentalUpgrades, flags.allowRCUpgrades, etcdClient, upgradeVars.cfg.DNS.Type, upgradeVars.client)
122+
availUpgrades, err := upgrade.GetAvailableUpgrades(versionGetter, flags.allowExperimentalUpgrades, flags.allowRCUpgrades, etcdClient, cfg.DNS.Type, client)
123123
if err != nil {
124124
return errors.Wrap(err, "[upgrade/versions] FATAL")
125125
}

0 commit comments

Comments
 (0)