From 50a8140f86df2ed4134b3d97327635e6ce8545fc Mon Sep 17 00:00:00 2001 From: Jared Allard Date: Tue, 15 Dec 2020 13:10:34 -0800 Subject: [PATCH] fix: use endpoint name for first headless pod, support resolving base service (#42) --- internal/proxier/handlers.go | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/internal/proxier/handlers.go b/internal/proxier/handlers.go index 3b8e591..370e621 100644 --- a/internal/proxier/handlers.go +++ b/internal/proxier/handlers.go @@ -42,9 +42,10 @@ func CreateHandlers(ctx context.Context, requester chan<- PortForwardRequest, return serviceChan, doneChan } -// Services +//nolint:funlen,gocyclo func serviceProcessor(ctx context.Context, event <-chan ServiceEvent, - doneChan chan struct{}, requester chan<- PortForwardRequest, k kubernetes.Interface, clusterDomain string) { + doneChan chan struct{}, requester chan<- PortForwardRequest, + k kubernetes.Interface, clusterDomain string) { for { select { case <-ctx.Done(): @@ -102,12 +103,39 @@ func serviceProcessor(ctx context.Context, event <-chan ServiceEvent, case ServiceTypeStatefulset: // TODO: This doesn't support multiple pods for a service right now // eventually we should support that. - name := fmt.Sprintf("%s.%s", info.Name+"-0", info.Name) + // grab the first endpoint to build the name. This sucks, but it's + // needed for Outreach's usecases. Please remove this. + obj, exists, err := kevents.GlobalCache.GetStore(&corev1.Endpoints{}).GetByKey(s.Service.Namespace + "/" + s.Service.Name) + if err != nil || !exists { + continue + } + endpoints := obj.(*corev1.Endpoints) + + refName := "" + + loop: + for _, sub := range endpoints.Subsets { + for _, a := range sub.Addresses { + if a.TargetRef != nil && a.TargetRef.Kind == "Pod" { + refName = a.TargetRef.Name + break loop + } + } + } + + name := fmt.Sprintf("%s.%s", refName, info.Name) msg = PortForwardRequest{ CreatePortForwardRequest: &CreatePortForwardRequest{ Service: info, Ports: ports, Hostnames: []string{ + // headless service to one of the endpoints + info.Name, + info.Name + "." + info.Namespace, + info.Name + "." + info.Namespace + ".svc", + info.Name + "." + info.Namespace + ".svc." + clusterDomain, + + // pod level name, fmt.Sprintf("%s.%s", name, info.Namespace), fmt.Sprintf("%s.%s.svc", name, info.Namespace),