Skip to content

Commit

Permalink
Move saving of hosts file to after all ports are forwarded
Browse files Browse the repository at this point in the history
This fixes #74
  • Loading branch information
rmja committed Aug 21, 2019
1 parent 6a86353 commit cb2bed3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
7 changes: 7 additions & 0 deletions cmd/kubefwd/services/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,17 @@ Try:
}
}

log.Printf("Saving hosts file\n")
err = hostFile.Save()
if err != nil {
log.Error("Error saving hosts file", err)
}

wg.Wait()

log.Printf("Done...\n")

log.Printf("Restoring hosts file\n")
err = hostFile.Save()
if err != nil {
log.Fatalf("Error saving hostfile: %s\n", err.Error())
Expand Down
32 changes: 13 additions & 19 deletions pkg/fwdport/fwdport.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"strconv"
"strings"

"github.com/prometheus/common/log"

"github.com/txn2/txeh"

"github.com/txn2/kubefwd/pkg/fwdpub"
Expand Down Expand Up @@ -113,28 +111,22 @@ func PortForward(pfo *PortForwardOpts) error {

}

err = pfo.Hostfile.Save()
if err != nil {
return err
cleanupHostfile := func() {
if pfo.Remote == false {
if pfo.Domain != "" {
pfo.Hostfile.RemoveHost(localServiceName + "." + pfo.Domain)
pfo.Hostfile.RemoveHost(nsServiceName + "." + pfo.Domain)
}
pfo.Hostfile.RemoveHost(localServiceName)
pfo.Hostfile.RemoveHost(nsServiceName)
}
pfo.Hostfile.RemoveHost(fullServiceName)
}

go func() {
<-signals
if stopChannel != nil {
if pfo.Remote == false {
if pfo.Domain != "" {
pfo.Hostfile.RemoveHost(localServiceName + "." + pfo.Domain)
pfo.Hostfile.RemoveHost(nsServiceName + "." + pfo.Domain)
}
pfo.Hostfile.RemoveHost(localServiceName)
pfo.Hostfile.RemoveHost(nsServiceName)
}
pfo.Hostfile.RemoveHost(fullServiceName)
err = pfo.Hostfile.Save()
if err != nil {
log.Error("Error saving hosts file", err)
}

cleanupHostfile()
close(stopChannel)
}
}()
Expand All @@ -146,6 +138,7 @@ func PortForward(pfo *PortForwardOpts) error {
fw, err := portforward.New(dialer, fwdPorts, stopChannel, readyChannel, &p, &p)
if err != nil {
signal.Stop(signals)
cleanupHostfile()
return err
}

Expand All @@ -154,6 +147,7 @@ func PortForward(pfo *PortForwardOpts) error {
err = fw.ForwardPorts()
if err != nil {
signal.Stop(signals)
cleanupHostfile()
return err
}

Expand Down

0 comments on commit cb2bed3

Please sign in to comment.