Use the new routes API from the CLI

This commit is contained in:
Juan Font 2022-11-26 00:04:02 +00:00 committed by Kristoffer Dalby
parent 34631dfcf5
commit 78819be03c
2 changed files with 142 additions and 183 deletions

View file

@ -13,27 +13,22 @@ import (
func init() { func init() {
rootCmd.AddCommand(routesCmd) rootCmd.AddCommand(routesCmd)
listRoutesCmd.Flags().Uint64P("identifier", "i", 0, "Node identifier (ID)") listRoutesCmd.Flags().Uint64P("identifier", "i", 0, "Node identifier (ID)")
err := listRoutesCmd.MarkFlagRequired("identifier")
if err != nil {
log.Fatalf(err.Error())
}
routesCmd.AddCommand(listRoutesCmd) routesCmd.AddCommand(listRoutesCmd)
enableRouteCmd.Flags(). enableRouteCmd.Flags().Uint64P("route", "r", 0, "Route identifier (ID)")
StringSliceP("route", "r", []string{}, "List (or repeated flags) of routes to enable") err := enableRouteCmd.MarkFlagRequired("route")
enableRouteCmd.Flags().Uint64P("identifier", "i", 0, "Node identifier (ID)")
enableRouteCmd.Flags().BoolP("all", "a", false, "All routes from host")
err = enableRouteCmd.MarkFlagRequired("identifier")
if err != nil { if err != nil {
log.Fatalf(err.Error()) log.Fatalf(err.Error())
} }
routesCmd.AddCommand(enableRouteCmd) routesCmd.AddCommand(enableRouteCmd)
nodeCmd.AddCommand(routesCmd) disableRouteCmd.Flags().Uint64P("route", "r", 0, "Route identifier (ID)")
err = disableRouteCmd.MarkFlagRequired("route")
if err != nil {
log.Fatalf(err.Error())
}
routesCmd.AddCommand(disableRouteCmd)
} }
var routesCmd = &cobra.Command{ var routesCmd = &cobra.Command{
@ -44,7 +39,7 @@ var routesCmd = &cobra.Command{
var listRoutesCmd = &cobra.Command{ var listRoutesCmd = &cobra.Command{
Use: "list", Use: "list",
Short: "List routes advertised and enabled by a given node", Short: "List all routes",
Aliases: []string{"ls", "show"}, Aliases: []string{"ls", "show"},
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
output, _ := cmd.Flags().GetString("output") output, _ := cmd.Flags().GetString("output")
@ -64,11 +59,10 @@ var listRoutesCmd = &cobra.Command{
defer cancel() defer cancel()
defer conn.Close() defer conn.Close()
request := &v1.GetMachineRouteRequest{ var routes []*v1.Route
MachineId: machineID,
}
response, err := client.GetMachineRoute(ctx, request) if machineID == 0 {
response, err := client.GetRoutes(ctx, &v1.GetRoutesRequest{})
if err != nil { if err != nil {
ErrorOutput( ErrorOutput(
err, err,
@ -79,13 +73,25 @@ var listRoutesCmd = &cobra.Command{
return return
} }
if output != "" { routes = response.Routes
SuccessOutput(response.Routes, "", output) } else {
response, err := client.GetMachineRoutes(ctx, &v1.GetMachineRoutesRequest{
MachineId: machineID,
})
if err != nil {
ErrorOutput(
err,
fmt.Sprintf("Cannot get routes for machine %d: %s", machineID, status.Convert(err).Message()),
output,
)
return return
} }
tableData := routesToPtables(response.Routes) routes = response.Routes
}
tableData := routesToPtables(routes)
if err != nil { if err != nil {
ErrorOutput(err, fmt.Sprintf("Error converting to table: %s", err), output) ErrorOutput(err, fmt.Sprintf("Error converting to table: %s", err), output)
@ -107,16 +113,12 @@ var listRoutesCmd = &cobra.Command{
var enableRouteCmd = &cobra.Command{ var enableRouteCmd = &cobra.Command{
Use: "enable", Use: "enable",
Short: "Set the enabled routes for a given node", Short: "Set a route as enabled",
Long: `This command will take a list of routes that will _replace_ Long: `This command will make as enabled a given route.`,
the current set of routes on a given node.
If you would like to disable a route, simply run the command again, but
omit the route you do not want to enable.
`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
output, _ := cmd.Flags().GetString("output") output, _ := cmd.Flags().GetString("output")
machineID, err := cmd.Flags().GetUint64("identifier") routeID, err := cmd.Flags().GetUint64("route")
if err != nil { if err != nil {
ErrorOutput( ErrorOutput(
err, err,
@ -131,52 +133,13 @@ omit the route you do not want to enable.
defer cancel() defer cancel()
defer conn.Close() defer conn.Close()
var routes []string response, err := client.EnableRoute(ctx, &v1.EnableRouteRequest{
RouteId: routeID,
isAll, _ := cmd.Flags().GetBool("all")
if isAll {
response, err := client.GetMachineRoute(ctx, &v1.GetMachineRouteRequest{
MachineId: machineID,
}) })
if err != nil { if err != nil {
ErrorOutput( ErrorOutput(
err, err,
fmt.Sprintf( fmt.Sprintf("Cannot enable route %d: %s", routeID, status.Convert(err).Message()),
"Cannot get machine routes: %s\n",
status.Convert(err).Message(),
),
output,
)
return
}
routes = response.GetRoutes().GetAdvertisedRoutes()
} else {
routes, err = cmd.Flags().GetStringSlice("route")
if err != nil {
ErrorOutput(
err,
fmt.Sprintf("Error getting routes from flag: %s", err),
output,
)
return
}
}
request := &v1.EnableMachineRoutesRequest{
MachineId: machineID,
Routes: routes,
}
response, err := client.EnableMachineRoutes(ctx, request)
if err != nil {
ErrorOutput(
err,
fmt.Sprintf(
"Cannot register machine: %s\n",
status.Convert(err).Message(),
),
output, output,
) )
@ -184,50 +147,71 @@ omit the route you do not want to enable.
} }
if output != "" { if output != "" {
SuccessOutput(response.Routes, "", output) SuccessOutput(response, "", output)
return return
} }
},
}
tableData := routesToPtables(response.Routes) var disableRouteCmd = &cobra.Command{
if err != nil { Use: "disable",
ErrorOutput(err, fmt.Sprintf("Error converting to table: %s", err), output) Short: "Set as disabled a given route",
Long: `This command will make as disabled a given route.`,
Run: func(cmd *cobra.Command, args []string) {
output, _ := cmd.Flags().GetString("output")
return routeID, err := cmd.Flags().GetUint64("route")
}
err = pterm.DefaultTable.WithHasHeader().WithData(tableData).Render()
if err != nil { if err != nil {
ErrorOutput( ErrorOutput(
err, err,
fmt.Sprintf("Failed to render pterm table: %s", err), fmt.Sprintf("Error getting machine id from flag: %s", err),
output, output,
) )
return return
} }
ctx, client, conn, cancel := getHeadscaleCLIClient()
defer cancel()
defer conn.Close()
response, err := client.DisableRoute(ctx, &v1.DisableRouteRequest{
RouteId: routeID,
})
if err != nil {
ErrorOutput(
err,
fmt.Sprintf("Cannot enable route %d: %s", routeID, status.Convert(err).Message()),
output,
)
return
}
if output != "" {
SuccessOutput(response, "", output)
return
}
}, },
} }
// routesToPtables converts the list of routes to a nice table. // routesToPtables converts the list of routes to a nice table.
func routesToPtables(routes *v1.Routes) pterm.TableData { func routesToPtables(routes []*v1.Route) pterm.TableData {
tableData := pterm.TableData{{"Route", "Enabled"}} tableData := pterm.TableData{{"ID", "Machine", "Prefix", "Advertised", "Enabled", "Primary"}}
for _, route := range routes.GetAdvertisedRoutes() { for _, route := range routes {
enabled := isStringInSlice(routes.EnabledRoutes, route) tableData = append(tableData,
[]string{
tableData = append(tableData, []string{route, strconv.FormatBool(enabled)}) strconv.FormatUint(route.Id, 10),
route.Machine.GivenName,
route.Prefix,
strconv.FormatBool(route.Advertised),
strconv.FormatBool(route.Enabled),
strconv.FormatBool(route.IsPrimary),
})
} }
return tableData return tableData
} }
func isStringInSlice(strs []string, s string) bool {
for _, s2 := range strs {
if s == s2 {
return true
}
}
return false
}

View file

@ -7,6 +7,7 @@ import (
"log" "log"
"net/http" "net/http"
"os" "os"
"strconv"
"testing" "testing"
"time" "time"
@ -1305,24 +1306,22 @@ func (s *IntegrationCLITestSuite) TestRouteCommand() {
"list", "list",
"--output", "--output",
"json", "json",
"--identifier",
"0",
}, },
[]string{}, []string{},
) )
assert.Nil(s.T(), err) assert.Nil(s.T(), err)
var listAll v1.Routes var routes []v1.Route
err = json.Unmarshal([]byte(listAllResult), &listAll) err = json.Unmarshal([]byte(listAllResult), &routes)
assert.Nil(s.T(), err) assert.Nil(s.T(), err)
assert.Len(s.T(), listAll.AdvertisedRoutes, 2) assert.Len(s.T(), routes, 2)
assert.Contains(s.T(), listAll.AdvertisedRoutes, "10.0.0.0/8") assert.Equal(s.T(), routes[0].Enabled, false)
assert.Contains(s.T(), listAll.AdvertisedRoutes, "192.168.1.0/24") assert.Equal(s.T(), routes[1].Enabled, false)
assert.Empty(s.T(), listAll.EnabledRoutes) routeIDToEnable := routes[1].Id
enableTwoRoutesResult, _, err := ExecuteCommand( _, _, err = ExecuteCommand(
&s.headscale, &s.headscale,
[]string{ []string{
"headscale", "headscale",
@ -1330,110 +1329,86 @@ func (s *IntegrationCLITestSuite) TestRouteCommand() {
"enable", "enable",
"--output", "--output",
"json", "json",
"--identifier",
"0",
"--route", "--route",
"10.0.0.0/8", strconv.FormatUint(routeIDToEnable, 10),
"--route",
"192.168.1.0/24",
}, },
[]string{}, []string{},
) )
assert.Nil(s.T(), err) assert.Nil(s.T(), err)
var enableTwoRoutes v1.Routes listAllResult, _, err = ExecuteCommand(
err = json.Unmarshal([]byte(enableTwoRoutesResult), &enableTwoRoutes) &s.headscale,
[]string{
"headscale",
"routes",
"list",
"--output",
"json",
},
[]string{},
)
assert.Nil(s.T(), err) assert.Nil(s.T(), err)
assert.Len(s.T(), enableTwoRoutes.AdvertisedRoutes, 2) assert.Nil(s.T(), err)
assert.Contains(s.T(), enableTwoRoutes.AdvertisedRoutes, "10.0.0.0/8")
assert.Contains(s.T(), enableTwoRoutes.AdvertisedRoutes, "192.168.1.0/24")
assert.Len(s.T(), enableTwoRoutes.EnabledRoutes, 2) err = json.Unmarshal([]byte(listAllResult), &routes)
assert.Contains(s.T(), enableTwoRoutes.EnabledRoutes, "10.0.0.0/8") assert.Nil(s.T(), err)
assert.Contains(s.T(), enableTwoRoutes.EnabledRoutes, "192.168.1.0/24")
assert.Len(s.T(), routes, 2)
for _, route := range routes {
if route.Id == routeIDToEnable {
assert.Equal(s.T(), route.Enabled, true)
assert.Equal(s.T(), route.IsPrimary, true)
} else {
assert.Equal(s.T(), route.Enabled, false)
}
}
// Enable only one route, effectively disabling one of the routes // Enable only one route, effectively disabling one of the routes
enableOneRouteResult, _, err := ExecuteCommand( _, _, err = ExecuteCommand(
&s.headscale, &s.headscale,
[]string{ []string{
"headscale", "headscale",
"routes", "routes",
"enable", "disable",
"--output", "--output",
"json", "json",
"--identifier",
"0",
"--route", "--route",
"10.0.0.0/8", strconv.FormatUint(routeIDToEnable, 10),
}, },
[]string{}, []string{},
) )
assert.Nil(s.T(), err) assert.Nil(s.T(), err)
var enableOneRoute v1.Routes listAllResult, _, err = ExecuteCommand(
err = json.Unmarshal([]byte(enableOneRouteResult), &enableOneRoute)
assert.Nil(s.T(), err)
assert.Len(s.T(), enableOneRoute.AdvertisedRoutes, 2)
assert.Contains(s.T(), enableOneRoute.AdvertisedRoutes, "10.0.0.0/8")
assert.Contains(s.T(), enableOneRoute.AdvertisedRoutes, "192.168.1.0/24")
assert.Len(s.T(), enableOneRoute.EnabledRoutes, 1)
assert.Contains(s.T(), enableOneRoute.EnabledRoutes, "10.0.0.0/8")
// Enable only one route, effectively disabling one of the routes
failEnableNonAdvertisedRoute, _, err := ExecuteCommand(
&s.headscale, &s.headscale,
[]string{ []string{
"headscale", "headscale",
"routes", "routes",
"enable", "list",
"--output", "--output",
"json", "json",
"--identifier",
"0",
"--route",
"11.0.0.0/8",
}, },
[]string{}, []string{},
) )
assert.Nil(s.T(), err) assert.Nil(s.T(), err)
assert.Contains(
s.T(),
string(failEnableNonAdvertisedRoute),
"route (route-machine) is not available on node",
)
// Enable all routes on host
enableAllRouteResult, _, err := ExecuteCommand(
&s.headscale,
[]string{
"headscale",
"routes",
"enable",
"--output",
"json",
"--identifier",
"0",
"--all",
},
[]string{},
)
assert.Nil(s.T(), err) assert.Nil(s.T(), err)
var enableAllRoute v1.Routes err = json.Unmarshal([]byte(listAllResult), &routes)
err = json.Unmarshal([]byte(enableAllRouteResult), &enableAllRoute)
assert.Nil(s.T(), err) assert.Nil(s.T(), err)
assert.Len(s.T(), enableAllRoute.AdvertisedRoutes, 2) assert.Len(s.T(), routes, 2)
assert.Contains(s.T(), enableAllRoute.AdvertisedRoutes, "10.0.0.0/8")
assert.Contains(s.T(), enableAllRoute.AdvertisedRoutes, "192.168.1.0/24")
assert.Len(s.T(), enableAllRoute.EnabledRoutes, 2) for _, route := range routes {
assert.Contains(s.T(), enableAllRoute.EnabledRoutes, "10.0.0.0/8") if route.Id == routeIDToEnable {
assert.Contains(s.T(), enableAllRoute.EnabledRoutes, "192.168.1.0/24") assert.Equal(s.T(), route.Enabled, false)
assert.Equal(s.T(), route.IsPrimary, false)
} else {
assert.Equal(s.T(), route.Enabled, false)
}
}
} }
func (s *IntegrationCLITestSuite) TestApiKeyCommand() { func (s *IntegrationCLITestSuite) TestApiKeyCommand() {