diff --git a/cmd/k8s-operator/egress-eps.go b/cmd/k8s-operator/egress-eps.go index fe270c5ea..f2e6e6fb9 100644 --- a/cmd/k8s-operator/egress-eps.go +++ b/cmd/k8s-operator/egress-eps.go @@ -135,12 +135,7 @@ func (er *egressEpsReconciler) Reconcile(ctx context.Context, req reconcile.Requ if err := er.List(ctx, podList, client.MatchingLabels(pgLabels(proxyGroupName, nil))); err != nil { return res, fmt.Errorf("error listing Pods for ProxyGroup %s: %w", proxyGroupName, err) } - // Leave newEndpoints nil (not an empty slice) so that when no Pod is ready it - // compares equal to a freshly created slice's nil Endpoints - otherwise the - // reflect.DeepEqual guard below would see nil != []Endpoint{} and, because this - // reconciler watches EndpointSlices, a needless write would re-trigger it. - //TODO(beckypauley): review swapping to nil vs empty. - var newEndpoints []discoveryv1.Endpoint + newEndpoints := make([]discoveryv1.Endpoint, 0) for _, pod := range podList.Items { ready, err := er.podIsReadyToRouteTraffic(ctx, pod, &cfg, tailnetSvc, eps.AddressType, lg) if err != nil { @@ -186,7 +181,6 @@ func (er *egressEpsReconciler) Reconcile(ctx context.Context, req reconcile.Requ // orphan guard above both early-return on a slice lacking our labels, so a // slice that reaches this point carries them. maps.Copy into a nil map panics, // so do not reorder those guards after this point. - // TODO(beckypauley): consider still having a nil guard here. maps.Copy(eps.Labels, egressSvcEpsLabels(svc, clusterIPSvc)) // Apply the owned fields, then compare the whole object against the pre-mutation diff --git a/cmd/k8s-operator/egress-eps_test.go b/cmd/k8s-operator/egress-eps_test.go index 19b6ec495..e37ef6c15 100644 --- a/cmd/k8s-operator/egress-eps_test.go +++ b/cmd/k8s-operator/egress-eps_test.go @@ -221,9 +221,7 @@ func TestTailscaleEgressEndpointSlices(t *testing.T) { mak.Set(&s.Data, egressservices.KeyEgressServices, stBs) }) expectReconciled(t, er, "operator-ns", "foo") - // No ready Pod: egress-eps writes nil Endpoints (not an empty slice) so a - // no-ready-Pods result compares equal to a freshly created slice's nil field. - eps.Endpoints = nil + eps.Endpoints = []discoveryv1.Endpoint{} expectEqual(t, fc, eps) }) @@ -291,7 +289,7 @@ func TestTailscaleEgressEndpointSlices(t *testing.T) { }) expectReconciled(t, er, "operator-ns", "foo-ipv6") // IPv4-only pod should not appear in the IPv6 EndpointSlice. - epsV6.Endpoints = nil + epsV6.Endpoints = []discoveryv1.Endpoint{} expectEqual(t, fc, epsV6) }) ipv6Pod := &corev1.Pod{ @@ -313,7 +311,7 @@ func TestTailscaleEgressEndpointSlices(t *testing.T) { mak.Set(&s.Data, egressservices.KeyEgressServices, stBs) }) expectReconciled(t, er, "operator-ns", "foo-ipv6") - epsV6.Endpoints = nil + epsV6.Endpoints = []discoveryv1.Endpoint{} expectEqual(t, fc, epsV6) }) t.Run("ipv6_pod_ready_to_route", func(t *testing.T) { diff --git a/cmd/k8s-operator/egress-services-readiness.go b/cmd/k8s-operator/egress-services-readiness.go index c051dced1..031e0d240 100644 --- a/cmd/k8s-operator/egress-services-readiness.go +++ b/cmd/k8s-operator/egress-services-readiness.go @@ -88,9 +88,8 @@ func (esrr *egressSvcsReadinessReconciler) Reconcile(ctx context.Context, req re return res, nil } // EndpointSlice list can also contain orphans (e.g. if an ExternalName Service's ProxyGroup is edited in place). - // We don't support editing ProxyGroup on ExternalName Services, but should guard against this case. - // We use the current ClusterIP Service's name to filter those out, mirroring the orphan guard in the egress EndpointSlices - // reconciler. Otherwise, an orphan slice could block the readiness condition. + // We don't support editing ProxyGroup on ExternalName Services, but should guard against this case. Otherwise, + // an orphan slice could block the readiness condition. epsList := &discoveryv1.EndpointSliceList{} if err = esrr.List(ctx, epsList, client.InNamespace(esrr.tsNamespace), client.MatchingLabels(crl)); err != nil { err = fmt.Errorf("error listing EndpointSlices: %w", err) diff --git a/cmd/k8s-operator/operator.go b/cmd/k8s-operator/operator.go index bfd1c6e55..0a8d53a1b 100644 --- a/cmd/k8s-operator/operator.go +++ b/cmd/k8s-operator/operator.go @@ -38,6 +38,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/cache" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/config" + "sigs.k8s.io/controller-runtime/pkg/event" "sigs.k8s.io/controller-runtime/pkg/handler" logf "sigs.k8s.io/controller-runtime/pkg/log" kzap "sigs.k8s.io/controller-runtime/pkg/log/zap" @@ -578,12 +579,21 @@ func runReconcilers(opts reconcilerOpts) { // tailscale/tailscale#20322). egressSvcFromEps maps a slice back to its // parent egress Service. egressSvcFromEpsFilterForSvcs := handler.EnqueueRequestsFromMapFunc(egressSvcFromEps) + // This reconciler only needs slice events to recreate a missing or deleted slice. + // A nil predicate.Funcs field means "allow", so the other event types must be set + // explicitly to filter them out. + egressSliceDeletedOnly := predicate.Funcs{ + CreateFunc: func(event.CreateEvent) bool { return false }, + UpdateFunc: func(event.UpdateEvent) bool { return false }, + GenericFunc: func(event.GenericEvent) bool { return false }, + DeleteFunc: func(event.DeleteEvent) bool { return true }, + } err = builder. ControllerManagedBy(mgr). Named("egress-svcs-reconciler"). Watches(&corev1.Service{}, egressSvcFilter). Watches(&tsapi.ProxyGroup{}, egressProxyGroupFilter). - Watches(&discoveryv1.EndpointSlice{}, egressSvcFromEpsFilterForSvcs). + Watches(&discoveryv1.EndpointSlice{}, egressSvcFromEpsFilterForSvcs, builder.WithPredicates(egressSliceDeletedOnly)). Complete(&egressSvcsReconciler{ Client: mgr.GetClient(), tsNamespace: opts.tailscaleNamespace,