Skip to content

Commit

Permalink
headless services support
Browse files Browse the repository at this point in the history
  • Loading branch information
alloran committed Apr 8, 2019
1 parent 8426e30 commit 4c5d47e
Showing 1 changed file with 74 additions and 64 deletions.
138 changes: 74 additions & 64 deletions cmd/kubefwd/services/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,87 +280,97 @@ func fwdServices(opts FwdServiceOpts) error {
continue
}

podName := ""
podPort := ""
podNamespace := ""
if svc.Spec.ClusterIP != "None" {
pods.Items = pods.Items[:1]
}

for _, pod := range pods.Items {

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

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

podName = pods.Items[0].Name
podNamespace = pods.Items[0].Namespace
podPort = port.TargetPort.String()
localPort := strconv.Itoa(int(port.Port))
for _, port := range svc.Spec.Ports {

if _, err := strconv.Atoi(podPort); err != nil {
// search a pods containers for the named port
if namedPodPort, ok := portSearch(podPort, pods.Items[0].Spec.Containers); ok == true {
podPort = namedPodPort
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
}
}
}

_, err = opts.ClientSet.CoreV1().Pods(podNamespace).Get(podName, 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 opts.ShortName != true {
serviceHostName = serviceHostName + "." + podNamespace
}
if svc.Spec.ClusterIP == "None" {
serviceHostName = pod.Name + "." + serviceHostName
}

if opts.Remote {
serviceHostName = fmt.Sprintf("%s.svc.cluster.%s", serviceHostName, opts.Context)
}
svcName = serviceHostName

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

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,
if verbose {
log.Printf("Resolving: %s%s to %s\n",
svc.Name,
serviceHostName,
localIp.String(),
)
}

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,
podName,
podPort,
)

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

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,
}

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()
}()

}
}
}

Expand Down

0 comments on commit 4c5d47e

Please sign in to comment.