Skip to content

Commit 7a4c271

Browse files
committed
fix(multicluster): make service cleanup logic respect namespaces
Signed-off-by: Cody Bond <[email protected]>
1 parent f039635 commit 7a4c271

3 files changed

Lines changed: 200 additions & 1 deletion

File tree

multicluster/service-mirror/cluster_watcher_headless.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func (rcsw *RemoteClusterServiceWatcher) createOrUpdateHeadlessEndpoints(ctx con
142142
}
143143

144144
// Fetch all Endpoint Mirror services that belong to the same Headless Mirror
145-
endpointMirrorServices, err := rcsw.localAPIClient.Svc().Lister().List(labels.Set(matchLabels).AsSelector())
145+
endpointMirrorServices, err := rcsw.localAPIClient.Svc().Lister().Services(exportedService.Namespace).List(labels.Set(matchLabels).AsSelector())
146146
if err != nil {
147147
return err
148148
}

multicluster/service-mirror/cluster_watcher_mirroring_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,75 @@ func TestRemoteEndpointsUpdatedMirroring(t *testing.T) {
967967
}
968968
}
969969

970+
func TestHeadlessEndpointMirrorCleanupRespectsNamespaces(t *testing.T) {
971+
for _, tt := range []mirroringTestCase{
972+
{
973+
description: "headless endpoint mirror GC is scoped to the appropriate namespace",
974+
environment: multiNamespaceHeadlessMirrorEndpointsWithSameServiceName,
975+
expectedLocalServices: []*corev1.Service{
976+
headlessMirrorService("service-one-remote", "ns1", "111", nil, []corev1.ServicePort{
977+
{Name: "port1", Protocol: "TCP", Port: 555},
978+
{Name: "port2", Protocol: "TCP", Port: 666},
979+
}),
980+
endpointMirrorService("pod-0", "service-one-remote", "ns1", "333", nil, []corev1.ServicePort{
981+
{Name: "port1", Protocol: "TCP", Port: 555},
982+
{Name: "port2", Protocol: "TCP", Port: 666},
983+
}),
984+
headlessMirrorService("service-one-remote", "ns2", "222", nil, []corev1.ServicePort{
985+
{Name: "port1", Protocol: "TCP", Port: 555},
986+
{Name: "port2", Protocol: "TCP", Port: 666},
987+
}),
988+
endpointMirrorService("pod-0", "service-one-remote", "ns2", "444", nil, []corev1.ServicePort{
989+
{Name: "port1", Protocol: "TCP", Port: 555},
990+
{Name: "port2", Protocol: "TCP", Port: 666},
991+
}),
992+
endpointMirrorService("pod-1", "service-one-remote", "ns2", "555", nil, []corev1.ServicePort{
993+
{Name: "port1", Protocol: "TCP", Port: 555},
994+
{Name: "port2", Protocol: "TCP", Port: 666},
995+
}),
996+
},
997+
expectedLocalEndpoints: []*corev1.Endpoints{
998+
headlessMirrorEndpoints(
999+
"service-one-remote",
1000+
"ns1",
1001+
nil,
1002+
"gateway-identity",
1003+
[]corev1.EndpointPort{
1004+
{Name: "port1", Protocol: "TCP", Port: 555},
1005+
{Name: "port2", Protocol: "TCP", Port: 666},
1006+
},
1007+
),
1008+
endpointMirrorEndpoints("service-one-remote", "ns1", nil, "pod-0", "192.0.2.127", "gateway-identity", []corev1.EndpointPort{
1009+
{Name: "port1", Protocol: "TCP", Port: 888},
1010+
{Name: "port2", Protocol: "TCP", Port: 888},
1011+
}),
1012+
headlessMirrorEndpointsUpdated(
1013+
"service-one-remote",
1014+
"ns2",
1015+
[]string{"pod-0", "pod-1"},
1016+
[]string{"", ""},
1017+
"gateway-identity",
1018+
[]corev1.EndpointPort{
1019+
{Name: "port1", Protocol: "TCP", Port: 555},
1020+
{Name: "port2", Protocol: "TCP", Port: 666},
1021+
},
1022+
),
1023+
endpointMirrorEndpoints("service-one-remote", "ns2", nil, "pod-0", "192.0.2.127", "gateway-identity", []corev1.EndpointPort{
1024+
{Name: "port1", Protocol: "TCP", Port: 888},
1025+
{Name: "port2", Protocol: "TCP", Port: 888},
1026+
}),
1027+
endpointMirrorEndpoints("service-one-remote", "ns2", nil, "pod-1", "192.0.2.127", "gateway-identity", []corev1.EndpointPort{
1028+
{Name: "port1", Protocol: "TCP", Port: 888},
1029+
{Name: "port2", Protocol: "TCP", Port: 888},
1030+
}),
1031+
},
1032+
},
1033+
} {
1034+
tc := tt // pin
1035+
tc.run(t)
1036+
}
1037+
}
1038+
9701039
func TestClusterUnregisteredMirroring(t *testing.T) {
9711040
for _, tt := range []mirroringTestCase{
9721041
{

multicluster/service-mirror/cluster_watcher_test_util.go

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,136 @@ var updateEndpointsWithChangedHosts = &testEnvironment{
717717
},
718718
},
719719
}
720+
721+
var multiNamespaceHeadlessMirrorEndpointsWithSameServiceName = &testEnvironment{
722+
events: []interface{}{
723+
&OnUpdateEndpointsCalled{
724+
ep: remoteHeadlessEndpoints(
725+
"service-one",
726+
"ns1",
727+
"999",
728+
"192.0.0.1",
729+
[]corev1.EndpointPort{
730+
{Name: "port1", Protocol: "TCP", Port: 555},
731+
{Name: "port2", Protocol: "TCP", Port: 666},
732+
},
733+
),
734+
},
735+
},
736+
remoteResources: []string{
737+
asYaml(gateway("gateway", "gateway-ns", "currentGatewayResVersion", "192.0.2.127", "mc-gateway", 888, "", defaultProbePort, defaultProbePath, defaultProbePeriod)),
738+
asYaml(remoteHeadlessService("service-one", "ns1", "111", map[string]string{consts.DefaultExportedServiceSelector: "true"}, []corev1.ServicePort{
739+
{Name: "port1", Protocol: "TCP", Port: 555},
740+
{Name: "port2", Protocol: "TCP", Port: 666},
741+
})),
742+
asYaml(remoteHeadlessService("service-one", "ns2", "222", map[string]string{consts.DefaultExportedServiceSelector: "true"}, []corev1.ServicePort{
743+
{Name: "port1", Protocol: "TCP", Port: 555},
744+
{Name: "port2", Protocol: "TCP", Port: 666},
745+
})),
746+
asYaml(remoteHeadlessEndpoints("service-one", "ns1", "998", "192.0.0.1", []corev1.EndpointPort{
747+
{Name: "port1", Protocol: "TCP", Port: 555},
748+
{Name: "port2", Protocol: "TCP", Port: 666},
749+
})),
750+
asYaml(remoteHeadlessEndpointsUpdate("service-one", "ns2", "997", "192.0.0.2", []corev1.EndpointPort{
751+
{Name: "port1", Protocol: "TCP", Port: 555},
752+
{Name: "port2", Protocol: "TCP", Port: 666},
753+
})),
754+
},
755+
localResources: []string{
756+
asYaml(namespace("ns1")),
757+
asYaml(namespace("ns2")),
758+
asYaml(headlessMirrorService("service-one-remote", "ns1", "111", nil, []corev1.ServicePort{
759+
{Name: "port1", Protocol: "TCP", Port: 555},
760+
{Name: "port2", Protocol: "TCP", Port: 666},
761+
})),
762+
asYaml(endpointMirrorService("pod-0", "service-one-remote", "ns1", "333", nil, []corev1.ServicePort{
763+
{Name: "port1", Protocol: "TCP", Port: 555},
764+
{Name: "port2", Protocol: "TCP", Port: 666},
765+
})),
766+
asYaml(headlessMirrorEndpoints(
767+
"service-one-remote",
768+
"ns1",
769+
nil,
770+
"gateway-identity",
771+
[]corev1.EndpointPort{
772+
{Name: "port1", Protocol: "TCP", Port: 555},
773+
{Name: "port2", Protocol: "TCP", Port: 666},
774+
},
775+
)),
776+
asYaml(endpointMirrorEndpoints(
777+
"service-one-remote",
778+
"ns1",
779+
nil,
780+
"pod-0",
781+
"192.0.2.127",
782+
"gateway-identity",
783+
[]corev1.EndpointPort{
784+
{Name: "port1", Protocol: "TCP", Port: 888},
785+
{Name: "port2", Protocol: "TCP", Port: 888},
786+
},
787+
)),
788+
asYaml(headlessMirrorService("service-one-remote", "ns2", "222", nil, []corev1.ServicePort{
789+
{Name: "port1", Protocol: "TCP", Port: 555},
790+
{Name: "port2", Protocol: "TCP", Port: 666},
791+
})),
792+
asYaml(endpointMirrorService("pod-0", "service-one-remote", "ns2", "444", nil, []corev1.ServicePort{
793+
{Name: "port1", Protocol: "TCP", Port: 555},
794+
{Name: "port2", Protocol: "TCP", Port: 666},
795+
})),
796+
asYaml(endpointMirrorService("pod-1", "service-one-remote", "ns2", "555", nil, []corev1.ServicePort{
797+
{Name: "port1", Protocol: "TCP", Port: 555},
798+
{Name: "port2", Protocol: "TCP", Port: 666},
799+
})),
800+
asYaml(headlessMirrorEndpointsUpdated(
801+
"service-one-remote",
802+
"ns2",
803+
[]string{"pod-0", "pod-1"},
804+
[]string{"", ""},
805+
"gateway-identity",
806+
[]corev1.EndpointPort{
807+
{Name: "port1", Protocol: "TCP", Port: 555},
808+
{Name: "port2", Protocol: "TCP", Port: 666},
809+
},
810+
)),
811+
asYaml(endpointMirrorEndpoints(
812+
"service-one-remote",
813+
"ns2",
814+
nil,
815+
"pod-0",
816+
"192.0.2.127",
817+
"gateway-identity",
818+
[]corev1.EndpointPort{
819+
{Name: "port1", Protocol: "TCP", Port: 888},
820+
{Name: "port2", Protocol: "TCP", Port: 888},
821+
},
822+
)),
823+
asYaml(endpointMirrorEndpoints(
824+
"service-one-remote",
825+
"ns2",
826+
nil,
827+
"pod-1",
828+
"192.0.2.127",
829+
"gateway-identity",
830+
[]corev1.EndpointPort{
831+
{Name: "port1", Protocol: "TCP", Port: 888},
832+
{Name: "port2", Protocol: "TCP", Port: 888},
833+
},
834+
)),
835+
},
836+
link: v1alpha3.Link{
837+
Spec: v1alpha3.LinkSpec{
838+
TargetClusterName: clusterName,
839+
TargetClusterDomain: clusterDomain,
840+
GatewayIdentity: "gateway-identity",
841+
GatewayAddress: "192.0.2.127",
842+
GatewayPort: "888",
843+
ProbeSpec: defaultProbeSpec,
844+
Selector: defaultSelector,
845+
RemoteDiscoverySelector: defaultRemoteDiscoverySelector,
846+
},
847+
},
848+
}
849+
720850
var clusterUnregistered = &testEnvironment{
721851
events: []interface{}{
722852
&ClusterUnregistered{},

0 commit comments

Comments
 (0)