Skip to content

Commit deecaf7

Browse files
authored
Merge pull request kubernetes#128763 from srivastav-abhishek/fix-err-string
Fixed failing UT TestWriteKubeletConfigFiles by removing privilege check and adding proper error handling
2 parents 5ee686b + 56e3c78 commit deecaf7

File tree

5 files changed

+31
-98
lines changed

5 files changed

+31
-98
lines changed

Diff for: cmd/kubeadm/app/cmd/phases/upgrade/kubeletconfig.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ import (
2828
"k8s.io/kubernetes/cmd/kubeadm/app/phases/upgrade"
2929
)
3030

31+
const (
32+
kubeletConfigDir = ""
33+
)
34+
3135
var (
3236
kubeletConfigLongDesc = cmdutil.LongDesc(`
3337
Upgrade the kubelet configuration for this node by downloading it from the kubelet-config ConfigMap stored in the cluster
@@ -60,7 +64,7 @@ func runKubeletConfigPhase(c workflow.RunData) error {
6064
// Write the configuration for the kubelet down to disk and print the generated manifests instead of dry-running.
6165
// If not dry-running, the kubelet config file will be backed up to the /etc/kubernetes/tmp/ dir, so that it could be
6266
// recovered if anything goes wrong.
63-
err := upgrade.WriteKubeletConfigFiles(data.InitCfg(), data.PatchesDir(), data.DryRun(), data.OutputWriter())
67+
err := upgrade.WriteKubeletConfigFiles(data.InitCfg(), kubeletConfigDir, data.PatchesDir(), data.DryRun(), data.OutputWriter())
6468
if err != nil {
6569
return err
6670
}

Diff for: cmd/kubeadm/app/phases/upgrade/postupgrade.go

+11-14
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,21 @@ func UnupgradedControlPlaneInstances(client clientset.Interface, nodeName string
9696
}
9797

9898
// WriteKubeletConfigFiles writes the kubelet config file to disk, but first creates a backup of any existing one.
99-
func WriteKubeletConfigFiles(cfg *kubeadmapi.InitConfiguration, patchesDir string, dryRun bool, out io.Writer) error {
100-
// Set up the kubelet directory to use. If dry-running, this will return a fake directory
101-
kubeletDir, err := GetKubeletDir(dryRun)
99+
func WriteKubeletConfigFiles(cfg *kubeadmapi.InitConfiguration, kubeletConfigDir string, patchesDir string, dryRun bool, out io.Writer) error {
100+
var (
101+
err error
102+
kubeletDir = kubeadmconstants.KubeletRunDirectory
103+
)
104+
// If dry-running, this will return a directory under /etc/kubernetes/tmp or kubeletConfigDir.
105+
if dryRun {
106+
kubeletDir, err = kubeadmconstants.CreateTempDir(kubeletConfigDir, "kubeadm-upgrade-dryrun")
107+
}
102108
if err != nil {
103109
// The error here should never occur in reality, would only be thrown if /tmp doesn't exist on the machine.
104110
return err
105111
}
106-
107-
// Create a copy of the kubelet config file in the /etc/kubernetes/tmp/ folder.
108-
backupDir, err := kubeadmconstants.CreateTempDir("", "kubeadm-kubelet-config")
112+
// Create a copy of the kubelet config file in the /etc/kubernetes/tmp or kubeletConfigDir.
113+
backupDir, err := kubeadmconstants.CreateTempDir(kubeletConfigDir, "kubeadm-kubelet-config")
109114
if err != nil {
110115
return err
111116
}
@@ -178,14 +183,6 @@ func WriteKubeletConfigFiles(cfg *kubeadmapi.InitConfiguration, patchesDir strin
178183
return nil
179184
}
180185

181-
// GetKubeletDir gets the kubelet directory based on whether the user is dry-running this command or not.
182-
func GetKubeletDir(dryRun bool) (string, error) {
183-
if dryRun {
184-
return kubeadmconstants.CreateTempDir("", "kubeadm-upgrade-dryrun")
185-
}
186-
return kubeadmconstants.KubeletRunDirectory, nil
187-
}
188-
189186
// UpdateKubeletLocalMode changes the Server URL in the kubelets kubeconfig to the local API endpoint if it is currently
190187
// set to the ControlPlaneEndpoint.
191188
// TODO: remove this function once kubeletKubeConfigFilePath goes GA and is hardcoded to enabled by default:

Diff for: cmd/kubeadm/app/phases/upgrade/postupgrade_others_test.go

-24
This file was deleted.

Diff for: cmd/kubeadm/app/phases/upgrade/postupgrade_test.go

+15-35
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"os"
2121
"path/filepath"
2222
"reflect"
23-
"regexp"
2423
"strings"
2524
"testing"
2625

@@ -35,7 +34,6 @@ import (
3534
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
3635
"k8s.io/kubernetes/cmd/kubeadm/app/componentconfigs"
3736
"k8s.io/kubernetes/cmd/kubeadm/app/constants"
38-
"k8s.io/kubernetes/cmd/kubeadm/app/preflight"
3937
testutil "k8s.io/kubernetes/cmd/kubeadm/test"
4038
)
4139

@@ -114,26 +112,15 @@ func TestRollbackFiles(t *testing.T) {
114112
}
115113

116114
func TestWriteKubeletConfigFiles(t *testing.T) {
117-
// exit early if the user doesn't have root permission as the test needs to create /etc/kubernetes directory
118-
// while the permission should be granted to the user.
119-
isPrivileged := preflight.IsPrivilegedUserCheck{}
120-
if _, err := isPrivileged.Check(); err != nil {
121-
return
122-
}
115+
tempDir := t.TempDir()
123116
testCases := []struct {
124-
name string
125-
dryrun bool
126-
patchesDir string
127-
errPattern string
128-
cfg *kubeadmapi.InitConfiguration
117+
name string
118+
patchesDir string
119+
expectedError bool
120+
cfg *kubeadmapi.InitConfiguration
129121
}{
130-
// Be careful that if the dryrun is set to false and the test is run on a live cluster, the kubelet config file might be overwritten.
131-
// However, you should be able to find the original config file in /etc/kubernetes/tmp/kubeadm-kubelet-configxxx folder.
132-
// The test haven't clean up the temporary file created under /etc/kubernetes/tmp/ as that could be accidentally delete other files in
133-
// that folder as well which might be unexpected.
134122
{
135-
name: "write kubelet config file successfully",
136-
dryrun: true,
123+
name: "write kubelet config file successfully",
137124
cfg: &kubeadmapi.InitConfiguration{
138125
ClusterConfiguration: kubeadmapi.ClusterConfiguration{
139126
ComponentConfigs: kubeadmapi.ComponentConfigMap{
@@ -143,16 +130,14 @@ func TestWriteKubeletConfigFiles(t *testing.T) {
143130
},
144131
},
145132
{
146-
name: "aggregate errs: no kubelet config file and cannot read config file",
147-
dryrun: true,
148-
errPattern: missingKubeletConfig,
149-
cfg: &kubeadmapi.InitConfiguration{},
133+
name: "aggregate errs: no kubelet config file and cannot read config file",
134+
expectedError: true,
135+
cfg: &kubeadmapi.InitConfiguration{},
150136
},
151137
{
152-
name: "only one err: patch dir does not exist",
153-
dryrun: true,
154-
patchesDir: "Bogus",
155-
errPattern: "could not list patch files for path \"Bogus\"",
138+
name: "only one err: patch dir does not exist",
139+
patchesDir: "Bogus",
140+
expectedError: true,
156141
cfg: &kubeadmapi.InitConfiguration{
157142
ClusterConfiguration: kubeadmapi.ClusterConfiguration{
158143
ComponentConfigs: kubeadmapi.ComponentConfigMap{
@@ -163,14 +148,9 @@ func TestWriteKubeletConfigFiles(t *testing.T) {
163148
},
164149
}
165150
for _, tc := range testCases {
166-
err := WriteKubeletConfigFiles(tc.cfg, tc.patchesDir, tc.dryrun, os.Stdout)
167-
if err != nil && tc.errPattern != "" {
168-
if match, _ := regexp.MatchString(tc.errPattern, err.Error()); !match {
169-
t.Fatalf("Expected error contains %q, got %v", tc.errPattern, err.Error())
170-
}
171-
}
172-
if err == nil && len(tc.errPattern) != 0 {
173-
t.Fatalf("WriteKubeletConfigFiles didn't return error expected %s", tc.errPattern)
151+
err := WriteKubeletConfigFiles(tc.cfg, tempDir, tc.patchesDir, true, os.Stdout)
152+
if (err != nil) != tc.expectedError {
153+
t.Fatalf("expected error: %v, got: %v, error: %v", tc.expectedError, err != nil, err)
174154
}
175155
}
176156
}

Diff for: cmd/kubeadm/app/phases/upgrade/postupgrade_windows_test.go

-24
This file was deleted.

0 commit comments

Comments
 (0)