Skip to content

Commit

Permalink
Merge pull request #102 from calmkart/master
Browse files Browse the repository at this point in the history
enhancement: After forward, remove lo0 interface alias
  • Loading branch information
cjimti authored Jan 15, 2020
2 parents ad9fa8c + 6b65457 commit be4b27f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
10 changes: 10 additions & 0 deletions pkg/fwdnet/fwdnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,13 @@ func ReadyInterface(a byte, b byte, c byte, d int, port string) (net.IP, int, er

return net.IP{}, d, errors.New("unable to find an available IP/Port")
}

// RemoveInterfaceAlias can remove the Interface alias after port forwarding.
// if -alias command get err, just print the error and continue.
func RemoveInterfaceAlias(ip net.IP) {
cmd := "ifconfig"
args := []string{"lo0", "-alias", ip.String()}
if err := exec.Command(cmd, args...).Run(); err != nil {
fmt.Println("Cannot ifconfig lo0 -alias " + ip.String() + "\r\n" + err.Error())
}
}
6 changes: 6 additions & 0 deletions pkg/fwdport/fwdport.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

log "github.com/sirupsen/logrus"
"github.com/txn2/kubefwd/pkg/fwdnet"
"github.com/txn2/kubefwd/pkg/fwdpub"
"github.com/txn2/txeh"
v1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -108,6 +109,7 @@ func (pfo *PortForwardOpts) PortForward() error {
close(signalChan)
if pfStopChannel != nil {
pfo.removeHosts()
pfo.removeInterfaceAlias()
close(pfStopChannel)
}
}()
Expand Down Expand Up @@ -246,6 +248,10 @@ func (pfo *PortForwardOpts) removeHosts() {
pfo.Hostfile.Unlock()
}

func (pfo *PortForwardOpts) removeInterfaceAlias() {
fwdnet.RemoveInterfaceAlias(pfo.LocalIp)
}

// Waiting for the pod running
func (pfo *PortForwardOpts) WaitForPodRunning(signalsChan chan struct{}) (*v1.Pod, error) {
pod, err := pfo.ClientSet.CoreV1().Pods(pfo.Namespace).Get(pfo.PodName, metav1.GetOptions{})
Expand Down

0 comments on commit be4b27f

Please sign in to comment.