Skip to content

Commit

Permalink
feat: add port map
Browse files Browse the repository at this point in the history
  • Loading branch information
ndj888 committed Mar 5, 2021
1 parent 78b485b commit 4bb6cb3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
19 changes: 19 additions & 0 deletions cmd/kubefwd/services/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"
Expand Down Expand Up @@ -53,6 +54,7 @@ var namespaces []string
var contexts []string
var verbose bool
var domain string
var port string

func init() {
// override error output from k8s.io/apimachinery/pkg/util/runtime
Expand All @@ -67,6 +69,8 @@ func init() {
Cmd.Flags().StringP("selector", "l", "", "Selector (label query) to filter on; supports '=', '==', and '!=' (e.g. -l key1=value1,key2=value2).")
Cmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "Verbose output.")
Cmd.Flags().StringVarP(&domain, "domain", "d", "", "Append a pseudo domain name to generated host names.")
// "The Issues https://github.com/txn2/kubefwd/issues/121"
Cmd.Flags().StringVarP(&port, "port", "p", "", "Map the ports you need.(e.g. host port:container port -p 8080:80")

}

Expand Down Expand Up @@ -410,6 +414,7 @@ func (opts *NamespaceOpts) AddServiceHandler(obj interface{}) {
PortForwards: make(map[string]*fwdport.PortForwardOpts),
SyncDebouncer: debounce.New(5 * time.Second),
DoneChannel: make(chan struct{}),
PortMap: opts.ParsePortMap(port),
}

// Add the service to the catalog of services being forwarded
Expand All @@ -435,3 +440,17 @@ func (opts *NamespaceOpts) UpdateServiceHandler(_ interface{}, new interface{})
log.Printf("update service %s.", key)
}
}

// parse string port to PortMap
func (opts *NamespaceOpts) ParsePortMap(port string) *[]fwdservice.PortMap {
var portList []fwdservice.PortMap
if port == "" {
return nil
}
strArr := strings.Split(port, ",")
for _, s := range strArr {
portInfo := strings.Split(s, ":")
portList = append(portList, fwdservice.PortMap{SourcePort: portInfo[0], TargetPort: portInfo[1]})
}
return &portList
}
35 changes: 31 additions & 4 deletions pkg/fwdservice/fwdservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ type ServiceFWD struct {
// while normally only a single pod is forwarded.
Headless bool

LastSyncedAt time.Time // When was the set of pods last synced

LastSyncedAt time.Time // When was the set of pods last synced
PortMap *[]PortMap // port map array.
// Use debouncer for listing pods so we don't hammer the k8s when a bunch of changes happen at once
SyncDebouncer func(f func())

Expand All @@ -70,6 +70,15 @@ type ServiceFWD struct {
DoneChannel chan struct{} // After shutdown is complete, this channel will be closed
}

/**
add port map
@url https://github.com/txn2/kubefwd/issues/121
*/
type PortMap struct {
SourcePort string
TargetPort string
}

// String representation of a ServiceFWD returns a unique name
// in the form SERVICE_NAME.NAMESPACE.CONTEXT
func (svcFwd *ServiceFWD) String() string {
Expand Down Expand Up @@ -252,8 +261,12 @@ func (svcFwd *ServiceFWD) LoopPodsToForward(pods []v1.Pod, includePodNameInHost
}

podPort = port.TargetPort.String()
localPort := strconv.Itoa(int(port.Port))

localPort := svcFwd.getPortMap(port.Port)
v, err := strconv.ParseInt(localPort, 10, 32)
if err != nil {
log.Fatal(err)
}
port.Port = int32(v)
if _, err := strconv.Atoi(podPort); err != nil {
// search a pods containers for the named port
if namedPodPort, ok := portSearch(podPort, pod.Spec.Containers); ok {
Expand Down Expand Up @@ -362,3 +375,17 @@ func portSearch(portName string, containers []v1.Container) (string, bool) {

return "", false
}

// port exist port map return
func (svcFwd *ServiceFWD) getPortMap(port int32) string {
p := strconv.Itoa(int(port))
if svcFwd.PortMap != nil {
for _, portMapInfo := range *svcFwd.PortMap {
if p == portMapInfo.TargetPort {
//use map port
return portMapInfo.SourcePort
}
}
}
return p
}

0 comments on commit 4bb6cb3

Please sign in to comment.