kubeconfig

package
v0.0.0-...-2dfab7d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 12, 2024 License: Apache-2.0, Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Copyright 2021 Jetify Inc. Copyright 2014 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.

You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

This file has been modified by Jetify Inc. to:

  • Support different ClientConfigs, depending on whether the kubeconfig is being provided via a file, in-cluster, or the contents are passed in directly.
  • The RESTClientGetter interface has been generalized to a ClientBuilder interface that also has helper methods for creating Dynamic Clients and Clientsets.

The original code is extracted from: https://github.com/kubernetes/cli-runtime/blob/master/pkg/genericclioptions/config_flags.go

Package kubeconfig makes it easy to create K8s clients from kubeconfig files.

There are two ways to pass a kubeconfig file. Either the contexts of the kubeconfig file can be passed directly:

```
kubeconfig.FromData(kubeconfigContents).ToDynamicClient()
```

Or you can read the kubeconfig file from a path, optionally specifying a kube-context and namespace:

```
args := kubeconfig.Args {
  KubeconfigPath: "path/to/kubeconfig"
  Context:        "my-default-cluster"
  Namespace:      "restrict-to-this-namespace"
}
kubeconfig.FromArgs(args).ToDynamicClient()
```

The default constructor follows similar rules to kubectl. It looks for a kubeconfig file in your the path specified by $KUBECONFIG or your home directory "${HOME}/.kube/config" and defaults to the currently selected kube-context. If no kubeconfig exists in those paths, it will attempt to load credentials from the current cluster (if the binary is running inside of a k8s cluster)

```
kubeconfig.FromDefaults().ToDynamicClient()
```

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InClusterPossible

func InClusterPossible() bool

InClusterPossible determines whether it's possible to configure an in-cluster client using the service account information.

func NewClientBuilder

func NewClientBuilder(opts ...Option) *clientbuilder

func NewInClusterClientConfig

func NewInClusterClientConfig(overrides *clientcmd.ConfigOverrides) clientcmd.ClientConfig

Types

type BoolFlag

type BoolFlag *bool

Convenience type so that we can distinguish when the flag has been set vs not set.

func Bool

func Bool(value bool) BoolFlag

type ClientBuilder

type ClientBuilder interface {
	ToRESTConfig() (*rest.Config, error)
	ToDiscoveryClient() (discovery.CachedDiscoveryInterface, error)
	ToRESTMapper() (meta.RESTMapper, error)
	ToRawClientConfig() (clientcmd.ClientConfig, error) // Changed from genercclioptions, means interfaces don't match perfectly.
	ToDynamicClient() (dynamic.Interface, error)
	ToClientset() (*kubernetes.Clientset, error)
	ToRawKubeConfigLoader() clientcmd.ClientConfig
}

TODO conform to pkg/cmd/util/Factory TODO rename to ClientFactory

func FromCurrentCluster

func FromCurrentCluster() ClientBuilder

func FromDefaultFile

func FromDefaultFile() ClientBuilder

func FromDefaults

func FromDefaults() ClientBuilder

func FromFile

func FromFile(path string) ClientBuilder

func FromYAML

func FromYAML(yaml string) ClientBuilder

type DefaultLoader

type DefaultLoader struct{}

DefaultLoader loads the kubeconfig from the current cluster if its running inside of a cluster. If its not running in a cluster it looks for a kubeconfig in the default file paths following the same logic as FilePathLoader.

type FilePathLoader

type FilePathLoader struct {
	Path string // Path to the kubeconfig file to use. Leave empty to use default paths
}

FilePathLoader loads kubeconfig files from the filesystem. It searches for a kubeconfig file in the following order: + If `Path` is set, it looks for a kubeconfig at that path. + The list of paths specified in the `$KUBECONFIG` env variable. + ${HOME}/.kube/config

type Flags

type Flags struct {
	CacheDir    string   // Default cache directory
	ReuseConfig BoolFlag // If true, caches the config file on load and re-uses it. Instead of loading repeatedly.

	// Flags – all the flags below map to a corresponding `kubectl` flag.
	ClusterName      string   // The name of the kubeconfig cluster to use
	AuthInfoName     string   // The name of the kubeconfig user to use
	Context          string   // The name of the kubeconfig context to use
	Namespace        string   // If present, the namespace scope for requests from this client.
	APIServer        string   // The address and port of the Kubernetes API server
	TLSServerName    string   // Server name to use for server certificate validation. If it is not provided, the hostname used to contact the server is used
	Insecure         BoolFlag // If true, the server's certificate will not be checked for validity. This will make your HTTPS connections insecure"
	CertFile         string   // Path to a client certificate file for TLS
	KeyFile          string   // Path to a client key file for TLS
	CAFile           string   // Path to a cert file for the certificate authority
	BearerToken      string   // Bearer token for authentication to the API server
	Impersonate      string   // Username to impersonate for the operation
	ImpersonateGroup []string // Groups to impersonate for the operation.
	Username         string   // Username for basic authentication to the API server
	Password         string   // Password for basic authentication to the API server
	Timeout          string   // The length of time to wait before giving up on a single server request. Non-zero values should contain a corresponding time unit (e.g. 1s, 2m, 3h). A value of zero means don't timeout requests.

	// If non-nil, wrap config function can transform the Config
	// before it is returned in ToRESTConfig function.
	WrapConfigFn func(*rest.Config) *rest.Config
}

func DefaultFlags

func DefaultFlags() *Flags

func (*Flags) ToOverrides

func (f *Flags) ToOverrides() *clientcmd.ConfigOverrides

type InClusterLoader

type InClusterLoader struct{}

InClusterLoader loads the kubeconfig file from the service account in the current cluster. The program must be running inside a k8s cluster for this to work.

type Loader

type Loader interface {
	// contains filtered or unexported methods
}

Loader loads a kubeconfig and returns the corresponding ClientConfig object. Different implementations load from different sources.

type Option

type Option func(*clientbuilder)

func WithFlags

func WithFlags(flags *Flags) Option

func WithLoader

func WithLoader(loader Loader) Option

type YamlLoader

type YamlLoader struct {
	YAML string // YAML *contents* from a kubeconfig file
}

YamlLoader uses the provided kubeconfig YAML contents and does *not* rely on a file.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL