client

package
v0.32.7 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2024 License: Apache-2.0, Apache-2.0 Imports: 31 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// NA Not available.
	NA = "n/a"

	// NamespaceAll designates the fictional all namespace.
	NamespaceAll = "all"

	// BlankNamespace designates no namespace.
	BlankNamespace = ""

	// DefaultNamespace designates the default namespace.
	DefaultNamespace = "default"

	// ClusterScope designates a resource is not namespaced.
	ClusterScope = "-"

	// NotNamespaced designates a non resource namespace.
	NotNamespaced = "*"

	// CreateVerb represents create access on a resource.
	CreateVerb = "create"

	// UpdateVerb represents an update access on a resource.
	UpdateVerb = "update"

	// PatchVerb represents a patch access on a resource.
	PatchVerb = "patch"

	// DeleteVerb represents a delete access on a resource.
	DeleteVerb = "delete"

	// GetVerb represents a get access on a resource.
	GetVerb = "get"

	// ListVerb represents a list access on a resource.
	ListVerb = "list"

	// WatchVerb represents a watch access on a resource.
	WatchVerb = "watch"
)
View Source
const MegaByte = 1024 * 1024

MegaByte represents a megabyte.

View Source
const (

	// UsePersistentConfig caches client config to avoid reloads.
	UsePersistentConfig = true
)

Variables

View Source
var (
	// PatchAccess patch a resource.
	PatchAccess = []string{PatchVerb}

	// GetAccess reads a resource.
	GetAccess = []string{GetVerb}

	// ListAccess list resources.
	ListAccess = []string{ListVerb}

	// MonitorAccess monitors a collection of resources.
	MonitorAccess = []string{ListVerb, WatchVerb}

	// ReadAllAccess represents an all read access to a resource.
	ReadAllAccess = []string{GetVerb, ListVerb, WatchVerb}
)
View Source
var NoGVR = GVR{}

Functions

func Can

func Can(verbs []string, v string) bool

Can determines the available actions for a given resource.

func CleanseNamespace

func CleanseNamespace(ns string) string

CleanseNamespace ensures all ns maps to blank.

func CoFQN added in v0.24.11

func CoFQN(m metav1.ObjectMeta, co string) string

CoFQN returns a fully qualified container name.

func FQN

func FQN(ns, n string) string

FQN returns a fully qualified resource name.

func IsAllNamespace

func IsAllNamespace(ns string) bool

IsAllNamespace returns true if ns == all.

func IsAllNamespaces

func IsAllNamespaces(ns string) bool

IsAllNamespaces returns true if all namespaces, false otherwise.

func IsClusterScoped

func IsClusterScoped(ns string) bool

IsClusterScoped returns true if resource is not namespaced.

func IsClusterWide

func IsClusterWide(ns string) bool

IsClusterWide returns true if ns designates cluster scope, false otherwise.

func IsNamespaced

func IsNamespaced(ns string) bool

IsNamespaced returns true if a specific ns is given.

func MetaFQN

func MetaFQN(m metav1.ObjectMeta) string

MetaFQN returns a fully qualified resource name.

func Namespaced

func Namespaced(p string) (string, string)

Namespaced converts a resource path to namespace and resource name.

func ResetMetrics added in v0.16.0

func ResetMetrics()

ResetMetrics resets the metric server handle.

func ToMB added in v0.16.0

func ToMB(v int64) int64

ToMB converts bytes to megabytes.

func ToPercentage added in v0.17.4

func ToPercentage(v1, v2 int64) int

ToPercentage computes percentage as string otherwise n/aa.

func ToPercentageStr added in v0.19.5

func ToPercentageStr(v1, v2 int64) string

ToPercentageStr computes percentage, but if v2 is 0, it will return NAValue instead of 0.

Types

type APIClient

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

APIClient represents a Kubernetes api client.

func InitConnection added in v0.20.0

func InitConnection(config *Config) (*APIClient, error)

InitConnection initialize connection from command line args. Checks for connectivity with the api server.

func NewTestAPIClient added in v0.23.5

func NewTestAPIClient() *APIClient

NewTestAPIClient for testing ONLY!!

func (*APIClient) ActiveContext added in v0.30.0

func (a *APIClient) ActiveContext() string

ActiveContext returns the current context name.

func (*APIClient) ActiveNamespace added in v0.19.0

func (a *APIClient) ActiveNamespace() string

ActiveNamespace returns the current namespace.

func (*APIClient) CachedDiscovery added in v0.20.0

func (a *APIClient) CachedDiscovery() (*disk.CachedDiscoveryClient, error)

CachedDiscovery returns a cached discovery client.

func (*APIClient) CanI

func (a *APIClient) CanI(ns, gvr, name string, verbs []string) (auth bool, err error)

CanI checks if user has access to a certain resource.

func (*APIClient) CheckConnectivity added in v0.13.4

func (a *APIClient) CheckConnectivity() bool

CheckConnectivity return true if api server is cool or false otherwise.

func (*APIClient) Config

func (a *APIClient) Config() *Config

Config return a kubernetes configuration.

func (*APIClient) ConnectionOK added in v0.20.0

func (a *APIClient) ConnectionOK() bool

ConnectionOK returns connection status.

func (*APIClient) CurrentNamespaceName

func (a *APIClient) CurrentNamespaceName() (string, error)

CurrentNamespaceName return namespace name set via either cli arg or cluster config.

func (*APIClient) Dial added in v0.20.0

func (a *APIClient) Dial() (kubernetes.Interface, error)

Dial returns a handle to api server or die.

func (*APIClient) DialLogs added in v0.25.13

func (a *APIClient) DialLogs() (kubernetes.Interface, error)

DialLogs returns a handle to api server for logs.

func (*APIClient) DynDial added in v0.20.0

func (a *APIClient) DynDial() (dynamic.Interface, error)

DynDial returns a handle to a dynamic interface.

func (*APIClient) HasMetrics

func (a *APIClient) HasMetrics() bool

HasMetrics checks if the cluster supports metrics.

func (*APIClient) IsActiveNamespace added in v0.19.0

func (a *APIClient) IsActiveNamespace(ns string) bool

IsActiveNamespace returns true if namespaces matches.

func (*APIClient) IsValidNamespace added in v0.30.0

func (a *APIClient) IsValidNamespace(ns string) bool

func (*APIClient) MXDial

func (a *APIClient) MXDial() (*versioned.Clientset, error)

MXDial returns a handle to the metrics server.

func (*APIClient) RestConfig added in v0.20.0

func (a *APIClient) RestConfig() (*restclient.Config, error)

RestConfig returns a rest api client.

func (*APIClient) ServerVersion

func (a *APIClient) ServerVersion() (*version.Info, error)

ServerVersion returns the current server version info.

func (*APIClient) SwitchContext added in v0.14.1

func (a *APIClient) SwitchContext(name string) error

SwitchContext handles kubeconfig context switches.

func (*APIClient) ValidNamespaceNames added in v0.30.0

func (a *APIClient) ValidNamespaceNames() (NamespaceNames, error)

ValidNamespaceNames returns all available namespaces.

type Authorizer

type Authorizer interface {
	// CanI returns true if the user can use these actions for a given resource.
	CanI(ns, gvr, n string, verbs []string) (bool, error)
}

Authorizer checks what a user can or cannot do to a resource.

type ClusterMetrics

type ClusterMetrics struct {
	PercCPU, PercMEM, PercEphemeral int
}

ClusterMetrics summarizes total node metrics as percentages.

type Config

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

Config tracks a kubernetes configuration.

func NewConfig

func NewConfig(f *genericclioptions.ConfigFlags) *Config

NewConfig returns a new k8s config or an error if the flags are invalid.

func (*Config) CallTimeout added in v0.21.0

func (c *Config) CallTimeout() time.Duration

CallTimeout returns the call timeout if set or the default if not set.

func (*Config) Clone added in v0.31.8

func (c *Config) Clone(ns string) (*genericclioptions.ConfigFlags, error)

func (*Config) ConfigAccess

func (c *Config) ConfigAccess() (clientcmd.ConfigAccess, error)

ConfigAccess return the current kubeconfig api server access configuration.

func (*Config) ContextNames

func (c *Config) ContextNames() (map[string]struct{}, error)

ContextNames fetch all available contexts.

func (*Config) Contexts

func (c *Config) Contexts() (map[string]*api.Context, error)

Contexts fetch all available contexts.

func (*Config) CurrentClusterName

func (c *Config) CurrentClusterName() (string, error)

CurrentClusterName returns the currently active cluster name.

func (*Config) CurrentContext added in v0.30.0

func (c *Config) CurrentContext() (*api.Context, error)

CurrentContext returns the current context configuration.

func (*Config) CurrentContextName

func (c *Config) CurrentContextName() (string, error)

CurrentContextName returns the currently active config context.

func (*Config) CurrentContextNamespace added in v0.29.0

func (c *Config) CurrentContextNamespace() (string, error)

func (*Config) CurrentGroupNames

func (c *Config) CurrentGroupNames() ([]string, error)

CurrentGroupNames retrieves the active group names.

func (*Config) CurrentNamespaceName

func (c *Config) CurrentNamespaceName() (string, error)

CurrentNamespaceName retrieves the active namespace.

func (*Config) CurrentUserName

func (c *Config) CurrentUserName() (string, error)

CurrentUserName retrieves the active user name.

func (*Config) DelContext

func (c *Config) DelContext(n string) error

DelContext remove a given context from the configuration.

func (*Config) Flags

func (c *Config) Flags() *genericclioptions.ConfigFlags

Flags returns configuration flags.

func (*Config) GetContext

func (c *Config) GetContext(n string) (*api.Context, error)

GetContext fetch a given context or error if it does not exists.

func (*Config) ImpersonateGroups added in v0.16.0

func (c *Config) ImpersonateGroups() (string, error)

ImpersonateGroups retrieves the active groups if set on the CLI.

func (*Config) ImpersonateUser added in v0.16.0

func (c *Config) ImpersonateUser() (string, error)

ImpersonateUser retrieves the active user name if set on the CLI.

func (*Config) RESTConfig

func (c *Config) RESTConfig() (*restclient.Config, error)

func (*Config) RawConfig

func (c *Config) RawConfig() (api.Config, error)

func (*Config) RenameContext added in v0.27.4

func (c *Config) RenameContext(old string, new string) error

RenameContext renames a context.

func (*Config) SwitchContext

func (c *Config) SwitchContext(name string) error

SwitchContext changes the kubeconfig context to a new cluster.

type Connection

type Connection interface {
	Authorizer

	// Config returns current config.
	Config() *Config

	// ConnectionOK checks api server connection status.
	ConnectionOK() bool

	// Dial connects to api server.
	Dial() (kubernetes.Interface, error)

	// DialLogs connects to api server for logs.
	DialLogs() (kubernetes.Interface, error)

	// SwitchContext switches cluster based on context.
	SwitchContext(ctx string) error

	// CachedDiscovery connects to discovery client.
	CachedDiscovery() (*disk.CachedDiscoveryClient, error)

	// RestConfig connects to rest client.
	RestConfig() (*restclient.Config, error)

	// MXDial connects to metrics server.
	MXDial() (*versioned.Clientset, error)

	// DynDial connects to dynamic client.
	DynDial() (dynamic.Interface, error)

	// HasMetrics checks if metrics server is available.
	HasMetrics() bool

	// ValidNamespaceNames returns all available namespace names.
	ValidNamespaceNames() (NamespaceNames, error)

	// IsValidNamespace checks if given namespace is known.
	IsValidNamespace(string) bool

	// ServerVersion returns current server version.
	ServerVersion() (*version.Info, error)

	// CheckConnectivity checks if api server connection is happy or not.
	CheckConnectivity() bool

	// ActiveContext returns the current context name.
	ActiveContext() string

	// ActiveNamespace returns the current namespace.
	ActiveNamespace() string

	// IsActiveNamespace checks if given ns is active.
	IsActiveNamespace(string) bool
}

Connection represents a Kubernetes apiserver connection.

type ContainersMetrics added in v0.23.8

type ContainersMetrics map[string]*mv1beta1.ContainerMetrics

ContainersMetrics tracks containers metrics.

type CurrentMetrics added in v0.13.3

type CurrentMetrics struct {
	CurrentCPU, CurrentMEM, CurrentEphemeral int64
}

CurrentMetrics tracks current cpu/mem.

type Error added in v0.23.6

type Error string

Error represents an error.

func (Error) Error added in v0.23.6

func (e Error) Error() string

Error returns the error text.

type GVR

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

GVR represents a kubernetes resource schema as a string. Format is group/version/resources:subresource.

func FromGVAndR

func FromGVAndR(gv, r string) GVR

FromGVAndR builds a gvr from a group/version and resource.

func NewGVR

func NewGVR(gvr string) GVR

NewGVR builds a new gvr from a group, version, resource.

func NewGVRFromMeta

func NewGVRFromMeta(a metav1.APIResource) GVR

NewGVRFromMeta builds a gvr from resource metadata.

func (GVR) AsResourceName

func (g GVR) AsResourceName() string

AsResourceName returns a resource . separated descriptor in the shape of kind.version.group.

func (GVR) FQN added in v0.24.0

func (g GVR) FQN(n string) string

FQN returns a fully qualified resource name.

func (GVR) G added in v0.13.1

func (g GVR) G() string

G returns the resource group name.

func (GVR) GR added in v0.13.1

func (g GVR) GR() *schema.GroupResource

GR returns a full schema representation.

func (GVR) GV added in v0.13.1

func (g GVR) GV() schema.GroupVersion

GV returns the group version scheme representation.

func (GVR) GVK added in v0.17.6

func (g GVR) GVK() schema.GroupVersionKind

GVK returns a full schema representation.

func (GVR) GVR added in v0.13.1

GVR returns a full schema representation.

func (GVR) IsDecodable added in v0.31.8

func (g GVR) IsDecodable() bool

IsDecodable checks if the k8s resource has a decodable view

func (GVR) R added in v0.13.1

func (g GVR) R() string

R returns the resource name.

func (GVR) RG added in v0.13.1

func (g GVR) RG() (string, string)

RG returns the resource and group.

func (GVR) String

func (g GVR) String() string

String returns gvr as string.

func (GVR) SubResource

func (g GVR) SubResource() string

SubResource returns a sub resource if available.

func (GVR) V added in v0.13.1

func (g GVR) V() string

V returns the resource version.

type GVRs

type GVRs []GVR

GVRs represents a collection of gvr.

func (GVRs) Len

func (g GVRs) Len() int

Len returns the list size.

func (GVRs) Less

func (g GVRs) Less(i, j int) bool

Less returns true if i < j.

func (GVRs) Swap

func (g GVRs) Swap(i, j int)

Swap swaps list values.

type MetricsServer

type MetricsServer struct {
	Connection
	// contains filtered or unexported fields
}

MetricsServer serves cluster metrics for nodes and pods.

var MetricsDial *MetricsServer

MetricsDial tracks global metric server handle.

func DialMetrics added in v0.16.0

func DialMetrics(c Connection) *MetricsServer

DialMetrics dials the metrics server.

func NewMetricsServer

func NewMetricsServer(c Connection) *MetricsServer

NewMetricsServer return a metric server instance.

func (*MetricsServer) ClusterLoad

func (m *MetricsServer) ClusterLoad(nos *v1.NodeList, nmx *mv1beta1.NodeMetricsList, mx *ClusterMetrics) error

ClusterLoad retrieves all cluster nodes metrics.

func (*MetricsServer) FetchContainersMetrics added in v0.23.8

func (m *MetricsServer) FetchContainersMetrics(ctx context.Context, fqn string) (ContainersMetrics, error)

FetchContainersMetrics returns a pod's containers metrics.

func (*MetricsServer) FetchNodeMetrics added in v0.23.8

func (m *MetricsServer) FetchNodeMetrics(ctx context.Context, n string) (*mv1beta1.NodeMetrics, error)

FetchNodeMetrics return all metrics for nodes.

func (*MetricsServer) FetchNodesMetrics

func (m *MetricsServer) FetchNodesMetrics(ctx context.Context) (*mv1beta1.NodeMetricsList, error)

FetchNodesMetrics return all metrics for nodes.

func (*MetricsServer) FetchNodesMetricsMap added in v0.23.8

func (m *MetricsServer) FetchNodesMetricsMap(ctx context.Context) (NodesMetricsMap, error)

FetchNodesMetricsMap fetch node metrics as a map.

func (*MetricsServer) FetchPodMetrics

func (m *MetricsServer) FetchPodMetrics(ctx context.Context, fqn string) (*mv1beta1.PodMetrics, error)

FetchPodMetrics return all metrics for pods in a given namespace.

func (*MetricsServer) FetchPodsMetrics

func (m *MetricsServer) FetchPodsMetrics(ctx context.Context, ns string) (*mv1beta1.PodMetricsList, error)

FetchPodsMetrics return all metrics for pods in a given namespace.

func (*MetricsServer) FetchPodsMetricsMap added in v0.23.8

func (m *MetricsServer) FetchPodsMetricsMap(ctx context.Context, ns string) (PodsMetricsMap, error)

FetchPodsMetricsMap fetch pods metrics as a map.

func (*MetricsServer) NodesMetrics

func (m *MetricsServer) NodesMetrics(nodes *v1.NodeList, metrics *mv1beta1.NodeMetricsList, mmx NodesMetrics)

NodesMetrics retrieves metrics for a given set of nodes.

func (*MetricsServer) PodsMetrics

func (m *MetricsServer) PodsMetrics(pods *mv1beta1.PodMetricsList, mmx PodsMetrics)

PodsMetrics retrieves metrics for all pods in a given namespace.

type NamespaceNames added in v0.25.8

type NamespaceNames map[string]struct{}

NamespaceNames tracks a collection of namespace names.

type NodeMetrics

type NodeMetrics struct {
	CurrentMetrics

	AllocatableCPU, AllocatableMEM, AllocatableEphemeral int64
	AvailableCPU, AvailableMEM, AvailableEphemeral       int64
	TotalCPU, TotalMEM, TotalEphemeral                   int64
}

NodeMetrics describes raw node metrics.

type NodesMetrics

type NodesMetrics map[string]NodeMetrics

NodesMetrics tracks usage metrics per nodes.

type NodesMetricsMap added in v0.23.8

type NodesMetricsMap map[string]*mv1beta1.NodeMetrics

NodesMetricsMap tracks node metrics.

type PodMetrics

type PodMetrics CurrentMetrics

PodMetrics represent an aggregation of all pod containers metrics.

type PodsMetrics

type PodsMetrics map[string]PodMetrics

PodsMetrics tracks usage metrics per pods.

type PodsMetricsMap added in v0.23.8

type PodsMetricsMap map[string]*mv1beta1.PodMetrics

PodsMetricsMap tracks pod metrics.

Jump to

Keyboard shortcuts

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