Skip to content

Commit

Permalink
Resolve merge
Browse files Browse the repository at this point in the history
  • Loading branch information
kradalby committed Feb 25, 2022
2 parents 6d699d3 + 3815986 commit 2fd36dd
Show file tree
Hide file tree
Showing 23 changed files with 284 additions and 1,974 deletions.
157 changes: 0 additions & 157 deletions cmd/headscale/cli/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,30 +46,6 @@ func init() {
log.Fatalf(err.Error())
}
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{
Expand Down Expand Up @@ -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(
currentNamespace string,
machines []*v1.Machine,
Expand Down
5 changes: 1 addition & 4 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ func (h *Headscale) initDB() error {
return err
}

err = db.AutoMigrate(&SharedMachine{})
if err != nil {
return err
}
_ = db.Migrator().DropTable("shared_machines")

err = db.AutoMigrate(&APIKey{})
if err != nil {
Expand Down
34 changes: 14 additions & 20 deletions dns_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package headscale

import (
"fmt"

"gopkg.in/check.v1"
"inet.af/netaddr"
"tailscale.com/tailcfg"
Expand Down Expand Up @@ -225,9 +223,6 @@ func (s *Suite) TestDNSConfigMapResponseWithMagicDNS(c *check.C) {
}
app.db.Save(machine2InShared1)

err = app.AddSharedMachineToNamespace(machineInShared2, namespaceShared1)
c.Assert(err, check.IsNil)

baseDomain := "foobar.headscale.net"
dnsConfigOrig := tailcfg.DNSConfig{
Routes: make(map[string][]dnstype.Resolver),
Expand All @@ -245,19 +240,21 @@ func (s *Suite) TestDNSConfigMapResponseWithMagicDNS(c *check.C) {
peersOfMachineInShared1,
)
c.Assert(dnsConfig, check.NotNil)
c.Assert(len(dnsConfig.Routes), check.Equals, 2)

domainRouteShared1 := fmt.Sprintf("%s.%s", namespaceShared1.Name, baseDomain)
_, ok := dnsConfig.Routes[domainRouteShared1]
c.Assert(ok, check.Equals, true)

domainRouteShared2 := fmt.Sprintf("%s.%s", namespaceShared2.Name, baseDomain)
_, ok = dnsConfig.Routes[domainRouteShared2]
c.Assert(ok, check.Equals, true)

domainRouteShared3 := fmt.Sprintf("%s.%s", namespaceShared3.Name, baseDomain)
_, ok = dnsConfig.Routes[domainRouteShared3]
c.Assert(ok, check.Equals, false)
// TODO: Remove comment out when we have all nodes available to every node
// c.Assert(len(dnsConfig.Routes), check.Equals, 2)

// domainRouteShared1 := fmt.Sprintf("%s.%s", namespaceShared1.Name, baseDomain)
// _, ok := dnsConfig.Routes[domainRouteShared1]
// c.Assert(ok, check.Equals, true)
//
// domainRouteShared2 := fmt.Sprintf("%s.%s", namespaceShared2.Name, baseDomain)
// _, ok = dnsConfig.Routes[domainRouteShared2]
// c.Assert(ok, check.Equals, true)
//
// domainRouteShared3 := fmt.Sprintf("%s.%s", namespaceShared3.Name, baseDomain)
// _, ok = dnsConfig.Routes[domainRouteShared3]
// c.Assert(ok, check.Equals, false)
}

func (s *Suite) TestDNSConfigMapResponseWithoutMagicDNS(c *check.C) {
Expand Down Expand Up @@ -374,9 +371,6 @@ func (s *Suite) TestDNSConfigMapResponseWithoutMagicDNS(c *check.C) {
}
app.db.Save(machine2InShared1)

err = app.AddSharedMachineToNamespace(machineInShared2, namespaceShared1)
c.Assert(err, check.IsNil)

baseDomain := "foobar.headscale.net"
dnsConfigOrig := tailcfg.DNSConfig{
Routes: make(map[string][]dnstype.Resolver),
Expand Down
5 changes: 2 additions & 3 deletions gen/go/headscale/v1/apikey.pb.go

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

5 changes: 2 additions & 3 deletions gen/go/headscale/v1/device.pb.go

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

Loading

0 comments on commit 2fd36dd

Please sign in to comment.