Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make port-forwarding work on Podman with apps listening on the loopback interface, via a new --forward-localhost flag #6629

Conversation

rm3l
Copy link
Member

@rm3l rm3l commented Mar 1, 2023

What type of PR is this:
/kind feature
/area odo-on-podman
/area dev

What does this PR do / why we need it:
This is the second and last PR (building on #6620) to address #6510.

For some context, the way odo currently does port-forwarding on Podman is via a HostPort in the relevant container in the pod spec. But as reported in #6510 and containers/podman#17353, Podman won't forward traffic to the container if the container port is bound to the container loopback interface.
As discussed in #6510 (comment) (and similar to how things work on DevSpaces), #6620 tried to detect if ports that need to be forwarded are bound to the container loopback interface. If this is the case:

  • by default, odo dev on Podman will error out with an error message indicating the issue, along with a recommendation to either change the application to listen on 0.0.0.0, or to run odo dev with --forward-localhost.
  • if odo dev is run with --ignore-localhost, this message will be displayed as a warning, but this won't prevent odo dev from running. However, any request sent out to the local port forwarded to the container on its loopback interface might not work with Podman.

This PR is about supporting a new --forward-localhost flag, to make port-forwarding work on Podman with applications bound to the loopback interface. It does so by injecting a dedicated side container (running the quay.io/devfile/base-developer-image image). Port-forwarding is then done by running a socat command in this container. It will listen on one port on all interfaces, and on the other hand forward traffic to the second port (which might be bound to the loopback interface).

The main interest of having this flag on Podman is to have on Podman the same behaviour as kubectl port-forward on Kubernetes, which is able to port-forward a container port bound on localhost. This is primarily useful for working with the debugger on localhost, without exposing it on all interfaces, for security reasons.

Which issue(s) this PR fixes:
Fixes #6510

PR acceptance criteria:

  • Unit test

  • Integration test

  • Documentation

How to test changes / Special notes to the reviewer:
This behavior should not affect the default cluster mode.

To test the changes on Podman, either use a project with an application listening on some port on localhost, or leverage some existing stacks like Node.JS which have debuggers listening on localhost.

$ mkdir /tmp/nodejs && cd /tmp/nodejs
$ odo init --name debug-nodejs --devfile nodejs --starter nodejs-starter
$ ODO_EXPERIMENTAL_MODE=t odo dev --platform=podman --debug --forward-localhost

...
↪ Running on podman in Dev mode
 ✓  Deploying pod [3s]
 ✓  Building your application in container (command: install) [4s]
 •  Executing the application (command: debug)  ...
 -  Forwarding from 127.0.0.1:20001 -> 3000
 -  Forwarding from 127.0.0.1:20002 -> 5858

↪ Dev mode
 Status:
 Watching for changes in the current directory /tmp/test/nodejs

 Keyboard Commands:
[Ctrl+c] - Exit and delete resources from podman
     [p] - Manually apply local changes to the application on podman

NOTE: a sidecar is added as long as --forward-localhost is passed, even if the application is not listening on the loopback interface. But this does not prevent port-forwarding from working correctly. In the example above, if I run ODO_EXPERIMENTAL_MODE=t odo dev --platform=podman --forward-localhost (without --debug), odo dev should start and work normally.

rm3l added 10 commits March 1, 2023 17:33
This avoids repeating the same methods in both interfaces,
and makes the intent clearer.
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
…ckage

This paves the way to providing a different implementation for Podman
Current implementation relies on the Devfile object,
so it makes more sense to be in the libdevfile package.
…mmand process

This allows callers to get more meaningful events about the process.
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] redhat-developer#6510
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.
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] redhat-developer#6510
@openshift-ci openshift-ci bot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. Required by Prow. label Mar 1, 2023
@openshift-ci
Copy link

openshift-ci bot commented Mar 1, 2023

Skipping CI for Draft Pull Request.
If you want CI signal for your change, please convert it to an actual PR.
You can still manually trigger a test run with /test all

@netlify
Copy link

netlify bot commented Mar 1, 2023

Deploy Preview for odo-docusaurus-preview ready!

Name Link
🔨 Latest commit 7b384d3
🔍 Latest deploy log https://app.netlify.com/sites/odo-docusaurus-preview/deploys/64022559b60d6400086d2384
😎 Deploy Preview https://deploy-preview-6629--odo-docusaurus-preview.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@odo-robot
Copy link

odo-robot bot commented Mar 1, 2023

OpenShift Unauthenticated Tests on commit dcac237 finished successfully.
View logs: TXT HTML

@odo-robot
Copy link

odo-robot bot commented Mar 1, 2023

NoCluster Tests on commit dcac237 finished successfully.
View logs: TXT HTML

@odo-robot
Copy link

odo-robot bot commented Mar 1, 2023

Unit Tests on commit dcac237 finished successfully.
View logs: TXT HTML

@odo-robot
Copy link

odo-robot bot commented Mar 1, 2023

Validate Tests on commit dcac237 finished successfully.
View logs: TXT HTML

@odo-robot
Copy link

odo-robot bot commented Mar 1, 2023

Kubernetes Tests on commit dcac237 finished successfully.
View logs: TXT HTML

@odo-robot
Copy link

odo-robot bot commented Mar 1, 2023

Windows Tests (OCP) on commit dcac237 finished successfully.
View logs: TXT HTML

@odo-robot
Copy link

odo-robot bot commented Mar 1, 2023

OpenShift Tests on commit dcac237 finished successfully.
View logs: TXT HTML

@odo-robot
Copy link

odo-robot bot commented Mar 1, 2023

Kubernetes Docs Tests on commit 8b4ccf5 finished successfully.
View logs: TXT HTML

@rm3l rm3l force-pushed the 6510-ability-to-forward-localhost-on-podman-via-a-side-container branch from 144797b to ef3238a Compare March 2, 2023 09:01
@rm3l rm3l changed the title [WIP] --forward-localhost on Podman Make port-forwarding work on Podman with apps listening on the loopback interface, via a new --forward-localhost flag Mar 2, 2023
@rm3l rm3l marked this pull request as ready for review March 2, 2023 14:51
@openshift-ci openshift-ci bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. Required by Prow. label Mar 2, 2023
@openshift-ci openshift-ci bot requested review from anandrkskd and valaparthvi March 2, 2023 14:52
@rm3l rm3l added kind/feature Categorizes issue as a feature request. For PRs, that means that the PR is the implementation area/dev Issues or PRs related to `odo dev` area/odo-on-podman Issues or PRs related to running odo against Podman labels Mar 2, 2023
@rm3l rm3l requested review from feloy and removed request for anandrkskd March 2, 2023 14:52

// GetAllResourcesFromSelector returns all resources of any kind (including CRs) matching the given label selector
GetAllResourcesFromSelector(selector string, ns string) ([]unstructured.Unstructured, error)
platform.Client
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

@sonarqubecloud
Copy link

sonarqubecloud bot commented Mar 3, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 6 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@openshift-ci openshift-ci bot added the lgtm Indicates that a PR is ready to be merged. Required by Prow. label Mar 6, 2023
@openshift-merge-robot openshift-merge-robot merged commit 29b5a38 into redhat-developer:main Mar 6, 2023
@rm3l rm3l deleted the 6510-ability-to-forward-localhost-on-podman-via-a-side-container branch March 6, 2023 08:30
anandrkskd pushed a commit to anandrkskd/odo that referenced this pull request Mar 7, 2023
…ck interface, via a new `--forward-localhost` flag (redhat-developer#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] redhat-developer#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] redhat-developer#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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/dev Issues or PRs related to `odo dev` area/odo-on-podman Issues or PRs related to running odo against Podman kind/feature Categorizes issue as a feature request. For PRs, that means that the PR is the implementation lgtm Indicates that a PR is ready to be merged. Required by Prow.
Projects
None yet
3 participants