Skip to content

Commit e30d994

Browse files
authored
Merge pull request kubernetes#127242 from SataQiu/refactor-upgrade-node
kubeadm: add addon and post-upgrade phases for 'kubeadm upgrade node'
2 parents abc0568 + 8420b09 commit e30d994

File tree

15 files changed

+131
-220
lines changed

15 files changed

+131
-220
lines changed

cmd/kubeadm/app/cmd/phases/upgrade/apply/addons.go renamed to cmd/kubeadm/app/cmd/phases/upgrade/addons.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
// Package apply implements phases of 'kubeadm upgrade apply'.
18-
package apply
17+
// Package upgrade holds the common phases for 'kubeadm upgrade'.
18+
package upgrade
1919

2020
import (
2121
"fmt"
@@ -28,7 +28,6 @@ import (
2828
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
2929
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/options"
3030
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases/workflow"
31-
cmdutil "k8s.io/kubernetes/cmd/kubeadm/app/cmd/util"
3231
dnsaddon "k8s.io/kubernetes/cmd/kubeadm/app/phases/addons/dns"
3332
proxyaddon "k8s.io/kubernetes/cmd/kubeadm/app/phases/addons/proxy"
3433
"k8s.io/kubernetes/cmd/kubeadm/app/phases/upgrade"
@@ -39,7 +38,6 @@ func NewAddonPhase() workflow.Phase {
3938
return workflow.Phase{
4039
Name: "addon",
4140
Short: "Upgrade the default kubeadm addons",
42-
Long: cmdutil.MacroCommandLongDescription,
4341
Phases: []workflow.Phase{
4442
{
4543
Name: "all",
@@ -98,7 +96,6 @@ func runCoreDNSAddon(c workflow.RunData) error {
9896
return nil
9997
}
10098

101-
// Upgrade CoreDNS
10299
if err := dnsaddon.EnsureDNSAddon(&cfg.ClusterConfiguration, client, patchesDir, out, dryRun); err != nil {
103100
return err
104101
}
@@ -121,7 +118,6 @@ func runKubeProxyAddon(c workflow.RunData) error {
121118
return nil
122119
}
123120

124-
// Upgrade kube-proxy
125121
if err := proxyaddon.EnsureProxyAddon(&cfg.ClusterConfiguration, &cfg.LocalAPIEndpoint, client, out, dryRun); err != nil {
126122
return err
127123
}

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

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,14 @@ limitations under the License.
1818
package apply
1919

2020
import (
21-
"io"
22-
23-
"k8s.io/apimachinery/pkg/util/sets"
24-
clientset "k8s.io/client-go/kubernetes"
25-
26-
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
21+
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases/upgrade"
2722
)
2823

2924
// Data is the interface to use for kubeadm upgrade apply phases.
3025
// The "applyData" type from "cmd/upgrade/apply.go" must satisfy this interface.
3126
type Data interface {
32-
EtcdUpgrade() bool
33-
RenewCerts() bool
34-
DryRun() bool
35-
Cfg() *kubeadmapi.UpgradeConfiguration
36-
InitCfg() *kubeadmapi.InitConfiguration
37-
Client() clientset.Interface
38-
IgnorePreflightErrors() sets.Set[string]
39-
PatchesDir() string
40-
OutputWriter() io.Writer
27+
upgrade.Data
28+
4129
SessionIsInteractive() bool
4230
AllowExperimentalUpgrades() bool
4331
AllowRCUpgrades() bool

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
2929
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/options"
3030
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases/workflow"
31-
cmdutil "k8s.io/kubernetes/cmd/kubeadm/app/cmd/util"
3231
kubeletphase "k8s.io/kubernetes/cmd/kubeadm/app/phases/kubelet"
3332
patchnodephase "k8s.io/kubernetes/cmd/kubeadm/app/phases/patchnode"
3433
"k8s.io/kubernetes/cmd/kubeadm/app/phases/uploadconfig"
@@ -40,7 +39,6 @@ func NewUploadConfigPhase() workflow.Phase {
4039
Name: "upload-config",
4140
Aliases: []string{"uploadconfig"},
4241
Short: "Upload the kubeadm and kubelet configurations to ConfigMaps",
43-
Long: cmdutil.MacroCommandLongDescription,
4442
Phases: []workflow.Phase{
4543
{
4644
Name: "all",
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
Copyright 2024 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Package upgrade holds the common phases for 'kubeadm upgrade'.
18+
package upgrade
19+
20+
import (
21+
"io"
22+
23+
"k8s.io/apimachinery/pkg/util/sets"
24+
clientset "k8s.io/client-go/kubernetes"
25+
26+
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
27+
)
28+
29+
// Data is the common interface to use for kubeadm upgrade phases.
30+
type Data interface {
31+
EtcdUpgrade() bool
32+
RenewCerts() bool
33+
DryRun() bool
34+
Cfg() *kubeadmapi.UpgradeConfiguration
35+
InitCfg() *kubeadmapi.InitConfiguration
36+
Client() clientset.Interface
37+
IgnorePreflightErrors() sets.Set[string]
38+
PatchesDir() string
39+
OutputWriter() io.Writer
40+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
Copyright 2024 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package upgrade
18+
19+
import (
20+
"io"
21+
22+
"k8s.io/apimachinery/pkg/util/sets"
23+
clientset "k8s.io/client-go/kubernetes"
24+
25+
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
26+
)
27+
28+
// a package local type for testing purposes.
29+
type testData struct{}
30+
31+
// testData must satisfy Data.
32+
var _ Data = &testData{}
33+
34+
func (t *testData) EtcdUpgrade() bool { return false }
35+
func (t *testData) RenewCerts() bool { return false }
36+
func (t *testData) DryRun() bool { return false }
37+
func (t *testData) Cfg() *kubeadmapi.UpgradeConfiguration { return nil }
38+
func (t *testData) InitCfg() *kubeadmapi.InitConfiguration { return nil }
39+
func (t *testData) Client() clientset.Interface { return nil }
40+
func (t *testData) IgnorePreflightErrors() sets.Set[string] { return nil }
41+
func (t *testData) PatchesDir() string { return "" }
42+
func (t *testData) OutputWriter() io.Writer { return nil }

cmd/kubeadm/app/cmd/phases/upgrade/apply/kubeletconfig.go renamed to cmd/kubeadm/app/cmd/phases/upgrade/kubeletconfig.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
// Package apply implements phases of 'kubeadm upgrade apply'.
18-
package apply
17+
// Package upgrade holds the common phases for 'kubeadm upgrade'.
18+
package upgrade
1919

2020
import (
2121
"fmt"
@@ -57,16 +57,14 @@ func runKubeletConfigPhase(c workflow.RunData) error {
5757
return errors.New("kubelet-config phase invoked with an invalid data struct")
5858
}
5959

60-
initCfg, dryRun := data.InitCfg(), data.DryRun()
61-
6260
// Write the configuration for the kubelet down to disk and print the generated manifests instead of dry-running.
6361
// If not dry-running, the kubelet config file will be backed up to the /etc/kubernetes/tmp/ dir, so that it could be
6462
// recovered if anything goes wrong.
65-
err := upgrade.WriteKubeletConfigFiles(initCfg, data.PatchesDir(), dryRun, data.OutputWriter())
63+
err := upgrade.WriteKubeletConfigFiles(data.InitCfg(), data.PatchesDir(), data.DryRun(), data.OutputWriter())
6664
if err != nil {
6765
return err
6866
}
6967

70-
fmt.Println("[upgrade/kubelet-config] The kubelet configuration for this node was successfully updated!")
68+
fmt.Println("[upgrade/kubelet-config] The kubelet configuration for this node was successfully upgraded!")
7169
return nil
7270
}

cmd/kubeadm/app/cmd/phases/upgrade/node/controlplane.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17+
// Package node implements phases of 'kubeadm upgrade node'.
1718
package node
1819

1920
import (
@@ -28,13 +29,14 @@ import (
2829
"k8s.io/kubernetes/cmd/kubeadm/app/util/apiclient"
2930
)
3031

31-
// NewControlPlane creates a kubeadm workflow phase that implements handling of control-plane upgrade.
32+
// NewControlPlane returns a new control-plane phase.
3233
func NewControlPlane() workflow.Phase {
3334
phase := workflow.Phase{
3435
Name: "control-plane",
3536
Short: "Upgrade the control plane instance deployed on this node, if any",
3637
Run: runControlPlane(),
3738
InheritFlags: []string{
39+
options.CfgPath,
3840
options.DryRun,
3941
options.KubeconfigPath,
4042
options.CertificateRenewal,
@@ -54,7 +56,7 @@ func runControlPlane() func(c workflow.RunData) error {
5456

5557
// if this is not a control-plane node, this phase should not be executed
5658
if !data.IsControlPlaneNode() {
57-
fmt.Println("[upgrade] Skipping phase. Not a control plane node.")
59+
fmt.Println("[upgrade/control-plane] Skipping phase. Not a control plane node.")
5860
return nil
5961
}
6062

@@ -67,8 +69,9 @@ func runControlPlane() func(c workflow.RunData) error {
6769
patchesDir := data.PatchesDir()
6870

6971
// Upgrade the control plane and etcd if installed on this node
70-
fmt.Printf("[upgrade] Upgrading your Static Pod-hosted control plane instance to version %q...\n", cfg.KubernetesVersion)
72+
fmt.Printf("[upgrade/control-plane] Upgrading your Static Pod-hosted control plane instance to version %q...\n", cfg.KubernetesVersion)
7173
if dryRun {
74+
fmt.Printf("[dryrun] Would upgrade your static Pod-hosted control plane to version %q", cfg.KubernetesVersion)
7275
return upgrade.DryRunStaticPodUpgrade(patchesDir, cfg)
7376
}
7477

@@ -78,11 +81,7 @@ func runControlPlane() func(c workflow.RunData) error {
7881
return errors.Wrap(err, "couldn't complete the static pod upgrade")
7982
}
8083

81-
if err := upgrade.PerformAddonsUpgrade(client, cfg, data.PatchesDir(), data.OutputWriter()); err != nil {
82-
return errors.Wrap(err, "failed to perform addons upgrade")
83-
}
84-
85-
fmt.Println("[upgrade] The control plane instance for this node was successfully updated!")
84+
fmt.Println("[upgrade/control-plane] The control plane instance for this node was successfully upgraded!")
8685

8786
return nil
8887
}

cmd/kubeadm/app/cmd/phases/upgrade/node/data.go

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,18 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17+
// Package node implements phases of 'kubeadm upgrade node'.
1718
package node
1819

1920
import (
20-
"io"
21-
22-
"k8s.io/apimachinery/pkg/util/sets"
23-
clientset "k8s.io/client-go/kubernetes"
24-
25-
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
21+
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases/upgrade"
2622
)
2723

2824
// Data is the interface to use for kubeadm upgrade node phases.
2925
// The "nodeData" type from "cmd/upgrade/node.go" must satisfy this interface.
3026
type Data interface {
31-
EtcdUpgrade() bool
32-
RenewCerts() bool
33-
DryRun() bool
34-
Cfg() *kubeadmapi.UpgradeConfiguration
35-
InitCfg() *kubeadmapi.InitConfiguration
27+
upgrade.Data
28+
3629
IsControlPlaneNode() bool
37-
Client() clientset.Interface
38-
IgnorePreflightErrors() sets.Set[string]
39-
PatchesDir() string
4030
KubeConfigPath() string
41-
OutputWriter() io.Writer
4231
}

cmd/kubeadm/app/cmd/phases/upgrade/node/kubeconfig.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
// Package node holds phases of "kubeadm upgrade node".
17+
// Package node implements phases of 'kubeadm upgrade node'.
1818
package node
1919

2020
import (
@@ -28,14 +28,15 @@ import (
2828
"k8s.io/kubernetes/cmd/kubeadm/app/phases/upgrade"
2929
)
3030

31-
// NewKubeconfigPhase creates a kubeadm workflow phase that implements handling of kubeconfig upgrade.
31+
// NewKubeconfigPhase returns a new kubeconfig phase.
3232
func NewKubeconfigPhase() workflow.Phase {
3333
phase := workflow.Phase{
3434
Name: "kubeconfig",
3535
Short: "Upgrade kubeconfig files for this node",
3636
Run: runKubeconfig(),
3737
Hidden: true,
3838
InheritFlags: []string{
39+
options.CfgPath,
3940
options.DryRun,
4041
options.KubeconfigPath,
4142
},
@@ -50,23 +51,22 @@ func runKubeconfig() func(c workflow.RunData) error {
5051
return errors.New("kubeconfig phase invoked with an invalid data struct")
5152
}
5253

53-
// if this is not a control-plane node, this phase should not be executed
54+
// If this is not a control-plane node, this phase should not be executed.
5455
if !data.IsControlPlaneNode() {
55-
fmt.Println("[upgrade] Skipping phase. Not a control plane node.")
56+
fmt.Println("[upgrade/kubeconfig] Skipping phase. Not a control plane node.")
5657
return nil
5758
}
5859

59-
// otherwise, retrieve all the info required for kubeconfig upgrade
60+
// Otherwise, retrieve all the info required for kubeconfig upgrade.
6061
cfg := data.InitCfg()
61-
dryRun := data.DryRun()
6262

6363
if features.Enabled(cfg.FeatureGates, features.ControlPlaneKubeletLocalMode) {
64-
if err := upgrade.UpdateKubeletLocalMode(cfg, dryRun); err != nil {
64+
if err := upgrade.UpdateKubeletLocalMode(cfg, data.DryRun()); err != nil {
6565
return errors.Wrap(err, "failed to update kubelet local mode")
6666
}
6767
}
6868

69-
fmt.Println("[upgrade] The kubeconfig for this node was successfully updated!")
69+
fmt.Println("[upgrade/kubeconfig] The kubeconfig files for this node were successfully upgraded!")
7070

7171
return nil
7272
}

0 commit comments

Comments
 (0)