Skip to content

Commit 3d60d99

Browse files
add tests
Signed-off-by: Clayton Gonsalves <[email protected]>
1 parent 6bc5b28 commit 3d60d99

File tree

2 files changed

+53
-15
lines changed

2 files changed

+53
-15
lines changed

internal/xdscache/v3/endpointstranslator.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,16 @@ type LoadBalancingEndpoint = envoy_endpoint_v3.LbEndpoint
3838

3939
// RecalculateEndpoints generates a slice of LoadBalancingEndpoint
4040
// resources by matching the given service port to the given v1.Endpoints.
41-
// ep may be nil, in which case, the result is also nil.
42-
func RecalculateEndpoints(port, healthPort v1.ServicePort, ep *v1.Endpoints) []*LoadBalancingEndpoint {
43-
if ep == nil {
41+
// eps may be nil, in which case, the result is also nil.
42+
func RecalculateEndpoints(port, healthPort v1.ServicePort, eps *v1.Endpoints) []*LoadBalancingEndpoint {
43+
if eps == nil {
4444
return nil
4545
}
4646

4747
var lb []*LoadBalancingEndpoint
4848
var healthCheckPort int32
4949

50-
for _, s := range ep.Subsets {
50+
for _, s := range eps.Subsets {
5151
// Skip subsets without ready addresses.
5252
if len(s.Addresses) < 1 {
5353
continue
@@ -199,16 +199,16 @@ func (c *EndpointsCache) SetClusters(clusters []*dag.ServiceCluster) error {
199199
return nil
200200
}
201201

202-
// UpdateEndpoint adds ep to the cache, or replaces it if it is
202+
// UpdateEndpoint adds eps to the cache, or replaces it if it is
203203
// already cached. Any ServiceClusters that are backed by a Service
204-
// that ep belongs become stale. Returns a boolean indicating whether
205-
// any ServiceClusters use ep or not.
206-
func (c *EndpointsCache) UpdateEndpoint(ep *v1.Endpoints) bool {
204+
// that eps belongs become stale. Returns a boolean indicating whether
205+
// any ServiceClusters use eps or not.
206+
func (c *EndpointsCache) UpdateEndpoint(eps *v1.Endpoints) bool {
207207
c.mu.Lock()
208208
defer c.mu.Unlock()
209209

210-
name := k8s.NamespacedNameOf(ep)
211-
c.endpoints[name] = ep.DeepCopy()
210+
name := k8s.NamespacedNameOf(eps)
211+
c.endpoints[name] = eps.DeepCopy()
212212

213213
// If any service clusters include this endpoint, mark them
214214
// all as stale.
@@ -220,14 +220,14 @@ func (c *EndpointsCache) UpdateEndpoint(ep *v1.Endpoints) bool {
220220
return false
221221
}
222222

223-
// DeleteEndpoint deletes ep from the cache. Any ServiceClusters
224-
// that are backed by a Service that ep belongs become stale. Returns
225-
// a boolean indicating whether any ServiceClusters use ep or not.
226-
func (c *EndpointsCache) DeleteEndpoint(ep *v1.Endpoints) bool {
223+
// DeleteEndpoint deletes eps from the cache. Any ServiceClusters
224+
// that are backed by a Service that eps belongs become stale. Returns
225+
// a boolean indicating whether any ServiceClusters use eps or not.
226+
func (c *EndpointsCache) DeleteEndpoint(eps *v1.Endpoints) bool {
227227
c.mu.Lock()
228228
defer c.mu.Unlock()
229229

230-
name := k8s.NamespacedNameOf(ep)
230+
name := k8s.NamespacedNameOf(eps)
231231
delete(c.endpoints, name)
232232

233233
// If any service clusters include this endpoint, mark them

internal/xdscache/v3/server_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/projectcontour/contour/internal/contour"
3131
"github.com/projectcontour/contour/internal/dag"
3232
"github.com/projectcontour/contour/internal/fixture"
33+
"github.com/projectcontour/contour/internal/ref"
3334
"github.com/projectcontour/contour/internal/xds"
3435
contour_xds_v3 "github.com/projectcontour/contour/internal/xds/v3"
3536
"github.com/projectcontour/contour/internal/xdscache"
@@ -39,6 +40,7 @@ import (
3940
"google.golang.org/grpc/credentials/insecure"
4041
"google.golang.org/grpc/status"
4142
v1 "k8s.io/api/core/v1"
43+
discoveryv1 "k8s.io/api/discovery/v1"
4244
networking_v1 "k8s.io/api/networking/v1"
4345
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
4446
"k8s.io/apimachinery/pkg/util/intstr"
@@ -48,6 +50,7 @@ func TestGRPC(t *testing.T) {
4850
// tr and et is recreated before the start of each test.
4951
var et *EndpointsTranslator
5052
var eh *contour.EventHandler
53+
var est *EndpointSliceTranslator
5154

5255
tests := map[string]func(*testing.T, *grpc.ClientConn){
5356
"StreamClusters": func(t *testing.T, cc *grpc.ClientConn) {
@@ -104,6 +107,39 @@ func TestGRPC(t *testing.T) {
104107
checkrecv(t, stream) // check we receive one notification
105108
checktimeout(t, stream) // check that the second receive times out
106109
},
110+
"StreamEndpointSlices": func(t *testing.T, cc *grpc.ClientConn) {
111+
et.OnAdd(&discoveryv1.EndpointSlice{
112+
ObjectMeta: metav1.ObjectMeta{
113+
Name: "kube-scheduler",
114+
Namespace: "kube-system",
115+
},
116+
AddressType: discoveryv1.AddressTypeIPv4,
117+
Endpoints: []discoveryv1.Endpoint{
118+
{
119+
Addresses: []string{
120+
"130.211.139.167",
121+
},
122+
},
123+
},
124+
Ports: []discoveryv1.EndpointPort{
125+
{
126+
Port: ref.To[int32](80),
127+
},
128+
{
129+
Port: ref.To[int32](80),
130+
},
131+
},
132+
}, false)
133+
134+
eds := envoy_service_endpoint_v3.NewEndpointDiscoveryServiceClient(cc)
135+
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
136+
defer cancel()
137+
stream, err := eds.StreamEndpoints(ctx)
138+
require.NoError(t, err)
139+
sendreq(t, stream, resource.EndpointType) // send initial notification
140+
checkrecv(t, stream) // check we receive one notification
141+
checktimeout(t, stream) // check that the second receive times out
142+
},
107143
"StreamListeners": func(t *testing.T, cc *grpc.ClientConn) {
108144
// add an ingress, which will create a non tls listener
109145
eh.OnAdd(&networking_v1.Ingress{
@@ -200,12 +236,14 @@ func TestGRPC(t *testing.T) {
200236
for name, fn := range tests {
201237
t.Run(name, func(t *testing.T) {
202238
et = NewEndpointsTranslator(fixture.NewTestLogger(t))
239+
est = NewEndpointSliceTranslator(fixture.NewTestLogger(t))
203240

204241
resources := []xdscache.ResourceCache{
205242
&ListenerCache{},
206243
&SecretCache{},
207244
&RouteCache{},
208245
&ClusterCache{},
246+
est,
209247
et,
210248
&RuntimeCache{},
211249
}

0 commit comments

Comments
 (0)