Skip to content

Commit 6ac76f9

Browse files
author
Dmitry Rozhkov
committed
kubeadm: restructure upgradeVariables
1 parent 836ce9f commit 6ac76f9

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
@@ -153,37 +153,37 @@ func runApply(flags *applyFlags) error {
153153
// Start with the basics, verify that the cluster is healthy and get the configuration from the cluster (using the ConfigMap)
154154
klog.V(1).Infof("[upgrade/apply] verifying health of cluster")
155155
klog.V(1).Infof("[upgrade/apply] retrieving configuration from cluster")
156-
upgradeVars, err := enforceRequirements(flags.applyPlanFlags, flags.dryRun, flags.newK8sVersionStr)
156+
client, versionGetter, cfg, err := enforceRequirements(flags.applyPlanFlags, flags.dryRun, flags.newK8sVersionStr)
157157
if err != nil {
158158
return err
159159
}
160160

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

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

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

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

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

@@ -194,12 +194,14 @@ func runApply(flags *applyFlags) error {
194194
}
195195
}
196196

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

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

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

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
@@ -84,7 +75,7 @@ func enforceRequirements(flags *applyPlanFlags, dryRun bool, newK8sVersion strin
8475
fmt.Println("")
8576
err = errors.Errorf("the ConfigMap %q in the %s namespace used for getting configuration information was not found", constants.KubeadmConfigConfigMap, metav1.NamespaceSystem)
8677
}
87-
return nil, errors.Wrap(err, "[upgrade/config] FATAL")
78+
return nil, nil, nil, errors.Wrap(err, "[upgrade/config] FATAL")
8879
}
8980

9081
// If a new k8s version should be set, apply the change before printing the config
@@ -96,7 +87,7 @@ func enforceRequirements(flags *applyPlanFlags, dryRun bool, newK8sVersion strin
9687
if flags.featureGatesString != "" {
9788
cfg.FeatureGates, err = features.NewFeatureGate(&features.InitFeatureGates, flags.featureGatesString)
9889
if err != nil {
99-
return nil, errors.Wrap(err, "[upgrade/config] FATAL")
90+
return nil, nil, nil, errors.Wrap(err, "[upgrade/config] FATAL")
10091
}
10192
}
10293

@@ -105,22 +96,16 @@ func enforceRequirements(flags *applyPlanFlags, dryRun bool, newK8sVersion strin
10596
for _, m := range msg {
10697
fmt.Printf("[upgrade/config] %s\n", m)
10798
}
108-
return nil, errors.New("[upgrade/config] FATAL. Unable to upgrade a cluster using deprecated feature-gate flags. Please see the release notes")
99+
return nil, nil, nil, errors.New("[upgrade/config] FATAL. Unable to upgrade a cluster using deprecated feature-gate flags. Please see the release notes")
109100
}
110101

111102
// If the user told us to print this information out; do it!
112103
if flags.printConfig {
113104
printConfiguration(&cfg.ClusterConfiguration, os.Stdout)
114105
}
115106

116-
return &upgradeVariables{
117-
client: client,
118-
cfg: cfg,
119-
// Use a real version getter interface that queries the API server, the kubeadm client and the Kubernetes CI system for latest versions
120-
versionGetter: upgrade.NewOfflineVersionGetter(upgrade.NewKubeVersionGetter(client, os.Stdout), newK8sVersion),
121-
// Use the waiter conditionally based on the dryrunning variable
122-
waiter: getWaiter(dryRun, client),
123-
}, nil
107+
// Use a real version getter interface that queries the API server, the kubeadm client and the Kubernetes CI system for latest versions
108+
return client, upgrade.NewOfflineVersionGetter(upgrade.NewKubeVersionGetter(client, os.Stdout), cfg.KubernetesVersion), cfg, nil
124109
}
125110

126111
// 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
@@ -90,7 +90,7 @@ func runPlan(flags *planFlags) error {
9090
// Start with the basics, verify that the cluster is healthy, build a client and a versionGetter. Never dry-run when planning.
9191
klog.V(1).Infof("[upgrade/plan] verifying health of cluster")
9292
klog.V(1).Infof("[upgrade/plan] retrieving configuration from cluster")
93-
upgradeVars, err := enforceRequirements(flags.applyPlanFlags, false, flags.newK8sVersionStr)
93+
client, versionGetter, cfg, err := enforceRequirements(flags.applyPlanFlags, false, flags.newK8sVersionStr)
9494
if err != nil {
9595
return err
9696
}
@@ -99,20 +99,20 @@ func runPlan(flags *planFlags) error {
9999

100100
// Currently this is the only method we have for distinguishing
101101
// external etcd vs static pod etcd
102-
isExternalEtcd := upgradeVars.cfg.Etcd.External != nil
102+
isExternalEtcd := cfg.Etcd.External != nil
103103
if isExternalEtcd {
104104
client, err := etcdutil.New(
105-
upgradeVars.cfg.Etcd.External.Endpoints,
106-
upgradeVars.cfg.Etcd.External.CAFile,
107-
upgradeVars.cfg.Etcd.External.CertFile,
108-
upgradeVars.cfg.Etcd.External.KeyFile)
105+
cfg.Etcd.External.Endpoints,
106+
cfg.Etcd.External.CAFile,
107+
cfg.Etcd.External.CertFile,
108+
cfg.Etcd.External.KeyFile)
109109
if err != nil {
110110
return err
111111
}
112112
etcdClient = client
113113
} else {
114114
// Connects to local/stacked etcd existing in the cluster
115-
client, err := etcdutil.NewFromCluster(upgradeVars.client, upgradeVars.cfg.CertificatesDir)
115+
client, err := etcdutil.NewFromCluster(client, cfg.CertificatesDir)
116116
if err != nil {
117117
return err
118118
}
@@ -121,7 +121,7 @@ func runPlan(flags *planFlags) error {
121121

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

0 commit comments

Comments
 (0)