Skip to content

Commit

Permalink
add cli for backfilling ips
Browse files Browse the repository at this point in the history
Fixes juanfont#614

Signed-off-by: Kristoffer Dalby <[email protected]>
  • Loading branch information
kradalby committed Apr 16, 2024
1 parent 599d4ec commit f7b2540
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
53 changes: 53 additions & 0 deletions cmd/headscale/cli/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ func init() {
tagCmd.Flags().
StringSliceP("tags", "t", []string{}, "List of tags to add to the node")
nodeCmd.AddCommand(tagCmd)

nodeCmd.AddCommand(backfillNodeIPsCmd)
}

var nodeCmd = &cobra.Command{
Expand Down Expand Up @@ -477,6 +479,57 @@ var moveNodeCmd = &cobra.Command{
},
}

var backfillNodeIPsCmd = &cobra.Command{
Use: "backfillips",
Short: "Backfill IPs missing from nodes",
Long: `
Backfill IPs can be used to add/remove IPs from nodes
based on the current configuration of Headscale.
If there are nodes that does not have IPv4 or IPv6
even if prefixes for both are configured in the config,
this command can be used to assign IPs of the sort to
all nodes that are missing.
If you remove IPv4 or IPv6 prefixes from the config,
it can be run to remove the IPs that should no longer
be assigned to nodes.`,
Run: func(cmd *cobra.Command, args []string) {
var err error
output, _ := cmd.Flags().GetString("output")

confirm := false
prompt := &survey.Confirm{
Message: "Are you sure that you want to assign/remove IPs to/from nodes?",
}
err = survey.AskOne(prompt, &confirm)
if err != nil {
return
}
if confirm {
ctx, client, conn, cancel := getHeadscaleCLIClient()
defer cancel()
defer conn.Close()

changes, err := client.BackfillNodeIPs(ctx, &v1.BackfillNodeIPsRequest{Confirmed: confirm})
if err != nil {
ErrorOutput(
err,
fmt.Sprintf(
"Error backfilling IPs: %s",
status.Convert(err).Message(),
),
output,
)

return
}

SuccessOutput(changes, "Node IPs backfilled successfully", output)
}
},
}

func nodesToPtables(
currentUser string,
showTags bool,
Expand Down
19 changes: 19 additions & 0 deletions hscontrol/grpcv1.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package hscontrol

import (
"context"
"errors"
"fmt"
"sort"
"strings"
Expand Down Expand Up @@ -468,6 +469,24 @@ func (api headscaleV1APIServer) MoveNode(
return &v1.MoveNodeResponse{Node: node.Proto()}, nil
}

func (api headscaleV1APIServer) BackfillNodeIPs(
ctx context.Context,
request *v1.BackfillNodeIPsRequest,
) (*v1.BackfillNodeIPsResponse, error) {
log.Trace().Msg("Backfill called")

if !request.Confirmed {
return nil, errors.New("not confirmed, aborting")
}

changes, err := api.h.db.BackfillNodeIPs(api.h.ipAlloc)
if err != nil {
return nil, err
}

return &v1.BackfillNodeIPsResponse{Changes: changes}, nil
}

func (api headscaleV1APIServer) GetRoutes(
ctx context.Context,
request *v1.GetRoutesRequest,
Expand Down
7 changes: 7 additions & 0 deletions proto/headscale/v1/headscale.proto
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ service HeadscaleService {
post: "/api/v1/node/{node_id}/user"
};
}

rpc BackfillNodeIPs(BackfillNodeIPsRequest) returns (BackfillNodeIPsResponse) {
option (google.api.http) = {
post: "/api/v1/node/backfillips"
};
}

// --- Node end ---

// --- Route start ---
Expand Down
8 changes: 8 additions & 0 deletions proto/headscale/v1/node.proto
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,11 @@ message DebugCreateNodeRequest {
message DebugCreateNodeResponse {
Node node = 1;
}

message BackfillNodeIPsRequest {
bool confirmed = 1;
}

message BackfillNodeIPsResponse {
repeated string changes = 1;
}

0 comments on commit f7b2540

Please sign in to comment.