From c329202ee8eb20a006c61838bdb92a60f81a1dab Mon Sep 17 00:00:00 2001 From: Indeed Date: Tue, 8 Jun 2021 15:23:25 -0700 Subject: [PATCH 1/3] update comments to reflect wildcard component. --- staging/src/k8s.io/controller-manager/config/types.go | 1 + staging/src/k8s.io/controller-manager/config/v1alpha1/types.go | 1 + 2 files changed, 2 insertions(+) diff --git a/staging/src/k8s.io/controller-manager/config/types.go b/staging/src/k8s.io/controller-manager/config/types.go index 1d95422d911..e8d2470eeaa 100644 --- a/staging/src/k8s.io/controller-manager/config/types.go +++ b/staging/src/k8s.io/controller-manager/config/types.go @@ -77,5 +77,6 @@ type ControllerLeaderConfiguration struct { // Component is the name of the component in which the controller should be running. // E.g. kube-controller-manager, cloud-controller-manager, etc + // Or '*' meaning the controller can be run under any component that participates in the migration Component string } diff --git a/staging/src/k8s.io/controller-manager/config/v1alpha1/types.go b/staging/src/k8s.io/controller-manager/config/v1alpha1/types.go index 84d3c1b82ff..9cd9eee9c69 100644 --- a/staging/src/k8s.io/controller-manager/config/v1alpha1/types.go +++ b/staging/src/k8s.io/controller-manager/config/v1alpha1/types.go @@ -76,5 +76,6 @@ type ControllerLeaderConfiguration struct { // Component is the name of the component in which the controller should be running. // E.g. kube-controller-manager, cloud-controller-manager, etc + // Or '*' meaning the controller can be run under any component that participates in the migration Component string `json:"component"` } From bade96ed78ffa1459996aeb1695a6bfb51dbd70c Mon Sep 17 00:00:00 2001 From: Indeed Date: Tue, 8 Jun 2021 15:19:38 -0700 Subject: [PATCH 2/3] implement wildcard component. --- .../pkg/leadermigration/config/default.go | 40 +++++++++++++++++++ .../pkg/leadermigration/migrator.go | 2 +- .../pkg/leadermigration/migrator_test.go | 38 ++++++++++++++++++ 3 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 staging/src/k8s.io/controller-manager/pkg/leadermigration/config/default.go diff --git a/staging/src/k8s.io/controller-manager/pkg/leadermigration/config/default.go b/staging/src/k8s.io/controller-manager/pkg/leadermigration/config/default.go new file mode 100644 index 00000000000..383058df34c --- /dev/null +++ b/staging/src/k8s.io/controller-manager/pkg/leadermigration/config/default.go @@ -0,0 +1,40 @@ +/* +Copyright 2021 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package config + +import internal "k8s.io/controller-manager/config" + +// DefaultLeaderMigrationConfiguration returns the default LeaderMigrationConfiguration +// that is valid for this release of Kubernetes. +func DefaultLeaderMigrationConfiguration() *internal.LeaderMigrationConfiguration { + return &internal.LeaderMigrationConfiguration{ + LeaderName: "cloud-provider-extraction-migration", + ResourceLock: ResourceLockLeases, + ControllerLeaders: []internal.ControllerLeaderConfiguration{ + { + Name: "route", + Component: "*", + }, { + Name: "service", + Component: "*", + }, { + Name: "cloud-node-lifecycle", + Component: "*", + }, + }, + } +} diff --git a/staging/src/k8s.io/controller-manager/pkg/leadermigration/migrator.go b/staging/src/k8s.io/controller-manager/pkg/leadermigration/migrator.go index 3ff19e3c1d0..4e7ec3d10df 100644 --- a/staging/src/k8s.io/controller-manager/pkg/leadermigration/migrator.go +++ b/staging/src/k8s.io/controller-manager/pkg/leadermigration/migrator.go @@ -36,7 +36,7 @@ type LeaderMigrator struct { func NewLeaderMigrator(config *internal.LeaderMigrationConfiguration, component string) *LeaderMigrator { migratedControllers := make(map[string]bool) for _, leader := range config.ControllerLeaders { - migratedControllers[leader.Name] = leader.Component == component + migratedControllers[leader.Name] = leader.Component == component || leader.Component == "*" } return &LeaderMigrator{ MigrationReady: make(chan struct{}), diff --git a/staging/src/k8s.io/controller-manager/pkg/leadermigration/migrator_test.go b/staging/src/k8s.io/controller-manager/pkg/leadermigration/migrator_test.go index 104990405dc..ddeb6a0943d 100644 --- a/staging/src/k8s.io/controller-manager/pkg/leadermigration/migrator_test.go +++ b/staging/src/k8s.io/controller-manager/pkg/leadermigration/migrator_test.go @@ -55,6 +55,22 @@ func TestLeaderMigratorFilterFunc(t *testing.T) { }, }, } + wildcardConfig := &internal.LeaderMigrationConfiguration{ + ResourceLock: "leases", + LeaderName: "cloud-provider-extraction-migration", + ControllerLeaders: []internal.ControllerLeaderConfiguration{ + { + Name: "route", + Component: "*", + }, { + Name: "service", + Component: "*", + }, { + Name: "cloud-node-lifecycle", + Component: "*", + }, + }, + } for _, tc := range []struct { name string config *internal.LeaderMigrationConfiguration @@ -106,6 +122,28 @@ func TestLeaderMigratorFilterFunc(t *testing.T) { "cloud-node-lifecycle": ControllerMigrated, }, }, + { + name: "wildcard config, kcm", + config: wildcardConfig, + component: "kube-controller-manager", + expectResult: map[string]FilterResult{ + "deployment": ControllerNonMigrated, // KCM only + "route": ControllerMigrated, + "service": ControllerMigrated, + "cloud-node-lifecycle": ControllerMigrated, + }, + }, + { + name: "wildcard config, ccm", + config: wildcardConfig, + component: "cloud-controller-manager", + expectResult: map[string]FilterResult{ + "cloud-node": ControllerNonMigrated, // CCM only + "route": ControllerMigrated, + "service": ControllerMigrated, + "cloud-node-lifecycle": ControllerMigrated, + }, + }, } { t.Run(tc.name, func(t *testing.T) { migrator := NewLeaderMigrator(tc.config, tc.component) From 6ba7b3d26b36d4436d5acdc4914879f831eafe64 Mon Sep 17 00:00:00 2001 From: Indeed Date: Tue, 8 Jun 2021 15:22:11 -0700 Subject: [PATCH 3/3] allow enabling Leader Migration without config flag. --- .../pkg/leadermigration/options/options.go | 3 ++- .../pkg/leadermigration/options/options_test.go | 6 ++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/staging/src/k8s.io/controller-manager/pkg/leadermigration/options/options.go b/staging/src/k8s.io/controller-manager/pkg/leadermigration/options/options.go index cc8eb83bd4d..5bd078ec782 100644 --- a/staging/src/k8s.io/controller-manager/pkg/leadermigration/options/options.go +++ b/staging/src/k8s.io/controller-manager/pkg/leadermigration/options/options.go @@ -73,7 +73,8 @@ func (o *LeaderMigrationOptions) ApplyTo(cfg *config.GenericControllerManagerCon return nil } if o.ControllerMigrationConfig == "" { - return fmt.Errorf("--leader-migration-config is required") + cfg.LeaderMigration = *migrationconfig.DefaultLeaderMigrationConfiguration() + return nil } leaderMigrationConfig, err := migrationconfig.ReadLeaderMigrationConfiguration(o.ControllerMigrationConfig) if err != nil { diff --git a/staging/src/k8s.io/controller-manager/pkg/leadermigration/options/options_test.go b/staging/src/k8s.io/controller-manager/pkg/leadermigration/options/options_test.go index 8d668896749..59af7ee1ae3 100644 --- a/staging/src/k8s.io/controller-manager/pkg/leadermigration/options/options_test.go +++ b/staging/src/k8s.io/controller-manager/pkg/leadermigration/options/options_test.go @@ -27,6 +27,7 @@ import ( featuregatetesting "k8s.io/component-base/featuregate/testing" "k8s.io/controller-manager/config" "k8s.io/controller-manager/pkg/features" + migrationconfig "k8s.io/controller-manager/pkg/leadermigration/config" ) func TestLeaderMigrationOptions(t *testing.T) { @@ -53,11 +54,12 @@ func TestLeaderMigrationOptions(t *testing.T) { expectErr: true, }, { - name: "enabled, but missing configuration file", + name: "enabled, with default configuration", flags: []string{"--enable-leader-migration"}, enableFeatureGate: true, expectEnabled: true, - expectErr: true, + expectErr: false, + expectConfig: migrationconfig.DefaultLeaderMigrationConfiguration(), }, { name: "enabled, with custom configuration file",