From ee6e28b5cfac62be8cb89962c8d870186b674470 Mon Sep 17 00:00:00 2001 From: chenyixiang <283158956@qq.com> Date: Sun, 28 Jul 2019 16:30:45 +0800 Subject: [PATCH 1/2] add options for name and namespace of leaderelection object Change-Id: Iaa62f5f1c3b24a4cc567a840707d9eba7cf901b8 --- .../app/controllermanager.go | 4 ++-- .../app/options/options.go | 3 +++ .../app/options/options_test.go | 24 +++++++++++-------- .../app/controllermanager.go | 5 ++-- .../app/options/options.go | 2 ++ .../app/options/options_test.go | 12 ++++++---- pkg/client/leaderelectionconfig/config.go | 6 +++++ .../src/k8s.io/component-base/config/types.go | 6 +++++ .../component-base/config/v1alpha1/types.go | 6 +++++ .../v1alpha1/zz_generated.conversion.go | 4 ++++ 10 files changed, 53 insertions(+), 19 deletions(-) diff --git a/cmd/cloud-controller-manager/app/controllermanager.go b/cmd/cloud-controller-manager/app/controllermanager.go index 26b37ab9266..726e06b63bc 100644 --- a/cmd/cloud-controller-manager/app/controllermanager.go +++ b/cmd/cloud-controller-manager/app/controllermanager.go @@ -186,8 +186,8 @@ func Run(c *cloudcontrollerconfig.CompletedConfig, stopCh <-chan struct{}) error // Lock required for leader election rl, err := resourcelock.New(c.ComponentConfig.Generic.LeaderElection.ResourceLock, - "kube-system", - "cloud-controller-manager", + c.ComponentConfig.Generic.LeaderElection.ResourceNamespace, + c.ComponentConfig.Generic.LeaderElection.ResourceName, c.LeaderElectionClient.CoreV1(), c.LeaderElectionClient.CoordinationV1(), resourcelock.ResourceLockConfig{ diff --git a/cmd/cloud-controller-manager/app/options/options.go b/cmd/cloud-controller-manager/app/options/options.go index 205e5da99b3..d64f0b02f51 100644 --- a/cmd/cloud-controller-manager/app/options/options.go +++ b/cmd/cloud-controller-manager/app/options/options.go @@ -107,6 +107,9 @@ func NewCloudControllerManagerOptions() (*CloudControllerManagerOptions, error) s.SecureServing.ServerCert.PairName = "cloud-controller-manager" s.SecureServing.BindPort = ports.CloudControllerManagerPort + s.Generic.LeaderElection.ResourceName = "cloud-controller-manager" + s.Generic.LeaderElection.ResourceNamespace = "kube-system" + return &s, nil } diff --git a/cmd/cloud-controller-manager/app/options/options_test.go b/cmd/cloud-controller-manager/app/options/options_test.go index 9505238b025..80eab7bafab 100644 --- a/cmd/cloud-controller-manager/app/options/options_test.go +++ b/cmd/cloud-controller-manager/app/options/options_test.go @@ -49,11 +49,13 @@ func TestDefaultFlags(t *testing.T) { }, ControllerStartInterval: metav1.Duration{Duration: 0}, LeaderElection: componentbaseconfig.LeaderElectionConfiguration{ - ResourceLock: "endpoints", - LeaderElect: true, - LeaseDuration: metav1.Duration{Duration: 15 * time.Second}, - RenewDeadline: metav1.Duration{Duration: 10 * time.Second}, - RetryPeriod: metav1.Duration{Duration: 2 * time.Second}, + ResourceLock: "endpoints", + LeaderElect: true, + LeaseDuration: metav1.Duration{Duration: 15 * time.Second}, + RenewDeadline: metav1.Duration{Duration: 10 * time.Second}, + RetryPeriod: metav1.Duration{Duration: 2 * time.Second}, + ResourceName: "cloud-controller-manager", + ResourceNamespace: "kube-system", }, Controllers: []string{"*"}, }, @@ -178,11 +180,13 @@ func TestAddFlags(t *testing.T) { }, ControllerStartInterval: metav1.Duration{Duration: 2 * time.Minute}, LeaderElection: componentbaseconfig.LeaderElectionConfiguration{ - ResourceLock: "configmap", - LeaderElect: false, - LeaseDuration: metav1.Duration{Duration: 30 * time.Second}, - RenewDeadline: metav1.Duration{Duration: 15 * time.Second}, - RetryPeriod: metav1.Duration{Duration: 5 * time.Second}, + ResourceLock: "configmap", + LeaderElect: false, + LeaseDuration: metav1.Duration{Duration: 30 * time.Second}, + RenewDeadline: metav1.Duration{Duration: 15 * time.Second}, + RetryPeriod: metav1.Duration{Duration: 5 * time.Second}, + ResourceName: "cloud-controller-manager", + ResourceNamespace: "kube-system", }, Controllers: []string{"foo", "bar"}, }, diff --git a/cmd/kube-controller-manager/app/controllermanager.go b/cmd/kube-controller-manager/app/controllermanager.go index 6fd08cfdc06..5a4da72fa6c 100644 --- a/cmd/kube-controller-manager/app/controllermanager.go +++ b/cmd/kube-controller-manager/app/controllermanager.go @@ -257,9 +257,10 @@ func Run(c *config.CompletedConfig, stopCh <-chan struct{}) error { // add a uniquifier so that two processes on the same host don't accidentally both become active id = id + "_" + string(uuid.NewUUID()) + rl, err := resourcelock.New(c.ComponentConfig.Generic.LeaderElection.ResourceLock, - "kube-system", - "kube-controller-manager", + c.ComponentConfig.Generic.LeaderElection.ResourceNamespace, + c.ComponentConfig.Generic.LeaderElection.ResourceName, c.LeaderElectionClient.CoreV1(), c.LeaderElectionClient.CoordinationV1(), resourcelock.ResourceLockConfig{ diff --git a/cmd/kube-controller-manager/app/options/options.go b/cmd/kube-controller-manager/app/options/options.go index cad1edb2979..cfa0d9d709c 100644 --- a/cmd/kube-controller-manager/app/options/options.go +++ b/cmd/kube-controller-manager/app/options/options.go @@ -184,6 +184,8 @@ func NewKubeControllerManagerOptions() (*KubeControllerManagerOptions, error) { } s.GarbageCollectorController.GCIgnoredResources = gcIgnoredResources + s.Generic.LeaderElection.ResourceName = "kube-controller-manager" + s.Generic.LeaderElection.ResourceNamespace = "kube-system" return &s, nil } diff --git a/cmd/kube-controller-manager/app/options/options_test.go b/cmd/kube-controller-manager/app/options/options_test.go index c1b10aea590..6290bcf1b5a 100644 --- a/cmd/kube-controller-manager/app/options/options_test.go +++ b/cmd/kube-controller-manager/app/options/options_test.go @@ -154,11 +154,13 @@ func TestAddFlags(t *testing.T) { }, ControllerStartInterval: metav1.Duration{Duration: 2 * time.Minute}, LeaderElection: componentbaseconfig.LeaderElectionConfiguration{ - ResourceLock: "configmap", - LeaderElect: false, - LeaseDuration: metav1.Duration{Duration: 30 * time.Second}, - RenewDeadline: metav1.Duration{Duration: 15 * time.Second}, - RetryPeriod: metav1.Duration{Duration: 5 * time.Second}, + ResourceLock: "configmap", + LeaderElect: false, + LeaseDuration: metav1.Duration{Duration: 30 * time.Second}, + RenewDeadline: metav1.Duration{Duration: 15 * time.Second}, + RetryPeriod: metav1.Duration{Duration: 5 * time.Second}, + ResourceName: "kube-controller-manager", + ResourceNamespace: "kube-system", }, Controllers: []string{"foo", "bar"}, }, diff --git a/pkg/client/leaderelectionconfig/config.go b/pkg/client/leaderelectionconfig/config.go index 223e24fa51b..bbdc977cc54 100644 --- a/pkg/client/leaderelectionconfig/config.go +++ b/pkg/client/leaderelectionconfig/config.go @@ -43,4 +43,10 @@ func BindFlags(l *componentbaseconfig.LeaderElectionConfiguration, fs *pflag.Fla fs.StringVar(&l.ResourceLock, "leader-elect-resource-lock", l.ResourceLock, ""+ "The type of resource object that is used for locking during "+ "leader election. Supported options are `endpoints` (default) and `configmaps`.") + fs.StringVar(&l.ResourceName, "leader-elect-resource-name", l.ResourceName, ""+ + "The name of resource object that is used for locking during "+ + "leader election.") + fs.StringVar(&l.ResourceNamespace, "leader-elect-resource-namespace", l.ResourceNamespace, ""+ + "The namespace of resource object that is used for locking during "+ + "leader election.") } diff --git a/staging/src/k8s.io/component-base/config/types.go b/staging/src/k8s.io/component-base/config/types.go index f0a20915cc8..da11e03c2c6 100644 --- a/staging/src/k8s.io/component-base/config/types.go +++ b/staging/src/k8s.io/component-base/config/types.go @@ -62,6 +62,12 @@ type LeaderElectionConfiguration struct { // resourceLock indicates the resource object type that will be used to lock // during leader election cycles. ResourceLock string + // resourceName indicates the name of resource object that will be used to lock + // during leader election cycles. + ResourceName string + // resourceName indicates the namespace of resource object that will be used to lock + // during leader election cycles. + ResourceNamespace string } // DebuggingConfiguration holds configuration for Debugging related features. diff --git a/staging/src/k8s.io/component-base/config/v1alpha1/types.go b/staging/src/k8s.io/component-base/config/v1alpha1/types.go index d5a0391a209..a5202e3e9d2 100644 --- a/staging/src/k8s.io/component-base/config/v1alpha1/types.go +++ b/staging/src/k8s.io/component-base/config/v1alpha1/types.go @@ -48,6 +48,12 @@ type LeaderElectionConfiguration struct { // resourceLock indicates the resource object type that will be used to lock // during leader election cycles. ResourceLock string `json:"resourceLock"` + // resourceName indicates the name of resource object that will be used to lock + // during leader election cycles. + ResourceName string `json:"resourceName"` + // resourceName indicates the namespace of resource object that will be used to lock + // during leader election cycles. + ResourceNamespace string `json:"resourceNamespace"` } // DebuggingConfiguration holds configuration for Debugging related features. diff --git a/staging/src/k8s.io/component-base/config/v1alpha1/zz_generated.conversion.go b/staging/src/k8s.io/component-base/config/v1alpha1/zz_generated.conversion.go index 55f0dac0390..c62ee734b89 100644 --- a/staging/src/k8s.io/component-base/config/v1alpha1/zz_generated.conversion.go +++ b/staging/src/k8s.io/component-base/config/v1alpha1/zz_generated.conversion.go @@ -135,6 +135,8 @@ func autoConvert_v1alpha1_LeaderElectionConfiguration_To_config_LeaderElectionCo out.RenewDeadline = in.RenewDeadline out.RetryPeriod = in.RetryPeriod out.ResourceLock = in.ResourceLock + out.ResourceName = in.ResourceName + out.ResourceNamespace = in.ResourceNamespace return nil } @@ -146,5 +148,7 @@ func autoConvert_config_LeaderElectionConfiguration_To_v1alpha1_LeaderElectionCo out.RenewDeadline = in.RenewDeadline out.RetryPeriod = in.RetryPeriod out.ResourceLock = in.ResourceLock + out.ResourceName = in.ResourceName + out.ResourceNamespace = in.ResourceNamespace return nil } From 41a435a812690431f9f449f08fe28967a8aed582 Mon Sep 17 00:00:00 2001 From: chenyixiang <283158956@qq.com> Date: Tue, 30 Jul 2019 01:45:05 +0800 Subject: [PATCH 2/2] migrate scheduler options to resourceName & resourceNamespace Change-Id: I743eda488320c97c123b49018d7efcc57525b152 --- cmd/kube-scheduler/app/options/deprecated.go | 5 +- cmd/kube-scheduler/app/options/options.go | 4 +- .../app/options/options_test.go | 42 ++-- pkg/scheduler/apis/config/types.go | 4 - pkg/scheduler/apis/config/v1alpha1/BUILD | 10 +- .../apis/config/v1alpha1/conversion.go | 68 ++++++ .../apis/config/v1alpha1/conversion_test.go | 193 ++++++++++++++++++ .../apis/config/v1alpha1/defaults.go | 14 +- .../v1alpha1/zz_generated.conversion.go | 40 ++-- .../apis/config/validation/validation.go | 6 - .../apis/config/validation/validation_test.go | 30 +-- .../config/validation/validation.go | 10 +- .../config/validation/validation_test.go | 26 ++- .../kube-scheduler/config/v1alpha1/types.go | 2 + 14 files changed, 368 insertions(+), 86 deletions(-) create mode 100644 pkg/scheduler/apis/config/v1alpha1/conversion.go create mode 100644 pkg/scheduler/apis/config/v1alpha1/conversion_test.go diff --git a/cmd/kube-scheduler/app/options/deprecated.go b/cmd/kube-scheduler/app/options/deprecated.go index 7d14a5c64f6..de85a0a34c6 100644 --- a/cmd/kube-scheduler/app/options/deprecated.go +++ b/cmd/kube-scheduler/app/options/deprecated.go @@ -18,6 +18,7 @@ package options import ( "fmt" + "github.com/spf13/pflag" "k8s.io/apimachinery/pkg/util/validation/field" @@ -58,8 +59,8 @@ func (o *DeprecatedOptions) AddFlags(fs *pflag.FlagSet, cfg *kubeschedulerconfig fs.Float32Var(&cfg.ClientConnection.QPS, "kube-api-qps", cfg.ClientConnection.QPS, "DEPRECATED: QPS to use while talking with kubernetes apiserver") fs.Int32Var(&cfg.ClientConnection.Burst, "kube-api-burst", cfg.ClientConnection.Burst, "DEPRECATED: burst to use while talking with kubernetes apiserver") fs.StringVar(&cfg.SchedulerName, "scheduler-name", cfg.SchedulerName, "DEPRECATED: name of the scheduler, used to select which pods will be processed by this scheduler, based on pod's \"spec.schedulerName\".") - fs.StringVar(&cfg.LeaderElection.LockObjectNamespace, "lock-object-namespace", cfg.LeaderElection.LockObjectNamespace, "DEPRECATED: define the namespace of the lock object.") - fs.StringVar(&cfg.LeaderElection.LockObjectName, "lock-object-name", cfg.LeaderElection.LockObjectName, "DEPRECATED: define the name of the lock object.") + fs.StringVar(&cfg.LeaderElection.ResourceNamespace, "lock-object-namespace", cfg.LeaderElection.ResourceNamespace, "DEPRECATED: define the namespace of the lock object. Will be removed in favor of leader-elect-resource-namespace.") + fs.StringVar(&cfg.LeaderElection.ResourceName, "lock-object-name", cfg.LeaderElection.ResourceName, "DEPRECATED: define the name of the lock object. Will be removed in favor of leader-elect-resource-name") fs.Int32Var(&cfg.HardPodAffinitySymmetricWeight, "hard-pod-affinity-symmetric-weight", cfg.HardPodAffinitySymmetricWeight, "RequiredDuringScheduling affinity is not symmetric, but there is an implicit PreferredDuringScheduling affinity rule corresponding "+ diff --git a/cmd/kube-scheduler/app/options/options.go b/cmd/kube-scheduler/app/options/options.go index c43ae2431e2..663dbb428e2 100644 --- a/cmd/kube-scheduler/app/options/options.go +++ b/cmd/kube-scheduler/app/options/options.go @@ -275,8 +275,8 @@ func makeLeaderElectionConfig(config kubeschedulerconfig.KubeSchedulerLeaderElec id := hostname + "_" + string(uuid.NewUUID()) rl, err := resourcelock.New(config.ResourceLock, - config.LockObjectNamespace, - config.LockObjectName, + config.ResourceNamespace, + config.ResourceName, client.CoreV1(), client.CoordinationV1(), resourcelock.ResourceLockConfig{ diff --git a/cmd/kube-scheduler/app/options/options_test.go b/cmd/kube-scheduler/app/options/options_test.go index 65418e72e6b..fa4f4f36258 100644 --- a/cmd/kube-scheduler/app/options/options_test.go +++ b/cmd/kube-scheduler/app/options/options_test.go @@ -234,14 +234,14 @@ pluginConfig: MetricsBindAddress: "0.0.0.0:10251", LeaderElection: kubeschedulerconfig.KubeSchedulerLeaderElectionConfiguration{ LeaderElectionConfiguration: componentbaseconfig.LeaderElectionConfiguration{ - LeaderElect: true, - LeaseDuration: metav1.Duration{Duration: 15 * time.Second}, - RenewDeadline: metav1.Duration{Duration: 10 * time.Second}, - RetryPeriod: metav1.Duration{Duration: 2 * time.Second}, - ResourceLock: "endpoints", + LeaderElect: true, + LeaseDuration: metav1.Duration{Duration: 15 * time.Second}, + RenewDeadline: metav1.Duration{Duration: 10 * time.Second}, + RetryPeriod: metav1.Duration{Duration: 2 * time.Second}, + ResourceLock: "endpoints", + ResourceNamespace: "kube-system", + ResourceName: "kube-scheduler", }, - LockObjectNamespace: "kube-system", - LockObjectName: "kube-scheduler", }, ClientConnection: componentbaseconfig.ClientConnectionConfiguration{ Kubeconfig: configKubeconfig, @@ -314,14 +314,14 @@ pluginConfig: MetricsBindAddress: "", // defaults empty when not running from config file LeaderElection: kubeschedulerconfig.KubeSchedulerLeaderElectionConfiguration{ LeaderElectionConfiguration: componentbaseconfig.LeaderElectionConfiguration{ - LeaderElect: true, - LeaseDuration: metav1.Duration{Duration: 15 * time.Second}, - RenewDeadline: metav1.Duration{Duration: 10 * time.Second}, - RetryPeriod: metav1.Duration{Duration: 2 * time.Second}, - ResourceLock: "endpoints", + LeaderElect: true, + LeaseDuration: metav1.Duration{Duration: 15 * time.Second}, + RenewDeadline: metav1.Duration{Duration: 10 * time.Second}, + RetryPeriod: metav1.Duration{Duration: 2 * time.Second}, + ResourceLock: "endpoints", + ResourceNamespace: "kube-system", + ResourceName: "kube-scheduler", }, - LockObjectNamespace: "kube-system", - LockObjectName: "kube-scheduler", }, ClientConnection: componentbaseconfig.ClientConnectionConfiguration{ Kubeconfig: flagKubeconfig, @@ -375,14 +375,14 @@ pluginConfig: MetricsBindAddress: "0.0.0.0:10251", LeaderElection: kubeschedulerconfig.KubeSchedulerLeaderElectionConfiguration{ LeaderElectionConfiguration: componentbaseconfig.LeaderElectionConfiguration{ - LeaderElect: true, - LeaseDuration: metav1.Duration{Duration: 15 * time.Second}, - RenewDeadline: metav1.Duration{Duration: 10 * time.Second}, - RetryPeriod: metav1.Duration{Duration: 2 * time.Second}, - ResourceLock: "endpoints", + LeaderElect: true, + LeaseDuration: metav1.Duration{Duration: 15 * time.Second}, + RenewDeadline: metav1.Duration{Duration: 10 * time.Second}, + RetryPeriod: metav1.Duration{Duration: 2 * time.Second}, + ResourceLock: "endpoints", + ResourceNamespace: "kube-system", + ResourceName: "kube-scheduler", }, - LockObjectNamespace: "kube-system", - LockObjectName: "kube-scheduler", }, ClientConnection: componentbaseconfig.ClientConnectionConfiguration{ Kubeconfig: configKubeconfig, diff --git a/pkg/scheduler/apis/config/types.go b/pkg/scheduler/apis/config/types.go index 4c2c5f8c2c2..9577d2d8c3b 100644 --- a/pkg/scheduler/apis/config/types.go +++ b/pkg/scheduler/apis/config/types.go @@ -138,10 +138,6 @@ type SchedulerPolicyConfigMapSource struct { // to include scheduler specific configuration. type KubeSchedulerLeaderElectionConfiguration struct { componentbaseconfig.LeaderElectionConfiguration - // LockObjectNamespace defines the namespace of the lock object - LockObjectNamespace string - // LockObjectName defines the lock object name - LockObjectName string } // Plugins include multiple extension points. When specified, the list of plugins for diff --git a/pkg/scheduler/apis/config/v1alpha1/BUILD b/pkg/scheduler/apis/config/v1alpha1/BUILD index 87559a24756..881255ab564 100644 --- a/pkg/scheduler/apis/config/v1alpha1/BUILD +++ b/pkg/scheduler/apis/config/v1alpha1/BUILD @@ -3,6 +3,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") go_library( name = "go_default_library", srcs = [ + "conversion.go", "defaults.go", "doc.go", "register.go", @@ -27,12 +28,19 @@ go_library( go_test( name = "go_default_test", - srcs = ["defaults_test.go"], + srcs = [ + "conversion_test.go", + "defaults_test.go", + ], embed = [":go_default_library"], deps = [ + "//pkg/scheduler/apis/config:go_default_library", "//staging/src/k8s.io/api/core/v1:go_default_library", "//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", + "//staging/src/k8s.io/apimachinery/pkg/conversion:go_default_library", "//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library", + "//staging/src/k8s.io/component-base/config:go_default_library", + "//staging/src/k8s.io/component-base/config/v1alpha1:go_default_library", "//staging/src/k8s.io/kube-scheduler/config/v1alpha1:go_default_library", ], ) diff --git a/pkg/scheduler/apis/config/v1alpha1/conversion.go b/pkg/scheduler/apis/config/v1alpha1/conversion.go new file mode 100644 index 00000000000..bc43dd6b8ab --- /dev/null +++ b/pkg/scheduler/apis/config/v1alpha1/conversion.go @@ -0,0 +1,68 @@ +/* +Copyright 2019 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 v1alpha1 + +import ( + "fmt" + + conversion "k8s.io/apimachinery/pkg/conversion" + v1alpha1 "k8s.io/kube-scheduler/config/v1alpha1" + config "k8s.io/kubernetes/pkg/scheduler/apis/config" +) + +// Convert_v1alpha1_KubeSchedulerLeaderElectionConfiguration_To_config_KubeSchedulerLeaderElectionConfiguration is an autogenerated conversion function. +func Convert_v1alpha1_KubeSchedulerLeaderElectionConfiguration_To_config_KubeSchedulerLeaderElectionConfiguration(in *v1alpha1.KubeSchedulerLeaderElectionConfiguration, out *config.KubeSchedulerLeaderElectionConfiguration, s conversion.Scope) error { + if err := autoConvert_v1alpha1_KubeSchedulerLeaderElectionConfiguration_To_config_KubeSchedulerLeaderElectionConfiguration(in, out, s); err != nil { + return err + } + if len(in.ResourceNamespace) > 0 && len(in.LockObjectNamespace) == 0 { + out.ResourceNamespace = in.ResourceNamespace + } else if len(in.ResourceNamespace) == 0 && len(in.LockObjectNamespace) > 0 { + out.ResourceNamespace = in.LockObjectNamespace + } else if len(in.ResourceNamespace) > 0 && len(in.LockObjectNamespace) > 0 { + if in.ResourceNamespace == in.LockObjectNamespace { + out.ResourceNamespace = in.ResourceNamespace + } else { + return fmt.Errorf("ResourceNamespace and LockObjectNamespace are both set and do not match, ResourceNamespace: %s, LockObjectNamespace: %s", in.ResourceNamespace, in.LockObjectNamespace) + } + } + + if len(in.ResourceName) > 0 && len(in.LockObjectName) == 0 { + out.ResourceName = in.ResourceName + } else if len(in.ResourceName) == 0 && len(in.LockObjectName) > 0 { + out.ResourceName = in.LockObjectName + } else if len(in.ResourceName) > 0 && len(in.LockObjectName) > 0 { + if in.ResourceName == in.LockObjectName { + out.ResourceName = in.ResourceName + } else { + return fmt.Errorf("ResourceName and LockObjectName are both set and do not match, ResourceName: %s, LockObjectName: %s", in.ResourceName, in.LockObjectName) + } + } + return nil +} + +// Convert_config_KubeSchedulerLeaderElectionConfiguration_To_v1alpha1_KubeSchedulerLeaderElectionConfiguration is an autogenerated conversion function. +func Convert_config_KubeSchedulerLeaderElectionConfiguration_To_v1alpha1_KubeSchedulerLeaderElectionConfiguration(in *config.KubeSchedulerLeaderElectionConfiguration, out *v1alpha1.KubeSchedulerLeaderElectionConfiguration, s conversion.Scope) error { + if err := autoConvert_config_KubeSchedulerLeaderElectionConfiguration_To_v1alpha1_KubeSchedulerLeaderElectionConfiguration(in, out, s); err != nil { + return err + } + out.ResourceNamespace = in.ResourceNamespace + out.LockObjectNamespace = in.ResourceNamespace + out.ResourceName = in.ResourceName + out.LockObjectName = in.ResourceName + return nil +} diff --git a/pkg/scheduler/apis/config/v1alpha1/conversion_test.go b/pkg/scheduler/apis/config/v1alpha1/conversion_test.go new file mode 100644 index 00000000000..b18473d3cc6 --- /dev/null +++ b/pkg/scheduler/apis/config/v1alpha1/conversion_test.go @@ -0,0 +1,193 @@ +/* +Copyright 2019 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 v1alpha1 + +import ( + "testing" + + conversion "k8s.io/apimachinery/pkg/conversion" + componentbaseconfig "k8s.io/component-base/config" + componentbaseconfigv1alpha1 "k8s.io/component-base/config/v1alpha1" + v1alpha1 "k8s.io/kube-scheduler/config/v1alpha1" + config "k8s.io/kubernetes/pkg/scheduler/apis/config" +) + +func TestV1alpha1ToConfigKubeSchedulerLeaderElectionConfiguration(t *testing.T) { + configuration := &v1alpha1.KubeSchedulerLeaderElectionConfiguration{ + LockObjectName: "name", + LockObjectNamespace: "namespace", + LeaderElectionConfiguration: componentbaseconfigv1alpha1.LeaderElectionConfiguration{ + ResourceName: "name", + ResourceNamespace: "namespace", + }, + } + emptyLockObjectNameConfig := configuration.DeepCopy() + emptyLockObjectNameConfig.LockObjectName = "" + + emptyLockObjectNamespaceConfig := configuration.DeepCopy() + emptyLockObjectNamespaceConfig.LockObjectNamespace = "" + + emptyResourceNameConfig := configuration.DeepCopy() + emptyResourceNameConfig.ResourceName = "" + + emptyResourceNamespaceConfig := configuration.DeepCopy() + emptyResourceNamespaceConfig.ResourceNamespace = "" + + differentNameConfig := configuration.DeepCopy() + differentNameConfig.LockObjectName = "name1" + + differentNamespaceConfig := configuration.DeepCopy() + differentNamespaceConfig.LockObjectNamespace = "namespace1" + + emptyconfig := &v1alpha1.KubeSchedulerLeaderElectionConfiguration{} + + scenarios := map[string]struct { + expectedResourceNamespace string + expectedResourceName string + expectedToFailed bool + config *v1alpha1.KubeSchedulerLeaderElectionConfiguration + }{ + "both-set-same-name-and-namespace": { + expectedResourceNamespace: "namespace", + expectedResourceName: "name", + expectedToFailed: false, + config: configuration, + }, + "not-set-lock-object-name": { + expectedResourceNamespace: "namespace", + expectedResourceName: "name", + expectedToFailed: false, + config: emptyLockObjectNameConfig, + }, + "not-set-lock-object-namespace": { + expectedResourceNamespace: "namespace", + expectedResourceName: "name", + expectedToFailed: false, + config: emptyLockObjectNamespaceConfig, + }, + "not-set-resource-name": { + expectedResourceNamespace: "namespace", + expectedResourceName: "name", + expectedToFailed: false, + config: emptyResourceNameConfig, + }, + "not-set-resource-namespace": { + expectedResourceNamespace: "namespace", + expectedResourceName: "name", + expectedToFailed: false, + config: emptyResourceNamespaceConfig, + }, + "set-different-name": { + expectedResourceNamespace: "", + expectedResourceName: "", + expectedToFailed: true, + config: differentNameConfig, + }, + "set-different-namespace": { + expectedResourceNamespace: "", + expectedResourceName: "", + expectedToFailed: true, + config: differentNamespaceConfig, + }, + "set-empty-name-and-namespace": { + expectedResourceNamespace: "", + expectedResourceName: "", + expectedToFailed: false, + config: emptyconfig, + }, + } + for name, scenario := range scenarios { + out := &config.KubeSchedulerLeaderElectionConfiguration{} + s := conversion.Scope(nil) + err := Convert_v1alpha1_KubeSchedulerLeaderElectionConfiguration_To_config_KubeSchedulerLeaderElectionConfiguration(scenario.config, out, s) + if err == nil && scenario.expectedToFailed { + t.Errorf("Unexpected success for scenario: %s", name) + } + if err == nil && !scenario.expectedToFailed { + if out.ResourceName != scenario.expectedResourceName { + t.Errorf("Unexpected success for scenario: %s, out.ResourceName: %s, expectedResourceName: %s", name, out.ResourceName, scenario.expectedResourceName) + } + if out.ResourceNamespace != scenario.expectedResourceNamespace { + t.Errorf("Unexpected success for scenario: %s, out.ResourceNamespace: %s, expectedResourceNamespace: %s", name, out.ResourceNamespace, scenario.expectedResourceNamespace) + } + } + if err != nil && !scenario.expectedToFailed { + t.Errorf("Unexpected failure for scenario: %s - %+v", name, err) + } + } +} + +func TestConfigToV1alpha1KubeSchedulerLeaderElectionConfiguration(t *testing.T) { + configuration := &config.KubeSchedulerLeaderElectionConfiguration{ + LeaderElectionConfiguration: componentbaseconfig.LeaderElectionConfiguration{ + ResourceName: "name", + ResourceNamespace: "namespace", + }, + } + emptyconfig := &config.KubeSchedulerLeaderElectionConfiguration{} + + scenarios := map[string]struct { + expectedResourceNamespace string + expectedResourceName string + expectedLockObjectNamespace string + expectedLockObjectName string + expectedToFailed bool + config *config.KubeSchedulerLeaderElectionConfiguration + }{ + "both-set-name-and-namespace": { + expectedResourceNamespace: "namespace", + expectedResourceName: "name", + expectedLockObjectNamespace: "namespace", + expectedLockObjectName: "name", + expectedToFailed: false, + config: configuration, + }, + "set-empty-name-and-namespace": { + expectedResourceNamespace: "", + expectedResourceName: "", + expectedLockObjectNamespace: "", + expectedLockObjectName: "", + expectedToFailed: false, + config: emptyconfig, + }, + } + for name, scenario := range scenarios { + out := &v1alpha1.KubeSchedulerLeaderElectionConfiguration{} + s := conversion.Scope(nil) + err := Convert_config_KubeSchedulerLeaderElectionConfiguration_To_v1alpha1_KubeSchedulerLeaderElectionConfiguration(scenario.config, out, s) + if err == nil && scenario.expectedToFailed { + t.Errorf("Unexpected success for scenario: %s", name) + } + if err == nil && !scenario.expectedToFailed { + if out.ResourceName != scenario.expectedResourceName { + t.Errorf("Unexpected success for scenario: %s, out.ResourceName: %s, expectedResourceName: %s", name, out.ResourceName, scenario.expectedResourceName) + } + if out.LockObjectName != scenario.expectedLockObjectName { + t.Errorf("Unexpected success for scenario: %s, out.LockObjectName: %s, expectedLockObjectName: %s", name, out.LockObjectName, scenario.expectedLockObjectName) + } + if out.ResourceNamespace != scenario.expectedResourceNamespace { + t.Errorf("Unexpected success for scenario: %s, out.ResourceNamespace: %s, expectedResourceNamespace: %s", name, out.ResourceNamespace, scenario.expectedResourceNamespace) + } + if out.LockObjectNamespace != scenario.expectedLockObjectNamespace { + t.Errorf("Unexpected success for scenario: %s, out.LockObjectNamespace: %s, expectedLockObjectNamespace: %s", name, out.LockObjectNamespace, scenario.expectedLockObjectNamespace) + } + } + if err != nil && !scenario.expectedToFailed { + t.Errorf("Unexpected failure for scenario: %s - %+v", name, err) + } + } +} diff --git a/pkg/scheduler/apis/config/v1alpha1/defaults.go b/pkg/scheduler/apis/config/v1alpha1/defaults.go index 447467615fd..5b417ae23c5 100644 --- a/pkg/scheduler/apis/config/v1alpha1/defaults.go +++ b/pkg/scheduler/apis/config/v1alpha1/defaults.go @@ -23,7 +23,7 @@ import ( "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/runtime" componentbaseconfigv1alpha1 "k8s.io/component-base/config/v1alpha1" - kubescedulerconfigv1alpha1 "k8s.io/kube-scheduler/config/v1alpha1" + kubeschedulerconfigv1alpha1 "k8s.io/kube-scheduler/config/v1alpha1" // this package shouldn't really depend on other k8s.io/kubernetes code api "k8s.io/kubernetes/pkg/apis/core" @@ -39,7 +39,7 @@ func addDefaultingFuncs(scheme *runtime.Scheme) error { } // SetDefaults_KubeSchedulerConfiguration sets additional defaults -func SetDefaults_KubeSchedulerConfiguration(obj *kubescedulerconfigv1alpha1.KubeSchedulerConfiguration) { +func SetDefaults_KubeSchedulerConfiguration(obj *kubeschedulerconfigv1alpha1.KubeSchedulerConfiguration) { if len(obj.SchedulerName) == 0 { obj.SchedulerName = api.DefaultSchedulerName } @@ -50,7 +50,7 @@ func SetDefaults_KubeSchedulerConfiguration(obj *kubescedulerconfigv1alpha1.Kube if obj.AlgorithmSource.Policy == nil && (obj.AlgorithmSource.Provider == nil || len(*obj.AlgorithmSource.Provider) == 0) { - val := kubescedulerconfigv1alpha1.SchedulerDefaultProviderName + val := kubeschedulerconfigv1alpha1.SchedulerDefaultProviderName obj.AlgorithmSource.Provider = &val } @@ -78,11 +78,11 @@ func SetDefaults_KubeSchedulerConfiguration(obj *kubescedulerconfigv1alpha1.Kube obj.MetricsBindAddress = net.JoinHostPort("0.0.0.0", strconv.Itoa(ports.InsecureSchedulerPort)) } - if len(obj.LeaderElection.LockObjectNamespace) == 0 { - obj.LeaderElection.LockObjectNamespace = kubescedulerconfigv1alpha1.SchedulerDefaultLockObjectNamespace + if len(obj.LeaderElection.LockObjectNamespace) == 0 && len(obj.LeaderElection.ResourceNamespace) == 0 { + obj.LeaderElection.LockObjectNamespace = kubeschedulerconfigv1alpha1.SchedulerDefaultLockObjectNamespace } - if len(obj.LeaderElection.LockObjectName) == 0 { - obj.LeaderElection.LockObjectName = kubescedulerconfigv1alpha1.SchedulerDefaultLockObjectName + if len(obj.LeaderElection.LockObjectName) == 0 && len(obj.LeaderElection.ResourceName) == 0 { + obj.LeaderElection.LockObjectName = kubeschedulerconfigv1alpha1.SchedulerDefaultLockObjectName } if len(obj.ClientConnection.ContentType) == 0 { diff --git a/pkg/scheduler/apis/config/v1alpha1/zz_generated.conversion.go b/pkg/scheduler/apis/config/v1alpha1/zz_generated.conversion.go index 863225c37fa..cb66f423eff 100644 --- a/pkg/scheduler/apis/config/v1alpha1/zz_generated.conversion.go +++ b/pkg/scheduler/apis/config/v1alpha1/zz_generated.conversion.go @@ -25,7 +25,7 @@ import ( conversion "k8s.io/apimachinery/pkg/conversion" runtime "k8s.io/apimachinery/pkg/runtime" - configv1alpha1 "k8s.io/component-base/config/v1alpha1" + componentbaseconfigv1alpha1 "k8s.io/component-base/config/v1alpha1" v1alpha1 "k8s.io/kube-scheduler/config/v1alpha1" config "k8s.io/kubernetes/pkg/scheduler/apis/config" ) @@ -137,6 +137,16 @@ func RegisterConversions(s *runtime.Scheme) error { }); err != nil { return err } + if err := s.AddConversionFunc((*config.KubeSchedulerLeaderElectionConfiguration)(nil), (*v1alpha1.KubeSchedulerLeaderElectionConfiguration)(nil), func(a, b interface{}, scope conversion.Scope) error { + return Convert_config_KubeSchedulerLeaderElectionConfiguration_To_v1alpha1_KubeSchedulerLeaderElectionConfiguration(a.(*config.KubeSchedulerLeaderElectionConfiguration), b.(*v1alpha1.KubeSchedulerLeaderElectionConfiguration), scope) + }); err != nil { + return err + } + if err := s.AddConversionFunc((*v1alpha1.KubeSchedulerLeaderElectionConfiguration)(nil), (*config.KubeSchedulerLeaderElectionConfiguration)(nil), func(a, b interface{}, scope conversion.Scope) error { + return Convert_v1alpha1_KubeSchedulerLeaderElectionConfiguration_To_config_KubeSchedulerLeaderElectionConfiguration(a.(*v1alpha1.KubeSchedulerLeaderElectionConfiguration), b.(*config.KubeSchedulerLeaderElectionConfiguration), scope) + }); err != nil { + return err + } return nil } @@ -149,12 +159,12 @@ func autoConvert_v1alpha1_KubeSchedulerConfiguration_To_config_KubeSchedulerConf if err := Convert_v1alpha1_KubeSchedulerLeaderElectionConfiguration_To_config_KubeSchedulerLeaderElectionConfiguration(&in.LeaderElection, &out.LeaderElection, s); err != nil { return err } - if err := configv1alpha1.Convert_v1alpha1_ClientConnectionConfiguration_To_config_ClientConnectionConfiguration(&in.ClientConnection, &out.ClientConnection, s); err != nil { + if err := componentbaseconfigv1alpha1.Convert_v1alpha1_ClientConnectionConfiguration_To_config_ClientConnectionConfiguration(&in.ClientConnection, &out.ClientConnection, s); err != nil { return err } out.HealthzBindAddress = in.HealthzBindAddress out.MetricsBindAddress = in.MetricsBindAddress - if err := configv1alpha1.Convert_v1alpha1_DebuggingConfiguration_To_config_DebuggingConfiguration(&in.DebuggingConfiguration, &out.DebuggingConfiguration, s); err != nil { + if err := componentbaseconfigv1alpha1.Convert_v1alpha1_DebuggingConfiguration_To_config_DebuggingConfiguration(&in.DebuggingConfiguration, &out.DebuggingConfiguration, s); err != nil { return err } out.DisablePreemption = in.DisablePreemption @@ -179,12 +189,12 @@ func autoConvert_config_KubeSchedulerConfiguration_To_v1alpha1_KubeSchedulerConf if err := Convert_config_KubeSchedulerLeaderElectionConfiguration_To_v1alpha1_KubeSchedulerLeaderElectionConfiguration(&in.LeaderElection, &out.LeaderElection, s); err != nil { return err } - if err := configv1alpha1.Convert_config_ClientConnectionConfiguration_To_v1alpha1_ClientConnectionConfiguration(&in.ClientConnection, &out.ClientConnection, s); err != nil { + if err := componentbaseconfigv1alpha1.Convert_config_ClientConnectionConfiguration_To_v1alpha1_ClientConnectionConfiguration(&in.ClientConnection, &out.ClientConnection, s); err != nil { return err } out.HealthzBindAddress = in.HealthzBindAddress out.MetricsBindAddress = in.MetricsBindAddress - if err := configv1alpha1.Convert_config_DebuggingConfiguration_To_v1alpha1_DebuggingConfiguration(&in.DebuggingConfiguration, &out.DebuggingConfiguration, s); err != nil { + if err := componentbaseconfigv1alpha1.Convert_config_DebuggingConfiguration_To_v1alpha1_DebuggingConfiguration(&in.DebuggingConfiguration, &out.DebuggingConfiguration, s); err != nil { return err } out.DisablePreemption = in.DisablePreemption @@ -201,33 +211,21 @@ func Convert_config_KubeSchedulerConfiguration_To_v1alpha1_KubeSchedulerConfigur } func autoConvert_v1alpha1_KubeSchedulerLeaderElectionConfiguration_To_config_KubeSchedulerLeaderElectionConfiguration(in *v1alpha1.KubeSchedulerLeaderElectionConfiguration, out *config.KubeSchedulerLeaderElectionConfiguration, s conversion.Scope) error { - if err := configv1alpha1.Convert_v1alpha1_LeaderElectionConfiguration_To_config_LeaderElectionConfiguration(&in.LeaderElectionConfiguration, &out.LeaderElectionConfiguration, s); err != nil { + if err := componentbaseconfigv1alpha1.Convert_v1alpha1_LeaderElectionConfiguration_To_config_LeaderElectionConfiguration(&in.LeaderElectionConfiguration, &out.LeaderElectionConfiguration, s); err != nil { return err } - out.LockObjectNamespace = in.LockObjectNamespace - out.LockObjectName = in.LockObjectName + // WARNING: in.LockObjectNamespace requires manual conversion: does not exist in peer-type + // WARNING: in.LockObjectName requires manual conversion: does not exist in peer-type return nil } -// Convert_v1alpha1_KubeSchedulerLeaderElectionConfiguration_To_config_KubeSchedulerLeaderElectionConfiguration is an autogenerated conversion function. -func Convert_v1alpha1_KubeSchedulerLeaderElectionConfiguration_To_config_KubeSchedulerLeaderElectionConfiguration(in *v1alpha1.KubeSchedulerLeaderElectionConfiguration, out *config.KubeSchedulerLeaderElectionConfiguration, s conversion.Scope) error { - return autoConvert_v1alpha1_KubeSchedulerLeaderElectionConfiguration_To_config_KubeSchedulerLeaderElectionConfiguration(in, out, s) -} - func autoConvert_config_KubeSchedulerLeaderElectionConfiguration_To_v1alpha1_KubeSchedulerLeaderElectionConfiguration(in *config.KubeSchedulerLeaderElectionConfiguration, out *v1alpha1.KubeSchedulerLeaderElectionConfiguration, s conversion.Scope) error { - if err := configv1alpha1.Convert_config_LeaderElectionConfiguration_To_v1alpha1_LeaderElectionConfiguration(&in.LeaderElectionConfiguration, &out.LeaderElectionConfiguration, s); err != nil { + if err := componentbaseconfigv1alpha1.Convert_config_LeaderElectionConfiguration_To_v1alpha1_LeaderElectionConfiguration(&in.LeaderElectionConfiguration, &out.LeaderElectionConfiguration, s); err != nil { return err } - out.LockObjectNamespace = in.LockObjectNamespace - out.LockObjectName = in.LockObjectName return nil } -// Convert_config_KubeSchedulerLeaderElectionConfiguration_To_v1alpha1_KubeSchedulerLeaderElectionConfiguration is an autogenerated conversion function. -func Convert_config_KubeSchedulerLeaderElectionConfiguration_To_v1alpha1_KubeSchedulerLeaderElectionConfiguration(in *config.KubeSchedulerLeaderElectionConfiguration, out *v1alpha1.KubeSchedulerLeaderElectionConfiguration, s conversion.Scope) error { - return autoConvert_config_KubeSchedulerLeaderElectionConfiguration_To_v1alpha1_KubeSchedulerLeaderElectionConfiguration(in, out, s) -} - func autoConvert_v1alpha1_Plugin_To_config_Plugin(in *v1alpha1.Plugin, out *config.Plugin, s conversion.Scope) error { out.Name = in.Name out.Weight = in.Weight diff --git a/pkg/scheduler/apis/config/validation/validation.go b/pkg/scheduler/apis/config/validation/validation.go index 0d44db9cd8d..d4851e7c055 100644 --- a/pkg/scheduler/apis/config/validation/validation.go +++ b/pkg/scheduler/apis/config/validation/validation.go @@ -57,11 +57,5 @@ func ValidateKubeSchedulerLeaderElectionConfiguration(cc *config.KubeSchedulerLe return allErrs } allErrs = append(allErrs, componentbasevalidation.ValidateLeaderElectionConfiguration(&cc.LeaderElectionConfiguration, field.NewPath("leaderElectionConfiguration"))...) - if len(cc.LockObjectNamespace) == 0 { - allErrs = append(allErrs, field.Required(fldPath.Child("lockObjectNamespace"), "")) - } - if len(cc.LockObjectName) == 0 { - allErrs = append(allErrs, field.Required(fldPath.Child("lockObjectName"), "")) - } return allErrs } diff --git a/pkg/scheduler/apis/config/validation/validation_test.go b/pkg/scheduler/apis/config/validation/validation_test.go index 4213a1e9eb2..f87ad274dd1 100644 --- a/pkg/scheduler/apis/config/validation/validation_test.go +++ b/pkg/scheduler/apis/config/validation/validation_test.go @@ -47,14 +47,14 @@ func TestValidateKubeSchedulerConfiguration(t *testing.T) { }, }, LeaderElection: config.KubeSchedulerLeaderElectionConfiguration{ - LockObjectNamespace: "name", - LockObjectName: "name", LeaderElectionConfiguration: componentbaseconfig.LeaderElectionConfiguration{ - ResourceLock: "configmap", - LeaderElect: true, - LeaseDuration: metav1.Duration{Duration: 30 * time.Second}, - RenewDeadline: metav1.Duration{Duration: 15 * time.Second}, - RetryPeriod: metav1.Duration{Duration: 5 * time.Second}, + ResourceLock: "configmap", + LeaderElect: true, + LeaseDuration: metav1.Duration{Duration: 30 * time.Second}, + RenewDeadline: metav1.Duration{Duration: 15 * time.Second}, + RetryPeriod: metav1.Duration{Duration: 5 * time.Second}, + ResourceNamespace: "name", + ResourceName: "name", }, }, BindTimeoutSeconds: &testTimeout, @@ -67,11 +67,11 @@ func TestValidateKubeSchedulerConfiguration(t *testing.T) { HardPodAffinitySymmetricWeightLt0 := validConfig.DeepCopy() HardPodAffinitySymmetricWeightLt0.HardPodAffinitySymmetricWeight = -1 - lockObjectNameNotSet := validConfig.DeepCopy() - lockObjectNameNotSet.LeaderElection.LockObjectName = "" + resourceNameNotSet := validConfig.DeepCopy() + resourceNameNotSet.LeaderElection.ResourceName = "" - lockObjectNamespaceNotSet := validConfig.DeepCopy() - lockObjectNamespaceNotSet.LeaderElection.LockObjectNamespace = "" + resourceNamespaceNotSet := validConfig.DeepCopy() + resourceNamespaceNotSet.LeaderElection.ResourceNamespace = "" metricsBindAddrHostInvalid := validConfig.DeepCopy() metricsBindAddrHostInvalid.MetricsBindAddress = "0.0.0.0.0:9090" @@ -103,13 +103,13 @@ func TestValidateKubeSchedulerConfiguration(t *testing.T) { expectedToFail: false, config: validConfig, }, - "bad-lock-object-names-not-set": { + "bad-resource-name-not-set": { expectedToFail: true, - config: lockObjectNameNotSet, + config: resourceNameNotSet, }, - "bad-lock-object-namespace-not-set": { + "bad-resource-namespace-not-set": { expectedToFail: true, - config: lockObjectNamespaceNotSet, + config: resourceNamespaceNotSet, }, "bad-healthz-port-invalid": { expectedToFail: true, diff --git a/staging/src/k8s.io/component-base/config/validation/validation.go b/staging/src/k8s.io/component-base/config/validation/validation.go index 6e66c0cec2e..4db15ea647d 100644 --- a/staging/src/k8s.io/component-base/config/validation/validation.go +++ b/staging/src/k8s.io/component-base/config/validation/validation.go @@ -40,7 +40,7 @@ func ValidateLeaderElectionConfiguration(cc *config.LeaderElectionConfiguration, allErrs = append(allErrs, field.Invalid(fldPath.Child("leaseDuration"), cc.LeaseDuration, "must be greater than zero")) } if cc.RenewDeadline.Duration <= 0 { - allErrs = append(allErrs, field.Invalid(fldPath.Child("renewDeadline"), cc.LeaseDuration, "must be greater than zero")) + allErrs = append(allErrs, field.Invalid(fldPath.Child("renewDeadline"), cc.RenewDeadline, "must be greater than zero")) } if cc.RetryPeriod.Duration <= 0 { allErrs = append(allErrs, field.Invalid(fldPath.Child("retryPeriod"), cc.RetryPeriod, "must be greater than zero")) @@ -49,7 +49,13 @@ func ValidateLeaderElectionConfiguration(cc *config.LeaderElectionConfiguration, allErrs = append(allErrs, field.Invalid(fldPath.Child("leaseDuration"), cc.RenewDeadline, "LeaseDuration must be greater than RenewDeadline")) } if len(cc.ResourceLock) == 0 { - allErrs = append(allErrs, field.Invalid(fldPath.Child("resourceLock"), cc.RenewDeadline, "resourceLock is required")) + allErrs = append(allErrs, field.Invalid(fldPath.Child("resourceLock"), cc.ResourceLock, "resourceLock is required")) + } + if len(cc.ResourceNamespace) == 0 { + allErrs = append(allErrs, field.Invalid(fldPath.Child("resourceNamespace"), cc.ResourceNamespace, "resourceNamespace is required")) + } + if len(cc.ResourceName) == 0 { + allErrs = append(allErrs, field.Invalid(fldPath.Child("resourceName"), cc.ResourceName, "resourceName is required")) } return allErrs } diff --git a/staging/src/k8s.io/component-base/config/validation/validation_test.go b/staging/src/k8s.io/component-base/config/validation/validation_test.go index 628fc1094f9..fc5a589941a 100644 --- a/staging/src/k8s.io/component-base/config/validation/validation_test.go +++ b/staging/src/k8s.io/component-base/config/validation/validation_test.go @@ -70,11 +70,13 @@ func TestValidateClientConnectionConfiguration(t *testing.T) { func TestValidateLeaderElectionConfiguration(t *testing.T) { validConfig := &config.LeaderElectionConfiguration{ - ResourceLock: "configmap", - LeaderElect: true, - LeaseDuration: metav1.Duration{Duration: 30 * time.Second}, - RenewDeadline: metav1.Duration{Duration: 15 * time.Second}, - RetryPeriod: metav1.Duration{Duration: 5 * time.Second}, + ResourceLock: "configmap", + LeaderElect: true, + LeaseDuration: metav1.Duration{Duration: 30 * time.Second}, + RenewDeadline: metav1.Duration{Duration: 15 * time.Second}, + RetryPeriod: metav1.Duration{Duration: 5 * time.Second}, + ResourceNamespace: "namespace", + ResourceName: "name", } renewDeadlineExceedsLeaseDuration := validConfig.DeepCopy() @@ -102,6 +104,12 @@ func TestValidateLeaderElectionConfiguration(t *testing.T) { resourceLockNotDefined := validConfig.DeepCopy() resourceLockNotDefined.ResourceLock = "" + resourceNameNotDefined := validConfig.DeepCopy() + resourceNameNotDefined.ResourceName = "" + + resourceNamespaceNotDefined := validConfig.DeepCopy() + resourceNamespaceNotDefined.ResourceNamespace = "" + scenarios := map[string]struct { expectedToFail bool config *config.LeaderElectionConfiguration @@ -142,6 +150,14 @@ func TestValidateLeaderElectionConfiguration(t *testing.T) { expectedToFail: true, config: resourceLockNotDefined, }, + "bad-resource-name-not-defined": { + expectedToFail: true, + config: resourceNameNotDefined, + }, + "bad-resource-namespace-not-defined": { + expectedToFail: true, + config: resourceNamespaceNotDefined, + }, } for name, scenario := range scenarios { diff --git a/staging/src/k8s.io/kube-scheduler/config/v1alpha1/types.go b/staging/src/k8s.io/kube-scheduler/config/v1alpha1/types.go index 1cea11f9fdb..2f6861782d8 100644 --- a/staging/src/k8s.io/kube-scheduler/config/v1alpha1/types.go +++ b/staging/src/k8s.io/kube-scheduler/config/v1alpha1/types.go @@ -135,8 +135,10 @@ type SchedulerPolicyConfigMapSource struct { type KubeSchedulerLeaderElectionConfiguration struct { componentbaseconfigv1alpha1.LeaderElectionConfiguration `json:",inline"` // LockObjectNamespace defines the namespace of the lock object + // DEPRECATED: will be removed in favor of resourceNamespace LockObjectNamespace string `json:"lockObjectNamespace"` // LockObjectName defines the lock object name + // DEPRECATED: will be removed in favor of resourceName LockObjectName string `json:"lockObjectName"` }