kubeadm: include the err got from PrintDryRunFile

The error was ingored which means if anything wrong from `PrintDryRunFiles`,
it was sliently ignored.

Signed-off-by: Dave Chen <dave.chen@arm.com>
This commit is contained in:
Dave Chen 2022-12-26 16:58:59 +08:00
parent c55a98fde9
commit 9e6e13e562
2 changed files with 106 additions and 1 deletions

View File

@ -166,7 +166,7 @@ func writeKubeletConfigFiles(client clientset.Interface, cfg *kubeadmapi.InitCon
}
if dryRun { // Print what contents would be written
dryrunutil.PrintDryRunFile(kubeadmconstants.KubeletConfigurationFileName, kubeletDir, kubeadmconstants.KubeletRunDirectory, os.Stdout)
errs = append(errs, dryrunutil.PrintDryRunFile(kubeadmconstants.KubeletConfigurationFileName, kubeletDir, kubeadmconstants.KubeletRunDirectory, os.Stdout))
}
return errorsutil.NewAggregate(errs)
}

View File

@ -19,12 +19,16 @@ package upgrade
import (
"os"
"path/filepath"
"regexp"
"strings"
"testing"
"github.com/pkg/errors"
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
"k8s.io/kubernetes/cmd/kubeadm/app/componentconfigs"
"k8s.io/kubernetes/cmd/kubeadm/app/constants"
"k8s.io/kubernetes/cmd/kubeadm/app/preflight"
testutil "k8s.io/kubernetes/cmd/kubeadm/test"
)
@ -101,3 +105,104 @@ func TestRollbackFiles(t *testing.T) {
t.Fatalf("Expected error contains %q, got %v", errString, err)
}
}
func TestWriteKubeletConfigFiles(t *testing.T) {
// exit early if the user doesn't have root permission as the test needs to create /etc/kubernetes directory
// while the permission should be granted to the user.
isPrivileged := preflight.IsPrivilegedUserCheck{}
if _, err := isPrivileged.Check(); err != nil {
return
}
testCases := []struct {
name string
dryrun bool
patchesDir string
errPattern string
cfg *kubeadmapi.InitConfiguration
}{
// 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.
// However, you should be able to find the original config file in /etc/kubernetes/tmp/kubeadm-kubelet-configxxx folder.
// The test haven't clean up the temporary file created under /etc/kubernetes/tmp/ as that could be accidentally delete other files in
// that folder as well which might be unexpected.
{
name: "write kubelet config file successfully",
dryrun: true,
cfg: &kubeadmapi.InitConfiguration{
ClusterConfiguration: kubeadmapi.ClusterConfiguration{
ComponentConfigs: kubeadmapi.ComponentConfigMap{
componentconfigs.KubeletGroup: &componentConfig{},
},
},
},
},
{
name: "aggregate errs: no kubelet config file and cannot read config file",
dryrun: true,
errPattern: "no kubelet component config found.*no such file or directory",
cfg: &kubeadmapi.InitConfiguration{},
},
{
name: "only one err: patch dir does not exist",
dryrun: true,
patchesDir: "Bogus",
errPattern: "could not list patch files for path \"Bogus\"",
cfg: &kubeadmapi.InitConfiguration{
ClusterConfiguration: kubeadmapi.ClusterConfiguration{
ComponentConfigs: kubeadmapi.ComponentConfigMap{
componentconfigs.KubeletGroup: &componentConfig{},
},
},
},
},
}
for _, tc := range testCases {
err := writeKubeletConfigFiles(nil, tc.cfg, tc.patchesDir, tc.dryrun, os.Stdout)
if err != nil && tc.errPattern != "" {
if match, _ := regexp.MatchString(tc.errPattern, err.Error()); !match {
t.Fatalf("Expected error contains %q, got %v", tc.errPattern, err.Error())
}
}
if err == nil && len(tc.errPattern) != 0 {
t.Fatalf("writeKubeletConfigFiles didn't return error expected %s", tc.errPattern)
}
}
}
// Just some stub code, the code could be enriched when necessary.
type componentConfig struct {
userSupplied bool
}
func (cc *componentConfig) DeepCopy() kubeadmapi.ComponentConfig {
result := &componentConfig{}
return result
}
func (cc *componentConfig) Marshal() ([]byte, error) {
return nil, nil
}
func (cc *componentConfig) Unmarshal(docmap kubeadmapi.DocumentMap) error {
return nil
}
func (cc *componentConfig) Get() interface{} {
return &cc
}
func (cc *componentConfig) Set(cfg interface{}) {
}
func (cc *componentConfig) Default(_ *kubeadmapi.ClusterConfiguration, _ *kubeadmapi.APIEndpoint, _ *kubeadmapi.NodeRegistrationOptions) {
}
func (cc *componentConfig) Mutate() error {
return nil
}
func (cc *componentConfig) IsUserSupplied() bool {
return false
}
func (cc *componentConfig) SetUserSupplied(userSupplied bool) {
cc.userSupplied = userSupplied
}