Skip to content

Commit

Permalink
daemon: Re-read hosts file for each request
Browse files Browse the repository at this point in the history
This is quite wasteful, but after hostsfile.Clean() has been called,
the parsed hostfile is no longer valid, and adding more data to it
causes some of its content to disappear, see
#23 (comment)

This issue can only be seen when the admin-helper daemon is used, and
when there's a limit to the number of entries per line, which means this
can only be reproduced on Windows.

By reparsing the hostfile each time we get a request, we workaround this
bug.
  • Loading branch information
cfergeau committed Nov 25, 2021
1 parent 0231dc9 commit 589c0d7
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
8 changes: 1 addition & 7 deletions cmd/admin-helper/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"net/http"

"github.com/code-ready/admin-helper/pkg/api"
"github.com/code-ready/admin-helper/pkg/hosts"
"github.com/kardianos/service"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -80,12 +79,7 @@ func (p *program) Start(s service.Service) error {
_ = logger.Error(err)
return
}
hosts, err := hosts.New()
if err != nil {
_ = logger.Error(err)
return
}
if err := http.Serve(ln, api.Mux(hosts)); err != nil {
if err := http.Serve(ln, api.Mux()); err != nil {
_ = logger.Error(err)
return
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/code-ready/admin-helper/pkg/types"
)

func Mux(hosts *hosts.Hosts) http.Handler {
func Mux() http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("/version", func(w http.ResponseWriter, r *http.Request) {
_, _ = fmt.Fprint(w, constants.Version)
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func TestMux(t *testing.T) {
ts := httptest.NewServer(Mux(nil))
ts := httptest.NewServer(Mux())
defer ts.Close()

client := client.New(http.DefaultClient, ts.URL)
Expand Down
24 changes: 24 additions & 0 deletions pkg/hosts/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ func defaultFilter(s string) bool {
return clusterRegexp.MatchString(s) || appRegexp.MatchString(s)
}

func Add(ip string, hosts []string) error {
h, err := New()
if err != nil {
return err
}
return h.Add(ip, hosts)
}

func (h *Hosts) Add(ip string, hosts []string) error {
if err := h.verifyHosts(hosts); err != nil {
return err
Expand Down Expand Up @@ -75,6 +83,14 @@ func (h *Hosts) Add(ip string, hosts []string) error {
return h.File.Flush()
}

func Remove(hosts []string) error {
h, err := New()
if err != nil {
return err
}
return h.Remove(hosts)
}

func (h *Hosts) Remove(hosts []string) error {
if err := h.verifyHosts(hosts); err != nil {
return err
Expand Down Expand Up @@ -102,6 +118,14 @@ func (h *Hosts) Remove(hosts []string) error {
return h.File.Flush()
}

func Clean(rawSuffixes []string) error {
h, err := New()
if err != nil {
return err
}
return h.Clean(rawSuffixes)
}

func (h *Hosts) Clean(rawSuffixes []string) error {
if err := h.checkIsWritable(); err != nil {
return err
Expand Down

0 comments on commit 589c0d7

Please sign in to comment.