Skip to content

Commit

Permalink
Merge pull request #76 from rmja/fix74
Browse files Browse the repository at this point in the history
Move saving of hosts file to after all ports are forwarded
  • Loading branch information
cjimti authored Aug 25, 2019
2 parents 6a86353 + cb2bed3 commit cf75499
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 cf75499

Please sign in to comment.