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: implement comparable interface for ir.Xds to skip unnecessary updates #1795

Merged
merged 3 commits into from
Aug 17, 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
30 changes: 30 additions & 0 deletions internal/ir/xds.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import (
"errors"
"net"
"reflect"

"github.com/tetratelabs/multierror"
"golang.org/x/exp/slices"

apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down Expand Up @@ -62,6 +64,34 @@
EnvoyPatchPolicies []*EnvoyPatchPolicy `json:"envoyPatchPolicies,omitempty" yaml:"envoyPatchPolicies,omitempty"`
}

// Equal implements the Comparable interface used by watchable.DeepEqual to skip unnecessary updates.
func (x *Xds) Equal(y *Xds) bool {
// Deep copy to avoid modifying the original ordering.
x = x.DeepCopy()
x.sort()
y = y.DeepCopy()
y.sort()
return reflect.DeepEqual(x, y)
}

// sort ensures the listeners are in a consistent order.
func (x *Xds) sort() {
slices.SortFunc(x.HTTP, func(l1, l2 *HTTPListener) bool {
return l1.Name < l2.Name
})

Check warning on line 81 in internal/ir/xds.go

View check run for this annotation

Codecov / codecov/patch

internal/ir/xds.go#L80-L81

Added lines #L80 - L81 were not covered by tests
for _, l := range x.HTTP {
slices.SortFunc(l.Routes, func(r1, r2 *HTTPRoute) bool {
return r1.Name < r2.Name
})
}
slices.SortFunc(x.TCP, func(l1, l2 *TCPListener) bool {
return l1.Name < l2.Name
})
slices.SortFunc(x.UDP, func(l1, l2 *UDPListener) bool {
return l1.Name < l2.Name
})
}

// Validate the fields within the Xds structure.
func (x Xds) Validate() error {
var errs error
Expand Down
75 changes: 75 additions & 0 deletions internal/ir/xds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package ir
import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -676,6 +677,80 @@ func TestValidateTLSListenerConfig(t *testing.T) {
}
}

func TestEqualXds(t *testing.T) {
tests := []struct {
desc string
a *Xds
b *Xds
equal bool
}{
{
desc: "out of order tcp listeners are equal",
a: &Xds{
TCP: []*TCPListener{
{Name: "listener-1"},
{Name: "listener-2"},
},
},
b: &Xds{
TCP: []*TCPListener{
{Name: "listener-2"},
{Name: "listener-1"},
},
},
equal: true,
},
{
desc: "out of order http routes are equal",
a: &Xds{
HTTP: []*HTTPListener{
{
Name: "listener-1",
Routes: []*HTTPRoute{
{Name: "route-1"},
{Name: "route-2"},
},
},
},
},
b: &Xds{
HTTP: []*HTTPListener{
{
Name: "listener-1",
Routes: []*HTTPRoute{
{Name: "route-2"},
{Name: "route-1"},
},
},
},
},
equal: true,
},
{
desc: "out of order udp listeners are equal",
a: &Xds{
UDP: []*UDPListener{
{Name: "listener-1"},
{Name: "listener-2"},
},
},
b: &Xds{
UDP: []*UDPListener{
{Name: "listener-2"},
{Name: "listener-1"},
},
},
equal: true,
},
}

for _, tc := range tests {
t.Run(tc.desc, func(t *testing.T) {
require.Equal(t, tc.equal, cmp.Equal(tc.a, tc.b))
})
}
}

func TestValidateUDPListener(t *testing.T) {
tests := []struct {
name string
Expand Down
65 changes: 65 additions & 0 deletions internal/message/watchutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/telepresenceio/watchable"

"github.com/envoyproxy/gateway/internal/ir"
"github.com/envoyproxy/gateway/internal/message"
)

Expand Down Expand Up @@ -59,3 +60,67 @@ func TestHandleSubscriptionAlreadyInitialized(t *testing.T) {
assert.Equal(t, 2, storeCalls)
assert.Equal(t, 1, deleteCalls)
}

func TestXdsIRUpdates(t *testing.T) {
tests := []struct {
desc string
xx []*ir.Xds
updates int
}{
{
desc: "HTTP listener order change skips update",
xx: []*ir.Xds{
{
HTTP: []*ir.HTTPListener{
{Name: "listener-1"},
{Name: "listener-2"},
},
},
{
HTTP: []*ir.HTTPListener{
{Name: "listener-2"},
{Name: "listener-1"},
},
},
},
updates: 1,
},
{
desc: "Additional HTTP listener triggers update",
xx: []*ir.Xds{
{
HTTP: []*ir.HTTPListener{
{Name: "listener-1"},
},
},
{
HTTP: []*ir.HTTPListener{
{Name: "listener-1"},
{Name: "listener-2"},
},
},
},
updates: 2,
},
}
for _, tc := range tests {
t.Run(tc.desc, func(t *testing.T) {
ctx := context.Background()
m := new(message.XdsIR)

snapshotC := m.Subscribe(ctx)
go func() {
for _, x := range tc.xx {
m.Store("test", x)
}
m.Close()
}()

updates := 0
message.HandleSubscription(snapshotC, func(u message.Update[string, *ir.Xds]) {
updates += 1
})
assert.Equal(t, tc.updates, updates)
})
}
}