Skip to content

Commit

Permalink
Set default leader election configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
astefanutti committed Jan 17, 2024
1 parent 8ea55cc commit 3c9ae17
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 18 deletions.
1 change: 1 addition & 0 deletions apis/config/v1beta1/configuration_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
configv1alpha1 "k8s.io/component-base/config/v1alpha1"
)

// +k8s:defaulter-gen=true
// +kubebuilder:object:root=true

// Configuration is the Schema for the kueueconfigurations API
Expand Down
25 changes: 15 additions & 10 deletions apis/config/v1beta1/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import (
"time"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/leaderelection/resourcelock"
configv1alpha1 "k8s.io/component-base/config/v1alpha1"
"k8s.io/utils/ptr"

"sigs.k8s.io/kueue/pkg/controller/jobs/job"
Expand All @@ -43,13 +44,6 @@ const (
DefaultClusterQueuesMaxCount int32 = 10
)

func addDefaultingFuncs(scheme *runtime.Scheme) error {
scheme.AddTypeDefaultingFunc(&Configuration{}, func(obj interface{}) {
SetDefaults_Configuration(obj.(*Configuration))
})
return nil
}

func getOperatorNamespace() string {
if data, err := os.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace"); err == nil {
if ns := strings.TrimSpace(string(data)); len(ns) > 0 {
Expand All @@ -73,10 +67,21 @@ func SetDefaults_Configuration(cfg *Configuration) {
if len(cfg.Health.HealthProbeBindAddress) == 0 {
cfg.Health.HealthProbeBindAddress = DefaultHealthProbeBindAddress
}
if cfg.LeaderElection != nil && cfg.LeaderElection.LeaderElect != nil &&
*cfg.LeaderElection.LeaderElect && len(cfg.LeaderElection.ResourceName) == 0 {

if cfg.LeaderElection == nil {
cfg.LeaderElection = &configv1alpha1.LeaderElectionConfiguration{}
}
if len(cfg.LeaderElection.ResourceName) == 0 {
cfg.LeaderElection.ResourceName = DefaultLeaderElectionID
}
if len(cfg.LeaderElection.ResourceLock) == 0 {
// Default to Lease as component-base still defaults to endpoint resources
// until core components migrate to using Leases. See k/k #80289 for more details.
cfg.LeaderElection.ResourceLock = resourcelock.LeasesResourceLock
}
// Use the default LeaderElectionConfiguration options
configv1alpha1.RecommendedDefaultLeaderElectionConfiguration(cfg.LeaderElection)

if cfg.InternalCertManagement == nil {
cfg.InternalCertManagement = &InternalCertManagement{}
}
Expand Down
39 changes: 32 additions & 7 deletions apis/config/v1beta1/defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ const (

func TestSetDefaults_Configuration(t *testing.T) {
defaultCtrlManagerConfigurationSpec := ControllerManager{
LeaderElection: &componentconfigv1alpha1.LeaderElectionConfiguration{
LeaderElect: ptr.To(true),
LeaseDuration: metav1.Duration{Duration: 15 * time.Second},
RenewDeadline: metav1.Duration{Duration: 10 * time.Second},
RetryPeriod: metav1.Duration{Duration: 2 * time.Second},
ResourceLock: "leases",
ResourceName: "c1f6bfd2.kueue.x-k8s.io",
},
Webhook: ControllerWebhook{
Port: ptr.To(DefaultWebhookPort),
},
Expand Down Expand Up @@ -138,8 +146,12 @@ func TestSetDefaults_Configuration(t *testing.T) {
HealthProbeBindAddress: DefaultHealthProbeBindAddress,
},
LeaderElection: &componentconfigv1alpha1.LeaderElectionConfiguration{
LeaderElect: ptr.To(true),
ResourceName: DefaultLeaderElectionID,
LeaderElect: ptr.To(true),
LeaseDuration: metav1.Duration{Duration: 15 * time.Second},
RenewDeadline: metav1.Duration{Duration: 10 * time.Second},
RetryPeriod: metav1.Duration{Duration: 2 * time.Second},
ResourceLock: "leases",
ResourceName: DefaultLeaderElectionID,
},
},
InternalCertManagement: &InternalCertManagement{
Expand All @@ -163,8 +175,12 @@ func TestSetDefaults_Configuration(t *testing.T) {
HealthProbeBindAddress: overwriteHealthProbeBindAddress,
},
LeaderElection: &componentconfigv1alpha1.LeaderElectionConfiguration{
LeaderElect: ptr.To(true),
ResourceName: overwriteLeaderElectionID,
LeaderElect: ptr.To(true),
LeaseDuration: metav1.Duration{Duration: 15 * time.Second},
RenewDeadline: metav1.Duration{Duration: 10 * time.Second},
RetryPeriod: metav1.Duration{Duration: 2 * time.Second},
ResourceLock: "leases",
ResourceName: overwriteLeaderElectionID,
},
},
InternalCertManagement: &InternalCertManagement{
Expand All @@ -186,8 +202,12 @@ func TestSetDefaults_Configuration(t *testing.T) {
HealthProbeBindAddress: overwriteHealthProbeBindAddress,
},
LeaderElection: &componentconfigv1alpha1.LeaderElectionConfiguration{
LeaderElect: ptr.To(true),
ResourceName: overwriteLeaderElectionID,
LeaderElect: ptr.To(true),
LeaseDuration: metav1.Duration{Duration: 15 * time.Second},
RenewDeadline: metav1.Duration{Duration: 10 * time.Second},
RetryPeriod: metav1.Duration{Duration: 2 * time.Second},
ResourceLock: "leases",
ResourceName: overwriteLeaderElectionID,
},
},
InternalCertManagement: &InternalCertManagement{
Expand Down Expand Up @@ -222,7 +242,12 @@ func TestSetDefaults_Configuration(t *testing.T) {
HealthProbeBindAddress: DefaultHealthProbeBindAddress,
},
LeaderElection: &componentconfigv1alpha1.LeaderElectionConfiguration{
LeaderElect: ptr.To(false),
LeaderElect: ptr.To(false),
LeaseDuration: metav1.Duration{Duration: 15 * time.Second},
RenewDeadline: metav1.Duration{Duration: 10 * time.Second},
RetryPeriod: metav1.Duration{Duration: 2 * time.Second},
ResourceLock: "leases",
ResourceName: "c1f6bfd2.kueue.x-k8s.io",
},
},
InternalCertManagement: &InternalCertManagement{
Expand Down
6 changes: 5 additions & 1 deletion apis/config/v1beta1/groupversion_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ var (
// SchemeBuilder is used to add go types to the GroupVersionKind scheme
SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion}

// localSchemeBuilder is used to register autogenerated conversion and defaults functions
// It is required by ./zz_generated.conversion.go and ./zz_generated.defaults.go
localSchemeBuilder = &SchemeBuilder.SchemeBuilder

// AddToScheme adds the types in this group-version to the given scheme.
AddToScheme = SchemeBuilder.AddToScheme
)
Expand All @@ -40,5 +44,5 @@ func init() {
// We only register manually written functions here. The registration of the
// generated functions takes place in the generated files. The separation
// makes the code compile even when the generated files are missing.
SchemeBuilder.SchemeBuilder.Register(addDefaultingFuncs)
localSchemeBuilder.Register(RegisterDefaults)
}
37 changes: 37 additions & 0 deletions apis/config/v1beta1/zz_generated.defaults.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3c9ae17

Please sign in to comment.