From 2bc8051ae52f7c48bfcf35d893a2b5563e7ffb2c Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Sat, 12 Feb 2022 20:46:05 +0000 Subject: [PATCH 1/5] Remove kv-namespace-worker This commit removes the namespace kv worker and related code, now that we talk over gRPC to the server, and not directly to the DB, we should not need this anymore. --- app.go | 16 ---------------- machine_test.go | 13 ------------- namespaces.go | 38 -------------------------------------- 3 files changed, 67 deletions(-) diff --git a/app.go b/app.go index bc7491ce..22b4ceee 100644 --- a/app.go +++ b/app.go @@ -269,20 +269,6 @@ func (h *Headscale) expireEphemeralNodesWorker() { } } -// WatchForKVUpdates checks the KV DB table for requests to perform tailnet upgrades -// This is a way to communitate the CLI with the headscale server. -func (h *Headscale) watchForKVUpdates(milliSeconds int64) { - ticker := time.NewTicker(time.Duration(milliSeconds) * time.Millisecond) - for range ticker.C { - h.watchForKVUpdatesWorker() - } -} - -func (h *Headscale) watchForKVUpdatesWorker() { - h.checkForNamespacesPendingUpdates() - // more functions will come here in the future -} - func (h *Headscale) grpcAuthenticationInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, @@ -521,8 +507,6 @@ func (h *Headscale) Serve() error { go h.scheduledDERPMapUpdateWorker(derpMapCancelChannel) } - // I HATE THIS - go h.watchForKVUpdates(updateInterval) go h.expireEphemeralNodes(updateInterval) httpServer := &http.Server{ diff --git a/machine_test.go b/machine_test.go index 1cc4d40c..ff1dc914 100644 --- a/machine_test.go +++ b/machine_test.go @@ -1,7 +1,6 @@ package headscale import ( - "encoding/json" "strconv" "time" @@ -88,18 +87,6 @@ func (s *Suite) TestDeleteMachine(c *check.C) { err = app.DeleteMachine(&machine) c.Assert(err, check.IsNil) - namespacesPendingUpdates, err := app.getValue("namespaces_pending_updates") - c.Assert(err, check.IsNil) - - names := []string{} - err = json.Unmarshal([]byte(namespacesPendingUpdates), &names) - c.Assert(err, check.IsNil) - c.Assert(names, check.DeepEquals, []string{namespace.Name}) - - app.checkForNamespacesPendingUpdates() - - namespacesPendingUpdates, _ = app.getValue("namespaces_pending_updates") - c.Assert(namespacesPendingUpdates, check.Equals, "") _, err = app.GetMachine(namespace.Name, "testmachine") c.Assert(err, check.NotNil) } diff --git a/namespaces.go b/namespaces.go index e512068d..223455af 100644 --- a/namespaces.go +++ b/namespaces.go @@ -235,44 +235,6 @@ func (h *Headscale) RequestMapUpdates(namespaceID uint) error { return h.setValue("namespaces_pending_updates", string(data)) } -func (h *Headscale) checkForNamespacesPendingUpdates() { - namespacesPendingUpdates, err := h.getValue("namespaces_pending_updates") - if err != nil { - return - } - if namespacesPendingUpdates == "" { - return - } - - namespaces := []string{} - err = json.Unmarshal([]byte(namespacesPendingUpdates), &namespaces) - if err != nil { - return - } - for _, namespace := range namespaces { - log.Trace(). - Str("func", "RequestMapUpdates"). - Str("machine", namespace). - Msg("Sending updates to nodes in namespacespace") - h.setLastStateChangeToNow(namespace) - } - newPendingUpdateValue, err := h.getValue("namespaces_pending_updates") - if err != nil { - return - } - if namespacesPendingUpdates == newPendingUpdateValue { // only clear when no changes, so we notified everybody - err = h.setValue("namespaces_pending_updates", "") - if err != nil { - log.Error(). - Str("func", "checkForNamespacesPendingUpdates"). - Err(err). - Msg("Could not save to KV") - - return - } - } -} - func (n *Namespace) toUser() *tailcfg.User { user := tailcfg.User{ ID: tailcfg.UserID(n.ID), From 6fa0903a8e05a0d8d98068c4ca281fe5c4495c98 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Sat, 12 Feb 2022 20:50:17 +0000 Subject: [PATCH 2/5] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index aa06be0c..045a394f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ - `ip_prefix` is now superseded by `ip_prefixes` in the configuration [#208](https://github.com/juanfont/headscale/pull/208) - Upgrade `tailscale` (1.20.4) and other dependencies to latest [#314](https://github.com/juanfont/headscale/pull/314) - fix swapped machine<->namespace labels in `/metrics` [#312](https://github.com/juanfont/headscale/pull/312) +- remove key-value based update mechanism for namespace changes [#316](https://github.com/juanfont/headscale/pull/316) **0.12.4 (2022-01-29):** From bb80b679bc561681ed68880d383c5660752deb6f Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Sat, 12 Feb 2022 21:04:00 +0000 Subject: [PATCH 3/5] Remove RequestMapUpdates function --- machine.go | 15 +++++--------- namespaces.go | 55 --------------------------------------------------- routes.go | 5 ----- sharing.go | 5 ----- 4 files changed, 5 insertions(+), 75 deletions(-) diff --git a/machine.go b/machine.go index 64fd11b8..2b462985 100644 --- a/machine.go +++ b/machine.go @@ -353,13 +353,12 @@ func (h *Headscale) DeleteMachine(machine *Machine) error { } machine.Registered = false - namespaceID := machine.NamespaceID h.db.Save(&machine) // we mark it as unregistered, just in case if err := h.db.Delete(&machine).Error; err != nil { return err } - return h.RequestMapUpdates(namespaceID) + return nil } func (h *Headscale) TouchMachine(machine *Machine) error { @@ -377,12 +376,11 @@ func (h *Headscale) HardDeleteMachine(machine *Machine) error { return err } - namespaceID := machine.NamespaceID if err := h.db.Unscoped().Delete(&machine).Error; err != nil { return err } - return h.RequestMapUpdates(namespaceID) + return nil } // GetHostInfo returns a Hostinfo struct for the machine. @@ -530,7 +528,9 @@ func (machine Machine) toNode( addrs = append(addrs, ip) } - allowedIPs := append([]netaddr.IPPrefix{}, addrs...) // we append the node own IP, as it is required by the clients + allowedIPs := append( + []netaddr.IPPrefix{}, + addrs...) // we append the node own IP, as it is required by the clients if includeRoutes { routesStr := []string{} @@ -862,11 +862,6 @@ func (h *Headscale) EnableRoutes(machine *Machine, routeStrs ...string) error { machine.EnabledRoutes = datatypes.JSON(routes) h.db.Save(&machine) - err = h.RequestMapUpdates(machine.NamespaceID) - if err != nil { - return err - } - return nil } diff --git a/namespaces.go b/namespaces.go index 223455af..bdd440cf 100644 --- a/namespaces.go +++ b/namespaces.go @@ -1,9 +1,7 @@ package headscale import ( - "encoding/json" "errors" - "fmt" "strconv" "time" @@ -104,11 +102,6 @@ func (h *Headscale) RenameNamespace(oldName, newName string) error { return result.Error } - err = h.RequestMapUpdates(oldNamespace.ID) - if err != nil { - return err - } - return nil } @@ -187,54 +180,6 @@ func (h *Headscale) SetMachineNamespace(machine *Machine, namespaceName string) return nil } -// TODO(kradalby): Remove the need for this. -// RequestMapUpdates signals the KV worker to update the maps for this namespace. -func (h *Headscale) RequestMapUpdates(namespaceID uint) error { - namespace := Namespace{} - if err := h.db.First(&namespace, namespaceID).Error; err != nil { - return err - } - - namespacesPendingUpdates, err := h.getValue("namespaces_pending_updates") - if err != nil || namespacesPendingUpdates == "" { - err = h.setValue( - "namespaces_pending_updates", - fmt.Sprintf(`["%s"]`, namespace.Name), - ) - if err != nil { - return err - } - - return nil - } - names := []string{} - err = json.Unmarshal([]byte(namespacesPendingUpdates), &names) - if err != nil { - err = h.setValue( - "namespaces_pending_updates", - fmt.Sprintf(`["%s"]`, namespace.Name), - ) - if err != nil { - return err - } - - return nil - } - - names = append(names, namespace.Name) - data, err := json.Marshal(names) - if err != nil { - log.Error(). - Str("func", "RequestMapUpdates"). - Err(err). - Msg("Could not marshal namespaces_pending_updates") - - return err - } - - return h.setValue("namespaces_pending_updates", string(data)) -} - func (n *Namespace) toUser() *tailcfg.User { user := tailcfg.User{ ID: tailcfg.UserID(n.ID), diff --git a/routes.go b/routes.go index 448095a5..0065a03f 100644 --- a/routes.go +++ b/routes.go @@ -143,10 +143,5 @@ func (h *Headscale) EnableNodeRoute( machine.EnabledRoutes = datatypes.JSON(routes) h.db.Save(&machine) - err = h.RequestMapUpdates(machine.NamespaceID) - if err != nil { - return err - } - return nil } diff --git a/sharing.go b/sharing.go index be1689d5..caac5319 100644 --- a/sharing.go +++ b/sharing.go @@ -67,11 +67,6 @@ func (h *Headscale) RemoveSharedMachineFromNamespace( return errMachineNotShared } - err := h.RequestMapUpdates(namespace.ID) - if err != nil { - return err - } - return nil } From b2889bc355140913ca3c80159b232030442ea4a0 Mon Sep 17 00:00:00 2001 From: ohdearaugustin Date: Mon, 14 Feb 2022 21:24:51 +0100 Subject: [PATCH 4/5] github/workflows: set specific go version --- .github/workflows/build.yml | 2 +- .github/workflows/release.yml | 2 +- .github/workflows/test-integration.yml | 2 +- .github/workflows/test.yml | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index d92e9e6e..37423c38 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -31,7 +31,7 @@ jobs: if: steps.changed-files.outputs.any_changed == 'true' uses: actions/setup-go@v2 with: - go-version: "1.17" + go-version: "1.17.7" - name: Install dependencies if: steps.changed-files.outputs.any_changed == 'true' diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index c9a40bc0..8999f7bf 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -18,7 +18,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v2 with: - go-version: 1.17 + go-version: 1.17.7 - name: Install dependencies run: | diff --git a/.github/workflows/test-integration.yml b/.github/workflows/test-integration.yml index 9f526f97..d9c52c7b 100644 --- a/.github/workflows/test-integration.yml +++ b/.github/workflows/test-integration.yml @@ -25,7 +25,7 @@ jobs: if: steps.changed-files.outputs.any_changed == 'true' uses: actions/setup-go@v2 with: - go-version: "1.17" + go-version: "1.17.7" - name: Run Integration tests if: steps.changed-files.outputs.any_changed == 'true' diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 9ce8a779..663220be 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -25,7 +25,7 @@ jobs: if: steps.changed-files.outputs.any_changed == 'true' uses: actions/setup-go@v2 with: - go-version: "1.17" + go-version: "1.17.7" - name: Install dependencies if: steps.changed-files.outputs.any_changed == 'true' From 0b9dd19ec7fc8d4a7b5bfb8c684c1ed266eb185a Mon Sep 17 00:00:00 2001 From: ohdearaugustin Date: Mon, 14 Feb 2022 21:25:11 +0100 Subject: [PATCH 5/5] Dockerfiles: update go version to 1.17.7 --- Dockerfile | 2 +- Dockerfile.alpine | 2 +- Dockerfile.debug | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index 050439b5..6e577753 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ # Builder image -FROM docker.io/golang:1.17.1-bullseye AS build +FROM docker.io/golang:1.17.7-bullseye AS build ENV GOPATH /go WORKDIR /go/src/headscale diff --git a/Dockerfile.alpine b/Dockerfile.alpine index a75bbfea..b07e1904 100644 --- a/Dockerfile.alpine +++ b/Dockerfile.alpine @@ -1,5 +1,5 @@ # Builder image -FROM docker.io/golang:1.17.1-alpine AS build +FROM docker.io/golang:1.17.7-alpine AS build ENV GOPATH /go WORKDIR /go/src/headscale diff --git a/Dockerfile.debug b/Dockerfile.debug index b81ee67e..3d2675f9 100644 --- a/Dockerfile.debug +++ b/Dockerfile.debug @@ -1,5 +1,5 @@ # Builder image -FROM docker.io/golang:1.17.1-bullseye AS build +FROM docker.io/golang:1.17.7-bullseye AS build ENV GOPATH /go WORKDIR /go/src/headscale