Skip to content

Commit

Permalink
Lookup named ports Fixes #13
Browse files Browse the repository at this point in the history
  • Loading branch information
cjimti committed Nov 12, 2018
1 parent ecbf37d commit acb417b
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions cmd/kubefwd/services/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"strconv"
"sync"

"k8s.io/api/core/v1"

"github.com/cbednarski/hostess"
"github.com/spf13/cobra"
"github.com/txn2/kubefwd/pkg/utils"
Expand Down Expand Up @@ -181,14 +183,16 @@ func fwdServices(opts FwdServiceOpts) error {
localPort := strconv.Itoa(int(port.Port))

if _, err := strconv.Atoi(podPort); err != nil {
// @TODO lookup named port
// search a pods containers for the named port
if namedPodPort, ok := portSearch(podPort, pods.Items[0].Spec.Containers); ok == true {
podPort = namedPodPort
}
}

_, err = opts.ClientSet.CoreV1().Pods(podNamespace).Get(podName, metav1.GetOptions{})
if err != nil {
fmt.Printf("Error getting pod: %s\n", err.Error())
// TODO: Check for other pods?
break // no need to check other ports if we can't get the pod
break
}

full := ""
Expand Down Expand Up @@ -231,6 +235,19 @@ func fwdServices(opts FwdServiceOpts) error {
return nil
}

func portSearch(portName string, containers []v1.Container) (string, bool) {

for _, container := range containers {
for _, cp := range container.Ports {
if cp.Name == portName {
return fmt.Sprint(cp.ContainerPort), true
}
}
}

return "", false
}

func homeDir() string {
if h := os.Getenv("HOME"); h != "" {
return h
Expand Down

0 comments on commit acb417b

Please sign in to comment.