generated from kedacore/github-template
-
Notifications
You must be signed in to change notification settings - Fork 141
feat: add environment variables for leader election timing configuration #1365
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
wozniakjan
merged 5 commits into
kedacore:main
from
nader-ziada:leader-election-env-vars
Nov 28, 2025
+387
−1
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
45fc5d5
feat: add environment variables for leader election timing configuration
nader-ziada 9821c9e
update changelog
nader-ziada 8893c44
add validation for leader election config values
nader-ziada 7542ddf
improve validation of leader election values
nader-ziada 81939a7
simplify validation by removing redundant check
nader-ziada File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| /* | ||
| Copyright 2025 The KEDA 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 main | ||
|
|
||
| import ( | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
|
|
||
| "github.com/kedacore/http-add-on/pkg/util" | ||
| ) | ||
|
|
||
| func TestLeaderElectionEnvVarsIntegration(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| envVars map[string]string | ||
| expectedLease *time.Duration | ||
| expectedRenew *time.Duration | ||
| expectedRetry *time.Duration | ||
| expectError bool | ||
| }{ | ||
| { | ||
| name: "all environment variables set with valid values", | ||
| envVars: map[string]string{ | ||
| "KEDA_HTTP_OPERATOR_LEADER_ELECTION_LEASE_DURATION": "30s", | ||
| "KEDA_HTTP_OPERATOR_LEADER_ELECTION_RENEW_DEADLINE": "20s", | ||
| "KEDA_HTTP_OPERATOR_LEADER_ELECTION_RETRY_PERIOD": "5s", | ||
| }, | ||
| expectedLease: durationPtr(30 * time.Second), | ||
| expectedRenew: durationPtr(20 * time.Second), | ||
| expectedRetry: durationPtr(5 * time.Second), | ||
| expectError: false, | ||
| }, | ||
| { | ||
| name: "no environment variables set - should return nil for defaults", | ||
| envVars: map[string]string{}, | ||
| expectedLease: nil, | ||
| expectedRenew: nil, | ||
| expectedRetry: nil, | ||
| expectError: false, | ||
| }, | ||
| { | ||
| name: "invalid lease duration", | ||
| envVars: map[string]string{ | ||
| "KEDA_HTTP_OPERATOR_LEADER_ELECTION_LEASE_DURATION": "invalid", | ||
| }, | ||
| expectError: true, | ||
| }, | ||
| { | ||
| name: "invalid renew deadline", | ||
| envVars: map[string]string{ | ||
| "KEDA_HTTP_OPERATOR_LEADER_ELECTION_RENEW_DEADLINE": "not-a-duration", | ||
| }, | ||
| expectError: true, | ||
| }, | ||
| { | ||
| name: "invalid retry period", | ||
| envVars: map[string]string{ | ||
| "KEDA_HTTP_OPERATOR_LEADER_ELECTION_RETRY_PERIOD": "xyz", | ||
| }, | ||
| expectError: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| for key, value := range tt.envVars { | ||
| t.Setenv(key, value) | ||
| } | ||
|
|
||
| leaseDuration, leaseErr := util.ResolveOsEnvDuration("KEDA_HTTP_OPERATOR_LEADER_ELECTION_LEASE_DURATION") | ||
| renewDeadline, renewErr := util.ResolveOsEnvDuration("KEDA_HTTP_OPERATOR_LEADER_ELECTION_RENEW_DEADLINE") | ||
| retryPeriod, retryErr := util.ResolveOsEnvDuration("KEDA_HTTP_OPERATOR_LEADER_ELECTION_RETRY_PERIOD") | ||
|
|
||
| if tt.expectError { | ||
| // At least one of the errors should be non-nil | ||
| hasError := false | ||
| if _, ok := tt.envVars["KEDA_HTTP_OPERATOR_LEADER_ELECTION_LEASE_DURATION"]; ok && leaseErr != nil { | ||
| hasError = true | ||
| } | ||
| if _, ok := tt.envVars["KEDA_HTTP_OPERATOR_LEADER_ELECTION_RENEW_DEADLINE"]; ok && renewErr != nil { | ||
| hasError = true | ||
| } | ||
| if _, ok := tt.envVars["KEDA_HTTP_OPERATOR_LEADER_ELECTION_RETRY_PERIOD"]; ok && retryErr != nil { | ||
| hasError = true | ||
| } | ||
| if !hasError { | ||
| t.Errorf("expected error but got none") | ||
| } | ||
| } else { | ||
| // No errors expected | ||
| if leaseErr != nil { | ||
| t.Errorf("unexpected error for lease duration: %v", leaseErr) | ||
| } | ||
| if renewErr != nil { | ||
| t.Errorf("unexpected error for renew deadline: %v", renewErr) | ||
| } | ||
| if retryErr != nil { | ||
| t.Errorf("unexpected error for retry period: %v", retryErr) | ||
| } | ||
|
|
||
| // Verify the parsed values match expectations | ||
| assert.Equal(t, tt.expectedLease, leaseDuration) | ||
| assert.Equal(t, tt.expectedRenew, renewDeadline) | ||
| assert.Equal(t, tt.expectedRetry, retryPeriod) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func durationPtr(d time.Duration) *time.Duration { | ||
| return &d | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.