-
Notifications
You must be signed in to change notification settings - Fork 56
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
Don't allow two DRPCs with conflicting selectors in the same namespace #1081
Closed
raghavendra-talur
wants to merge
4
commits into
RamenDR:main
from
raghavendra-talur:rtalur-drpc-conflicting-pvc-labelselector
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5563c55
drpc: don't allow two drpc that have overlapping selectors
raghavendra-talur 4a46bc8
testing drpc labels
raghavendra-talur 2a26c0b
integration test for drpc with overlapping selectors
raghavendra-talur 9b6a2fa
test: allow using existing kubernetes cluster for integration tests
raghavendra-talur 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 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,63 @@ | ||
// SPDX-FileCopyrightText: The RamenDR authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package controllers_test | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
"k8s.io/apimachinery/pkg/types" | ||
|
||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
rmn "github.com/ramendr/ramen/api/v1alpha1" | ||
plrv1 "github.com/stolostron/multicloud-operators-placementrule/pkg/apis/apps/v1" | ||
) | ||
|
||
var _ = Describe("DRPlacementControl Reconciler New", func() { | ||
Specify("DRClusters", func() { | ||
populateDRClusters() | ||
}) | ||
Context("DRPlacementControl Reconciler Async DR using PlacementRule (Subscription)", func() { | ||
var userPlacementRule *plrv1.PlacementRule | ||
var drpc *rmn.DRPlacementControl | ||
When("An Application is deployed for the first time", func() { | ||
It("Should deploy to East1ManagedCluster", func() { | ||
By("Initial Deployment") | ||
var placementObj client.Object | ||
placementObj, drpc = InitialDeploymentAsync( | ||
DRPCNamespaceName, UserPlacementRuleName, East1ManagedCluster, UsePlacementRule) | ||
Check failure on line 29 in controllers/drpc_namespace_conflict_test.go GitHub Actions / Golangci Lint
|
||
testLogger.Info("dprc rtalur logging", "drpc", drpc) | ||
userPlacementRule = placementObj.(*plrv1.PlacementRule) | ||
Expect(userPlacementRule).NotTo(BeNil()) | ||
verifyInitialDRPCDeployment(userPlacementRule, East1ManagedCluster) | ||
verifyDRPCOwnedByPlacement(userPlacementRule, getLatestDRPC()) | ||
Check failure on line 34 in controllers/drpc_namespace_conflict_test.go GitHub Actions / Golangci Lint
|
||
drpc2 := createDRPC(drpc.Spec.PlacementRef.Name, "overlappingdrpc", drpc.Namespace, drpc.Spec.DRPolicyRef.Name) | ||
Check failure on line 35 in controllers/drpc_namespace_conflict_test.go GitHub Actions / Golangci Lint
|
||
testLogger.Info("dprc rtalur logging", "drpc", drpc2) | ||
drpc2, err := getLatestDRPCByNamespacedName(types.NamespacedName{Namespace: drpc2.Namespace, Name: drpc2.Name}) | ||
Expect(err).NotTo(HaveOccurred()) | ||
testLogger.Info("dprc rtalur logging", "drpc", drpc2) | ||
}) | ||
}) | ||
//When("Deleting user PlacementRule", func() { | ||
// It("Should cleanup DRPC", func() { | ||
// // ----------------------------- DELETE DRPC from PRIMARY -------------------------------------- | ||
// By("\n\n*** DELETE User PlacementRule ***\n\n") | ||
// deleteUserPlacementRule() | ||
// }) | ||
//}) | ||
|
||
//When("Deleting DRPC", func() { | ||
// It("Should delete VRG and NS MWs and MCVs from Primary (East1ManagedCluster)", func() { | ||
// // ----------------------------- DELETE DRPC from PRIMARY -------------------------------------- | ||
// By("\n\n*** DELETE DRPC ***\n\n") | ||
// Expect(getManifestWorkCount(East1ManagedCluster)).Should(Equal(3)) // DRCluster + VRG MW | ||
// deleteDRPC() | ||
// waitForCompletion("deleted") | ||
// Expect(getManifestWorkCount(East1ManagedCluster)).Should(Equal(2)) // DRCluster + NS MW only | ||
// Expect(getManagedClusterViewCount(East1ManagedCluster)).Should(Equal(0)) // NS + VRG MCV | ||
// deleteNamespaceMWsFromAllClusters(DRPCNamespaceName) | ||
// }) | ||
//}) | ||
}) | ||
}) |
This file contains 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 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 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 @@ | ||
// SPDX-FileCopyrightText: The RamenDR authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package controllers | ||
|
||
import ( | ||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
// Relationship type between two value sets. | ||
type relationship int | ||
|
||
const ( | ||
overlap relationship = iota | ||
disjoint | ||
subset | ||
superset | ||
equal | ||
) | ||
|
||
func determineRelationship(a, b []string) relationship { | ||
setA := make(map[string]bool) | ||
setB := make(map[string]bool) | ||
|
||
for _, v := range a { | ||
setA[v] = true | ||
} | ||
for _, v := range b { | ||
setB[v] = true | ||
} | ||
|
||
// Check if A is a subset or equal to B | ||
isSubset := true | ||
for v := range setA { | ||
if !setB[v] { | ||
isSubset = false | ||
break | ||
} | ||
} | ||
|
||
// Check if B is a subset or equal to A | ||
isSuperset := true | ||
for v := range setB { | ||
if !setA[v] { | ||
isSuperset = false | ||
break | ||
} | ||
} | ||
|
||
if isSubset && isSuperset { | ||
return equal | ||
} | ||
if isSubset { | ||
return subset | ||
} | ||
if isSuperset { | ||
return superset | ||
} | ||
|
||
for v := range setA { | ||
if setB[v] { | ||
return overlap | ||
} | ||
} | ||
|
||
return disjoint | ||
} | ||
|
||
func isConflicting(op1, op2 v1.LabelSelectorOperator, rel relationship) bool { | ||
switch op1 { | ||
case v1.LabelSelectorOpIn: | ||
switch op2 { | ||
case v1.LabelSelectorOpIn: | ||
return rel == disjoint | ||
case v1.LabelSelectorOpNotIn: | ||
return rel == subset || rel == equal | ||
case v1.LabelSelectorOpDoesNotExist: | ||
return rel != disjoint | ||
} | ||
case v1.LabelSelectorOpNotIn: | ||
switch op2 { | ||
case v1.LabelSelectorOpIn: | ||
return rel == superset || rel == equal | ||
case v1.LabelSelectorOpDoesNotExist: | ||
return rel != disjoint | ||
} | ||
case v1.LabelSelectorOpExists: | ||
switch op2 { | ||
case v1.LabelSelectorOpNotIn, v1.LabelSelectorOpDoesNotExist: | ||
return true | ||
} | ||
case v1.LabelSelectorOpDoesNotExist: | ||
switch op2 { | ||
case v1.LabelSelectorOpIn, v1.LabelSelectorOpNotIn, v1.LabelSelectorOpExists: | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func canMatchSameElements(selector1, selector2 *v1.LabelSelector) bool { | ||
// Convert matchLabels to LabelSelectorRequirements for unified processing | ||
allExprs1 := labelsToExpressions(selector1.MatchLabels) | ||
allExprs2 := labelsToExpressions(selector2.MatchLabels) | ||
|
||
// Consolidate all matchExpressions | ||
allExprs1 = append(allExprs1, selector1.MatchExpressions...) | ||
allExprs2 = append(allExprs2, selector2.MatchExpressions...) | ||
|
||
// Check if any expression contradicts the other selector | ||
return !selectorsWillNeverMatch(allExprs1, allExprs2) | ||
} | ||
|
||
func labelsToExpressions(matchLabels map[string]string) []v1.LabelSelectorRequirement { | ||
var exprs []v1.LabelSelectorRequirement | ||
for k, v := range matchLabels { | ||
exprs = append(exprs, v1.LabelSelectorRequirement{ | ||
Key: k, | ||
Operator: v1.LabelSelectorOpIn, | ||
Values: []string{v}, | ||
}) | ||
} | ||
return exprs | ||
} | ||
|
||
func selectorsWillNeverMatch(exprs1, exprs2 []v1.LabelSelectorRequirement) bool { | ||
for _, e1 := range exprs1 { | ||
for _, e2 := range exprs2 { | ||
if e1.Key != e2.Key { | ||
continue | ||
} | ||
|
||
relationship := determineRelationship(e1.Values, e2.Values) | ||
if isConflicting(e1.Operator, e2.Operator, relationship) { | ||
return true | ||
} | ||
} | ||
} | ||
return false | ||
} |
This file contains 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,117 @@ | ||
// SPDX-FileCopyrightText: The RamenDR authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package controllers | ||
|
||
import ( | ||
"testing" | ||
|
||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
//nolint:funlen | ||
func TestCanMatchSameElements(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
selectorA *v1.LabelSelector | ||
selectorB *v1.LabelSelector | ||
shouldMatch bool | ||
}{ | ||
{ | ||
name: "Test with two keys where one can match", | ||
selectorA: &v1.LabelSelector{ | ||
MatchExpressions: []v1.LabelSelectorRequirement{ | ||
{Key: "tier", Operator: v1.LabelSelectorOpIn, Values: []string{"frontend"}}, | ||
{Key: "env", Operator: v1.LabelSelectorOpIn, Values: []string{"dev", "staging"}}, | ||
}, | ||
}, | ||
selectorB: &v1.LabelSelector{ | ||
MatchExpressions: []v1.LabelSelectorRequirement{ | ||
{Key: "tier", Operator: v1.LabelSelectorOpIn, Values: []string{"frontend", "backend"}}, | ||
{Key: "env", Operator: v1.LabelSelectorOpNotIn, Values: []string{"prod"}}, | ||
}, | ||
}, | ||
shouldMatch: true, | ||
}, | ||
{ | ||
name: "Test with one k=v and one k exists", | ||
selectorA: &v1.LabelSelector{ | ||
MatchExpressions: []v1.LabelSelectorRequirement{ | ||
{Key: "tier", Operator: v1.LabelSelectorOpIn, Values: []string{"frontend"}}, | ||
}, | ||
}, | ||
selectorB: &v1.LabelSelector{ | ||
MatchExpressions: []v1.LabelSelectorRequirement{ | ||
{Key: "tier", Operator: v1.LabelSelectorOpExists}, | ||
}, | ||
}, | ||
shouldMatch: true, | ||
}, | ||
{ | ||
name: "Test with k={v1} and k={v2}", | ||
selectorA: &v1.LabelSelector{ | ||
MatchExpressions: []v1.LabelSelectorRequirement{ | ||
{Key: "tier", Operator: v1.LabelSelectorOpIn, Values: []string{"cache"}}, | ||
}, | ||
}, | ||
selectorB: &v1.LabelSelector{ | ||
MatchExpressions: []v1.LabelSelectorRequirement{ | ||
{Key: "tier", Operator: v1.LabelSelectorOpIn, Values: []string{"backup"}}, | ||
}, | ||
}, | ||
shouldMatch: false, | ||
}, | ||
{ | ||
name: "Test with k1={v1},k2!={v3} and k={v2},k2!={v3}", | ||
selectorA: &v1.LabelSelector{ | ||
MatchExpressions: []v1.LabelSelectorRequirement{ | ||
{Key: "tier", Operator: v1.LabelSelectorOpIn, Values: []string{"cache"}}, | ||
{Key: "env", Operator: v1.LabelSelectorOpNotIn, Values: []string{"dev"}}, | ||
}, | ||
}, | ||
selectorB: &v1.LabelSelector{ | ||
MatchExpressions: []v1.LabelSelectorRequirement{ | ||
{Key: "tier", Operator: v1.LabelSelectorOpIn, Values: []string{"backup"}}, | ||
{Key: "env", Operator: v1.LabelSelectorOpNotIn, Values: []string{"dev"}}, | ||
}, | ||
}, | ||
shouldMatch: false, | ||
}, | ||
{ | ||
name: "Test with cache and not cache", | ||
selectorA: &v1.LabelSelector{ | ||
MatchExpressions: []v1.LabelSelectorRequirement{ | ||
{Key: "tier", Operator: v1.LabelSelectorOpIn, Values: []string{"cache"}}, | ||
}, | ||
}, | ||
selectorB: &v1.LabelSelector{ | ||
MatchExpressions: []v1.LabelSelectorRequirement{ | ||
{Key: "tier", Operator: v1.LabelSelectorOpNotIn, Values: []string{"cache"}}, | ||
}, | ||
}, | ||
shouldMatch: false, | ||
}, | ||
{ | ||
name: "Test with empty selector", | ||
selectorA: &v1.LabelSelector{}, | ||
selectorB: &v1.LabelSelector{ | ||
MatchExpressions: []v1.LabelSelectorRequirement{ | ||
{Key: "tier", Operator: v1.LabelSelectorOpIn, Values: []string{"backup"}}, | ||
}, | ||
}, | ||
shouldMatch: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if match := canMatchSameElements(tt.selectorA, tt.selectorB); match != tt.shouldMatch { | ||
if tt.shouldMatch { | ||
t.Fatalf("Expected selectors to potentially match the same elements, but they were determined not to.") | ||
} else { | ||
t.Fatalf("Expected selectors to not match the same elements, but they were determined to.") | ||
} | ||
} | ||
}) | ||
} | ||
} |
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.
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.
Is this test related to overlapping selector? how?