From 58b15ae69276588d858b4b8416766497bb344931 Mon Sep 17 00:00:00 2001 From: Vishal Dalwadi Date: Wed, 19 Feb 2025 21:49:47 +0530 Subject: [PATCH 1/2] feat(go): add deprecation warning. --- cli/cmd/node/create_ingress.go | 11 ++++++----- cli/cmd/node/create_relay.go | 9 +++++---- cli/cmd/node/delete_ingress.go | 11 ++++++----- cli/cmd/node/delete_relay.go | 9 +++++---- 4 files changed, 22 insertions(+), 18 deletions(-) diff --git a/cli/cmd/node/create_ingress.go b/cli/cmd/node/create_ingress.go index e9cf5cc8a..5279c7bda 100644 --- a/cli/cmd/node/create_ingress.go +++ b/cli/cmd/node/create_ingress.go @@ -6,11 +6,12 @@ import ( ) var nodeCreateIngressCmd = &cobra.Command{ - Use: "create_remote_access_gateway [NETWORK NAME] [NODE ID]", - Args: cobra.ExactArgs(2), - Short: "Turn a Node into a Remote Access Gateway (Ingress)", - Long: `Turn a Node into a Remote Access Gateway (Ingress) for a Network.`, - Aliases: []string{"create_rag"}, + Use: "create_remote_access_gateway [NETWORK NAME] [NODE ID]", + Args: cobra.ExactArgs(2), + Short: "Turn a Node into a Remote Access Gateway (Ingress)", + Long: `Turn a Node into a Remote Access Gateway (Ingress) for a Network.`, + Deprecated: "in favour of the `gateway` subcommand.", + Aliases: []string{"create_rag"}, Run: func(cmd *cobra.Command, args []string) { functions.PrettyPrint(functions.CreateIngress(args[0], args[1], failover)) }, diff --git a/cli/cmd/node/create_relay.go b/cli/cmd/node/create_relay.go index e17dce57f..1c0f8f3c3 100644 --- a/cli/cmd/node/create_relay.go +++ b/cli/cmd/node/create_relay.go @@ -8,10 +8,11 @@ import ( ) var hostCreateRelayCmd = &cobra.Command{ - Use: "create_relay [NETWORK][NODE ID] [RELAYED NODE IDS (comma separated)]", - Args: cobra.ExactArgs(3), - Short: "Turn a Node into a Relay", - Long: `Turn a Node into a Relay`, + Use: "create_relay [NETWORK][NODE ID] [RELAYED NODE IDS (comma separated)]", + Args: cobra.ExactArgs(3), + Short: "Turn a Node into a Relay", + Long: `Turn a Node into a Relay`, + Deprecated: "in favour of the `gateway` subcommand.", Run: func(cmd *cobra.Command, args []string) { functions.PrettyPrint(functions.CreateRelay(args[0], args[1], strings.Split(args[2], ","))) }, diff --git a/cli/cmd/node/delete_ingress.go b/cli/cmd/node/delete_ingress.go index 6176fdddc..d1a2cf63c 100644 --- a/cli/cmd/node/delete_ingress.go +++ b/cli/cmd/node/delete_ingress.go @@ -6,11 +6,12 @@ import ( ) var nodeDeleteIngressCmd = &cobra.Command{ - Use: "delete_remote_access_gateway [NETWORK NAME] [NODE ID]", - Args: cobra.ExactArgs(2), - Short: "Delete Remote Access Gateway role from a Node", - Long: `Delete Remote Access Gateway role from a Node`, - Aliases: []string{"delete_rag"}, + Use: "delete_remote_access_gateway [NETWORK NAME] [NODE ID]", + Args: cobra.ExactArgs(2), + Short: "Delete Remote Access Gateway role from a Node", + Long: `Delete Remote Access Gateway role from a Node`, + Deprecated: "in favour of the `gateway` subcommand.", + Aliases: []string{"delete_rag"}, Run: func(cmd *cobra.Command, args []string) { functions.PrettyPrint(functions.DeleteIngress(args[0], args[1])) }, diff --git a/cli/cmd/node/delete_relay.go b/cli/cmd/node/delete_relay.go index 91e2522d9..674cfa067 100644 --- a/cli/cmd/node/delete_relay.go +++ b/cli/cmd/node/delete_relay.go @@ -6,10 +6,11 @@ import ( ) var hostDeleteRelayCmd = &cobra.Command{ - Use: "delete_relay [NETWORK] [NODE ID]", - Args: cobra.ExactArgs(2), - Short: "Delete Relay from a node", - Long: `Delete Relay from a node`, + Use: "delete_relay [NETWORK] [NODE ID]", + Args: cobra.ExactArgs(2), + Short: "Delete Relay from a node", + Long: `Delete Relay from a node`, + Deprecated: "in favour of the `gateway` subcommand.", Run: func(cmd *cobra.Command, args []string) { functions.PrettyPrint(functions.DeleteRelay(args[0], args[1])) }, From e31658f4d438de62d6a9ae0651ff06ce237f8250 Mon Sep 17 00:00:00 2001 From: Vishal Dalwadi Date: Wed, 19 Feb 2025 21:50:15 +0530 Subject: [PATCH 2/2] feat(go): add support for gateway commands. --- cli/cmd/gateway/create.go | 55 +++++++++++++++++++++++++++++++++++++++ cli/cmd/gateway/delete.go | 27 +++++++++++++++++++ cli/cmd/gateway/root.go | 18 +++++++++++++ cli/cmd/root.go | 2 ++ cli/functions/gateway.go | 18 +++++++++++++ 5 files changed, 120 insertions(+) create mode 100644 cli/cmd/gateway/create.go create mode 100644 cli/cmd/gateway/delete.go create mode 100644 cli/cmd/gateway/root.go create mode 100644 cli/functions/gateway.go diff --git a/cli/cmd/gateway/create.go b/cli/cmd/gateway/create.go new file mode 100644 index 000000000..47229fefa --- /dev/null +++ b/cli/cmd/gateway/create.go @@ -0,0 +1,55 @@ +package gateway + +import ( + "github.com/gravitl/netmaker/cli/functions" + "github.com/gravitl/netmaker/models" + "github.com/spf13/cobra" + "strings" +) + +var externalClientDNS string +var isInternetGateway bool +var metadata string +var persistentKeepAlive uint +var mtu uint + +var gatewayCreateCmd = &cobra.Command{ + Use: "create [NETWORK NAME] [NODE ID] [RELAYED NODES ID (comma separated)]", + Args: cobra.ExactArgs(3), + Short: "Create a new Gateway on a Netmaker network.", + Long: ` +Configures a node as a gateway in a specified network, allowing it to relay traffic for other nodes. The gateway can also function as an internet gateway if specified. + +Arguments: +NETWORK NAME: The name of the network where the gateway will be created. +NODE ID: The ID of the node to be configured as a gateway. +RELAYED NODES ID: A comma-separated list of node IDs that will be relayed through this gateway. +`, + Run: func(cmd *cobra.Command, args []string) { + functions.PrettyPrint( + functions.CreateGateway( + models.IngressRequest{ + ExtclientDNS: externalClientDNS, + IsInternetGateway: isInternetGateway, + Metadata: metadata, + PersistentKeepalive: int32(persistentKeepAlive), + MTU: int32(mtu), + }, + models.RelayRequest{ + NodeID: args[0], + NetID: args[1], + RelayedNodes: strings.Split(args[2], ","), + }, + ), + ) + }, +} + +func init() { + gatewayCreateCmd.Flags().StringVarP(&externalClientDNS, "dns", "d", "", "the IP address of the DNS server to be used by external clients") + gatewayCreateCmd.Flags().BoolVarP(&isInternetGateway, "internet", "i", false, "if set, the gateway will route traffic to the internet") + gatewayCreateCmd.Flags().StringVarP(&metadata, "note", "n", "", "description or metadata to be associated with the gateway") + gatewayCreateCmd.Flags().UintVarP(&persistentKeepAlive, "keep-alive", "k", 20, "the keep-alive interval (in seconds) for maintaining persistent connections") + gatewayCreateCmd.Flags().UintVarP(&mtu, "mtu", "m", 1420, "the maximum transmission unit (MTU) size in bytes") + rootCmd.AddCommand(gatewayCreateCmd) +} diff --git a/cli/cmd/gateway/delete.go b/cli/cmd/gateway/delete.go new file mode 100644 index 000000000..ef8c4c881 --- /dev/null +++ b/cli/cmd/gateway/delete.go @@ -0,0 +1,27 @@ +package gateway + +import ( + "github.com/gravitl/netmaker/cli/functions" + "github.com/spf13/cobra" +) + +var gatewayDeleteCmd = &cobra.Command{ + Use: "delete [NETWORK NAME] [NODE ID]", + Args: cobra.ExactArgs(2), + Short: "Delete a Gateway.", + Long: ` +Removes the gateway configuration from a node in a specified network. The node itself remains, but it will no longer function as a gateway. + +Arguments: +NETWORK NAME: The name of the network from which the gateway configuration should be removed. +NODE ID: The ID of the node that is currently acting as a gateway. +`, + Aliases: []string{"rm"}, + Run: func(cmd *cobra.Command, args []string) { + functions.PrettyPrint(functions.DeleteGateway(args[0], args[1])) + }, +} + +func init() { + rootCmd.AddCommand(gatewayDeleteCmd) +} diff --git a/cli/cmd/gateway/root.go b/cli/cmd/gateway/root.go new file mode 100644 index 000000000..aa17af299 --- /dev/null +++ b/cli/cmd/gateway/root.go @@ -0,0 +1,18 @@ +package gateway + +import ( + "github.com/spf13/cobra" +) + +// rootCmd represents the base command when called without any subcommands. +var rootCmd = &cobra.Command{ + Use: "gateway", + Short: "Manage Gateways.", + Long: `Manage Gateways.`, + Aliases: []string{"gw"}, +} + +// GetRoot returns the root subcommand. +func GetRoot() *cobra.Command { + return rootCmd +} diff --git a/cli/cmd/root.go b/cli/cmd/root.go index b2164d3f1..ebe9c9576 100644 --- a/cli/cmd/root.go +++ b/cli/cmd/root.go @@ -1,6 +1,7 @@ package cmd import ( + "github.com/gravitl/netmaker/cli/cmd/gateway" "os" "github.com/gravitl/netmaker/cli/cmd/acl" @@ -55,4 +56,5 @@ func init() { rootCmd.AddCommand(host.GetRoot()) rootCmd.AddCommand(enrollment_key.GetRoot()) rootCmd.AddCommand(failover.GetRoot()) + rootCmd.AddCommand(gateway.GetRoot()) } diff --git a/cli/functions/gateway.go b/cli/functions/gateway.go new file mode 100644 index 000000000..b33b71752 --- /dev/null +++ b/cli/functions/gateway.go @@ -0,0 +1,18 @@ +package functions + +import ( + "fmt" + "github.com/gravitl/netmaker/models" + "net/http" +) + +func CreateGateway(ingressRequest models.IngressRequest, relayRequest models.RelayRequest) *models.ApiNode { + return request[models.ApiNode](http.MethodPost, fmt.Sprintf("/api/nodes/%s/%s/gateway", relayRequest.NetID, relayRequest.NodeID), &models.CreateGwReq{ + IngressRequest: ingressRequest, + RelayRequest: relayRequest, + }) +} + +func DeleteGateway(networkID, nodeID string) *models.ApiNode { + return request[models.ApiNode](http.MethodDelete, fmt.Sprintf("/api/nodes/%s/%s/gateway", networkID, nodeID), nil) +}