-
Notifications
You must be signed in to change notification settings - Fork 134
feat: add label intersection mode for precise pod targeting #777
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
Open
XploY04
wants to merge
4
commits into
litmuschaos:master
Choose a base branch
from
XploY04:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6a3965a
feat: add LabelMatchMode to AppDetails and implement intersection lab…
XploY04 f6ba5cb
test: add unit tests for GetTargets function with various label match…
XploY04 ff07962
refactor: optimize getPodsWithIntersectionLabels to use a single labe…
XploY04 a7891ef
fix: improve GetTargets function to handle empty entries and validate…
XploY04 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| package types | ||
|
|
||
| import ( | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestGetTargets_UnionModeDefault(t *testing.T) { | ||
| // Test that union is the default mode when not specified | ||
| targets := "deployment:default:[app=nginx,tier=frontend]" | ||
| result := GetTargets(targets) | ||
|
|
||
| if len(result) != 1 { | ||
| t.Errorf("Expected 1 AppDetail, got %d", len(result)) | ||
| } | ||
|
|
||
| if result[0].LabelMatchMode != "union" { | ||
| t.Errorf("Expected default LabelMatchMode to be 'union', got '%s'", result[0].LabelMatchMode) | ||
| } | ||
|
|
||
| if result[0].Kind != "deployment" { | ||
| t.Errorf("Expected Kind to be 'deployment', got '%s'", result[0].Kind) | ||
| } | ||
|
|
||
| if result[0].Namespace != "default" { | ||
| t.Errorf("Expected Namespace to be 'default', got '%s'", result[0].Namespace) | ||
| } | ||
|
|
||
| if len(result[0].Labels) != 2 { | ||
| t.Errorf("Expected 2 labels, got %d", len(result[0].Labels)) | ||
| } | ||
| } | ||
|
|
||
| func TestGetTargets_ExplicitUnionMode(t *testing.T) { | ||
| // Test explicit union mode | ||
| targets := "statefulset:prod:[app=postgres,role=primary]:union" | ||
| result := GetTargets(targets) | ||
|
|
||
| if len(result) != 1 { | ||
| t.Errorf("Expected 1 AppDetail, got %d", len(result)) | ||
| } | ||
|
|
||
| if result[0].LabelMatchMode != "union" { | ||
| t.Errorf("Expected LabelMatchMode to be 'union', got '%s'", result[0].LabelMatchMode) | ||
| } | ||
|
|
||
| if result[0].Kind != "statefulset" { | ||
| t.Errorf("Expected Kind to be 'statefulset', got '%s'", result[0].Kind) | ||
| } | ||
| } | ||
|
|
||
| func TestGetTargets_IntersectionMode(t *testing.T) { | ||
| // Test intersection mode | ||
| targets := "cluster:default:[cnpg.io/instanceRole=primary,cnpg.io/cluster=pg-eu]:intersection" | ||
| result := GetTargets(targets) | ||
|
|
||
| if len(result) != 1 { | ||
| t.Errorf("Expected 1 AppDetail, got %d", len(result)) | ||
| } | ||
|
|
||
| if result[0].LabelMatchMode != "intersection" { | ||
| t.Errorf("Expected LabelMatchMode to be 'intersection', got '%s'", result[0].LabelMatchMode) | ||
| } | ||
|
|
||
| if result[0].Kind != "cluster" { | ||
| t.Errorf("Expected Kind to be 'cluster', got '%s'", result[0].Kind) | ||
| } | ||
|
|
||
| if result[0].Namespace != "default" { | ||
| t.Errorf("Expected Namespace to be 'default', got '%s'", result[0].Namespace) | ||
| } | ||
|
|
||
| if len(result[0].Labels) != 2 { | ||
| t.Errorf("Expected 2 labels, got %d", len(result[0].Labels)) | ||
| } | ||
|
|
||
| expectedLabels := []string{"cnpg.io/instanceRole=primary", "cnpg.io/cluster=pg-eu"} | ||
| for i, label := range result[0].Labels { | ||
| if label != expectedLabels[i] { | ||
| t.Errorf("Expected label[%d] to be '%s', got '%s'", i, expectedLabels[i], label) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestGetTargets_InvalidMode(t *testing.T) { | ||
| // Test that invalid mode falls back to union | ||
| targets := "deployment:default:[app=nginx]:invalid" | ||
| result := GetTargets(targets) | ||
|
|
||
| if len(result) != 1 { | ||
| t.Errorf("Expected 1 AppDetail, got %d", len(result)) | ||
| } | ||
|
|
||
| // Invalid mode should fall back to union | ||
| if result[0].LabelMatchMode != "union" { | ||
| t.Errorf("Expected invalid mode to fall back to 'union', got '%s'", result[0].LabelMatchMode) | ||
| } | ||
| } | ||
|
|
||
| func TestGetTargets_MultipleSemicolonSeparated(t *testing.T) { | ||
| // Test multiple targets with different modes | ||
| targets := "deployment:ns1:[app=web]:union;statefulset:ns2:[db=postgres,env=prod]:intersection" | ||
| result := GetTargets(targets) | ||
|
|
||
| if len(result) != 2 { | ||
| t.Errorf("Expected 2 AppDetails, got %d", len(result)) | ||
| } | ||
|
|
||
| // First target - union | ||
| if result[0].LabelMatchMode != "union" { | ||
| t.Errorf("Expected first target LabelMatchMode to be 'union', got '%s'", result[0].LabelMatchMode) | ||
| } | ||
|
|
||
| // Second target - intersection | ||
| if result[1].LabelMatchMode != "intersection" { | ||
| t.Errorf("Expected second target LabelMatchMode to be 'intersection', got '%s'", result[1].LabelMatchMode) | ||
| } | ||
| } | ||
|
|
||
| func TestGetTargets_WithNames(t *testing.T) { | ||
| // Test that Names parsing still works with the new field | ||
| targets := "pod:default:[pod1,pod2,pod3]" | ||
| result := GetTargets(targets) | ||
|
|
||
| if len(result) != 1 { | ||
| t.Errorf("Expected 1 AppDetail, got %d", len(result)) | ||
| } | ||
|
|
||
| if len(result[0].Names) != 3 { | ||
| t.Errorf("Expected 3 names, got %d", len(result[0].Names)) | ||
| } | ||
|
|
||
| if len(result[0].Labels) != 0 { | ||
| t.Errorf("Expected 0 labels when Names are provided, got %d", len(result[0].Labels)) | ||
| } | ||
|
|
||
| // Default mode should still be union | ||
| if result[0].LabelMatchMode != "union" { | ||
| t.Errorf("Expected default LabelMatchMode to be 'union', got '%s'", result[0].LabelMatchMode) | ||
| } | ||
| } |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we check for array length to avoid array index out of bound?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
raise an error if len(val) < 3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @ispeakc0de
I have made the required changes.