mirror of
https://github.com/juanfont/headscale.git
synced 2024-11-23 18:15:26 +00:00
Remove CLI and tests for Shared node
This commit is contained in:
parent
69cdfbb56f
commit
9ceac5c0fc
@ -46,30 +46,6 @@ func init() {
|
|||||||
log.Fatalf(err.Error())
|
log.Fatalf(err.Error())
|
||||||
}
|
}
|
||||||
nodeCmd.AddCommand(deleteNodeCmd)
|
nodeCmd.AddCommand(deleteNodeCmd)
|
||||||
|
|
||||||
shareMachineCmd.Flags().StringP("namespace", "n", "", "Namespace")
|
|
||||||
err = shareMachineCmd.MarkFlagRequired("namespace")
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf(err.Error())
|
|
||||||
}
|
|
||||||
shareMachineCmd.Flags().Uint64P("identifier", "i", 0, "Node identifier (ID)")
|
|
||||||
err = shareMachineCmd.MarkFlagRequired("identifier")
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf(err.Error())
|
|
||||||
}
|
|
||||||
nodeCmd.AddCommand(shareMachineCmd)
|
|
||||||
|
|
||||||
unshareMachineCmd.Flags().StringP("namespace", "n", "", "Namespace")
|
|
||||||
err = unshareMachineCmd.MarkFlagRequired("namespace")
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf(err.Error())
|
|
||||||
}
|
|
||||||
unshareMachineCmd.Flags().Uint64P("identifier", "i", 0, "Node identifier (ID)")
|
|
||||||
err = unshareMachineCmd.MarkFlagRequired("identifier")
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf(err.Error())
|
|
||||||
}
|
|
||||||
nodeCmd.AddCommand(unshareMachineCmd)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var nodeCmd = &cobra.Command{
|
var nodeCmd = &cobra.Command{
|
||||||
@ -317,139 +293,6 @@ var deleteNodeCmd = &cobra.Command{
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func sharingWorker(
|
|
||||||
cmd *cobra.Command,
|
|
||||||
) (string, *v1.Machine, *v1.Namespace, error) {
|
|
||||||
output, _ := cmd.Flags().GetString("output")
|
|
||||||
namespaceStr, err := cmd.Flags().GetString("namespace")
|
|
||||||
if err != nil {
|
|
||||||
ErrorOutput(err, fmt.Sprintf("Error getting namespace: %s", err), output)
|
|
||||||
|
|
||||||
return "", nil, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx, client, conn, cancel := getHeadscaleCLIClient()
|
|
||||||
defer cancel()
|
|
||||||
defer conn.Close()
|
|
||||||
|
|
||||||
identifier, err := cmd.Flags().GetUint64("identifier")
|
|
||||||
if err != nil {
|
|
||||||
ErrorOutput(err, fmt.Sprintf("Error converting ID to integer: %s", err), output)
|
|
||||||
|
|
||||||
return "", nil, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
machineRequest := &v1.GetMachineRequest{
|
|
||||||
MachineId: identifier,
|
|
||||||
}
|
|
||||||
|
|
||||||
machineResponse, err := client.GetMachine(ctx, machineRequest)
|
|
||||||
if err != nil {
|
|
||||||
ErrorOutput(
|
|
||||||
err,
|
|
||||||
fmt.Sprintf("Error getting node node: %s", status.Convert(err).Message()),
|
|
||||||
output,
|
|
||||||
)
|
|
||||||
|
|
||||||
return "", nil, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
namespaceRequest := &v1.GetNamespaceRequest{
|
|
||||||
Name: namespaceStr,
|
|
||||||
}
|
|
||||||
|
|
||||||
namespaceResponse, err := client.GetNamespace(ctx, namespaceRequest)
|
|
||||||
if err != nil {
|
|
||||||
ErrorOutput(
|
|
||||||
err,
|
|
||||||
fmt.Sprintf("Error getting node node: %s", status.Convert(err).Message()),
|
|
||||||
output,
|
|
||||||
)
|
|
||||||
|
|
||||||
return "", nil, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return output, machineResponse.GetMachine(), namespaceResponse.GetNamespace(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
var shareMachineCmd = &cobra.Command{
|
|
||||||
Use: "share",
|
|
||||||
Short: "Shares a node from the current namespace to the specified one",
|
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
|
||||||
output, machine, namespace, err := sharingWorker(cmd)
|
|
||||||
if err != nil {
|
|
||||||
ErrorOutput(
|
|
||||||
err,
|
|
||||||
fmt.Sprintf("Failed to fetch namespace or machine: %s", err),
|
|
||||||
output,
|
|
||||||
)
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx, client, conn, cancel := getHeadscaleCLIClient()
|
|
||||||
defer cancel()
|
|
||||||
defer conn.Close()
|
|
||||||
|
|
||||||
request := &v1.ShareMachineRequest{
|
|
||||||
MachineId: machine.Id,
|
|
||||||
Namespace: namespace.Name,
|
|
||||||
}
|
|
||||||
|
|
||||||
response, err := client.ShareMachine(ctx, request)
|
|
||||||
if err != nil {
|
|
||||||
ErrorOutput(
|
|
||||||
err,
|
|
||||||
fmt.Sprintf("Error sharing node: %s", status.Convert(err).Message()),
|
|
||||||
output,
|
|
||||||
)
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
SuccessOutput(response.Machine, "Node shared", output)
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
var unshareMachineCmd = &cobra.Command{
|
|
||||||
Use: "unshare",
|
|
||||||
Short: "Unshares a node from the specified namespace",
|
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
|
||||||
output, machine, namespace, err := sharingWorker(cmd)
|
|
||||||
if err != nil {
|
|
||||||
ErrorOutput(
|
|
||||||
err,
|
|
||||||
fmt.Sprintf("Failed to fetch namespace or machine: %s", err),
|
|
||||||
output,
|
|
||||||
)
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx, client, conn, cancel := getHeadscaleCLIClient()
|
|
||||||
defer cancel()
|
|
||||||
defer conn.Close()
|
|
||||||
|
|
||||||
request := &v1.UnshareMachineRequest{
|
|
||||||
MachineId: machine.Id,
|
|
||||||
Namespace: namespace.Name,
|
|
||||||
}
|
|
||||||
|
|
||||||
response, err := client.UnshareMachine(ctx, request)
|
|
||||||
if err != nil {
|
|
||||||
ErrorOutput(
|
|
||||||
err,
|
|
||||||
fmt.Sprintf("Error unsharing node: %s", status.Convert(err).Message()),
|
|
||||||
output,
|
|
||||||
)
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
SuccessOutput(response.Machine, "Node unshared", output)
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
func nodesToPtables(
|
func nodesToPtables(
|
||||||
currentNamespace string,
|
currentNamespace string,
|
||||||
machines []*v1.Machine,
|
machines []*v1.Machine,
|
||||||
|
@ -529,7 +529,7 @@ func (s *IntegrationCLITestSuite) TestNodeCommand() {
|
|||||||
namespace, err := s.createNamespace("machine-namespace")
|
namespace, err := s.createNamespace("machine-namespace")
|
||||||
assert.Nil(s.T(), err)
|
assert.Nil(s.T(), err)
|
||||||
|
|
||||||
sharedNamespace, err := s.createNamespace("shared-namespace")
|
secondNamespace, err := s.createNamespace("other-namespace")
|
||||||
assert.Nil(s.T(), err)
|
assert.Nil(s.T(), err)
|
||||||
|
|
||||||
// Randomly generated machine keys
|
// Randomly generated machine keys
|
||||||
@ -589,7 +589,7 @@ func (s *IntegrationCLITestSuite) TestNodeCommand() {
|
|||||||
|
|
||||||
assert.Len(s.T(), machines, len(machineKeys))
|
assert.Len(s.T(), machines, len(machineKeys))
|
||||||
|
|
||||||
// Test list all nodes after added shared
|
// Test list all nodes after added seconds
|
||||||
listAllResult, err := ExecuteCommand(
|
listAllResult, err := ExecuteCommand(
|
||||||
&s.headscale,
|
&s.headscale,
|
||||||
[]string{
|
[]string{
|
||||||
@ -627,14 +627,14 @@ func (s *IntegrationCLITestSuite) TestNodeCommand() {
|
|||||||
assert.True(s.T(), listAll[3].Registered)
|
assert.True(s.T(), listAll[3].Registered)
|
||||||
assert.True(s.T(), listAll[4].Registered)
|
assert.True(s.T(), listAll[4].Registered)
|
||||||
|
|
||||||
sharedMachineKeys := []string{
|
otherNamespaceMachineKeys := []string{
|
||||||
"b5b444774186d4217adcec407563a1223929465ee2c68a4da13af0d0185b4f8e",
|
"b5b444774186d4217adcec407563a1223929465ee2c68a4da13af0d0185b4f8e",
|
||||||
"dc721977ac7415aafa87f7d4574cbe07c6b171834a6d37375782bdc1fb6b3584",
|
"dc721977ac7415aafa87f7d4574cbe07c6b171834a6d37375782bdc1fb6b3584",
|
||||||
}
|
}
|
||||||
sharedMachines := make([]*v1.Machine, len(sharedMachineKeys))
|
otherNamespaceMachines := make([]*v1.Machine, len(otherNamespaceMachineKeys))
|
||||||
assert.Nil(s.T(), err)
|
assert.Nil(s.T(), err)
|
||||||
|
|
||||||
for index, machineKey := range sharedMachineKeys {
|
for index, machineKey := range otherNamespaceMachineKeys {
|
||||||
_, err := ExecuteCommand(
|
_, err := ExecuteCommand(
|
||||||
&s.headscale,
|
&s.headscale,
|
||||||
[]string{
|
[]string{
|
||||||
@ -642,9 +642,9 @@ func (s *IntegrationCLITestSuite) TestNodeCommand() {
|
|||||||
"debug",
|
"debug",
|
||||||
"create-node",
|
"create-node",
|
||||||
"--name",
|
"--name",
|
||||||
fmt.Sprintf("shared-machine-%d", index+1),
|
fmt.Sprintf("otherNamespace-machine-%d", index+1),
|
||||||
"--namespace",
|
"--namespace",
|
||||||
sharedNamespace.Name,
|
secondNamespace.Name,
|
||||||
"--key",
|
"--key",
|
||||||
machineKey,
|
machineKey,
|
||||||
"--output",
|
"--output",
|
||||||
@ -660,7 +660,7 @@ func (s *IntegrationCLITestSuite) TestNodeCommand() {
|
|||||||
"headscale",
|
"headscale",
|
||||||
"nodes",
|
"nodes",
|
||||||
"--namespace",
|
"--namespace",
|
||||||
sharedNamespace.Name,
|
secondNamespace.Name,
|
||||||
"register",
|
"register",
|
||||||
"--key",
|
"--key",
|
||||||
machineKey,
|
machineKey,
|
||||||
@ -675,13 +675,13 @@ func (s *IntegrationCLITestSuite) TestNodeCommand() {
|
|||||||
err = json.Unmarshal([]byte(machineResult), &machine)
|
err = json.Unmarshal([]byte(machineResult), &machine)
|
||||||
assert.Nil(s.T(), err)
|
assert.Nil(s.T(), err)
|
||||||
|
|
||||||
sharedMachines[index] = &machine
|
otherNamespaceMachines[index] = &machine
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.Len(s.T(), sharedMachines, len(sharedMachineKeys))
|
assert.Len(s.T(), otherNamespaceMachines, len(otherNamespaceMachineKeys))
|
||||||
|
|
||||||
// Test list all nodes after added shared
|
// Test list all nodes after added otherNamespace
|
||||||
listAllWithSharedResult, err := ExecuteCommand(
|
listAllWithotherNamespaceResult, err := ExecuteCommand(
|
||||||
&s.headscale,
|
&s.headscale,
|
||||||
[]string{
|
[]string{
|
||||||
"headscale",
|
"headscale",
|
||||||
@ -694,31 +694,34 @@ func (s *IntegrationCLITestSuite) TestNodeCommand() {
|
|||||||
)
|
)
|
||||||
assert.Nil(s.T(), err)
|
assert.Nil(s.T(), err)
|
||||||
|
|
||||||
var listAllWithShared []v1.Machine
|
var listAllWithotherNamespace []v1.Machine
|
||||||
err = json.Unmarshal([]byte(listAllWithSharedResult), &listAllWithShared)
|
err = json.Unmarshal(
|
||||||
|
[]byte(listAllWithotherNamespaceResult),
|
||||||
|
&listAllWithotherNamespace,
|
||||||
|
)
|
||||||
assert.Nil(s.T(), err)
|
assert.Nil(s.T(), err)
|
||||||
|
|
||||||
// All nodes, machines + shared
|
// All nodes, machines + otherNamespace
|
||||||
assert.Len(s.T(), listAllWithShared, 7)
|
assert.Len(s.T(), listAllWithotherNamespace, 7)
|
||||||
|
|
||||||
assert.Equal(s.T(), uint64(6), listAllWithShared[5].Id)
|
assert.Equal(s.T(), uint64(6), listAllWithotherNamespace[5].Id)
|
||||||
assert.Equal(s.T(), uint64(7), listAllWithShared[6].Id)
|
assert.Equal(s.T(), uint64(7), listAllWithotherNamespace[6].Id)
|
||||||
|
|
||||||
assert.Equal(s.T(), "shared-machine-1", listAllWithShared[5].Name)
|
assert.Equal(s.T(), "otherNamespace-machine-1", listAllWithotherNamespace[5].Name)
|
||||||
assert.Equal(s.T(), "shared-machine-2", listAllWithShared[6].Name)
|
assert.Equal(s.T(), "otherNamespace-machine-2", listAllWithotherNamespace[6].Name)
|
||||||
|
|
||||||
assert.True(s.T(), listAllWithShared[5].Registered)
|
assert.True(s.T(), listAllWithotherNamespace[5].Registered)
|
||||||
assert.True(s.T(), listAllWithShared[6].Registered)
|
assert.True(s.T(), listAllWithotherNamespace[6].Registered)
|
||||||
|
|
||||||
// Test list all nodes after added shared
|
// Test list all nodes after added otherNamespace
|
||||||
listOnlySharedMachineNamespaceResult, err := ExecuteCommand(
|
listOnlyotherNamespaceMachineNamespaceResult, err := ExecuteCommand(
|
||||||
&s.headscale,
|
&s.headscale,
|
||||||
[]string{
|
[]string{
|
||||||
"headscale",
|
"headscale",
|
||||||
"nodes",
|
"nodes",
|
||||||
"list",
|
"list",
|
||||||
"--namespace",
|
"--namespace",
|
||||||
sharedNamespace.Name,
|
secondNamespace.Name,
|
||||||
"--output",
|
"--output",
|
||||||
"json",
|
"json",
|
||||||
},
|
},
|
||||||
@ -726,23 +729,31 @@ func (s *IntegrationCLITestSuite) TestNodeCommand() {
|
|||||||
)
|
)
|
||||||
assert.Nil(s.T(), err)
|
assert.Nil(s.T(), err)
|
||||||
|
|
||||||
var listOnlySharedMachineNamespace []v1.Machine
|
var listOnlyotherNamespaceMachineNamespace []v1.Machine
|
||||||
err = json.Unmarshal(
|
err = json.Unmarshal(
|
||||||
[]byte(listOnlySharedMachineNamespaceResult),
|
[]byte(listOnlyotherNamespaceMachineNamespaceResult),
|
||||||
&listOnlySharedMachineNamespace,
|
&listOnlyotherNamespaceMachineNamespace,
|
||||||
)
|
)
|
||||||
assert.Nil(s.T(), err)
|
assert.Nil(s.T(), err)
|
||||||
|
|
||||||
assert.Len(s.T(), listOnlySharedMachineNamespace, 2)
|
assert.Len(s.T(), listOnlyotherNamespaceMachineNamespace, 2)
|
||||||
|
|
||||||
assert.Equal(s.T(), uint64(6), listOnlySharedMachineNamespace[0].Id)
|
assert.Equal(s.T(), uint64(6), listOnlyotherNamespaceMachineNamespace[0].Id)
|
||||||
assert.Equal(s.T(), uint64(7), listOnlySharedMachineNamespace[1].Id)
|
assert.Equal(s.T(), uint64(7), listOnlyotherNamespaceMachineNamespace[1].Id)
|
||||||
|
|
||||||
assert.Equal(s.T(), "shared-machine-1", listOnlySharedMachineNamespace[0].Name)
|
assert.Equal(
|
||||||
assert.Equal(s.T(), "shared-machine-2", listOnlySharedMachineNamespace[1].Name)
|
s.T(),
|
||||||
|
"otherNamespace-machine-1",
|
||||||
|
listOnlyotherNamespaceMachineNamespace[0].Name,
|
||||||
|
)
|
||||||
|
assert.Equal(
|
||||||
|
s.T(),
|
||||||
|
"otherNamespace-machine-2",
|
||||||
|
listOnlyotherNamespaceMachineNamespace[1].Name,
|
||||||
|
)
|
||||||
|
|
||||||
assert.True(s.T(), listOnlySharedMachineNamespace[0].Registered)
|
assert.True(s.T(), listOnlyotherNamespaceMachineNamespace[0].Registered)
|
||||||
assert.True(s.T(), listOnlySharedMachineNamespace[1].Registered)
|
assert.True(s.T(), listOnlyotherNamespaceMachineNamespace[1].Registered)
|
||||||
|
|
||||||
// Delete a machines
|
// Delete a machines
|
||||||
_, err = ExecuteCommand(
|
_, err = ExecuteCommand(
|
||||||
@ -786,120 +797,6 @@ func (s *IntegrationCLITestSuite) TestNodeCommand() {
|
|||||||
assert.Nil(s.T(), err)
|
assert.Nil(s.T(), err)
|
||||||
|
|
||||||
assert.Len(s.T(), listOnlyMachineNamespaceAfterDelete, 4)
|
assert.Len(s.T(), listOnlyMachineNamespaceAfterDelete, 4)
|
||||||
|
|
||||||
// test: share node
|
|
||||||
|
|
||||||
shareMachineResult, err := ExecuteCommand(
|
|
||||||
&s.headscale,
|
|
||||||
[]string{
|
|
||||||
"headscale",
|
|
||||||
"nodes",
|
|
||||||
"share",
|
|
||||||
"--namespace",
|
|
||||||
namespace.Name,
|
|
||||||
"--identifier",
|
|
||||||
"7",
|
|
||||||
"--output",
|
|
||||||
"json",
|
|
||||||
},
|
|
||||||
[]string{},
|
|
||||||
)
|
|
||||||
assert.Nil(s.T(), err)
|
|
||||||
|
|
||||||
var shareMachine v1.Machine
|
|
||||||
err = json.Unmarshal([]byte(shareMachineResult), &shareMachine)
|
|
||||||
assert.Nil(s.T(), err)
|
|
||||||
|
|
||||||
assert.Equal(s.T(), uint64(7), shareMachine.Id)
|
|
||||||
|
|
||||||
assert.Equal(s.T(), "shared-machine-2", shareMachine.Name)
|
|
||||||
|
|
||||||
assert.True(s.T(), shareMachine.Registered)
|
|
||||||
|
|
||||||
// Test: list main namespace after machine has been shared
|
|
||||||
listOnlyMachineNamespaceAfterShareResult, err := ExecuteCommand(
|
|
||||||
&s.headscale,
|
|
||||||
[]string{
|
|
||||||
"headscale",
|
|
||||||
"nodes",
|
|
||||||
"list",
|
|
||||||
"--namespace",
|
|
||||||
namespace.Name,
|
|
||||||
"--output",
|
|
||||||
"json",
|
|
||||||
},
|
|
||||||
[]string{},
|
|
||||||
)
|
|
||||||
assert.Nil(s.T(), err)
|
|
||||||
|
|
||||||
var listOnlyMachineNamespaceAfterShare []v1.Machine
|
|
||||||
err = json.Unmarshal(
|
|
||||||
[]byte(listOnlyMachineNamespaceAfterShareResult),
|
|
||||||
&listOnlyMachineNamespaceAfterShare,
|
|
||||||
)
|
|
||||||
assert.Nil(s.T(), err)
|
|
||||||
|
|
||||||
assert.Len(s.T(), listOnlyMachineNamespaceAfterShare, 5)
|
|
||||||
|
|
||||||
assert.Equal(s.T(), uint64(7), listOnlyMachineNamespaceAfterShare[4].Id)
|
|
||||||
|
|
||||||
assert.Equal(s.T(), "shared-machine-2", listOnlyMachineNamespaceAfterShare[4].Name)
|
|
||||||
|
|
||||||
assert.True(s.T(), listOnlyMachineNamespaceAfterShare[4].Registered)
|
|
||||||
|
|
||||||
// test: unshare node
|
|
||||||
|
|
||||||
unshareMachineResult, err := ExecuteCommand(
|
|
||||||
&s.headscale,
|
|
||||||
[]string{
|
|
||||||
"headscale",
|
|
||||||
"nodes",
|
|
||||||
"unshare",
|
|
||||||
"--namespace",
|
|
||||||
namespace.Name,
|
|
||||||
"--identifier",
|
|
||||||
"7",
|
|
||||||
"--output",
|
|
||||||
"json",
|
|
||||||
},
|
|
||||||
[]string{},
|
|
||||||
)
|
|
||||||
assert.Nil(s.T(), err)
|
|
||||||
|
|
||||||
var unshareMachine v1.Machine
|
|
||||||
err = json.Unmarshal([]byte(unshareMachineResult), &unshareMachine)
|
|
||||||
assert.Nil(s.T(), err)
|
|
||||||
|
|
||||||
assert.Equal(s.T(), uint64(7), unshareMachine.Id)
|
|
||||||
|
|
||||||
assert.Equal(s.T(), "shared-machine-2", unshareMachine.Name)
|
|
||||||
|
|
||||||
assert.True(s.T(), unshareMachine.Registered)
|
|
||||||
|
|
||||||
// Test: list main namespace after machine has been shared
|
|
||||||
listOnlyMachineNamespaceAfterUnshareResult, err := ExecuteCommand(
|
|
||||||
&s.headscale,
|
|
||||||
[]string{
|
|
||||||
"headscale",
|
|
||||||
"nodes",
|
|
||||||
"list",
|
|
||||||
"--namespace",
|
|
||||||
namespace.Name,
|
|
||||||
"--output",
|
|
||||||
"json",
|
|
||||||
},
|
|
||||||
[]string{},
|
|
||||||
)
|
|
||||||
assert.Nil(s.T(), err)
|
|
||||||
|
|
||||||
var listOnlyMachineNamespaceAfterUnshare []v1.Machine
|
|
||||||
err = json.Unmarshal(
|
|
||||||
[]byte(listOnlyMachineNamespaceAfterUnshareResult),
|
|
||||||
&listOnlyMachineNamespaceAfterUnshare,
|
|
||||||
)
|
|
||||||
assert.Nil(s.T(), err)
|
|
||||||
|
|
||||||
assert.Len(s.T(), listOnlyMachineNamespaceAfterUnshare, 4)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *IntegrationCLITestSuite) TestNodeExpireCommand() {
|
func (s *IntegrationCLITestSuite) TestNodeExpireCommand() {
|
||||||
|
Loading…
Reference in New Issue
Block a user