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

Upgrade go-control-plane / data-plane-api #240

Merged
merged 9 commits into from
Feb 15, 2018
18 changes: 15 additions & 3 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cmd/contourcli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

"google.golang.org/grpc"

v2 "github.com/envoyproxy/go-control-plane/api"
"github.com/envoyproxy/go-control-plane/envoy/api/v2"
"github.com/gogo/protobuf/proto"
)

Expand Down
24 changes: 16 additions & 8 deletions internal/contour/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import (
"sort"
"sync"

v2 "github.com/envoyproxy/go-control-plane/api"
"github.com/envoyproxy/go-control-plane/envoy/api/v2"
"github.com/envoyproxy/go-control-plane/envoy/api/v2/route"
)

// clusterCache is a thread safe, atomic, copy on write cache of *v2.Cluster objects.
Expand Down Expand Up @@ -235,23 +236,30 @@ func (l listenersByName) Len() int { return len(l) }
func (l listenersByName) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
func (l listenersByName) Less(i, j int) bool { return l[i].Name < l[j].Name }

// VirtualHostCache is a thread safe, atomic, copy on write cache of v2.VirtualHost objects.
// VirtualHostCache is a thread safe, atomic, copy on write cache of route.VirtualHost objects.
type virtualHostCache struct {
sync.Mutex
values []*v2.VirtualHost
values []*route.VirtualHost
}

// Values returns a copy of the contents of the cache.
func (vc *virtualHostCache) Values() []*v2.VirtualHost {
// Although internally we store pointers to route.VirtualHost
// items, because the output of Values is used in a v2.RouteConfiguration
// rather than copying the pointer values, we create a slice of dereferenced
// values, this creates a copy of each element in the cache.
func (vc *virtualHostCache) Values() []route.VirtualHost {
vc.Lock()
r := append([]*v2.VirtualHost{}, vc.values...)
r := make([]route.VirtualHost, len(vc.values))
for i, v := range vc.values {
r[i] = *v
}
vc.Unlock()
return r
}

// Add adds an entry to the cache. If a VirtualHost with the same
// name exists, it is replaced.
func (vc *virtualHostCache) Add(virtualhosts ...*v2.VirtualHost) {
func (vc *virtualHostCache) Add(virtualhosts ...*route.VirtualHost) {
if len(virtualhosts) == 0 {
return
}
Expand All @@ -265,7 +273,7 @@ func (vc *virtualHostCache) Add(virtualhosts ...*v2.VirtualHost) {

// add adds v to the cache. If v is already present, the cached value of v is overwritten.
// invariant: vc.values should be sorted on entry.
func (vc *virtualHostCache) add(v *v2.VirtualHost) {
func (vc *virtualHostCache) add(v *route.VirtualHost) {
i := sort.Search(len(vc.values), func(i int) bool { return vc.values[i].Name >= v.Name })
if i < len(vc.values) && vc.values[i].Name == v.Name {
// c is already present, replace
Expand Down Expand Up @@ -301,7 +309,7 @@ func (vc *virtualHostCache) remove(name string) {
}
}

type virtualHostsByName []*v2.VirtualHost
type virtualHostsByName []*route.VirtualHost

func (v virtualHostsByName) Len() int { return len(v) }
func (v virtualHostsByName) Swap(i, j int) { v[i], v[j] = v[j], v[i] }
Expand Down
45 changes: 15 additions & 30 deletions internal/contour/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import (
"reflect"
"testing"

v2 "github.com/envoyproxy/go-control-plane/api"
"github.com/envoyproxy/go-control-plane/envoy/api/v2"
"github.com/envoyproxy/go-control-plane/envoy/api/v2/route"
)

func TestClusterCacheValuesReturnsACopyOfItsInternalSlice(t *testing.T) {
Expand Down Expand Up @@ -350,7 +351,7 @@ func TestListenerCacheRemove(t *testing.T) {

func TestVirtualHostCacheValuesReturnsACopyOfItsInternalSlice(t *testing.T) {
var cc virtualHostCache
c := &v2.VirtualHost{
c := &route.VirtualHost{
Name: "alpha",
}
cc.Add(c)
Expand All @@ -361,38 +362,22 @@ func TestVirtualHostCacheValuesReturnsACopyOfItsInternalSlice(t *testing.T) {
if &v1[0] == &v2[0] {
// the address of the 0th element of the values slice should not be the same
// if it is, then we don't have a copy.
t.Fatalf("VirtualHostCache, consecutive calls to Values return the same backing slice: got: %p, want: %p", &v1[0], &v2[0])
}
}

func TestVirtualHostCacheValuesReturnsTheSameContents(t *testing.T) {
var cc virtualHostCache
c := &v2.VirtualHost{
Name: "alpha",
}
cc.Add(c)

v1 := cc.Values()
v2 := cc.Values()

if v1[0] != v2[0] {
// the value of the 0th element, a pointer to a v2.VirtualHost should be the same
t.Fatalf("VirtualHostCache, consecutive calls to Values returned different slice contents: got: %p, want: %p", v1[0], v2[0])
t.Fatalf("VirtualHostCache, consecutive calls to Values return the same backing slice: got: %v, want: %v", v1[0], v2[0])
}
}

func TestVirtualHostCacheAddInsertsTwoElementsInSortOrder(t *testing.T) {
var cc virtualHostCache
c1 := &v2.VirtualHost{
c1 := &route.VirtualHost{
Name: "beta",
}
cc.Add(c1)
c2 := &v2.VirtualHost{
c2 := &route.VirtualHost{
Name: "alpha",
}
cc.Add(c2)
got := cc.Values()
want := []*v2.VirtualHost{{
want := []route.VirtualHost{{
Name: "alpha",
}, {
Name: "beta",
Expand All @@ -404,23 +389,23 @@ func TestVirtualHostCacheAddInsertsTwoElementsInSortOrder(t *testing.T) {

func TestVirtualHostCacheAddOverwritesElementsWithTheSameName(t *testing.T) {
var cc virtualHostCache
c1 := &v2.VirtualHost{
c1 := &route.VirtualHost{
Name: "alpha",
Domains: []string{
"example.com",
},
}
cc.Add(c1)
c2 := &v2.VirtualHost{
c2 := &route.VirtualHost{
Name: "alpha",
Domains: []string{
"heptio.com",
},
}
cc.Add(c2)
got := cc.Values()
want := []*v2.VirtualHost{
c2,
want := []route.VirtualHost{
*c2,
}
if !reflect.DeepEqual(got, want) {
t.Fatalf("VirtualHostCache.Add/Values returned a stale element, got: %v, want: %v", got, want)
Expand All @@ -429,13 +414,13 @@ func TestVirtualHostCacheAddOverwritesElementsWithTheSameName(t *testing.T) {

func TestVirtualHostCacheAddIsCopyOnWrite(t *testing.T) {
var cc virtualHostCache
c1 := &v2.VirtualHost{
c1 := &route.VirtualHost{
Name: "alpha",
}
cc.Add(c1)
v1 := cc.Values()

c2 := &v2.VirtualHost{
c2 := &route.VirtualHost{
Name: "beta",
}
cc.Add(c2)
Expand All @@ -448,13 +433,13 @@ func TestVirtualHostCacheAddIsCopyOnWrite(t *testing.T) {

func TestVirtualHostCacheRemove(t *testing.T) {
var cc virtualHostCache
c1 := &v2.VirtualHost{
c1 := &route.VirtualHost{
Name: "alpha",
}
cc.Add(c1)
cc.Remove("alpha")
got := cc.Values()
want := []*v2.VirtualHost{}
want := []route.VirtualHost{}
if !reflect.DeepEqual(got, want) {
t.Fatalf("VirtualHostCache.Remove: got: %v, want: %v", got, want)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/contour/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"strconv"
"time"

v2 "github.com/envoyproxy/go-control-plane/api"
"github.com/envoyproxy/go-control-plane/envoy/api/v2"
"k8s.io/api/core/v1"
)

Expand Down
2 changes: 1 addition & 1 deletion internal/contour/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/intstr"

v2 "github.com/envoyproxy/go-control-plane/api"
"github.com/envoyproxy/go-control-plane/envoy/api/v2"
)

func TestClusterCacheRecomputeService(t *testing.T) {
Expand Down
38 changes: 17 additions & 21 deletions internal/contour/clusterloadassignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ package contour
import (
"strconv"

v2 "github.com/envoyproxy/go-control-plane/api"
"github.com/envoyproxy/go-control-plane/envoy/api/v2"
"github.com/envoyproxy/go-control-plane/envoy/api/v2/core"
"github.com/envoyproxy/go-control-plane/envoy/api/v2/endpoint"
"k8s.io/api/core/v1"
)

Expand Down Expand Up @@ -74,9 +76,7 @@ func (cc *ClusterLoadAssignmentCache) recomputeClusterLoadAssignment(oldep, newe
clas[name] = cla
}
for _, a := range s.Addresses {
cla.Endpoints[0].LbEndpoints = append(cla.Endpoints[0].LbEndpoints, &v2.LbEndpoint{
Endpoint: endpoint(a.IP, p.Port),
})
cla.Endpoints[0].LbEndpoints = append(cla.Endpoints[0].LbEndpoints, lbendpoint(a.IP, p.Port))
}
}
}
Expand Down Expand Up @@ -111,30 +111,26 @@ func (cc *ClusterLoadAssignmentCache) recomputeClusterLoadAssignment(oldep, newe
}
}

func clusterloadassignment(name string, lbendpoints ...*v2.LbEndpoint) *v2.ClusterLoadAssignment {
func clusterloadassignment(name string, lbendpoints ...endpoint.LbEndpoint) *v2.ClusterLoadAssignment {
return &v2.ClusterLoadAssignment{
ClusterName: name,
Endpoints: []*v2.LocalityLbEndpoints{{
Endpoints: []endpoint.LocalityLbEndpoints{{
LbEndpoints: lbendpoints,
}},
}
}

func lbendpoint(addr string, port int32) *v2.LbEndpoint {
return &v2.LbEndpoint{
Endpoint: endpoint(addr, port),
}
}

func endpoint(addr string, port int32) *v2.Endpoint {
return &v2.Endpoint{
Address: &v2.Address{
Address: &v2.Address_SocketAddress{
SocketAddress: &v2.SocketAddress{
Protocol: v2.SocketAddress_TCP,
Address: addr,
PortSpecifier: &v2.SocketAddress_PortValue{
PortValue: uint32(port),
func lbendpoint(addr string, port int32) endpoint.LbEndpoint {
return endpoint.LbEndpoint{
Endpoint: &endpoint.Endpoint{
Address: &core.Address{
Address: &core.Address_SocketAddress{
SocketAddress: &core.SocketAddress{
Protocol: core.TCP,
Address: addr,
PortSpecifier: &core.SocketAddress_PortValue{
PortValue: uint32(port),
},
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion internal/contour/clusterloadassignment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"reflect"
"testing"

v2 "github.com/envoyproxy/go-control-plane/api"
"github.com/envoyproxy/go-control-plane/envoy/api/v2"
"k8s.io/api/core/v1"
)

Expand Down
Loading