Skip to content

Commit

Permalink
Merge pull request #569 from Kazauwa/362-add-move-command
Browse files Browse the repository at this point in the history
  • Loading branch information
kradalby authored May 2, 2022
2 parents 02a78e5 + b9ea83f commit ddb87af
Show file tree
Hide file tree
Showing 13 changed files with 827 additions and 153 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Fix labels cardinality error when registering unknown pre-auth key [#519](https://github.com/juanfont/headscale/pull/519)
- Fix send on closed channel crash in polling [#542](https://github.com/juanfont/headscale/pull/542)
- Fixed spurious calls to setLastStateChangeToNow from ephemeral nodes [#566](https://github.com/juanfont/headscale/pull/566)
- Add command for moving nodes between namespaces [#362](https://github.com/juanfont/headscale/issues/362)

## 0.15.0 (2022-03-20)

Expand Down
89 changes: 89 additions & 0 deletions cmd/headscale/cli/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,21 @@ func init() {
log.Fatalf(err.Error())
}
nodeCmd.AddCommand(deleteNodeCmd)

moveNodeCmd.Flags().Uint64P("identifier", "i", 0, "Node identifier (ID)")

err = moveNodeCmd.MarkFlagRequired("identifier")
if err != nil {
log.Fatalf(err.Error())
}

moveNodeCmd.Flags().StringP("namespace", "n", "", "New namespace")

err = moveNodeCmd.MarkFlagRequired("namespace")
if err != nil {
log.Fatalf(err.Error())
}
nodeCmd.AddCommand(moveNodeCmd)
}

var nodeCmd = &cobra.Command{
Expand Down Expand Up @@ -296,6 +311,80 @@ var deleteNodeCmd = &cobra.Command{
},
}

var moveNodeCmd = &cobra.Command{
Use: "move",
Short: "Move node to another namespace",
Aliases: []string{"mv"},
Run: func(cmd *cobra.Command, args []string) {
output, _ := cmd.Flags().GetString("output")

identifier, err := cmd.Flags().GetUint64("identifier")
if err != nil {
ErrorOutput(
err,
fmt.Sprintf("Error converting ID to integer: %s", err),
output,
)

return
}

namespace, err := cmd.Flags().GetString("namespace")
if err != nil {
ErrorOutput(
err,
fmt.Sprintf("Error getting namespace: %s", err),
output,
)

return
}

ctx, client, conn, cancel := getHeadscaleCLIClient()
defer cancel()
defer conn.Close()

getRequest := &v1.GetMachineRequest{
MachineId: identifier,
}

_, err = client.GetMachine(ctx, getRequest)
if err != nil {
ErrorOutput(
err,
fmt.Sprintf(
"Error getting node: %s",
status.Convert(err).Message(),
),
output,
)

return
}

moveRequest := &v1.MoveMachineRequest{
MachineId: identifier,
Namespace: namespace,
}

moveResponse, err := client.MoveMachine(ctx, moveRequest)
if err != nil {
ErrorOutput(
err,
fmt.Sprintf(
"Error moving node: %s",
status.Convert(err).Message(),
),
output,
)

return
}

SuccessOutput(moveResponse.Machine, "Node moved to another namespace", output)
},
}

func nodesToPtables(
currentNamespace string,
machines []*v1.Machine,
Expand Down
202 changes: 107 additions & 95 deletions gen/go/headscale/v1/headscale.pb.go

Large diffs are not rendered by default.

117 changes: 117 additions & 0 deletions gen/go/headscale/v1/headscale.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 36 additions & 4 deletions gen/go/headscale/v1/headscale_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ddb87af

Please sign in to comment.