Skip to content

Commit

Permalink
Add deregistration test and comment
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Stucki committed Apr 7, 2022
1 parent c913f3f commit 5edbd65
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
1 change: 1 addition & 0 deletions control-plane/connect-inject/endpoints_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ func (r *EndpointsController) Reconcile(ctx context.Context, req ctrl.Request) (
serviceName, ok := pod.Annotations[annotationKubernetesService]
if ok && serviceEndpoints.Name != serviceName {
r.Log.Info("ignoring endpoint because it doesn't match explicit service annotation", "name", serviceEndpoints.Name, "ns", serviceEndpoints.Namespace)
// deregistration happens later because we don't add this pod to the endpointAddressMap
continue
}

Expand Down
25 changes: 22 additions & 3 deletions control-plane/connect-inject/endpoints_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3455,15 +3455,34 @@ func TestReconcile_podSpecifiesExplicitService(t *testing.T) {
ConsulClientCfg: cfg,
}

// Run the reconcile process to check service registration.
serviceName := badEndpoint.Name

// Initially register the pod with the bad endpoint
err = consulClient.Agent().ServiceRegister(&api.AgentServiceRegistration{
ID: "pod1-" + serviceName,
Name: serviceName,
Port: 0,
Address: "1.2.3.4",
Meta: map[string]string{
"k8s-namespace": namespace,
"k8s-service-name": serviceName,
"managed-by": "consul-k8s-endpoints-controller",
"pod-name": "pod1",
},
})
require.NoError(t, err)
serviceInstances, _, err := consulClient.Catalog().Service(serviceName, "", nil)
require.NoError(t, err)
require.Len(t, serviceInstances, 1)

// Run the reconcile process to check service deregistration.
namespacedName := types.NamespacedName{Namespace: badEndpoint.Namespace, Name: serviceName}
resp, err := ep.Reconcile(context.Background(), ctrl.Request{NamespacedName: namespacedName})
require.NoError(t, err)
require.False(t, resp.Requeue)

// Check that no services are registered with Consul.
serviceInstances, _, err := consulClient.Catalog().Service(serviceName, "", nil)
// Check that the service has been deregistered with Consul.
serviceInstances, _, err = consulClient.Catalog().Service(serviceName, "", nil)
require.NoError(t, err)
require.Len(t, serviceInstances, 0)
proxyServiceInstances, _, err := consulClient.Catalog().Service(serviceName+"-sidecar-proxy", "", nil)
Expand Down

0 comments on commit 5edbd65

Please sign in to comment.