Skip to content

Commit

Permalink
Update to headscale v0.23.0
Browse files Browse the repository at this point in the history
  • Loading branch information
majst01 committed Sep 30, 2024
1 parent 65f0c68 commit 304ef21
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 334 deletions.
30 changes: 15 additions & 15 deletions cmd/metal-api/internal/headscale/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"time"

headscalev1 "github.com/juanfont/headscale/gen/go/headscale/v1"
"github.com/juanfont/headscale/hscontrol"
"github.com/juanfont/headscale/hscontrol/db"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
timestamppb "google.golang.org/protobuf/types/known/timestamppb"
Expand Down Expand Up @@ -90,7 +90,7 @@ func (h *HeadscaleClient) CreateUser(ctx context.Context, name string) error {
}
_, err := h.client.CreateUser(ctx, req)
// TODO: this error check is pretty rough, but it's not easily possible to compare the proto error directly :/
if err != nil && !strings.Contains(err.Error(), hscontrol.ErrUserExists.Error()) {
if err != nil && !strings.Contains(err.Error(), db.ErrUserExists.Error()) {
return fmt.Errorf("failed to create new VPN user: %w", err)
}

Expand All @@ -111,42 +111,42 @@ func (h *HeadscaleClient) CreatePreAuthKey(ctx context.Context, user string, exp
return resp.PreAuthKey.Key, nil
}

func (h *HeadscaleClient) MachinesConnected(ctx context.Context) ([]*headscalev1.Machine, error) {
resp, err := h.client.ListMachines(ctx, &headscalev1.ListMachinesRequest{})
func (h *HeadscaleClient) NodesConnected(ctx context.Context) ([]*headscalev1.Node, error) {
resp, err := h.client.ListNodes(ctx, &headscalev1.ListNodesRequest{})
if err != nil || resp == nil {
return nil, fmt.Errorf("failed to list machines: %w", err)
}

return resp.Machines, nil
return resp.Nodes, nil
}

// DeleteMachine removes the node entry from headscale DB
func (h *HeadscaleClient) DeleteMachine(ctx context.Context, machineID, projectID string) (err error) {
machine, err := h.getMachine(ctx, machineID, projectID)
// DeleteNode removes the node entry from headscale DB
func (h *HeadscaleClient) DeleteNode(ctx context.Context, machineID, projectID string) (err error) {
machine, err := h.getNode(ctx, machineID, projectID)
if err != nil || machine == nil {
return err
}

req := &headscalev1.DeleteMachineRequest{
MachineId: machine.Id,
req := &headscalev1.DeleteNodeRequest{
NodeId: machine.Id,
}
if _, err := h.client.DeleteMachine(ctx, req); err != nil {
if _, err := h.client.DeleteNode(ctx, req); err != nil {
return fmt.Errorf("failed to delete machine: %w", err)
}

return nil
}

func (h *HeadscaleClient) getMachine(ctx context.Context, machineID, projectID string) (machine *headscalev1.Machine, err error) {
req := &headscalev1.ListMachinesRequest{
func (h *HeadscaleClient) getNode(ctx context.Context, machineID, projectID string) (machine *headscalev1.Node, err error) {
req := &headscalev1.ListNodesRequest{
User: projectID,
}
resp, err := h.client.ListMachines(ctx, req)
resp, err := h.client.ListNodes(ctx, req)
if err != nil || resp == nil {
return nil, fmt.Errorf("failed to list machines: %w", err)
}

for _, m := range resp.Machines {
for _, m := range resp.Nodes {
if m.Name == machineID {
return m, nil
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/metal-api/internal/service/async-actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (a *asyncActor) freeMachine(ctx context.Context, pub bus.Publisher, m *meta

if headscaleClient != nil && m.Allocation != nil {
// always call DeleteMachine, in case machine is not registered it will return nil
if err := headscaleClient.DeleteMachine(ctx, m.ID, m.Allocation.Project); err != nil {
if err := headscaleClient.DeleteNode(ctx, m.ID, m.Allocation.Project); err != nil {
logger.Error("unable to delete Node entry from headscale DB", "machineID", m.ID, "error", err)
}
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/metal-api/internal/service/vpn-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (r *vpnResource) getVPNAuthKey(request *restful.Request, response *restful.
}

type headscaleMachineLister interface {
MachinesConnected(ctx context.Context) ([]*headscalev1.Machine, error)
NodesConnected(ctx context.Context) ([]*headscalev1.Node, error)
}

func EvaluateVPNConnected(log *slog.Logger, ds *datastore.RethinkStore, lister headscaleMachineLister) error {
Expand All @@ -119,7 +119,7 @@ func EvaluateVPNConnected(log *slog.Logger, ds *datastore.RethinkStore, lister h
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
defer cancel()

headscaleMachines, err := lister.MachinesConnected(ctx)
headscaleNodes, err := lister.NodesConnected(ctx)
if err != nil {
return err
}
Expand All @@ -131,7 +131,7 @@ func EvaluateVPNConnected(log *slog.Logger, ds *datastore.RethinkStore, lister h
continue
}

index := slices.IndexFunc(headscaleMachines, func(hm *headscalev1.Machine) bool {
index := slices.IndexFunc(headscaleNodes, func(hm *headscalev1.Node) bool {
if hm.Name != m.ID {
return false
}
Expand All @@ -147,7 +147,7 @@ func EvaluateVPNConnected(log *slog.Logger, ds *datastore.RethinkStore, lister h
continue
}

connected := headscaleMachines[index].Online
connected := headscaleNodes[index].Online

if m.Allocation.VPN.Connected == connected {
log.Info("not updating vpn because already up-to-date", "machine", m.ID, "connected", connected)
Expand Down
8 changes: 4 additions & 4 deletions cmd/metal-api/internal/service/vpn-service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func Test_EvaluateVPNConnected(t *testing.T) {
tests := []struct {
name string
mockFn func(mock *r.Mock)
headscaleMachines []*headscalev1.Machine
headscaleMachines []*headscalev1.Node
wantErr error
}{
{
Expand Down Expand Up @@ -60,7 +60,7 @@ func Test_EvaluateVPNConnected(t *testing.T) {
// unfortunately, it's too hard to check the replace exactly for specific fields...
mock.On(r.DB("mockdb").Table("machine").Get("toggle").Replace(r.MockAnything())).Return(testdata.EmptyResult, nil)
},
headscaleMachines: []*headscalev1.Machine{
headscaleMachines: []*headscalev1.Node{
{
Name: "toggle",
User: &headscalev1.User{
Expand Down Expand Up @@ -104,9 +104,9 @@ func Test_EvaluateVPNConnected(t *testing.T) {
}

type headscaleTest struct {
ms []*headscalev1.Machine
ms []*headscalev1.Node
}

func (h *headscaleTest) MachinesConnected(ctx context.Context) ([]*headscalev1.Machine, error) {
func (h *headscaleTest) NodesConnected(ctx context.Context) ([]*headscalev1.Node, error) {
return h.ms, nil
}
Loading

0 comments on commit 304ef21

Please sign in to comment.