Skip to content

Commit

Permalink
fix: use endpoint name for first headless pod, support resolving base…
Browse files Browse the repository at this point in the history
… service (#42)
  • Loading branch information
jaredallard authored Dec 15, 2020
1 parent 242d125 commit 50a8140
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions internal/proxier/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit 50a8140

Please sign in to comment.