From 8c7609fbe2963363316989b71f9fd3d17f0e8a94 Mon Sep 17 00:00:00 2001 From: Dave Chen Date: Mon, 31 Oct 2022 15:19:17 +0800 Subject: [PATCH] kubeadm: bump to use golang generic Signed-off-by: Dave Chen --- .../app/apis/kubeadm/validation/validation.go | 10 ++--- .../kubeadm/validation/validation_test.go | 22 +++++------ cmd/kubeadm/app/cmd/init.go | 6 +-- cmd/kubeadm/app/cmd/init_test.go | 6 +-- cmd/kubeadm/app/cmd/join.go | 6 +-- cmd/kubeadm/app/cmd/join_test.go | 12 +++--- cmd/kubeadm/app/cmd/phases/init/data.go | 2 +- cmd/kubeadm/app/cmd/phases/init/data_test.go | 38 +++++++++---------- cmd/kubeadm/app/cmd/phases/join/data.go | 2 +- cmd/kubeadm/app/cmd/phases/join/data_test.go | 2 +- cmd/kubeadm/app/cmd/phases/reset/data.go | 2 +- cmd/kubeadm/app/cmd/phases/reset/data_test.go | 18 ++++----- .../app/cmd/phases/upgrade/node/data.go | 2 +- .../app/cmd/phases/upgrade/node/data_test.go | 20 +++++----- cmd/kubeadm/app/cmd/reset.go | 6 +-- cmd/kubeadm/app/cmd/reset_test.go | 10 ++--- cmd/kubeadm/app/cmd/upgrade/apply.go | 4 +- cmd/kubeadm/app/cmd/upgrade/common.go | 4 +- cmd/kubeadm/app/cmd/upgrade/node.go | 7 ++-- .../app/phases/controlplane/manifests_test.go | 4 +- .../app/phases/controlplane/volumes.go | 6 +-- cmd/kubeadm/app/phases/upgrade/health.go | 2 +- cmd/kubeadm/app/phases/upgrade/preflight.go | 2 +- cmd/kubeadm/app/preflight/checks.go | 12 +++--- cmd/kubeadm/app/preflight/checks_test.go | 16 ++++---- cmd/kubeadm/app/util/pkiutil/pki_helpers.go | 4 +- 26 files changed, 112 insertions(+), 113 deletions(-) diff --git a/cmd/kubeadm/app/apis/kubeadm/validation/validation.go b/cmd/kubeadm/app/apis/kubeadm/validation/validation.go index e8076530aa0..4b6bf334ba0 100644 --- a/cmd/kubeadm/app/apis/kubeadm/validation/validation.go +++ b/cmd/kubeadm/app/apis/kubeadm/validation/validation.go @@ -238,7 +238,7 @@ func ValidateTokenGroups(usages []string, groups []string, fldPath *field.Path) allErrs := field.ErrorList{} // adding groups only makes sense for authentication - usagesSet := sets.NewString(usages...) + usagesSet := sets.New(usages...) usageAuthentication := strings.TrimPrefix(bootstrapapi.BootstrapTokenUsageAuthentication, bootstrapapi.BootstrapTokenUsagePrefix) if len(groups) > 0 && !usagesSet.Has(usageAuthentication) { allErrs = append(allErrs, field.Invalid(fldPath, groups, fmt.Sprintf("token groups cannot be specified unless --usages includes %q", usageAuthentication))) @@ -564,7 +564,7 @@ func ValidateMixedArguments(flag *pflag.FlagSet) error { } func isAllowedFlag(flagName string) bool { - allowedFlags := sets.NewString(kubeadmcmdoptions.CfgPath, + allowedFlags := sets.New(kubeadmcmdoptions.CfgPath, kubeadmcmdoptions.IgnorePreflightErrors, kubeadmcmdoptions.DryRun, kubeadmcmdoptions.KubeconfigPath, @@ -603,8 +603,8 @@ func ValidateAPIEndpoint(c *kubeadm.APIEndpoint, fldPath *field.Path) field.Erro // ValidateIgnorePreflightErrors validates duplicates in: // - ignore-preflight-errors flag and // - ignorePreflightErrors field in {Init,Join}Configuration files. -func ValidateIgnorePreflightErrors(ignorePreflightErrorsFromCLI, ignorePreflightErrorsFromConfigFile []string) (sets.String, error) { - ignoreErrors := sets.NewString() +func ValidateIgnorePreflightErrors(ignorePreflightErrorsFromCLI, ignorePreflightErrorsFromConfigFile []string) (sets.Set[string], error) { + ignoreErrors := sets.New[string]() allErrs := field.ErrorList{} for _, item := range ignorePreflightErrorsFromConfigFile { @@ -623,7 +623,7 @@ func ValidateIgnorePreflightErrors(ignorePreflightErrorsFromCLI, ignorePreflight } if ignoreErrors.Has("all") && ignoreErrors.Len() > 1 { - allErrs = append(allErrs, field.Invalid(field.NewPath("ignore-preflight-errors"), strings.Join(ignoreErrors.List(), ","), "don't specify individual checks if 'all' is used")) + allErrs = append(allErrs, field.Invalid(field.NewPath("ignore-preflight-errors"), strings.Join(sets.List(ignoreErrors), ","), "don't specify individual checks if 'all' is used")) } return ignoreErrors, allErrs.ToAggregate() diff --git a/cmd/kubeadm/app/apis/kubeadm/validation/validation_test.go b/cmd/kubeadm/app/apis/kubeadm/validation/validation_test.go index a99a3f5b066..89fe51e62be 100644 --- a/cmd/kubeadm/app/apis/kubeadm/validation/validation_test.go +++ b/cmd/kubeadm/app/apis/kubeadm/validation/validation_test.go @@ -761,67 +761,67 @@ func TestValidateIgnorePreflightErrors(t *testing.T) { var tests = []struct { ignorePreflightErrorsFromCLI []string ignorePreflightErrorsFromConfigFile []string - expectedSet sets.String + expectedSet sets.Set[string] expectedError bool }{ { // empty lists in CLI and config file []string{}, []string{}, - sets.NewString(), + sets.New[string](), false, }, { // empty list in CLI only []string{}, []string{"a"}, - sets.NewString("a"), + sets.New("a"), false, }, { // empty list in config file only []string{"a"}, []string{}, - sets.NewString("a"), + sets.New("a"), false, }, { // no duplicates, no overlap []string{"a", "b"}, []string{"c", "d"}, - sets.NewString("a", "b", "c", "d"), + sets.New("a", "b", "c", "d"), false, }, { // some duplicates, with some overlapping duplicates []string{"a", "b", "a"}, []string{"c", "b"}, - sets.NewString("a", "b", "c"), + sets.New("a", "b", "c"), false, }, { // non-duplicate, but 'all' present together with individual checks in CLI []string{"a", "b", "all"}, []string{}, - sets.NewString(), + sets.New[string](), true, }, { // empty list in CLI, but 'all' present in config file, which is forbidden []string{}, []string{"all"}, - sets.NewString(), + sets.New[string](), true, }, { // non-duplicate, but 'all' present in config file, which is forbidden []string{"a", "b"}, []string{"all"}, - sets.NewString(), + sets.New[string](), true, }, { // non-duplicate, but 'all' present in CLI, while values are in config file, which is forbidden []string{"all"}, []string{"a", "b"}, - sets.NewString(), + sets.New[string](), true, }, { // skip all checks []string{"all"}, []string{}, - sets.NewString("all"), + sets.New("all"), false, }, } diff --git a/cmd/kubeadm/app/cmd/init.go b/cmd/kubeadm/app/cmd/init.go index dc03e0490e4..4399364574a 100644 --- a/cmd/kubeadm/app/cmd/init.go +++ b/cmd/kubeadm/app/cmd/init.go @@ -76,7 +76,7 @@ type initData struct { dryRun bool kubeconfigDir string kubeconfigPath string - ignorePreflightErrors sets.String + ignorePreflightErrors sets.Set[string] certificatesDir string dryRunDir string externalCA bool @@ -307,7 +307,7 @@ func newInitData(cmd *cobra.Command, args []string, options *initOptions, out io return nil, err } // Also set the union of pre-flight errors to InitConfiguration, to provide a consistent view of the runtime configuration: - cfg.NodeRegistration.IgnorePreflightErrors = ignorePreflightErrorsSet.List() + cfg.NodeRegistration.IgnorePreflightErrors = sets.List(ignorePreflightErrorsSet) // override node name from the command line option if options.externalInitCfg.NodeRegistration.Name != "" { @@ -416,7 +416,7 @@ func (d *initData) SkipTokenPrint() bool { } // IgnorePreflightErrors returns the IgnorePreflightErrors flag. -func (d *initData) IgnorePreflightErrors() sets.String { +func (d *initData) IgnorePreflightErrors() sets.Set[string] { return d.ignorePreflightErrors } diff --git a/cmd/kubeadm/app/cmd/init_test.go b/cmd/kubeadm/app/cmd/init_test.go index 840d3d70bbc..b3f12e0e014 100644 --- a/cmd/kubeadm/app/cmd/init_test.go +++ b/cmd/kubeadm/app/cmd/init_test.go @@ -174,13 +174,13 @@ func TestNewInitData(t *testing.T) { } func expectedInitIgnorePreflightErrors(expectedItems ...string) func(t *testing.T, data *initData) { - expected := sets.NewString(expectedItems...) + expected := sets.New(expectedItems...) return func(t *testing.T, data *initData) { if !expected.Equal(data.ignorePreflightErrors) { - t.Errorf("Invalid ignore preflight errors. Expected: %v. Actual: %v", expected.List(), data.ignorePreflightErrors.List()) + t.Errorf("Invalid ignore preflight errors. Expected: %v. Actual: %v", sets.List(expected), sets.List(data.ignorePreflightErrors)) } if !expected.HasAll(data.cfg.NodeRegistration.IgnorePreflightErrors...) { - t.Errorf("Invalid ignore preflight errors in InitConfiguration. Expected: %v. Actual: %v", expected.List(), data.cfg.NodeRegistration.IgnorePreflightErrors) + t.Errorf("Invalid ignore preflight errors in InitConfiguration. Expected: %v. Actual: %v", sets.List(expected), data.cfg.NodeRegistration.IgnorePreflightErrors) } } } diff --git a/cmd/kubeadm/app/cmd/join.go b/cmd/kubeadm/app/cmd/join.go index af8f06f8832..2c524a00d29 100644 --- a/cmd/kubeadm/app/cmd/join.go +++ b/cmd/kubeadm/app/cmd/join.go @@ -147,7 +147,7 @@ type joinData struct { initCfg *kubeadmapi.InitConfiguration tlsBootstrapCfg *clientcmdapi.Config client clientset.Interface - ignorePreflightErrors sets.String + ignorePreflightErrors sets.Set[string] outputWriter io.Writer patchesDir string dryRun bool @@ -432,7 +432,7 @@ func newJoinData(cmd *cobra.Command, args []string, opt *joinOptions, out io.Wri return nil, err } // Also set the union of pre-flight errors to JoinConfiguration, to provide a consistent view of the runtime configuration: - cfg.NodeRegistration.IgnorePreflightErrors = ignorePreflightErrorsSet.List() + cfg.NodeRegistration.IgnorePreflightErrors = sets.List(ignorePreflightErrorsSet) // override node name and CRI socket from the command line opt if opt.externalcfg.NodeRegistration.Name != "" { @@ -558,7 +558,7 @@ func (j *joinData) Client() (clientset.Interface, error) { } // IgnorePreflightErrors returns the list of preflight errors to ignore. -func (j *joinData) IgnorePreflightErrors() sets.String { +func (j *joinData) IgnorePreflightErrors() sets.Set[string] { return j.ignorePreflightErrors } diff --git a/cmd/kubeadm/app/cmd/join_test.go b/cmd/kubeadm/app/cmd/join_test.go index 4b08a1d261c..743e4d3aee4 100644 --- a/cmd/kubeadm/app/cmd/join_test.go +++ b/cmd/kubeadm/app/cmd/join_test.go @@ -236,14 +236,14 @@ func TestNewJoinData(t *testing.T) { options.IgnorePreflightErrors: "a,b", options.FileDiscovery: "https://foo", //required only to pass discovery validation }, - validate: expectedJoinIgnorePreflightErrors(sets.NewString("a", "b")), + validate: expectedJoinIgnorePreflightErrors(sets.New("a", "b")), }, { name: "pre-flights errors from JoinConfiguration only", flags: map[string]string{ options.CfgPath: configFilePath, }, - validate: expectedJoinIgnorePreflightErrors(sets.NewString("c", "d")), + validate: expectedJoinIgnorePreflightErrors(sets.New("c", "d")), }, { name: "pre-flights errors from both CLI args and JoinConfiguration", @@ -251,7 +251,7 @@ func TestNewJoinData(t *testing.T) { options.CfgPath: configFilePath, options.IgnorePreflightErrors: "a,b", }, - validate: expectedJoinIgnorePreflightErrors(sets.NewString("a", "b", "c", "d")), + validate: expectedJoinIgnorePreflightErrors(sets.New("a", "b", "c", "d")), }, { name: "warn if --control-plane flag is not set", @@ -315,13 +315,13 @@ func TestNewJoinData(t *testing.T) { } } -func expectedJoinIgnorePreflightErrors(expected sets.String) func(t *testing.T, data *joinData) { +func expectedJoinIgnorePreflightErrors(expected sets.Set[string]) func(t *testing.T, data *joinData) { return func(t *testing.T, data *joinData) { if !expected.Equal(data.ignorePreflightErrors) { - t.Errorf("Invalid ignore preflight errors. Expected: %v. Actual: %v", expected.List(), data.ignorePreflightErrors.List()) + t.Errorf("Invalid ignore preflight errors. Expected: %v. Actual: %v", sets.List(expected), sets.List(data.ignorePreflightErrors)) } if !expected.HasAll(data.cfg.NodeRegistration.IgnorePreflightErrors...) { - t.Errorf("Invalid ignore preflight errors in JoinConfiguration. Expected: %v. Actual: %v", expected.List(), data.cfg.NodeRegistration.IgnorePreflightErrors) + t.Errorf("Invalid ignore preflight errors in JoinConfiguration. Expected: %v. Actual: %v", sets.List(expected), data.cfg.NodeRegistration.IgnorePreflightErrors) } } } diff --git a/cmd/kubeadm/app/cmd/phases/init/data.go b/cmd/kubeadm/app/cmd/phases/init/data.go index 5942593b4ea..db0f3b707e3 100644 --- a/cmd/kubeadm/app/cmd/phases/init/data.go +++ b/cmd/kubeadm/app/cmd/phases/init/data.go @@ -35,7 +35,7 @@ type InitData interface { Cfg() *kubeadmapi.InitConfiguration DryRun() bool SkipTokenPrint() bool - IgnorePreflightErrors() sets.String + IgnorePreflightErrors() sets.Set[string] CertificateWriteDir() string CertificateDir() string KubeConfigDir() string diff --git a/cmd/kubeadm/app/cmd/phases/init/data_test.go b/cmd/kubeadm/app/cmd/phases/init/data_test.go index f7a67445646..9c711499e96 100644 --- a/cmd/kubeadm/app/cmd/phases/init/data_test.go +++ b/cmd/kubeadm/app/cmd/phases/init/data_test.go @@ -31,22 +31,22 @@ type testInitData struct{} // testInitData must satisfy InitData. var _ InitData = &testInitData{} -func (t *testInitData) UploadCerts() bool { return false } -func (t *testInitData) CertificateKey() string { return "" } -func (t *testInitData) SetCertificateKey(key string) {} -func (t *testInitData) SkipCertificateKeyPrint() bool { return false } -func (t *testInitData) Cfg() *kubeadmapi.InitConfiguration { return nil } -func (t *testInitData) DryRun() bool { return false } -func (t *testInitData) SkipTokenPrint() bool { return false } -func (t *testInitData) IgnorePreflightErrors() sets.String { return nil } -func (t *testInitData) CertificateWriteDir() string { return "" } -func (t *testInitData) CertificateDir() string { return "" } -func (t *testInitData) KubeConfigDir() string { return "" } -func (t *testInitData) KubeConfigPath() string { return "" } -func (t *testInitData) ManifestDir() string { return "" } -func (t *testInitData) KubeletDir() string { return "" } -func (t *testInitData) ExternalCA() bool { return false } -func (t *testInitData) OutputWriter() io.Writer { return nil } -func (t *testInitData) Client() (clientset.Interface, error) { return nil, nil } -func (t *testInitData) Tokens() []string { return nil } -func (t *testInitData) PatchesDir() string { return "" } +func (t *testInitData) UploadCerts() bool { return false } +func (t *testInitData) CertificateKey() string { return "" } +func (t *testInitData) SetCertificateKey(key string) {} +func (t *testInitData) SkipCertificateKeyPrint() bool { return false } +func (t *testInitData) Cfg() *kubeadmapi.InitConfiguration { return nil } +func (t *testInitData) DryRun() bool { return false } +func (t *testInitData) SkipTokenPrint() bool { return false } +func (t *testInitData) IgnorePreflightErrors() sets.Set[string] { return nil } +func (t *testInitData) CertificateWriteDir() string { return "" } +func (t *testInitData) CertificateDir() string { return "" } +func (t *testInitData) KubeConfigDir() string { return "" } +func (t *testInitData) KubeConfigPath() string { return "" } +func (t *testInitData) ManifestDir() string { return "" } +func (t *testInitData) KubeletDir() string { return "" } +func (t *testInitData) ExternalCA() bool { return false } +func (t *testInitData) OutputWriter() io.Writer { return nil } +func (t *testInitData) Client() (clientset.Interface, error) { return nil, nil } +func (t *testInitData) Tokens() []string { return nil } +func (t *testInitData) PatchesDir() string { return "" } diff --git a/cmd/kubeadm/app/cmd/phases/join/data.go b/cmd/kubeadm/app/cmd/phases/join/data.go index a988582ff6f..773caba2dd1 100644 --- a/cmd/kubeadm/app/cmd/phases/join/data.go +++ b/cmd/kubeadm/app/cmd/phases/join/data.go @@ -34,7 +34,7 @@ type JoinData interface { TLSBootstrapCfg() (*clientcmdapi.Config, error) InitCfg() (*kubeadmapi.InitConfiguration, error) Client() (clientset.Interface, error) - IgnorePreflightErrors() sets.String + IgnorePreflightErrors() sets.Set[string] OutputWriter() io.Writer PatchesDir() string DryRun() bool diff --git a/cmd/kubeadm/app/cmd/phases/join/data_test.go b/cmd/kubeadm/app/cmd/phases/join/data_test.go index 3fb696b0c30..94762f5908f 100644 --- a/cmd/kubeadm/app/cmd/phases/join/data_test.go +++ b/cmd/kubeadm/app/cmd/phases/join/data_test.go @@ -37,7 +37,7 @@ func (j *testJoinData) Cfg() *kubeadmapi.JoinConfiguration { return func (j *testJoinData) TLSBootstrapCfg() (*clientcmdapi.Config, error) { return nil, nil } func (j *testJoinData) InitCfg() (*kubeadmapi.InitConfiguration, error) { return nil, nil } func (j *testJoinData) Client() (clientset.Interface, error) { return nil, nil } -func (j *testJoinData) IgnorePreflightErrors() sets.String { return nil } +func (j *testJoinData) IgnorePreflightErrors() sets.Set[string] { return nil } func (j *testJoinData) OutputWriter() io.Writer { return nil } func (j *testJoinData) PatchesDir() string { return "" } func (t *testJoinData) DryRun() bool { return false } diff --git a/cmd/kubeadm/app/cmd/phases/reset/data.go b/cmd/kubeadm/app/cmd/phases/reset/data.go index 27a961de991..34720b9ceef 100644 --- a/cmd/kubeadm/app/cmd/phases/reset/data.go +++ b/cmd/kubeadm/app/cmd/phases/reset/data.go @@ -30,7 +30,7 @@ import ( type resetData interface { ForceReset() bool InputReader() io.Reader - IgnorePreflightErrors() sets.String + IgnorePreflightErrors() sets.Set[string] Cfg() *kubeadmapi.InitConfiguration DryRun() bool Client() clientset.Interface diff --git a/cmd/kubeadm/app/cmd/phases/reset/data_test.go b/cmd/kubeadm/app/cmd/phases/reset/data_test.go index bdf95a9daa2..a0f9e4d593e 100644 --- a/cmd/kubeadm/app/cmd/phases/reset/data_test.go +++ b/cmd/kubeadm/app/cmd/phases/reset/data_test.go @@ -31,12 +31,12 @@ type testData struct{} // testData must satisfy resetData. var _ resetData = &testData{} -func (t *testData) ForceReset() bool { return false } -func (t *testData) InputReader() io.Reader { return nil } -func (t *testData) IgnorePreflightErrors() sets.String { return nil } -func (t *testData) Cfg() *kubeadmapi.InitConfiguration { return nil } -func (t *testData) DryRun() bool { return false } -func (t *testData) Client() clientset.Interface { return nil } -func (t *testData) CertificatesDir() string { return "" } -func (t *testData) CRISocketPath() string { return "" } -func (t *testData) CleanupTmpDir() bool { return false } +func (t *testData) ForceReset() bool { return false } +func (t *testData) InputReader() io.Reader { return nil } +func (t *testData) IgnorePreflightErrors() sets.Set[string] { return nil } +func (t *testData) Cfg() *kubeadmapi.InitConfiguration { return nil } +func (t *testData) DryRun() bool { return false } +func (t *testData) Client() clientset.Interface { return nil } +func (t *testData) CertificatesDir() string { return "" } +func (t *testData) CRISocketPath() string { return "" } +func (t *testData) CleanupTmpDir() bool { return false } diff --git a/cmd/kubeadm/app/cmd/phases/upgrade/node/data.go b/cmd/kubeadm/app/cmd/phases/upgrade/node/data.go index 429ce185937..0b920cb23ea 100644 --- a/cmd/kubeadm/app/cmd/phases/upgrade/node/data.go +++ b/cmd/kubeadm/app/cmd/phases/upgrade/node/data.go @@ -34,7 +34,7 @@ type Data interface { Cfg() *kubeadmapi.InitConfiguration IsControlPlaneNode() bool Client() clientset.Interface - IgnorePreflightErrors() sets.String + IgnorePreflightErrors() sets.Set[string] PatchesDir() string KubeConfigPath() string OutputWriter() io.Writer diff --git a/cmd/kubeadm/app/cmd/phases/upgrade/node/data_test.go b/cmd/kubeadm/app/cmd/phases/upgrade/node/data_test.go index 8b200d3d467..28d6ae1082d 100644 --- a/cmd/kubeadm/app/cmd/phases/upgrade/node/data_test.go +++ b/cmd/kubeadm/app/cmd/phases/upgrade/node/data_test.go @@ -31,13 +31,13 @@ type testData struct{} // testData must satisfy Data. var _ Data = &testData{} -func (t *testData) EtcdUpgrade() bool { return false } -func (t *testData) RenewCerts() bool { return false } -func (t *testData) DryRun() bool { return false } -func (t *testData) Cfg() *kubeadmapi.InitConfiguration { return nil } -func (t *testData) IsControlPlaneNode() bool { return false } -func (t *testData) Client() clientset.Interface { return nil } -func (t *testData) IgnorePreflightErrors() sets.String { return nil } -func (t *testData) PatchesDir() string { return "" } -func (t *testData) KubeConfigPath() string { return "" } -func (t *testData) OutputWriter() io.Writer { return nil } +func (t *testData) EtcdUpgrade() bool { return false } +func (t *testData) RenewCerts() bool { return false } +func (t *testData) DryRun() bool { return false } +func (t *testData) Cfg() *kubeadmapi.InitConfiguration { return nil } +func (t *testData) IsControlPlaneNode() bool { return false } +func (t *testData) Client() clientset.Interface { return nil } +func (t *testData) IgnorePreflightErrors() sets.Set[string] { return nil } +func (t *testData) PatchesDir() string { return "" } +func (t *testData) KubeConfigPath() string { return "" } +func (t *testData) OutputWriter() io.Writer { return nil } diff --git a/cmd/kubeadm/app/cmd/reset.go b/cmd/kubeadm/app/cmd/reset.go index 724e57e205e..7e631136e6b 100644 --- a/cmd/kubeadm/app/cmd/reset.go +++ b/cmd/kubeadm/app/cmd/reset.go @@ -76,7 +76,7 @@ type resetData struct { client clientset.Interface criSocketPath string forceReset bool - ignorePreflightErrors sets.String + ignorePreflightErrors sets.Set[string] inputReader io.Reader outputWriter io.Writer cfg *kubeadmapi.InitConfiguration @@ -116,7 +116,7 @@ func newResetData(cmd *cobra.Command, options *resetOptions, in io.Reader, out i } if cfg != nil { // Also set the union of pre-flight errors to InitConfiguration, to provide a consistent view of the runtime configuration: - cfg.NodeRegistration.IgnorePreflightErrors = ignorePreflightErrorsSet.List() + cfg.NodeRegistration.IgnorePreflightErrors = sets.List(ignorePreflightErrorsSet) } var criSocketPath string @@ -249,7 +249,7 @@ func (r *resetData) InputReader() io.Reader { } // IgnorePreflightErrors returns the list of preflight errors to ignore. -func (r *resetData) IgnorePreflightErrors() sets.String { +func (r *resetData) IgnorePreflightErrors() sets.Set[string] { return r.ignorePreflightErrors } diff --git a/cmd/kubeadm/app/cmd/reset_test.go b/cmd/kubeadm/app/cmd/reset_test.go index 54bac67dfef..e71906b7582 100644 --- a/cmd/kubeadm/app/cmd/reset_test.go +++ b/cmd/kubeadm/app/cmd/reset_test.go @@ -49,7 +49,7 @@ func TestNewResetData(t *testing.T) { data: &resetData{ certificatesDir: "/tmp", criSocketPath: "unix:///var/run/crio/crio.sock", - ignorePreflightErrors: sets.NewString("all"), + ignorePreflightErrors: sets.New("all"), forceReset: true, dryRun: true, cleanupTmpDir: true, @@ -67,7 +67,7 @@ func TestNewResetData(t *testing.T) { flags: map[string]string{ options.IgnorePreflightErrors: "a,b", }, - validate: expectedResetIgnorePreflightErrors(sets.NewString("a", "b")), + validate: expectedResetIgnorePreflightErrors(sets.New("a", "b")), }, } for _, tc := range testCases { @@ -103,13 +103,13 @@ func TestNewResetData(t *testing.T) { } } -func expectedResetIgnorePreflightErrors(expected sets.String) func(t *testing.T, data *resetData) { +func expectedResetIgnorePreflightErrors(expected sets.Set[string]) func(t *testing.T, data *resetData) { return func(t *testing.T, data *resetData) { if !expected.Equal(data.ignorePreflightErrors) { - t.Errorf("Invalid ignore preflight errors. Expected: %v. Actual: %v", expected.List(), data.ignorePreflightErrors.List()) + t.Errorf("Invalid ignore preflight errors. Expected: %v. Actual: %v", sets.List(expected), sets.List(data.ignorePreflightErrors)) } if data.cfg != nil && !expected.HasAll(data.cfg.NodeRegistration.IgnorePreflightErrors...) { - t.Errorf("Invalid ignore preflight errors in InitConfiguration. Expected: %v. Actual: %v", expected.List(), data.cfg.NodeRegistration.IgnorePreflightErrors) + t.Errorf("Invalid ignore preflight errors in InitConfiguration. Expected: %v. Actual: %v", sets.List(expected), data.cfg.NodeRegistration.IgnorePreflightErrors) } } } diff --git a/cmd/kubeadm/app/cmd/upgrade/apply.go b/cmd/kubeadm/app/cmd/upgrade/apply.go index 3722f97af89..09107f8c7a8 100644 --- a/cmd/kubeadm/app/cmd/upgrade/apply.go +++ b/cmd/kubeadm/app/cmd/upgrade/apply.go @@ -143,7 +143,7 @@ func runApply(flags *applyFlags, args []string) error { fmt.Println("[upgrade/prepull] Pulling images required for setting up a Kubernetes cluster") fmt.Println("[upgrade/prepull] This might take a minute or two, depending on the speed of your internet connection") fmt.Println("[upgrade/prepull] You can also perform this action in beforehand using 'kubeadm config images pull'") - if err := preflight.RunPullImagesCheck(utilsexec.New(), cfg, sets.NewString(cfg.NodeRegistration.IgnorePreflightErrors...)); err != nil { + if err := preflight.RunPullImagesCheck(utilsexec.New(), cfg, sets.New(cfg.NodeRegistration.IgnorePreflightErrors...)); err != nil { return err } } else { @@ -164,7 +164,7 @@ func runApply(flags *applyFlags, args []string) error { } if flags.dryRun { - fmt.Println("[upgrade/successful] Finished dryrunning successfully!") + fmt.Println("[upgrade/successful] Finished dryrunning successfully!") return nil } diff --git a/cmd/kubeadm/app/cmd/upgrade/common.go b/cmd/kubeadm/app/cmd/upgrade/common.go index c71ec2c2d3c..a4334e4ea68 100644 --- a/cmd/kubeadm/app/cmd/upgrade/common.go +++ b/cmd/kubeadm/app/cmd/upgrade/common.go @@ -177,7 +177,7 @@ func enforceRequirements(flags *applyPlanFlags, args []string, dryRun bool, upgr return nil, nil, nil, err } // Also set the union of pre-flight errors to InitConfiguration, to provide a consistent view of the runtime configuration: - cfg.NodeRegistration.IgnorePreflightErrors = ignorePreflightErrorsSet.List() + cfg.NodeRegistration.IgnorePreflightErrors = sets.List(ignorePreflightErrorsSet) // Ensure the user is root klog.V(1).Info("running preflight checks") @@ -234,7 +234,7 @@ func printConfiguration(clustercfg *kubeadmapi.ClusterConfiguration, w io.Writer } // runPreflightChecks runs the root preflight check -func runPreflightChecks(client clientset.Interface, ignorePreflightErrors sets.String, cfg *kubeadmapi.ClusterConfiguration, printer output.Printer) error { +func runPreflightChecks(client clientset.Interface, ignorePreflightErrors sets.Set[string], cfg *kubeadmapi.ClusterConfiguration, printer output.Printer) error { printer.Printf("[preflight] Running pre-flight checks.\n") err := preflight.RunRootCheckOnly(ignorePreflightErrors) if err != nil { diff --git a/cmd/kubeadm/app/cmd/upgrade/node.go b/cmd/kubeadm/app/cmd/upgrade/node.go index 43c0d9c41ce..2d4d59bf4c4 100644 --- a/cmd/kubeadm/app/cmd/upgrade/node.go +++ b/cmd/kubeadm/app/cmd/upgrade/node.go @@ -61,7 +61,7 @@ type nodeData struct { isControlPlaneNode bool client clientset.Interface patchesDir string - ignorePreflightErrors sets.String + ignorePreflightErrors sets.Set[string] kubeConfigPath string outputWriter io.Writer } @@ -151,8 +151,7 @@ func newNodeData(cmd *cobra.Command, args []string, options *nodeOptions, out io return nil, err } // Also set the union of pre-flight errors to JoinConfiguration, to provide a consistent view of the runtime configuration: - cfg.NodeRegistration.IgnorePreflightErrors = ignorePreflightErrorsSet.List() - + cfg.NodeRegistration.IgnorePreflightErrors = sets.List(ignorePreflightErrorsSet) return &nodeData{ etcdUpgrade: options.etcdUpgrade, renewCerts: options.renewCerts, @@ -203,7 +202,7 @@ func (d *nodeData) PatchesDir() string { } // IgnorePreflightErrors returns the list of preflight errors to ignore. -func (d *nodeData) IgnorePreflightErrors() sets.String { +func (d *nodeData) IgnorePreflightErrors() sets.Set[string] { return d.ignorePreflightErrors } diff --git a/cmd/kubeadm/app/phases/controlplane/manifests_test.go b/cmd/kubeadm/app/phases/controlplane/manifests_test.go index 0a0e1add4f7..e5af8566dab 100644 --- a/cmd/kubeadm/app/phases/controlplane/manifests_test.go +++ b/cmd/kubeadm/app/phases/controlplane/manifests_test.go @@ -530,9 +530,9 @@ func errorDiffArguments(t *testing.T, name string, actual, expected []string) { // removeCommon removes common items from left list // makes compairing two cmdline (with lots of arguments) easier func removeCommon(left, right []string) []string { - origSet := sets.NewString(left...) + origSet := sets.New(left...) origSet.Delete(right...) - return origSet.List() + return sets.List(origSet) } func TestGetControllerManagerCommand(t *testing.T) { diff --git a/cmd/kubeadm/app/phases/controlplane/volumes.go b/cmd/kubeadm/app/phases/controlplane/volumes.go index 51276a0ddcc..d193b4e0186 100644 --- a/cmd/kubeadm/app/phases/controlplane/volumes.go +++ b/cmd/kubeadm/app/phases/controlplane/volumes.go @@ -175,7 +175,7 @@ func (c *controlPlaneHostPathMounts) addComponentVolumeMount(component string, v // getEtcdCertVolumes returns the volumes/volumemounts needed for talking to an external etcd cluster func getEtcdCertVolumes(etcdCfg *kubeadmapi.ExternalEtcd, k8sCertificatesDir string) ([]v1.Volume, []v1.VolumeMount) { certPaths := []string{etcdCfg.CAFile, etcdCfg.CertFile, etcdCfg.KeyFile} - certDirs := sets.NewString() + certDirs := sets.New[string]() for _, certPath := range certPaths { certDir := filepath.ToSlash(filepath.Dir(certPath)) // Ignore ".", which is the result of passing an empty path. @@ -193,7 +193,7 @@ func getEtcdCertVolumes(etcdCfg *kubeadmapi.ExternalEtcd, k8sCertificatesDir str } // Filter out any existing hostpath mounts in the list that contains a subset of the path alreadyExists := false - for _, existingCertDir := range certDirs.List() { + for _, existingCertDir := range sets.List(certDirs) { // If the current directory is a parent of an existing one, remove the already existing one if strings.HasPrefix(existingCertDir, certDir) { certDirs.Delete(existingCertDir) @@ -211,7 +211,7 @@ func getEtcdCertVolumes(etcdCfg *kubeadmapi.ExternalEtcd, k8sCertificatesDir str volumes := []v1.Volume{} volumeMounts := []v1.VolumeMount{} pathType := v1.HostPathDirectoryOrCreate - for i, certDir := range certDirs.List() { + for i, certDir := range sets.List(certDirs) { name := fmt.Sprintf("etcd-certs-%d", i) volumes = append(volumes, staticpodutil.NewVolume(name, certDir, &pathType)) volumeMounts = append(volumeMounts, staticpodutil.NewVolumeMount(name, certDir, true)) diff --git a/cmd/kubeadm/app/phases/upgrade/health.go b/cmd/kubeadm/app/phases/upgrade/health.go index ec09ee9f39e..b9e47c1acde 100644 --- a/cmd/kubeadm/app/phases/upgrade/health.go +++ b/cmd/kubeadm/app/phases/upgrade/health.go @@ -66,7 +66,7 @@ func (c *healthCheck) Name() string { // - the API /healthz endpoint is healthy // - all control-plane Nodes are Ready // - (if static pod-hosted) that all required Static Pod manifests exist on disk -func CheckClusterHealth(client clientset.Interface, cfg *kubeadmapi.ClusterConfiguration, ignoreChecksErrors sets.String) error { +func CheckClusterHealth(client clientset.Interface, cfg *kubeadmapi.ClusterConfiguration, ignoreChecksErrors sets.Set[string]) error { fmt.Println("[upgrade] Running cluster health checks") healthChecks := []preflight.Checker{ diff --git a/cmd/kubeadm/app/phases/upgrade/preflight.go b/cmd/kubeadm/app/phases/upgrade/preflight.go index 03e844d702d..bb5ea0949a7 100644 --- a/cmd/kubeadm/app/phases/upgrade/preflight.go +++ b/cmd/kubeadm/app/phases/upgrade/preflight.go @@ -54,7 +54,7 @@ func (c CoreDNSCheck) Check() (warnings, errors []error) { } // RunCoreDNSMigrationCheck initializes checks related to CoreDNS migration. -func RunCoreDNSMigrationCheck(client clientset.Interface, ignorePreflightErrors sets.String) error { +func RunCoreDNSMigrationCheck(client clientset.Interface, ignorePreflightErrors sets.Set[string]) error { migrationChecks := []preflight.Checker{ &CoreDNSCheck{ name: "CoreDNSUnsupportedPlugins", diff --git a/cmd/kubeadm/app/preflight/checks.go b/cmd/kubeadm/app/preflight/checks.go index 149382b1b0f..9d2b9b713bd 100644 --- a/cmd/kubeadm/app/preflight/checks.go +++ b/cmd/kubeadm/app/preflight/checks.go @@ -892,7 +892,7 @@ func (MemCheck) Name() string { // The boolean flag 'isSecondaryControlPlane' controls whether we are running checks in a --join-control-plane scenario. // The boolean flag 'downloadCerts' controls whether we should skip checks on certificates because we are downloading them. // If the flag is set to true we should skip checks already executed by RunJoinNodeChecks. -func RunInitNodeChecks(execer utilsexec.Interface, cfg *kubeadmapi.InitConfiguration, ignorePreflightErrors sets.String, isSecondaryControlPlane bool, downloadCerts bool) error { +func RunInitNodeChecks(execer utilsexec.Interface, cfg *kubeadmapi.InitConfiguration, ignorePreflightErrors sets.Set[string], isSecondaryControlPlane bool, downloadCerts bool) error { if !isSecondaryControlPlane { // First, check if we're root separately from the other preflight checks and fail fast if err := RunRootCheckOnly(ignorePreflightErrors); err != nil { @@ -972,7 +972,7 @@ func RunInitNodeChecks(execer utilsexec.Interface, cfg *kubeadmapi.InitConfigura } // RunJoinNodeChecks executes all individual, applicable to node checks. -func RunJoinNodeChecks(execer utilsexec.Interface, cfg *kubeadmapi.JoinConfiguration, ignorePreflightErrors sets.String) error { +func RunJoinNodeChecks(execer utilsexec.Interface, cfg *kubeadmapi.JoinConfiguration, ignorePreflightErrors sets.Set[string]) error { // First, check if we're root separately from the other preflight checks and fail fast if err := RunRootCheckOnly(ignorePreflightErrors); err != nil { return err @@ -1049,7 +1049,7 @@ func addCommonChecks(execer utilsexec.Interface, k8sVersion string, nodeReg *kub } // RunRootCheckOnly initializes checks slice of structs and call RunChecks -func RunRootCheckOnly(ignorePreflightErrors sets.String) error { +func RunRootCheckOnly(ignorePreflightErrors sets.Set[string]) error { checks := []Checker{ IsPrivilegedUserCheck{}, } @@ -1058,7 +1058,7 @@ func RunRootCheckOnly(ignorePreflightErrors sets.String) error { } // RunPullImagesCheck will pull images kubeadm needs if they are not found on the system -func RunPullImagesCheck(execer utilsexec.Interface, cfg *kubeadmapi.InitConfiguration, ignorePreflightErrors sets.String) error { +func RunPullImagesCheck(execer utilsexec.Interface, cfg *kubeadmapi.InitConfiguration, ignorePreflightErrors sets.Set[string]) error { containerRuntime, err := utilruntime.NewContainerRuntime(utilsexec.New(), cfg.NodeRegistration.CRISocket) if err != nil { return &Error{Msg: err.Error()} @@ -1072,7 +1072,7 @@ func RunPullImagesCheck(execer utilsexec.Interface, cfg *kubeadmapi.InitConfigur // RunChecks runs each check, displays it's warnings/errors, and once all // are processed will exit if any errors occurred. -func RunChecks(checks []Checker, ww io.Writer, ignorePreflightErrors sets.String) error { +func RunChecks(checks []Checker, ww io.Writer, ignorePreflightErrors sets.Set[string]) error { var errsBuffer bytes.Buffer for _, c := range checks { @@ -1099,7 +1099,7 @@ func RunChecks(checks []Checker, ww io.Writer, ignorePreflightErrors sets.String } // setHasItemOrAll is helper function that return true if item is present in the set (case insensitive) or special key 'all' is present -func setHasItemOrAll(s sets.String, item string) bool { +func setHasItemOrAll(s sets.Set[string], item string) bool { if s.Has("all") || s.Has(strings.ToLower(item)) { return true } diff --git a/cmd/kubeadm/app/preflight/checks_test.go b/cmd/kubeadm/app/preflight/checks_test.go index e27973c7555..0814213437c 100644 --- a/cmd/kubeadm/app/preflight/checks_test.go +++ b/cmd/kubeadm/app/preflight/checks_test.go @@ -436,7 +436,7 @@ func TestRunChecks(t *testing.T) { } for _, rt := range tokenTest { buf := new(bytes.Buffer) - actual := RunChecks(rt.p, buf, sets.NewString()) + actual := RunChecks(rt.p, buf, sets.New[string]()) if (actual == nil) != rt.expected { t.Errorf( "failed RunChecks:\n\texpected: %t\n\t actual: %t", @@ -836,16 +836,16 @@ func TestKubeletVersionCheck(t *testing.T) { func TestSetHasItemOrAll(t *testing.T) { var tests = []struct { - ignoreSet sets.String + ignoreSet sets.Set[string] testString string expectedResult bool }{ - {sets.NewString(), "foo", false}, - {sets.NewString("all"), "foo", true}, - {sets.NewString("all", "bar"), "foo", true}, - {sets.NewString("bar"), "foo", false}, - {sets.NewString("baz", "foo", "bar"), "foo", true}, - {sets.NewString("baz", "bar", "foo"), "Foo", true}, + {sets.New[string](), "foo", false}, + {sets.New("all"), "foo", true}, + {sets.New("all", "bar"), "foo", true}, + {sets.New("bar"), "foo", false}, + {sets.New("baz", "foo", "bar"), "foo", true}, + {sets.New("baz", "bar", "foo"), "Foo", true}, } for _, rt := range tests { diff --git a/cmd/kubeadm/app/util/pkiutil/pki_helpers.go b/cmd/kubeadm/app/util/pkiutil/pki_helpers.go index ff51d740248..95386cafabf 100644 --- a/cmd/kubeadm/app/util/pkiutil/pki_helpers.go +++ b/cmd/kubeadm/app/util/pkiutil/pki_helpers.go @@ -500,7 +500,7 @@ func getAltNames(cfg *kubeadmapi.InitConfiguration, certName string) (*certutil. // valid IP address strings are parsed and added to altNames.IPs as net.IP's // RFC-1123 compliant DNS strings are added to altNames.DNSNames as strings // RFC-1123 compliant wildcard DNS strings are added to altNames.DNSNames as strings -// certNames is used to print user facing warnings and should be the name of the cert the altNames will be used for +// certNames is used to print user facing warnings and should be the name of the cert the altNames will be used for func appendSANsToAltNames(altNames *certutil.AltNames, SANs []string, certName string) { for _, altname := range SANs { if ip := netutils.ParseIPSloppy(altname); ip != nil { @@ -680,7 +680,7 @@ func RemoveDuplicateAltNames(altNames *certutil.AltNames) { } if altNames.DNSNames != nil { - altNames.DNSNames = sets.NewString(altNames.DNSNames...).List() + altNames.DNSNames = sets.List(sets.New(altNames.DNSNames...)) } ipsKeys := make(map[string]struct{})