binbashar / leverage

Binbash Leverage CLI intended to orchestrate Leverage Reference Architecture for AWS (www.binbash.co/leverage)
https://pypi.org/project/leverage/
Apache License 2.0
17 stars 2 forks source link

FEATURE | map a port from Toolbox container to host #273

Open juanmatias opened 3 months ago

juanmatias commented 3 months ago

Describe the Feature

It would be useful to bind a container's port to the host to allow port forwarding with the kubectl Leverage subcommand.

Expected Behavior

Be able to map a configurable port to the host.

Use Case

Port forwarding from an EKS cluster:

leverage kubectl shell --port "8080:8080"
$ kubectl port-forward -n namespace svcname 8080:1234

Describe Ideal Solution

I don't know about ideals...

Alternatives Considered

I'm using a custom Leverage version with this hardcoded in leverage/container.py file, line 93:

        ports = [8080]
        port_bindings = {8080:8080}
        if env_vars is not None and 'ports' in env_vars:
            for port in env_vars['ports'].split(','):
                port_origin = port.split(':')[0]
                port_target = port.split(':')[1]
                port_bindings[port_origin] = port_target
                ports.append(port_origin)

        mounts = [Mount(source=source, target=target, type="bind") for source, target in mounts] if mounts else []
        self.host_config = self.client.api.create_host_config(security_opt=["label:disable"], mounts=mounts, port_bindings=port_bindings)
        self.container_config = {
            "image": f"{self.image}:{self.image_tag}",
            "command": "",
            "stdin_open": True,
            "environment": env_vars or {},
            "entrypoint": "",
            "working_dir": f"{self.paths.guest_base_path}/{self.paths.cwd.relative_to(self.paths.root_dir).as_posix()}",
            "host_config": self.host_config,
            "ports": ports
        }

But it is a good idea to make this configurable and add some try/catch login in case port is busy.