k8s

package module
v0.0.0-...-ab74ee0 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2024 License: Apache-2.0 Imports: 22 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Debug = true

Functions

func Do

func Do(ctx context.Context, kr *rest.Request) rest.Result

func Is404

func Is404(err error) bool

func OAuth2RoundTripper

func OAuth2RoundTripper(rt http.RoundTripper, creds oauth2.TokenSource) http.RoundTripper

Return a wrapper round tripper.

func RegisterTokenProvider

func RegisterTokenProvider(name string, creds oauth2.TokenSource)

Register an oauth2 token source. This takes a dep on the oauth2 library, but client already depends on it. Alternative: set WrapTransport directly on the rest.Config.

func SetK8SLogging

func SetK8SLogging(flags string)

Init klog.InitFlags from an env (to avoid messing with the CLI of the app). For example -v=9 lists full request content, -v=7 lists requests headers

Types

type K8S

type K8S struct {
	Config *K8SConfig

	TokenProvider interface{}

	// InCluster (if possible), followed by LoadKubeConfig or GKE config.
	Default *K8SCluster

	// LoadKubeConfig will populate this from a kubeconfig file
	ByName map[string]*K8SCluster
}

K8S implements the common interface for a set of K8S APIservers or servers implementing same patterns.

func NewK8S

func NewK8S(kc *K8SConfig) *K8S

NewK8S will initialize a K8S cluster set.

If running in cluster, the 'local' cluster will be the default. Additional clusters can be loaded from istio kubeconfig files, kubeconfig, GKE, Fleet.

func (*K8S) GetCM

func (kr *K8S) GetCM(ctx context.Context, ns string, name string) (map[string]string, error)

func (*K8S) GetSecret

func (kr *K8S) GetSecret(ctx context.Context, ns string, name string) (map[string][]byte, error)

func (*K8S) GetToken

func (kr *K8S) GetToken(ctx context.Context, aud string) (string, error)

GetToken returns a token with the given audience for the current KSA, using CreateToken request. Used by the STS token exchanger.

func (*K8S) InitK8SClient

func (kr *K8S) InitK8SClient(ctx context.Context) error

InitK8SClient will discover a K8S config cluster and return the client.

- KUBE_CONFIG takes priority, is checked first - in cluster is probed if KUBE_CONFIG is missing.

Istio Server.initKubeClient handles it for Istio: - FileDir fakes it using files (config controller) - local MeshConfig from args is read - if no configSources or CLI kubeconfig - use it.

func (*K8S) LoadKubeConfig

func (kr *K8S) LoadKubeConfig(kc string) error

LoadConfig gets the default k8s client, using environment variables to decide how:

  • KUBECONFIG or $HOME/.kube/config will be tried first
  • GKE is checked - using env or metadata server to get PROJECT_ID, CLUSTER_LOCATION, CLUSTER_NAME (if not set), and construct a kube config to use.
  • (in future other vendor-specific methods may be added)
  • finally in-cluster will be checked.

Once the cluster is found, additional config can be loaded from the cluster.

error is set if KUBECONFIG is set or ~/.kube/config exists and fail to load. If the file doesn't exist, err is nil.

func (*K8S) LocalCluster

func (kr *K8S) LocalCluster() error

LocalCluster returns a cluster determined based on in-cluster or MDS config. The extended MDS server is used to cache cluster info to avoid GKE lookups. Equivalent to rest.InClusterConfig.

type K8SCluster

type K8SCluster struct {
	// Loaded Config.
	// The URL can be extracted with rest.DefaultServerURLFor(Config)
	// Http client properly configured with rest.HTTPClientFor(Config)
	Config *rest.Config

	// The name should be mangled - gke_PROJECT_LOCATION_NAME or connectgateway_PROJECT_NAME
	// or hostname.
	// Best practice: fleet name, also part of the domain suffix
	// Using the VENDOR_PROJECT_REGION_NAME for all would also be nice.
	Name string

	Namespace string
	KSA       string

	// TODO: lazy load. Should be cached.
	Client *kubernetes.Clientset
}

K8SCluster represents a single K8S cluster

func (*K8SCluster) ConfigFor

func (kr *K8SCluster) ConfigFor(apiPath, version string, group string,
	c runtime.NegotiatedSerializer) *rest.Config

func (*K8SCluster) GcpInfo

func (k *K8SCluster) GcpInfo() (string, string, string)

func (*K8SCluster) GetCM

func (kr *K8SCluster) GetCM(ctx context.Context, ns string, name string) (map[string]string, error)

func (*K8SCluster) GetSecret

func (kr *K8SCluster) GetSecret(ctx context.Context, ns string, name string) (map[string][]byte, error)

func (*K8SCluster) GetToken

func (k *K8SCluster) GetToken(ctx context.Context, aud string) (string, error)

func (*K8SCluster) GetTokenRaw

func (k *K8SCluster) GetTokenRaw(ctx context.Context,
	ns, ksa, aud string) (string, error)

func (*K8SCluster) InitConfig

func (kr *K8SCluster) InitConfig(config *rest.Config) error

func (*K8SCluster) RestClient

func (kr *K8SCluster) RestClient(apiPath, version string, group string,
	c runtime.NegotiatedSerializer) (*rest.RESTClient, error)

RestClient returns a K8S RESTClient for a specific resource. apiPath is typically /api or /apis version is v1, etc group is "" for core resources. Serializer defaults to scheme.Codecs.WithoutConversion()

func (*K8SCluster) WithNamespace

func (kr *K8SCluster) WithNamespace(ns, n string) *K8SCluster

type K8SConfig

type K8SConfig struct {
	// Logging options for K8S. Will be set in klog.
	Logging string

	Namespace string

	KSA string
	GSA string
}

K8SConfig has general config for a set of clusters.

type MDSRoundTripper

type MDSRoundTripper struct {
	// contains filtered or unexported fields
}

Round-tripper adding tokens from an oauth2 source - including MDS server.

func (*MDSRoundTripper) RoundTrip

func (m *MDSRoundTripper) RoundTrip(request *http.Request) (*http.Response, error)

Jump to

Keyboard shortcuts

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