Skip to content

Commit

Permalink
Implement comparable interface for ir.Xds to skip unnecessary updates
Browse files Browse the repository at this point in the history
  • Loading branch information
dboslee committed Aug 16, 2023
1 parent 677cf03 commit 74d296b
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
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 @@ package ir
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 @@ type Xds struct {
EnvoyPatchPolicies []*EnvoyPatchPolicy `json:"envoyPatchPolicies,omitempty" yaml:"envoyPatchPolicies,omitempty"`
}

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

// 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
})
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

0 comments on commit 74d296b

Please sign in to comment.