Quickstart

After Installing kubeconfig, you are ready to start working with your configs.

Reading your Config

We’ll start by reading your current configs, with the result being returned as a dict:

from kubeconfig import KubeConfig

conf = KubeConfig()
print(conf.view())

Since we’re just calling kubectl under the covers, your KUBECONFIG environment variable will be referenced, and all kubeconfig merging rules will apply.

If you want to read a specific kubeconfig, you can pass that in as well:

conf = KubeConfig('path-to-your-config')

Creating or modifying credentials

Use KubeConfig.set_credentials to create or modify credentials:

from kubeconfig import KubeConfig

conf = KubeConfig()
conf.set_credentials(name='my-user, token='super-secret-token')

Creating or modifying a cluster

Use KubeConfig.set_cluster to create or modify clusters:

from kubeconfig import KubeConfig

conf = KubeConfig()
conf.set_cluster(
    name='my-cluster,
    server='https://my-k8s-api-server.xxx/'
    certificate_authority='/path/to/ca.crt',
)

Creating or modifying a context

Use KubeConfig.set_cluster to create or modify contexts:

from kubeconfig import KubeConfig

conf = KubeConfig()
conf.set_context(
    name='my-context,
    cluster='my-cluster'
    user='my-user',
)

Changing your current context

If you’d like to switch to another context in your config file, this is done via KubeConfig.use_context:

from kubeconfig import KubeConfig

conf = KubeConfig()
conf_doc = conf.view()
print('Current context:', conf.current_context())
conf.use_context('new-context')
# Re-read the config.
conf_doc = conf.view()
print('Current context:', conf.current_context())