Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jukie authored and jmurret committed Mar 7, 2024
1 parent b8e5ece commit ee26768
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 34 deletions.
35 changes: 18 additions & 17 deletions control-plane/catalog/to-consul/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ type ServiceResource struct {

// endpointSliceListMap uses the same keys as serviceMap but maps to the EndpointSliceList
// of each service.
endpointSliceListMap map[string]*discoveryv1.EndpointSliceList
endpointSliceListMap map[string][]discoveryv1.EndpointSlice

// EnableIngress enables syncing of the hostname from an Ingress resource
// to the service registration if an Ingress rule matches the service.
Expand Down Expand Up @@ -201,7 +201,7 @@ func (t *ServiceResource) Upsert(key string, raw interface{}) error {
// We expect a Service. If it isn't a service then just ignore it.
service, ok := raw.(*corev1.Service)
if !ok {
t.Log.Warn("upsert got invalid type", "raw", raw)
t.Log.Warn("upsert got invalid type svc", "raw", raw)
return nil
}

Expand Down Expand Up @@ -242,9 +242,9 @@ func (t *ServiceResource) Upsert(key string, raw interface{}) error {
return err // Ensure to handle or return the error appropriately
} else {
if t.endpointSliceListMap == nil {
t.endpointSliceListMap = make(map[string]*discoveryv1.EndpointSliceList)
t.endpointSliceListMap = make(map[string][]discoveryv1.EndpointSlice)
}
t.endpointSliceListMap[key] = endpointSliceList
t.endpointSliceListMap[key] = endpointSliceList.Items
t.Log.Debug("[ServiceResource.Upsert] adding service's endpoint slices to endpointSliceListMap", "key", key, "service", service, "endpointSliceList", endpointSliceList)
}
}
Expand Down Expand Up @@ -592,12 +592,12 @@ func (t *ServiceResource) generateRegistrations(key string) {
return
}

endpointsList := t.endpointSliceListMap[key]
if endpointsList == nil {
endpointSliceList := t.endpointSliceListMap[key]
if endpointSliceList == nil {
return
}

for _, endpointSlice := range endpointsList.Items {
for _, endpointSlice := range endpointSliceList {
for _, endpoint := range endpointSlice.Endpoints {
// Check that the node name exists
// subsetAddr.NodeName is of type *string
Expand Down Expand Up @@ -694,14 +694,14 @@ func (t *ServiceResource) registerServiceInstance(
}

seen := map[string]struct{}{}
for _, endpointSlices := range endpointSliceList.Items {
for _, endpointSlice := range endpointSliceList {
// For ClusterIP services and if LoadBalancerEndpointsSync is true, we use the endpoint port instead
// of the service port because we're registering each endpoint
// as a separate service instance.
epPort := baseService.Port
if overridePortName != "" {
// If we're supposed to use a specific named port, find it.
for _, p := range endpointSlices.Ports {
for _, p := range endpointSlice.Ports {
if overridePortName == *p.Name {
epPort = int(*p.Port)
break
Expand All @@ -710,12 +710,12 @@ func (t *ServiceResource) registerServiceInstance(
} else if overridePortNumber == 0 {
// Otherwise we'll just use the first port in the list
// (unless the port number was overridden by an annotation).
for _, p := range endpointSlices.Ports {
for _, p := range endpointSlice.Ports {
epPort = int(*p.Port)
break
}
}
for _, endpoint := range endpointSlices.Endpoints {
for _, endpoint := range endpointSlice.Endpoints {
for _, endpointAddr := range endpoint.Addresses {

var addr string
Expand Down Expand Up @@ -767,7 +767,7 @@ func (t *ServiceResource) registerServiceInstance(
}

r.Check = &consulapi.AgentCheck{
CheckID: consulHealthCheckID(endpointSlices.Namespace, serviceID(r.Service.Service, addr)),
CheckID: consulHealthCheckID(endpointSlice.Namespace, serviceID(r.Service.Service, addr)),
Name: consulKubernetesCheckName,
Namespace: baseService.Namespace,
Type: consulKubernetesCheckType,
Expand Down Expand Up @@ -847,9 +847,9 @@ func (t *serviceEndpointsResource) Informer() cache.SharedIndexInformer {
func (t *serviceEndpointsResource) Upsert(key string, raw interface{}) error {
svc := t.Service

endpointSliceList, ok := raw.(*discoveryv1.EndpointSliceList)
endpointSlice, ok := raw.(*discoveryv1.EndpointSlice)
if !ok {
svc.Log.Warn("upsert got invalid type", "raw", raw)
svc.Log.Warn("upsert got invalid type for ep", "raw", raw)
}

svc.serviceLock.Lock()
Expand All @@ -862,9 +862,10 @@ func (t *serviceEndpointsResource) Upsert(key string, raw interface{}) error {

// We are tracking this service so let's keep track of the endpoints
if svc.endpointSliceListMap == nil {
svc.endpointSliceListMap = make(map[string]*discoveryv1.EndpointSliceList)
svc.endpointSliceListMap = make(map[string][]discoveryv1.EndpointSlice)
}
svc.endpointSliceListMap[key] = endpointSliceList
//svc.endpointSliceListMap[key] = &discoveryv1.EndpointSliceList{Items: []discoveryv1.EndpointSlice{*endpointSliceList}}
svc.endpointSliceListMap[key] = []discoveryv1.EndpointSlice{*endpointSlice}

// Update the registration and trigger a sync
svc.generateRegistrations(key)
Expand Down Expand Up @@ -931,7 +932,7 @@ func (t *serviceIngressResource) Upsert(key string, raw interface{}) error {
svc := t.Service
ingress, ok := raw.(*networkingv1.Ingress)
if !ok {
svc.Log.Warn("upsert got invalid type", "raw", raw)
svc.Log.Warn("upsert got invalid type ing", "raw", raw)
return nil
}

Expand Down
51 changes: 34 additions & 17 deletions control-plane/catalog/to-consul/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,8 @@ func TestServiceResource_lbRegisterEndpoints(t *testing.T) {
context.Background(),
&discoveryv1.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
GenerateName: "foo-",
Labels: map[string]string{discoveryv1.LabelServiceName: "foo"},
},
AddressType: discoveryv1.AddressTypeIPv4,
Endpoints: []discoveryv1.Endpoint{
Expand Down Expand Up @@ -909,27 +910,43 @@ func TestServiceResource_nodePort_singleEndpoint(t *testing.T) {

node1, _ := createNodes(t, client)

// Insert the endpoints
_, err := client.CoreV1().Endpoints(metav1.NamespaceDefault).Create(
// Insert the endpoint slice

_, err := client.DiscoveryV1().EndpointSlices(metav1.NamespaceDefault).Create(
context.Background(),
&corev1.Endpoints{
&discoveryv1.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
GenerateName: "foo-",
Labels: map[string]string{discoveryv1.LabelServiceName: "foo"},
},

Subsets: []corev1.EndpointSubset{
AddressType: discoveryv1.AddressTypeIPv4,
Endpoints: []discoveryv1.Endpoint{
{
Addresses: []corev1.EndpointAddress{
{NodeName: &node1.Name, IP: "1.2.3.4"},
},
Ports: []corev1.EndpointPort{
{Name: "http", Port: 8080},
{Name: "rpc", Port: 2000},
Addresses: []string{"1.2.3.4"},
Conditions: discoveryv1.EndpointConditions{
Ready: pointer.Bool(true),
Serving: pointer.Bool(true),
Terminating: pointer.Bool(false),
},
TargetRef: &corev1.ObjectReference{Kind: "pod", Name: "foo", Namespace: metav1.NamespaceDefault},
NodeName: &node1.Name,
Zone: pointer.String("us-west-2a"),
},
},
Ports: []discoveryv1.EndpointPort{
{
Name: pointer.String("http"),
Port: pointer.Int32(8080),
},
{
Name: pointer.String("rpc"),
Port: pointer.Int32(2000),
},
},
},
metav1.CreateOptions{})
metav1.CreateOptions{},
)

require.NoError(t, err)

// Insert the service
Expand Down Expand Up @@ -2081,18 +2098,18 @@ func createNodes(t *testing.T, client *fake.Clientset) (*corev1.Node, *corev1.No
return node1, node2
}

// createEndpoints calls the fake k8s client to create two endpoint slices across two nodes.
// createEndpointSlices calls the fake k8s client to create an endpoint slices with two endpoints on different nodes.
func createEndpointSlice(t *testing.T, client *fake.Clientset, serviceName string, namespace string) {
node1 := nodeName1
node2 := nodeName2
targetRef := corev1.ObjectReference{Kind: "pod", Name: "foobar"}

_, err := client.DiscoveryV1().EndpointSlices(metav1.NamespaceDefault).Create(
_, err := client.DiscoveryV1().EndpointSlices(namespace).Create(
context.Background(),
&discoveryv1.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{
GenerateName: serviceName + "-",
Namespace: namespace,
Labels: map[string]string{discoveryv1.LabelServiceName: serviceName},
},
AddressType: discoveryv1.AddressTypeIPv4,
Endpoints: []discoveryv1.Endpoint{
Expand Down

0 comments on commit ee26768

Please sign in to comment.