Skip to content

Commit 90ed615

Browse files
authored
fix(endpoint): debug message when owner label is missing (#5788)
* fix: debug message of FilterEndpointsByOwnerID in case owner label is missing * more consistent messages * add unit test for changed debug message --------- Co-authored-by: Pascal Bachor <[email protected]>
1 parent bd2f8a4 commit 90ed615

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

endpoint/endpoint.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,11 @@ func (e *Endpoint) Describe() string {
378378
func FilterEndpointsByOwnerID(ownerID string, eps []*Endpoint) []*Endpoint {
379379
filtered := []*Endpoint{}
380380
for _, ep := range eps {
381-
if endpointOwner, ok := ep.Labels[OwnerLabelKey]; !ok || endpointOwner != ownerID {
382-
log.Debugf(`Skipping endpoint %v because owner id does not match, found: "%s", required: "%s"`, ep, endpointOwner, ownerID)
381+
endpointOwner, ok := ep.Labels[OwnerLabelKey]
382+
if !ok {
383+
log.Debugf(`Skipping endpoint %v because of missing owner label (required: "%s")`, ep, ownerID)
384+
} else if endpointOwner != ownerID {
385+
log.Debugf(`Skipping endpoint %v because owner id does not match (found: "%s", required: "%s")`, ep, endpointOwner, ownerID)
383386
} else {
384387
filtered = append(filtered, ep)
385388
}

internal/testutils/endpoint_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import (
2323
"sort"
2424
"testing"
2525

26+
log "github.com/sirupsen/logrus"
27+
2628
"github.com/stretchr/testify/assert"
2729
"sigs.k8s.io/external-dns/endpoint"
2830
)
@@ -489,3 +491,63 @@ func TestWithLabel(t *testing.T) {
489491
assert.Equal(t, "orig", e.Labels["existing"])
490492
assert.Equal(t, "val", e.Labels["new"])
491493
}
494+
495+
func TestFilterEndpointsByOwnerIDLogging(t *testing.T) {
496+
noOwner := &endpoint.Endpoint{}
497+
ownedByFoo := &endpoint.Endpoint{
498+
Labels: endpoint.Labels{
499+
endpoint.OwnerLabelKey: "foo",
500+
},
501+
}
502+
ownedByBar := &endpoint.Endpoint{
503+
Labels: endpoint.Labels{
504+
endpoint.OwnerLabelKey: "bar",
505+
},
506+
}
507+
tests := []struct {
508+
name string
509+
ownerID string
510+
endpoints []*endpoint.Endpoint
511+
messages []string
512+
messages_not []string
513+
result []*endpoint.Endpoint
514+
}{
515+
{
516+
name: "one_matches",
517+
ownerID: "foo",
518+
endpoints: []*endpoint.Endpoint{ownedByFoo},
519+
messages: []string{},
520+
messages_not: []string{""},
521+
result: []*endpoint.Endpoint{ownedByFoo},
522+
},
523+
{
524+
name: "wrong_owner",
525+
ownerID: "foo",
526+
endpoints: []*endpoint.Endpoint{ownedByFoo, ownedByBar},
527+
messages: []string{"because owner id does not match"},
528+
messages_not: []string{},
529+
result: []*endpoint.Endpoint{ownedByFoo},
530+
},
531+
{
532+
name: "no_owner",
533+
ownerID: "bar",
534+
endpoints: []*endpoint.Endpoint{noOwner, ownedByBar},
535+
messages: []string{"because of missing owner label"},
536+
messages_not: []string{"because owner id does not match"},
537+
result: []*endpoint.Endpoint{ownedByBar},
538+
},
539+
}
540+
541+
for _, tt := range tests {
542+
t.Run(tt.name, func(t *testing.T) {
543+
hook := LogsUnderTestWithLogLevel(log.DebugLevel, t)
544+
endpoint.FilterEndpointsByOwnerID(tt.ownerID, tt.endpoints)
545+
for _, m := range tt.messages {
546+
TestHelperLogContains(m, hook, t)
547+
}
548+
for _, m := range tt.messages_not {
549+
TestHelperLogNotContains(m, hook, t)
550+
}
551+
})
552+
}
553+
}

0 commit comments

Comments
 (0)