Skip to content

Commit

Permalink
workaround for headless services fixes #71
Browse files Browse the repository at this point in the history
  • Loading branch information
cjimti committed Aug 8, 2019
1 parent 77c6a2d commit aa66c0a
Showing 1 changed file with 80 additions and 75 deletions.
155 changes: 80 additions & 75 deletions cmd/kubefwd/services/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,106 +289,111 @@ func fwdServices(opts FwdServiceOpts) error {
continue
}

if svc.Spec.ClusterIP != "None" {
pods.Items = pods.Items[:1]
}
podLoop := func(pods []v1.Pod, podName bool) {
for _, pod := range pods {

for _, pod := range pods.Items {
podPort := ""
svcName := ""

podPort := ""
svcName := ""
localIp, dInc, err := fwdnet.ReadyInterface(127, 1, opts.IpC, d, podPort)
d = dInc

localIp, dInc, err := fwdnet.ReadyInterface(127, 1, opts.IpC, d, podPort)
d = dInc
for _, port := range svc.Spec.Ports {

for _, port := range svc.Spec.Ports {
podPort = port.TargetPort.String()
localPort := strconv.Itoa(int(port.Port))

podPort = port.TargetPort.String()
localPort := strconv.Itoa(int(port.Port))
if _, err := strconv.Atoi(podPort); err != nil {
// search a pods containers for the named port
if namedPodPort, ok := portSearch(podPort, pod.Spec.Containers); ok == true {
podPort = namedPodPort
}
}

if _, err := strconv.Atoi(podPort); err != nil {
// search a pods containers for the named port
if namedPodPort, ok := portSearch(podPort, pod.Spec.Containers); ok == true {
podPort = namedPodPort
_, err = opts.ClientSet.CoreV1().Pods(pod.Namespace).Get(pod.Name, metav1.GetOptions{})
if err != nil {
log.Printf("WARNING: Error getting pod: %s\n", err.Error())
break
}
}

_, err = opts.ClientSet.CoreV1().Pods(pod.Namespace).Get(pod.Name, metav1.GetOptions{})
if err != nil {
log.Printf("WARNING: Error getting pod: %s\n", err.Error())
break
}
serviceHostName := svc.Name

serviceHostName := svc.Name
if podName {
serviceHostName = pod.Name + "." + serviceHostName
}

if svc.Spec.ClusterIP == "None" {
serviceHostName = pod.Name + "." + serviceHostName
}
svcName = serviceHostName

svcName = serviceHostName
if opts.ShortName != true {
serviceHostName = serviceHostName + "." + pod.Namespace
}

if opts.ShortName != true {
serviceHostName = serviceHostName + "." + pod.Namespace
}
if opts.Domain != "" {
if verbose {
log.Printf("Using domain %s in generated hostnames", opts.Domain)
}
serviceHostName = serviceHostName + "." + opts.Domain
}

if opts.Domain != "" {
if verbose {
log.Printf("Using domain %s in generated hostnames", opts.Domain)
if opts.Remote {
serviceHostName = fmt.Sprintf("%s.svc.cluster.%s", serviceHostName, opts.Context)
}
serviceHostName = serviceHostName + "." + opts.Domain
}

if opts.Remote {
serviceHostName = fmt.Sprintf("%s.svc.cluster.%s", serviceHostName, opts.Context)
}
if verbose {
log.Printf("Resolving: %s%s to %s\n",
svc.Name,
serviceHostName,
localIp.String(),
)
}

if verbose {
log.Printf("Resolving: %s%s to %s\n",
svc.Name,
log.Printf("Forwarding: %s:%d to pod %s:%s\n",
serviceHostName,
localIp.String(),
port.Port,
pod.Name,
podPort,
)
}

log.Printf("Forwarding: %s:%d to pod %s:%s\n",
serviceHostName,
port.Port,
pod.Name,
podPort,
)

pfo := &fwdport.PortForwardOpts{
Out: publisher,
Config: opts.ClientConfig,
ClientSet: opts.ClientSet,
Context: opts.Context,
Namespace: pod.Namespace,
Service: svcName,
PodName: pod.Name,
PodPort: podPort,
LocalIp: localIp,
LocalPort: localPort,
Hostfile: opts.Hostfile,
ShortName: opts.ShortName,
Remote: opts.Remote,
ExitOnFail: exitOnFail,
Domain: domain,
}

opts.Wg.Add(1)
go func() {
err := fwdport.PortForward(pfo)
if err != nil {
log.Printf("ERROR: %s", err.Error())
pfo := &fwdport.PortForwardOpts{
Out: publisher,
Config: opts.ClientConfig,
ClientSet: opts.ClientSet,
Context: opts.Context,
Namespace: pod.Namespace,
Service: svcName,
PodName: pod.Name,
PodPort: podPort,
LocalIp: localIp,
LocalPort: localPort,
Hostfile: opts.Hostfile,
ShortName: opts.ShortName,
Remote: opts.Remote,
ExitOnFail: exitOnFail,
Domain: domain,
}

log.Printf("Stopped forwarding %s in %s.", pfo.Service, pfo.Namespace)
opts.Wg.Add(1)
go func() {
err := fwdport.PortForward(pfo)
if err != nil {
log.Printf("ERROR: %s", err.Error())
}

log.Printf("Stopped forwarding %s in %s.", pfo.Service, pfo.Namespace)

opts.Wg.Done()
}()
opts.Wg.Done()
}()

}
}
}

podLoop(pods.Items[:1], false)

if svc.Spec.ClusterIP == "None" {
podLoop(pods.Items, true)
}

}

return nil
Expand Down

0 comments on commit aa66c0a

Please sign in to comment.