Client Configuration
Configuring the client library to connect to a Kubernetes cluster is essential for any application that needs to interact with the API. ^1 This outline describes the available configuration options and how to use them effectively.
Client Configuration Options
- In-cluster Authentication: Configure a client running inside the Kubernetes cluster by using the Service Account token mounted at
/var/run/secrets/kubernetes.io/serviceaccount
. ^2 - Out-of-cluster Authentication: Configure a client to access a Kubernetes cluster from outside. ^2 This can be done by using the
kubeconfig
file that contains context information about the cluster. ^3 - Fake Client: Use a fake client in tests. ^4
- Managing Resources with API: Create, get, update, or delete a Deployment resource using the dynamic package. ^5
Auth Plugins
Several plugins are available for obtaining credentials from external sources. To enable these plugins in your program, import them in your main package. ^1
- All Auth Plugins:
import _ "k8s.io/client-go/plugin/pkg/client/auth"
- Specific Auth Plugins:
import _ "k8s.io/client-go/plugin/pkg/client/auth/azure"
import _ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
import _ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
Client Configuration API
The ClientConfig
interface provides methods for retrieving and managing configuration settings. ^6
// ClientConfig is used to make it easy to get an api server client
type ClientConfig interface {
// RawConfig returns the merged result of all overrides
RawConfig() (clientcmdapi.Config, error)
// ClientConfig returns a complete client config
ClientConfig() (*restclient.Config, error)
// Namespace returns the namespace resulting from the merged
// result of all overrides and a boolean indicating if it was
// overridden
Namespace() (string, bool, error)
// ConfigAccess returns the rules for loading/persisting the config.
ConfigAccess() ConfigAccess
}
Configuration Access
The ConfigAccess
interface is used to access and persist client configuration settings. ^7
// ConfigAccess implements ClientConfig
func (config *DirectClientConfig) ConfigAccess() ConfigAccess {
return config.configAccess
}
Direct Client Configuration
The DirectClientConfig
struct represents a client configuration that is backed by a clientcmdapi.Config
and overrides. ^8
// DirectClientConfig is a ClientConfig interface that is backed by a clientcmdapi.Config, options overrides, and an optional fallbackReader for auth information
type DirectClientConfig struct {
config clientcmdapi.Config
contextName string
overrides *ConfigOverrides
fallbackReader io.Reader
configAccess ConfigAccess
// promptedCredentials store the credentials input by the user
promptedCredentials promptedCredentials
}
Client Configuration Loading Rules
The ClientConfigLoadingRules
struct defines the rules for loading and persisting configuration settings. ^9
// GetStartingConfig implements ConfigAccess
func (rules *ClientConfigLoadingRules) GetStartingConfig() (*clientcmdapi.Config, error) {
clientConfig := NewNonInteractiveDeferredLoadingClientConfig(rules, &ConfigOverrides{})
rawConfig, err := clientConfig.RawConfig()
if os.IsNotExist(err) {
return clientcmdapi.NewConfig(), nil
}
if err != nil {
return nil, err
}
return &rawConfig, nil
}
References
Top-Level Directory Explanations
applyconfigurations/ - This directory contains examples and tests for applying Kubernetes configurations using the client-go library.
discovery/ - This directory contains code related to service discovery in Kubernetes.
dynamic/ - This directory contains code for working with dynamic resources in the Kubernetes API.
examples/ - This directory contains example usage of the client-go library.
gentype/ - This directory contains generated Go types for the Kubernetes API.
informers/ - This directory contains code for caching Kubernetes resources using informers.
kubernetes/ - This directory contains the main package for the client-go library, which provides types and functions for interacting with the Kubernetes API.
listers/ - This directory contains interfaces and implementations for listing Kubernetes resources.
metadata/ - This directory contains code related to metadata in Kubernetes.
openapi/ - This directory contains OpenAPI definitions for the Kubernetes API.
pkg/ - This directory contains compiled Go packages for the client-go library.
plugin/ - This directory contains code for loading plugins for the client-go library.
rest/ - This directory contains code for working with the REST API in the client-go library.
scale/ - This directory contains code for working with scale and autoscaling in Kubernetes.
tools/ - This directory contains various tools for working with the client-go library.
util/ - This directory contains utility functions for the client-go library.