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

feat: port forwarding backend part #7379

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

feat: port forwarding backend part #7379

wants to merge 1 commit into from

Conversation

vzhukovs
Copy link
Contributor

@vzhukovs vzhukovs commented May 30, 2024

What does this PR do?

One of the several PR, that is decided to merge separately.

The purpose of this pull request is to provide the server side code, that responsible for enabling port forwarding in a Kubernetes environment. The main part of code can be divided into two logical components: 1) managing user configurations (creation, updating, listing, deletion); 2) starting the forwarding process for a specific configuration.

The configuration management is based on binding to the currently used context. In other words, if a user calls a method to obtain a service KubernetesPortForwardServiceProvider.getService, Kubernetes configuration must be provided as an argument to this service provider. If the service has already been created previously, it will be returned from the local cache to prevent duplication of the service itself.

All user configurations are stored by default in the application's configuration directory in the file port-forwarding.json. This file is essentially a dictionary where the keys are the context names, and the values are the user configurations.

The general principle of using this service is as follows:

  1. In Kubernetes client should be created four methods that interact with the frontend side.
  2. When frontend calls the particular method in Kubernetes client, first thing that does is that Kubernetes config passes to the port forward service provider and retrieves the particular service for the current context.
  3. Calls the particular method from the port forward service.

Screenshot / video of UI

No screenshots.

What issues does this PR fix or reference?

part of #7078

How to test this PR?

To be able to test the PR locally, this branch should be cloned locally and preliminary steps need to be performed:

  1. Created minikube cluster
  2. Applied the following deployment:
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - name: http
          containerPort: 80
        volumeMounts:
        - name: demo-page
          mountPath: /usr/share/nginx/html
      volumes:
      - name: demo-page
        configMap:
          name: demo-page-config

---
apiVersion: v1
kind: ConfigMap
metadata:
  name: demo-page-config
data:
  index.html: |
    <!DOCTYPE html>
    <html>
    <head>
        <title>Welcome to nginx!</title>
        <style>
            body {
                width: 35em;
                margin: 0 auto;
                font-family: Tahoma, Verdana, Arial, sans-serif;
            }
        </style>
    </head>
    <body>
    <h1>Welcome to nginx!</h1>
    <p>If you see this page, the nginx web server is successfully installed and
    working. Further configuration is required.</p>

    <p>For online documentation and support please refer to
    <a href="http://nginx.org/">nginx.org</a>.<br/>
    Commercial support is available at
    <a href="http://nginx.com/">nginx.com</a>.</p>

    <p><em>Thank you for using nginx.</em></p>
    </body>
    </html>
  1. Applied the following service:
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
  - name: http
    protocol: TCP
    port: 80
    targetPort: http
  type: LoadBalancer
  1. Created test file in packages/main/src/plugin, e.g. kubernetes-port-forward.spec.ts with the following content:
import * as k8s from '@kubernetes/client-node';
import { test } from 'vitest';

import type { ForwardConfig, PortMapping } from '/@/plugin/kubernetes-port-forward-model.js';
import { WorkloadKind } from '/@/plugin/kubernetes-port-forward-model.js';
import { KubernetesPortForwardServiceProvider } from '/@/plugin/kubernetes-port-forward-service.js';

test(
  'Port Forward test',
  async () => {
    const kc = new k8s.KubeConfig();
    kc.loadFromDefault();

    const provider = new KubernetesPortForwardServiceProvider();
    const portForwardService = provider.getService(kc);

    const config = {
      name: 'nginx-service',
      namespace: 'default',
      kind: WorkloadKind.SERVICE,
      forwards: [
        {
          localPort: 8892,
          remotePort: 80,
        } as PortMapping,
      ],
    } as ForwardConfig;

    const disposable = await portForwardService.startForward(config);
    await pauseForFiveMinutes();
    disposable.dispose();
  },
  5 * 60 * 1000,
);

function pauseForFiveMinutes(): Promise<void> {
  return new Promise(resolve => {
    setTimeout(resolve, 5 * 60 * 1000);
  });
}
  1. Run the following test, it will run for a five minutes.
  2. Check that URL http://localhost:8892 provides the content of page.
  3. Stop the test.
  4. Check that http://localhost:8892 no more reachable.
  • Tests are covering the bug fix or the new feature

Signed-off-by: Vladyslav Zhukovskyi <[email protected]>
@vzhukovs vzhukovs requested review from benoitf and a team as code owners May 30, 2024 12:07
@vzhukovs vzhukovs requested review from feloy and axel7083 and removed request for a team May 30, 2024 12:07
@vzhukovs vzhukovs self-assigned this May 30, 2024
@vzhukovs vzhukovs added kind/enhancement ✨ Issue for requesting an improvement area/kubernetes ☸️ labels May 30, 2024
@vzhukovs vzhukovs linked an issue May 30, 2024 that may be closed by this pull request
4 tasks
@vzhukovs vzhukovs requested a review from cdrage May 30, 2024 12:27
@vzhukovs
Copy link
Contributor Author

Some unit tests need to be updated to be run on Windows. Will provide updates for this ASAP.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/kubernetes ☸️ kind/enhancement ✨ Issue for requesting an improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Ability to configure port forwarding in kubernetes environment
1 participant