Skip to content

Commit

Permalink
Introduce "orchard {port-forward, vnc} worker WORKER_NAME" (#140)
Browse files Browse the repository at this point in the history
* Fix potential NPE in Client.wsRequest()

* Introduce "orchard {port-forward, vnc} worker WORKER_NAME"

* portspec.go: simplify logic and respect [LOCAL_PORT]:REMOTE_PORT format
  • Loading branch information
edigaryev committed Oct 9, 2023
1 parent 0634056 commit 13b4e19
Show file tree
Hide file tree
Showing 16 changed files with 405 additions and 89 deletions.
65 changes: 47 additions & 18 deletions api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ paths:
content:
application/json:
schema:
$ref: '#components/schemas/ControllerInfo'
$ref: '#/components/schemas/ControllerInfo'
/cluster-settings:
get:
summary: "Retrieve cluster settings"
Expand All @@ -27,7 +27,7 @@ paths:
content:
application/json:
schema:
$ref: '#components/schemas/ClusterSettings'
$ref: '#/components/schemas/ClusterSettings'
put:
summary: "Update cluster settings"
tags:
Expand All @@ -38,7 +38,7 @@ paths:
content:
application/json:
schema:
$ref: '#components/schemas/ClusterSettings'
$ref: '#/components/schemas/ClusterSettings'
/service-accounts:
post:
summary: "Create a Service Account"
Expand All @@ -50,7 +50,7 @@ paths:
content:
application/json:
schema:
$ref: '#components/schemas/ServiceAccount'
$ref: '#/components/schemas/ServiceAccount'
'409':
description: Service Account resource with with the same name already exists
get:
Expand All @@ -65,7 +65,7 @@ paths:
schema:
type: array
items:
$ref: '#components/schemas/ServiceAccount'
$ref: '#/components/schemas/ServiceAccount'
/service-accounts/{name}:
get:
summary: "Retrieve a Service Account"
Expand All @@ -77,7 +77,7 @@ paths:
content:
application/json:
schema:
$ref: '#components/schemas/ServiceAccount'
$ref: '#/components/schemas/ServiceAccount'
'404':
description: Service Account resource with the given name doesn't exist
put:
Expand All @@ -90,7 +90,7 @@ paths:
content:
application/json:
schema:
$ref: '#components/schemas/ServiceAccount'
$ref: '#/components/schemas/ServiceAccount'
'404':
description: Service Account resource with the given name doesn't exist
delete:
Expand All @@ -113,7 +113,7 @@ paths:
content:
application/json:
schema:
$ref: '#components/schemas/Worker'
$ref: '#/components/schemas/Worker'
'409':
description: Worker resource with with the same name already exists
get:
Expand All @@ -128,7 +128,7 @@ paths:
schema:
type: array
items:
$ref: '#components/schemas/Worker'
$ref: '#/components/schemas/Worker'
/workers/{name}:
get:
summary: "Retrieve a Worker"
Expand All @@ -140,7 +140,7 @@ paths:
content:
application/json:
schema:
$ref: '#components/schemas/Worker'
$ref: '#/components/schemas/Worker'
'404':
description: Worker resource with the given name doesn't exist
put:
Expand All @@ -153,7 +153,7 @@ paths:
content:
application/json:
schema:
$ref: '#components/schemas/Worker'
$ref: '#/components/schemas/Worker'
'404':
description: Worker resource with the given name doesn't exist
delete:
Expand All @@ -165,6 +165,35 @@ paths:
description: Worker resource was successfully deleted
'404':
description: Worker resource with the given name doesn't exist
/workers/{name}/port-forward:
get:
summary: "Port-forward to a worker using WebSocket protocol"
tags:
- workers
parameters:
- in: query
name: port
description: Worker's TCP port number to connect to
schema:
type: integer
minimum: 1
maximum: 65535
required: true
- in: header
name: Connection
description: WebSocket protocol required header
required: true
- in: header
name: Upgrade
description: WebSocket protocol required header
required: true
responses:
'400':
description: Invalid port specified
'404':
description: Worker resource with the given name doesn't exist
'503':
description: Failed to establish connection with the requested worker
/vms:
post:
summary: "Create a VM"
Expand All @@ -176,7 +205,7 @@ paths:
content:
application/json:
schema:
$ref: '#components/schemas/VM'
$ref: '#/components/schemas/VM'
'409':
description: VM resource with with the same name already exists
get:
Expand All @@ -191,7 +220,7 @@ paths:
schema:
type: array
items:
$ref: '#components/schemas/VM'
$ref: '#/components/schemas/VM'
/vms/{name}:
get:
summary: "Retrieve a VM"
Expand All @@ -203,7 +232,7 @@ paths:
content:
application/json:
schema:
$ref: '#components/schemas/VM'
$ref: '#/components/schemas/VM'
'404':
description: VM resource with the given name doesn't exist
put:
Expand All @@ -216,7 +245,7 @@ paths:
content:
application/json:
schema:
$ref: '#components/schemas/VM'
$ref: '#/components/schemas/VM'
'404':
description: VM resource with the given name doesn't exist
delete:
Expand All @@ -239,7 +268,7 @@ paths:
content:
application/json:
schema:
$ref: '#components/schemas/Events'
$ref: '#/components/schemas/Events'
'404':
description: VM resource with the given name doesn't exist
post:
Expand All @@ -252,7 +281,7 @@ paths:
content:
application/json:
schema:
$ref: '#components/schemas/Events'
$ref: '#/components/schemas/Events'
'404':
description: VM resource with the given name doesn't exist
/vms/{name}/port-forward:
Expand Down Expand Up @@ -339,7 +368,7 @@ components:
title: Events
type: object
items:
$ref: '#components/schemas/Event'
$ref: '#/components/schemas/Event'
Event:
title: Generic Resource Event
type: object
Expand Down
2 changes: 1 addition & 1 deletion internal/command/portforward/portforward.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ func NewCommand() *cobra.Command {
Short: "Forward TCP port to the resources",
}

command.AddCommand(newPortForwardVMCommand())
command.AddCommand(newPortForwardVMCommand(), newPortForwardWorkerCommand())

return command
}
52 changes: 34 additions & 18 deletions internal/command/portforward/portspec.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,47 @@ type PortSpec struct {
func NewPortSpec(portSpecRaw string) (*PortSpec, error) {
splits := strings.Split(portSpecRaw, ":")

if len(splits) > 2 {
return nil, fmt.Errorf("%w: expected no more than 2 components delimited by \":\", found %d",
ErrInvalidPortSpec, len(splits))
}

localPort, err := strconv.ParseUint(splits[0], 10, 16)
if err != nil {
return nil, err
}
switch len(splits) {
case 1:
remotePort, err := parsePort(splits[0])
if err != nil {
return nil, err
}

remotePort := localPort
return &PortSpec{
LocalPort: 0,
RemotePort: remotePort,
}, nil
case 2:
localPort, err := parsePort(splits[0])
if err != nil {
return nil, err
}

if len(splits) > 1 {
remotePort, err = strconv.ParseUint(splits[1], 10, 16)
remotePort, err := parsePort(splits[1])
if err != nil {
return nil, err
}

return &PortSpec{
LocalPort: localPort,
RemotePort: remotePort,
}, nil
default:
return nil, fmt.Errorf("%w: expected 1 or 2 components delimited by \":\", found %d",
ErrInvalidPortSpec, len(splits))
}
}

func parsePort(s string) (uint16, error) {
port, err := strconv.ParseUint(s, 10, 16)
if err != nil {
return 0, err
}

if localPort < 1 || remotePort < 1 || localPort > 65535 || remotePort > 65535 {
return nil, fmt.Errorf("%w: only ports in range [1, 65535] are allowed", ErrInvalidPortSpec)
if port < 1 || port > 65535 {
return 0, fmt.Errorf("%w: only ports in range [1, 65535] are allowed", ErrInvalidPortSpec)
}

return &PortSpec{
LocalPort: uint16(localPort),
RemotePort: uint16(remotePort),
}, nil
return uint16(port), nil
}
2 changes: 1 addition & 1 deletion internal/command/portforward/portspec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
func TestPortSpecNormal(t *testing.T) {
portSpec, err := portforward.NewPortSpec("5555")
require.NoError(t, err)
require.Equal(t, &portforward.PortSpec{LocalPort: 5555, RemotePort: 5555}, portSpec)
require.Equal(t, &portforward.PortSpec{LocalPort: 0, RemotePort: 5555}, portSpec)

portSpec, err = portforward.NewPortSpec("8000:80")
require.NoError(t, err)
Expand Down
83 changes: 83 additions & 0 deletions internal/command/portforward/worker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package portforward

import (
"fmt"
"github.com/cirruslabs/orchard/internal/proxy"
"github.com/cirruslabs/orchard/pkg/client"
"github.com/spf13/cobra"
"net"
)

func newPortForwardWorkerCommand() *cobra.Command {
command := &cobra.Command{
Use: "worker WORKER_NAME [LOCAL_PORT]:REMOTE_PORT",
Short: "Forward TCP port to the worker",
Args: cobra.ExactArgs(2),
RunE: runPortForwardWorkerCommand,
}

return command
}

func runPortForwardWorkerCommand(cmd *cobra.Command, args []string) (err error) {
name := args[0]
portSpecRaw := args[1]

portSpec, err := NewPortSpec(portSpecRaw)
if err != nil {
return err
}

client, err := client.New()
if err != nil {
return err
}

listener, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", portSpec.LocalPort))
if err != nil {
return err
}
defer func() {
if listenerErr := listener.Close(); listenerErr != nil && err == nil {
err = listenerErr
}
}()

fmt.Printf("forwarding %s -> %s:%d...\n", listener.Addr(), name, portSpec.RemotePort)

errCh := make(chan error, 1)

go func() {
for {
conn, err := listener.Accept()
if err != nil {
errCh <- err

return
}

go func() {
defer conn.Close()

wsConn, err := client.Workers().PortForward(cmd.Context(), name, portSpec.RemotePort)
if err != nil {
fmt.Printf("failed to forward port: %v\n", err)

return
}
defer wsConn.Close()

if err := proxy.Connections(wsConn, conn); err != nil {
fmt.Printf("failed to forward port: %v\n", err)
}
}()
}
}()

select {
case <-cmd.Context().Done():
return cmd.Context().Err()
case err := <-errCh:
return err
}
}
2 changes: 1 addition & 1 deletion internal/command/vnc/vnc.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ func NewCommand() *cobra.Command {
Short: "Open VNC session with the resource",
}

command.AddCommand(newVNCVMCommand())
command.AddCommand(newVNCVMCommand(), newVNCWorkerCommand())

return command
}
Loading

0 comments on commit 13b4e19

Please sign in to comment.