diff --git a/cmd/cloud-controller-manager/app/options/dynamic.go b/cmd/cloud-controller-manager/app/options/dynamic.go index 05ee991d76..10ae67e2c5 100644 --- a/cmd/cloud-controller-manager/app/options/dynamic.go +++ b/cmd/cloud-controller-manager/app/options/dynamic.go @@ -37,7 +37,7 @@ func (o *DynamicReloadingOptions) AddFlags(fs *pflag.FlagSet) { } fs.BoolVar(&o.EnableDynamicReloading, "enable-dynamic-reloading", false, "Enable re-configuring cloud controller manager from secret without restarting") - fs.StringVar(&o.CloudConfigSecretName, "cloud-config-secret-name", "cloud-provider-config", "The name of the cloud config secret, default to 'cloud-provider-config'") + fs.StringVar(&o.CloudConfigSecretName, "cloud-config-secret-name", "azure-cloud-provider", "The name of the cloud config secret, default to 'cloud-provider-config'") fs.StringVar(&o.CloudConfigSecretNamespace, "cloud-config-secret-namespace", "kube-system", "The k8s namespace of the cloud config secret, default to 'kube-system'") fs.StringVar(&o.CloudConfigKey, "cloud-config-key", "cloud-config", "The key of the config data in the cloud config secret, default to 'cloud-config'") } @@ -64,8 +64,8 @@ func (o *DynamicReloadingOptions) Validate() []error { func defaultDynamicReloadingOptions() *DynamicReloadingOptions { return &DynamicReloadingOptions{ EnableDynamicReloading: false, - CloudConfigSecretName: "", - CloudConfigSecretNamespace: "", + CloudConfigSecretName: "azure-cloud-provider", + CloudConfigSecretNamespace: "kube-system", CloudConfigKey: "", } } diff --git a/cmd/cloud-controller-manager/app/options/options_test.go b/cmd/cloud-controller-manager/app/options/options_test.go index deb07a9d0b..ce705e78bf 100644 --- a/cmd/cloud-controller-manager/app/options/options_test.go +++ b/cmd/cloud-controller-manager/app/options/options_test.go @@ -141,8 +141,8 @@ func TestDefaultFlags(t *testing.T) { NodeStatusUpdateFrequency: metav1.Duration{Duration: 5 * time.Minute}, DynamicReloading: &DynamicReloadingOptions{ EnableDynamicReloading: false, - CloudConfigSecretName: "", - CloudConfigSecretNamespace: "", + CloudConfigSecretName: "azure-cloud-provider", + CloudConfigSecretNamespace: "kube-system", CloudConfigKey: "", }, } diff --git a/pkg/consts/consts.go b/pkg/consts/consts.go index ecd02d4ed5..af1f983bff 100644 --- a/pkg/consts/consts.go +++ b/pkg/consts/consts.go @@ -340,3 +340,10 @@ const ( RouteNameFmt = "%s____%s" RouteNameSeparator = "____" ) + +// cloud provider config secret +const ( + DefaultCloudProviderConfigSecName = "azure-cloud-provider" + DefaultCloudProviderConfigSecNamespace = "kube-system" + DefaultCloudProviderConfigSecKey = "cloud-config" +) diff --git a/pkg/provider/azure.go b/pkg/provider/azure.go index 7cc69b6528..c06295f8ec 100644 --- a/pkg/provider/azure.go +++ b/pkg/provider/azure.go @@ -372,6 +372,24 @@ func NewCloudFromConfigFile(configFilePath string, syncZones bool) (cloudprovide return cloud, nil } +func (az *Cloud) configSecretMetadata(secretName, secretNamespace, cloudConfigKey string) { + if secretName == "" { + secretName = consts.DefaultCloudProviderConfigSecName + } + if secretNamespace == "" { + secretNamespace = consts.DefaultCloudProviderConfigSecNamespace + } + if cloudConfigKey == "" { + cloudConfigKey = consts.DefaultCloudProviderConfigSecKey + } + + az.InitSecretConfig = InitSecretConfig{ + SecretName: secretName, + SecretNamespace: secretNamespace, + CloudConfigKey: cloudConfigKey, + } +} + func NewCloudFromSecret(clientBuilder cloudprovider.ControllerClientBuilder, secretName, secretNamespace, cloudConfigKey string) (cloudprovider.Interface, error) { az := &Cloud{ nodeNames: sets.NewString(), @@ -379,13 +397,10 @@ func NewCloudFromSecret(clientBuilder cloudprovider.ControllerClientBuilder, sec nodeResourceGroups: map[string]string{}, unmanagedNodes: sets.NewString(), routeCIDRs: map[string]string{}, - InitSecretConfig: InitSecretConfig{ - SecretName: secretName, - SecretNamespace: secretNamespace, - CloudConfigKey: cloudConfigKey, - }, } + az.configSecretMetadata(secretName, secretNamespace, cloudConfigKey) + az.Initialize(clientBuilder, wait.NeverStop) err := az.InitializeCloudFromSecret() diff --git a/pkg/provider/azure_config_test.go b/pkg/provider/azure_config_test.go index b9b087157f..68350af176 100644 --- a/pkg/provider/azure_config_test.go +++ b/pkg/provider/azure_config_test.go @@ -20,6 +20,8 @@ import ( "context" "testing" + "sigs.k8s.io/cloud-provider-azure/pkg/consts" + "github.com/golang/mock/gomock" "sigs.k8s.io/cloud-provider-azure/pkg/azureclients/zoneclient/mockzoneclient" @@ -286,3 +288,36 @@ func TestInitializeCloudFromSecret(t *testing.T) { }) } } + +func TestConfigSecretMetadata(t *testing.T) { + for _, testCase := range []struct { + description string + secretName, secretNamespace, cloudConfigKey string + expectedsecretName, expectedSsecretNamespace, expectedClouConfigKey string + }{ + { + description: "configSecretMetadata should set the secret metadata from the given parameters", + secretName: "cloud-provider-config", + secretNamespace: "123456", + cloudConfigKey: "azure.json", + expectedsecretName: "cloud-provider-config", + expectedSsecretNamespace: "123456", + expectedClouConfigKey: "azure.json", + }, + { + description: "configSecretMetadata should set the secret metadata from the default values", + expectedsecretName: consts.DefaultCloudProviderConfigSecName, + expectedSsecretNamespace: consts.DefaultCloudProviderConfigSecNamespace, + expectedClouConfigKey: consts.DefaultCloudProviderConfigSecKey, + }, + } { + t.Run(testCase.description, func(t *testing.T) { + az := &Cloud{} + az.configSecretMetadata(testCase.secretName, testCase.secretNamespace, testCase.cloudConfigKey) + + assert.Equal(t, testCase.expectedsecretName, az.SecretName) + assert.Equal(t, testCase.expectedSsecretNamespace, az.SecretNamespace) + assert.Equal(t, testCase.expectedClouConfigKey, az.CloudConfigKey) + }) + } +}