Files
odo/pkg/remotecmd/types.go
Armel Soro 29b5a38f88 Make port-forwarding work on Podman with apps listening on the loopback interface, via a new --forward-localhost flag (#6629)
* Embed platform.Client interface in platform-specific interfaces

This avoids repeating the same methods in both interfaces,
and makes the intent clearer.

* Verify interface compliance of PodmanCli at compile time

This is recommended in the Coding Conventions guidelines [1].
Specifically, what's important here is checking that it meets the 'platform.Client' contract.

[1] https://github.com/redhat-developer/odo/wiki/Dev:-Coding-Conventions#verify-interface-compliance

* Move K8s-specific implementation of port-forwarding to a dedicated package

This paves the way to providing a different implementation for Podman

* Remove GetPortsToForward method from the portForward.Client interface

Current implementation relies on the Devfile object,
so it makes more sense to be in the libdevfile package.

* Monitor and send appropriate status events after starting a remote command process

This allows callers to get more meaningful events about the process.

* Implement port-forwarding logic on Podman

As explained in [1], this makes use of a helper sidecar container
(aptly named "odo-helper-port-forwarding") to be added to the Pod Spec created by odo.
In this scope, port-forwarding will be equivalent of executing a socat command in this
helper container, like so:

socat -d tcp-listen:20002,reuseaddr,fork tcp:localhost:5858

In the command above, this will open up port 20001 on the helper container,
and forwarding requests to localhost:5858
(which would be in the application container, part of the same Pod)

[1] https://github.com/redhat-developer/odo/issues/6510

* Update portForward.Client interface methods

Specifically, the 'StartPortForwarding' method
can now accept an explicit list of ports that needs to
be forwarded, if the caller can compute provide such information.

This is currently useful on Podman where the ports
(even the random ones) are known in advance.

* Add helper sidecar container to the Pod Spec generated on Podman

As explained in [1], this helper sidecar container (aptly named "odo-helper-port-forwarding")
will hold the actual container/host ports mapping in the spec
(to overcome the limitation of using hostPort against a container app listening on the loopback interface);
this running container will be used to execute the actual port-forwarding commands (based on socat), e.g:

```
apiVersion: v1
kind: Pod
metadata:
  name: my-component-app
  labels:
    app: my-component-app
spec:
  containers:
  - name: runtime
    command: [ 'tail', '-f', '/dev/null']
    # Omitted for brevity, but imagine an application being executed by odo
    # and listening on both 0.0.0.0:3000 and 127.0.0.1:5858

  - name: odo-helper-port-forwarding
    image: quay.io/devfile/base-developer-image:ubi8-latest
    command: [ 'tail', '-f', '/dev/null']
    ports:
    - containerPort: 20001
      # A command will be executed by odo, e.g.: 'socat -d -d tcp-listen:20001,reuseaddr,fork tcp:localhost:3000'
      hostPort: 20001
    - containerPort: 20002
      # A command will be executed by odo, e.g.: 'socat -d -d tcp-listen:20002,reuseaddr,fork tcp:localhost:5858'
      hostPort: 20002

# ... Omitted for brevity
```

In this scope, port-forwarding from 20002 to 5858 for example will be equivalent of executing
a socat command in this helper container, like so:

socat -d tcp-listen:20002,reuseaddr,fork tcp:localhost:5858

In the command above, this will open up port 20001 on the helper container,
and forwarding requests to localhost:5858
(which would be in the 'runtime' container, part of the same Pod)

[1] https://github.com/redhat-developer/odo/issues/6510

* Inject the right portForward client depending on the platform

* Delegate port-forwarding on Podman to the appropriate client

* Implement --forward-localhost on Podman

* Add unit and integration test cases

* Cover more debug-related test cases on Podman

* Expose the endpoint protocol so as to instruct socat to listen and forward the right protocol

* Fix sub-deps of EXEC and PORT_FORWARD, as suggested in review
2023-03-06 08:12:22 +01:00

68 lines
2.1 KiB
Go

package remotecmd
// RemoteProcessStatus is an enum type for representing process statuses.
type RemoteProcessStatus string
const (
// Unknown represents a process for which the status cannot be determined reliably or is not handled yet by us.
Unknown RemoteProcessStatus = "unknown"
// Starting represents a process that is just about to start.
Starting = "starting"
// Stopped represents a process stopped.
Stopped = "stopped"
// Errored represents a process that errored out, i.e. exited with a non-zero status code.
Errored = "errored"
// Running represents a running process.
Running = "running"
)
const (
// ShellExecutable is the shell executable
ShellExecutable = "/bin/sh"
)
// RemoteProcessInfo represents a given remote process linked to a given Devfile command
type RemoteProcessInfo struct {
// Pid of the process
Pid int
// Status of the process
Status RemoteProcessStatus
}
// CommandDefinition represents the structure of any given command that would be handled by implementations of RemoteProcessHandler.
type CommandDefinition struct {
// Id is any unique (and short) identifier that helps manage the process associated to this command.
Id string
// PidDirectory is the directory where the PID file for this process will be stored.
// The directory needs to be present in the remote container and be writable by the user (in the container) executing the command.
PidDirectory string
// WorkingDir is the working directory from which the command should get executed.
WorkingDir string
// EnvVars are environment variables to set.
EnvVars []CommandEnvVar
// CmdLine is the command-line that will get executed.
CmdLine string
}
// CommandEnvVar represents an environment variable used as part of running any CommandDefinition.
type CommandEnvVar struct {
// Key of the environment variable.
Key string
// Value of the environment variable.
Value string
}
// CommandOutputHandler is a function that is expected to handle the output and error returned by a command executed.
type CommandOutputHandler func(status RemoteProcessStatus, stdout []string, stderr []string, err error)