Giter VIP home page Giter VIP logo

k8sfwd's Introduction

k8s:fwd — config-based kubernetes multi-port forwarding

A tool for handling port-forwards to multiple services and across multiple clusters.

For Linux (GNU and musl) you can download pre-built binaries from the Releases page. For other platforms the setup is currently based on cargo until platform-specific binaries can be provided. To manually build and install the latest version for your system (or update to it), run:

cargo install k8sfwd

Please note that the application internally relies on kubectl, so it needs to be present in your path. If kubectl is not on your path, you may specify it via the --kubectl argument or the KUBECTL_PATH environment variable.

Depending on your configuration, you'll be greeted with something along the lines of:

██╗░░██╗░█████╗░░██████╗░░░░░███████╗██╗░░░░░░░██╗██████╗
██║░██╔╝██╔══██╗██╔════╝░██╗░██╔════╝██║░░██╗░░██║██╔══██╗
█████═╝░╚█████╔╝╚█████╗░░╚═╝░█████╗░░╚██╗████╗██╔╝██║░░██║
██╔═██╗░██╔══██╗░╚═══██╗░██╗░██╔══╝░░░████╔═████║░██║░░██║
██║░╚██╗╚█████╔╝██████╔╝░╚═╝░██║░░░░░░╚██╔╝░╚██╔╝░██████╔╝
╚═╝░░╚═╝░╚════╝░╚═════╝░░░░░░╚═╝░░░░░░░╚═╝░░░╚═╝░░╚═════╝
k8s:fwd 0.3.0 - a Kubernetes multi-cluster port forwarder
Using kubectl version v1.24.12-dispatcher
Using config from 2 locations

Forwarding to the following targets:
#0 Items API (Staging)
   target:  service/foo.test-api
   context: (default)
   cluster: (default)
#1 Items API (Production)
   target:  pod/foo-59b58f5d68-6t6bh.test-api
   context: (default)
   cluster: production

Spawning child processes:
#0: Error from server (NotFound): pods "foo-59b58f5d68-6t6bh" not found
#0: Process exited with exit status: 1 - will retry in 5 sec
#1: Forwarding from 127.0.0.1:5012 -> 80
#1: Forwarding from 127.0.0.1:46737 -> 8080
#1: Forwarding from [::1]:5012 -> 80
#1: Forwarding from [::1]:46737 -> 8080
#0: Error from server (NotFound): pods "foo-59b58f5d68-6t6bh" not found
#0: Process exited with exit status: 1 - will retry in 5 sec

Command-Line Options

Filters

Targets can be selected through prefix filters specified on the command-line. Only targets (and target names) starting with the specified prefixes will be forwarded. In the following example, services starting with foo and bar will be selected:

k8sfwd foo bar

Filters can operate in combination with tags as well:

k8sfwd -t test foo bar

Tags

Targets can be labeled with tags. When k8sfwd is started with one or more space-separated --tags parameters, targets are filtered down to match the selection. If multiple values are specified (e.g. --tags foo bar), any matching tag results in the target being selected. If two tags are combined with a plus sign (e.g. --tags foo+bar) only targets matching both tags are selected.

Target tags --tags argument Selected
(none) (none) ✅ yes
(none) --tags some ❌ no
["foo", "bar", "baz"] (none) ✅ yes
["foo", "bar", "baz"] --tags fubar ❌ no
["foo", "bar", "baz"] --tags foo bar ✅ yes
["foo", "bar", "baz"] --tags bar ✅ yes
["foo", "bar", "baz"] --tags foo+baz ✅ yes
["foo", "bar", "baz"] --tags foo+fubar ❌ no
["foo", "bar", "baz"] --tags foo+baz fubar ✅ yes
["fubar"] --tags foo+baz fubar ✅ yes

Configuration

The configuration is provided as a YAML file.

  • If one or more files are specified on program launch via the --file argument(s), their configuration is loaded.
  • If no configuration file is specified, k8sfwd will recursively look for a .k8sfwd file in
    • the current directory hierarchy,
    • your home directory and
    • your configuration directory, in that order.

Non-target configuration (e.g., retry delays) are always loaded from the hierarchy stated above regardless of whether a --file argument is present. However, all target configuration that is not directly specified through a file pointed to by the --file argument is ignored.

See k8sfwd-example.yaml for an example.

---
version: 0.2.0
config:
  # Optional: Number of seconds to wait before attempting to re-establish
  # a broken connection.
  retry_delay_sec: 5.0
targets:
  - name: Test API (Staging)    # Optional, for display purposes.
    target: foo                 # The name of the resource to forward to.
    tags:                       # Optional, for use with `--tags <tag1> <tag2>+<tag3>`
      - integration
    type: service               # Can be service, deployment or pod.
    namespace: bar              # The namespace of the resource.
    context: null               # Optional; will default to current context.
    cluster: null               # Optional; will default to current cluster.
    ports:
      - "5012:80"               # Forward resource port 80 to local port 5012.
      - "8080"                  # Forward resource port 8080 to random local port. 
  - name: Test API (Production)
    target: foo-59b58f5d68-6t6bh
    type: pod
    namespace: bar
    cluster: production
    listen_addrs:               # Select the listen addresses; defaults to `localhost`.
      - "127.1.0.1"
    ports:
      - "5012:80"

k8sfwd's People

Contributors

pophilpo avatar sunsided avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

pophilpo

k8sfwd's Issues

Provide an option for generating an example configuration

There is a plan for providing filtering from the command arguments, so a subcommand might be a suboptimal route. Maybe something like --print-example-config would do as well, only printing it to stdout and then exiting with a success code.

Allow mapping of cluster and context names to other values

Port-forward configuration files could be checked into VCS. Given that the config file allows for the specification of cluster and context values that might not be identical across different machines, it could be useful to allow local overrides.

When a file name is specified locally, we could still read the .k8sfwd file(s) up the hierarchy and retain their configuration as defaults.

Program should stop retrying port-forwarding on Exit Code 1

The current implementation retries port-forwarding regardless of the exit code returned by the kubectl port-forward command. This behavior leads to unnecessary retries, especially when the exit code is 1, which indicates that the kubectl command itself was successful but the port-forwarding operation failed due to permissions or other non-retryable reasons.

If the exit code for the kubectl port-forward command is 1, the program should stop retrying and possibly alert the user, as this is typically a non-recoverable issue.

Watch configuration file for changes

Especially when using a pod based forward, restarts of the pod will require a stop-edit-restart cycle of the configuration file.

By allowing to watch all relevant (see e.g. #1, #2) configuration files for changes, only the configuration parts and their child processes that actually did change require a restart.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.