Skip to content
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

Fix bug on service intention CRDs causing missed updates #2194

Merged
merged 1 commit into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/2194.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:
crd: fix bug on service intentions CRD causing some updates to be ignored.
```
19 changes: 18 additions & 1 deletion control-plane/api/v1alpha1/serviceintentions_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,18 +234,34 @@ func (in *ServiceIntentions) ConsulGlobalResource() bool {
return false
}

func normalizeEmptyToDefault(value string) string {
if value == "" {
return "default"
}
return value
}

func (in *ServiceIntentions) MatchesConsul(candidate api.ConfigEntry) bool {
configEntry, ok := candidate.(*capi.ServiceIntentionsConfigEntry)
if !ok {
return false
}

specialEquality := cmp.Options{
cmp.FilterPath(func(path cmp.Path) bool {
return path.String() == "Sources.Namespace"
}, cmp.Transformer("NormalizeNamespace", normalizeEmptyToDefault)),
cmp.FilterPath(func(path cmp.Path) bool {
return path.String() == "Sources.Partition"
}, cmp.Transformer("NormalizePartition", normalizeEmptyToDefault)),
}

// No datacenter is passed to ToConsul as we ignore the Meta field when checking for equality.
return cmp.Equal(
in.ToConsul(""),
configEntry,
cmpopts.IgnoreFields(capi.ServiceIntentionsConfigEntry{}, "Partition", "Namespace", "Meta", "ModifyIndex", "CreateIndex"),
cmpopts.IgnoreFields(capi.SourceIntention{}, "Partition", "Namespace", "LegacyID", "LegacyMeta", "LegacyCreateTime", "LegacyUpdateTime", "Precedence", "Type"),
cmpopts.IgnoreFields(capi.SourceIntention{}, "LegacyID", "LegacyMeta", "LegacyCreateTime", "LegacyUpdateTime", "Precedence", "Type"),
cmpopts.IgnoreUnexported(),
cmpopts.EquateEmpty(),
// Consul will sort the sources by precedence when returning the resource
Expand All @@ -255,6 +271,7 @@ func (in *ServiceIntentions) MatchesConsul(candidate api.ConfigEntry) bool {
// piggyback on strings.Compare that returns -1 if a < b.
return strings.Compare(sourceIntentionSortKey(a), sourceIntentionSortKey(b)) == -1
}),
specialEquality,
)
}

Expand Down
72 changes: 72 additions & 0 deletions control-plane/api/v1alpha1/serviceintentions_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,78 @@ func TestServiceIntentions_MatchesConsul(t *testing.T) {
},
Matches: true,
},
"namespaces and partitions equate `default` and empty strings": {
Ours: ServiceIntentions{
ObjectMeta: metav1.ObjectMeta{
Name: "name",
},
Spec: ServiceIntentionsSpec{
Destination: IntentionDestination{
Name: "svc-name",
Namespace: "ns1",
},
Sources: []*SourceIntention{
{
Name: "svc1",
Namespace: "",
Partition: "default",
Action: "allow",
},
},
},
},
Theirs: &capi.ServiceIntentionsConfigEntry{
Kind: capi.ServiceIntentions,
Name: "svc-name",
Namespace: "ns1",
Sources: []*capi.SourceIntention{
{
Name: "svc1",
Namespace: "default",
Partition: "",
Action: "allow",
Precedence: 0,
},
},
},
Matches: true,
},
"source namespaces and partitions are compared": {
Ours: ServiceIntentions{
ObjectMeta: metav1.ObjectMeta{
Name: "name",
},
Spec: ServiceIntentionsSpec{
Destination: IntentionDestination{
Name: "svc-name",
Namespace: "test",
},
Sources: []*SourceIntention{
{
Name: "svc1",
Namespace: "test",
Partition: "test",
Action: "allow",
},
},
},
},
Theirs: &capi.ServiceIntentionsConfigEntry{
Kind: capi.ServiceIntentions,
Name: "svc-name",
Namespace: "test",
Sources: []*capi.SourceIntention{
{
Name: "svc1",
Namespace: "not-test",
Partition: "not-test",
Action: "allow",
Precedence: 0,
},
},
},
Matches: false,
},
"all fields set matches": {
Ours: ServiceIntentions{
ObjectMeta: metav1.ObjectMeta{
Expand Down