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

Fix mirroring all services when remote selector is empty #11344

Merged
merged 4 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions multicluster/service-mirror/cluster_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ func (rcsw *RemoteClusterServiceWatcher) getMirrorServices() (*corev1.ServiceLis
}

func (rcsw *RemoteClusterServiceWatcher) handleOnDelete(service *corev1.Service) {
if rcsw.isExported(service.Labels) {
if rcsw.isExported(service.Labels) || rcsw.isRemoteDiscovery(service.Labels) {
rcsw.eventsQueue.Add(&RemoteServiceDeleted{
Name: service.Name,
Namespace: service.Namespace,
Expand Down Expand Up @@ -1242,12 +1242,7 @@ func (rcsw *RemoteClusterServiceWatcher) isExported(l map[string]string) bool {
rcsw.log.Errorf("Invalid selector: %s", err)
return false
}
remoteDiscoverySelector, err := metav1.LabelSelectorAsSelector(&rcsw.link.RemoteDiscoverySelector)
if err != nil {
rcsw.log.Errorf("Invalid selector: %s", err)
return false
}
return selector.Matches(labels.Set(l)) || remoteDiscoverySelector.Matches(labels.Set(l))
return selector.Matches(labels.Set(l))
}

func (rcsw *RemoteClusterServiceWatcher) isRemoteDiscovery(l map[string]string) bool {
Expand Down
58 changes: 58 additions & 0 deletions multicluster/service-mirror/cluster_watcher_mirroring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,64 @@ func TestRemoteServiceUpdatedMirroring(t *testing.T) {
}
}

// TestEmptyRemoteSelectors asserts that empty selectors do not introduce side
// effects, such as mirroring unexported services. An empty label selector
// functions as a catch-all (i.e. matches everything), the cluster watcher must
// uphold an invariant whereby empty selectors do not export everything by
// default.
func TestEmptyRemoteSelectors(t *testing.T) {
for _, tt := range []mirroringTestCase{
{
description: "empty remote discovery selector does not result in exports",
environment: createEnvWithSelector(defaultSelector, &metav1.LabelSelector{}),
expectedEventsInQueue: []interface{}{&RemoteServiceCreated{
service: remoteService("service-one", "ns1", "111", map[string]string{
consts.DefaultExportedServiceSelector: "true",
}, []corev1.ServicePort{
{
Name: "default1",
Protocol: "TCP",
Port: 555,
},
{
Name: "default2",
Protocol: "TCP",
Port: 666,
},
}),
},
},
},
{
description: "empty default selector does not result in exports",
environment: createEnvWithSelector(&metav1.LabelSelector{}, defaultRemoteDiscoverySelector),
expectedEventsInQueue: []interface{}{&RemoteServiceCreated{
service: remoteService("service-two", "ns1", "111", map[string]string{
consts.DefaultExportedServiceSelector: "remote-discovery",
}, []corev1.ServicePort{
{
Name: "remote1",
Protocol: "TCP",
Port: 777,
},
{
Name: "remote2",
Protocol: "TCP",
Port: 888,
},
}),
}},
},
{
description: "no selector in link does not result in exports",
environment: createEnvWithSelector(&metav1.LabelSelector{}, &metav1.LabelSelector{}),
},
} {
tc := tt
tc.run(t)
}
}

func TestRemoteEndpointsUpdatedMirroring(t *testing.T) {
for _, tt := range []mirroringTestCase{
{
Expand Down
64 changes: 64 additions & 0 deletions multicluster/service-mirror/cluster_watcher_test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -1222,3 +1222,67 @@ func endpointMirrorEndpointsAsYaml(name, namespace, hostname, gatewayIP, gateway

return string(bytes)
}

// createEmptySelectorEnv will create a test environment with two services. It
// accepts a default and a remote discovery selector which it will use for the
// link creation. This function is used to create environments that differ only
// in the selector used in the link.
func createEnvWithSelector(defaultSelector, remoteSelector *metav1.LabelSelector) *testEnvironment {
return &testEnvironment{
events: []interface{}{
&OnAddCalled{
svc: remoteService("service-one", "ns1", "111", map[string]string{
consts.DefaultExportedServiceSelector: "true",
}, []corev1.ServicePort{
{
Name: "default1",
Protocol: "TCP",
Port: 555,
},
{
Name: "default2",
Protocol: "TCP",
Port: 666,
},
}),
},
&OnAddCalled{
svc: remoteService("service-two", "ns1", "111", map[string]string{
consts.DefaultExportedServiceSelector: "remote-discovery",
}, []corev1.ServicePort{
{
Name: "remote1",
Protocol: "TCP",
Port: 777,
},
{
Name: "remote2",
Protocol: "TCP",
Port: 888,
},
}),
},
},
localResources: []string{
namespaceAsYaml("ns1"),
},
remoteResources: []string{
endpointsAsYaml("service-one", "ns1", "192.0.2.127", "gateway-identity", []corev1.EndpointPort{}),
endpointsAsYaml("service-two", "ns1", "192.0.3.127", "gateway-identity", []corev1.EndpointPort{}),
},
link: multicluster.Link{
TargetClusterName: clusterName,
TargetClusterDomain: clusterDomain,
GatewayIdentity: "",
GatewayAddress: "",
GatewayPort: 0,
ProbeSpec: multicluster.ProbeSpec{
Path: "",
Port: 0,
Period: time.Duration(0) * time.Second,
},
Selector: *defaultSelector,
RemoteDiscoverySelector: *remoteSelector,
},
}
}
Loading