Skip to main content

tfo2kc: Terraform/Terragrunt/openTofu Output 2 KubeConfig: CLI to merge kubeconfig obtained from TF output as string into ~/.kube/config

Project description

tfo2kc: TerraForm Output 2 KubeConfig

A CLI tool which updates ~/.kube/config with relevant cluster, user and context information sourced from single kubeconfig string extracted from output of Terraform-compatible tool (Terragrunt/OpenTofu). It is useful in scenarios where some TF provider bootstraps a Kubernetes cluster and initial admin kubeconfig file is retrievable as output.

Some benefits:

  • single command to prepare terminal to use fresh k8s cluster after TF finishes boostrap: TF -> tfo2kc -> kubectl
  • no need to manage many paths in environmental variable KUBECONFIG - all config entries can be stored in single ~/.kube/config so any tool can consume them
  • automatic rename of cluster, user and context objects, which tend to be all "default" when coming from many common k8s bootstrap tools; this is especially important when merging many kubeconfigs
  • support for any source tool which behaves like terraform output -json
  • automatic backup of ~/.kube/config
  • tfo2kc.ini config stored in TF directory for context dependent translation

Installation

PyPI: tfo2kc

pipx install tfo2kc

Usage

--help

Usage: tfo2kc [OPTIONS]

  tfo2kc: Terraform/Terragrunt/openTofu Output 2 KubeConfig: CLI to merge
  kubeconfig obtained from TF output as string into ~/.kube/config

Options:
  -t, --terraform-binary TEXT  Terraform-compatible CLI (terraform, tofu,
                               terragrunt).
  -o, --output-key TEXT        Terraform output key (dot-path) for the
                               kubeconfig string.
  -c, --cluster-name TEXT      Name under which to store the cluster in
                               kubeconfig.
  -u, --user-name TEXT         Username in kubeconfig (defaults to same as
                               cluster name).
  -x, --context-name TEXT      Context name (defaults to <cluster>-ctx).
  -k, --kubeconfig TEXT        Path to existing kubeconfig file (default
                               ~/.kube/config).
  -d, --tf-dir TEXT            Directory where Terraform/terragrunt/tofu lives
                               (default cwd).
  -f, --config-file TEXT       INI file name or path in TF dir for defaults;
                               default tfo2kc.ini.
  --help                       Show this message and exit.

Direct invocation

tfo2kc -t terraform -o myKubeConfig -c my-cluster -d /path/to/terraform/dir
tfo2kc -t terragrunt -o myKubeConfig -c my-cluster -d /path/to/terraform/dir
tfo2kc -t /path/to/opentofu -o myKubeConfig -c my-cluster -d /path/to/terraform/dir

Running in current directory

tfo2kc -o myKubeConfig -c my-cluster

Running in another directory

tfo2kc -d /path/to/other/dir -o myKubeConfig -c other-cluster

Using an INI file

Place a tfo2kc.ini file alongside your Terraform files:

[default]
terraform_binary = terraform
output_key       = myKubeConfig
cluster_name     = my-cluster
user_name        = my-user
context_name     = my-context
kubeconfig_path  = ~/.kube/config

Then run:

tfo2kc -d /path/to/terraform/dir

Example usage in Terraform

k0s_cluster:

resource "k0s_cluster" "example" {
  # ... cluster setup ...
}

output "myKubeConfig" {
  value     = k0s_cluster.example.kubeconfig
  sensitive = true
}

tool invocation:

tfo2kc -d . -o myKubeConfig -c example-cluster

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tfo2kc-0.2.1.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tfo2kc-0.2.1-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file tfo2kc-0.2.1.tar.gz.

File metadata

  • Download URL: tfo2kc-0.2.1.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Hatch/1.16.5 cpython/3.14.5 HTTPX/0.28.1

File hashes

Hashes for tfo2kc-0.2.1.tar.gz
Algorithm Hash digest
SHA256 9a5a4364a57bc0279ea9cc20372b4ff31e445a7cd268ecbb8a932c70921c1226
MD5 657036b14c846fb706874c21a9f04a3d
BLAKE2b-256 2fd40ecf9ddfe721e8afc35fce1b3d6f80897c93af3ba159c072a636e6f54a7f

See more details on using hashes here.

File details

Details for the file tfo2kc-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: tfo2kc-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Hatch/1.16.5 cpython/3.14.5 HTTPX/0.28.1

File hashes

Hashes for tfo2kc-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7f75b87d1a44c1b72d455bce26aa77fb1e658d27e5c5ec40f169e297a626278c
MD5 2ffd554cbe69aa5a604805aacf60fc2c
BLAKE2b-256 196a05a0e5c96d5a22083668a05922aaba612982135178920077f2a996824cbc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page