client

package module
v0.0.0-...-6d46272 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2023 License: Apache-2.0 Imports: 39 Imported by: 58

Documentation

Overview

Package client implements Kubernetes client utilities.

Index

Constants

View Source
const (

	// DefaultNamespacePollInterval is the default namespace poll interval.
	DefaultNamespacePollInterval = 15 * time.Second
	// DefaultNamespaceDeletionInterval is the default namespace deletion interval.
	DefaultNamespaceDeletionInterval = 15 * time.Second
	// DefaultNamespaceDeletionTimeout is the default namespace deletion timeout.
	DefaultNamespaceDeletionTimeout = 30 * time.Minute
)

Variables

This section is empty.

Functions

func CheckPodLogs

func CheckPodLogs(
	lg *zap.Logger,
	logWriter io.Writer,
	stopc chan struct{},
	c k8s_client.Interface,
	namespace string,
	podName string,
	opts ...OpOption) (logs string, err error)

Checks the last 100 lines of logs from a pod

func CreateNamespace

func CreateNamespace(lg *zap.Logger, c k8s_client.Interface, namespace string) error

CreateNamespace creates a single namespace with given name.

func DefaultKubectlDownloadURL

func DefaultKubectlDownloadURL() string

func DefaultKubectlPath

func DefaultKubectlPath() string

func DeleteConfigmap

func DeleteConfigmap(lg *zap.Logger, c k8s_client.Interface, namespace string, name string) error

DeleteService deletes namespace with given name.

func DeleteCronJob

func DeleteCronJob(lg *zap.Logger, c k8s_client.Interface, namespace string, name string) error

DeleteCronJob deletes CronJob with given name.

func DeleteDaemonSet

func DeleteDaemonSet(lg *zap.Logger, c k8s_client.Interface, namespace string, name string) error

DeleteDaemonSet deletes namespace with given name.

func DeleteDeployment

func DeleteDeployment(lg *zap.Logger, c k8s_client.Interface, namespace string, deploymentName string) error

DeleteDeployment deletes namespace with given name.

func DeleteJob

func DeleteJob(lg *zap.Logger, c k8s_client.Interface, namespace string, name string) error

DeleteJob deletes Job with given name.

func DeleteNamespaceAndWait

func DeleteNamespaceAndWait(
	lg *zap.Logger,
	c k8s_client.Interface,
	namespace string,
	pollInterval time.Duration,
	timeout time.Duration,
	opts ...OpOption) error

DeleteNamespaceAndWait deletes namespace with given name and waits for its deletion. Default interval is 5-second and default timeout is 10-min.

func DeletePod

func DeletePod(lg *zap.Logger, c k8s_client.Interface, namespace string, Name string) error

DeletePod deletes Pod with given name.

func DeleteRBACRoleBinding

func DeleteRBACRoleBinding(lg *zap.Logger, c k8s_client.Interface, namespace string, name string) error

DeleteRBACRoleBinding deletes RoleBinding with given name.

func DeleteService

func DeleteService(lg *zap.Logger, c k8s_client.Interface, namespace string, svcName string) error

DeleteService deletes namespace with given name.

func DeleteServiceAccount

func DeleteServiceAccount(lg *zap.Logger, c k8s_client.Interface, namespace string, name string) error

DeleteService deletes namespace with given name.

func FindServiceIngressHostname

func FindServiceIngressHostname(
	lg *zap.Logger,
	c k8s_client.Interface,
	namespace string,
	svcName string,
	stopc chan struct{},
	waitDur time.Duration,
	accountID string,
	region string,
	opts ...OpOption) (hostName string, elbARN string, elbName string, exists bool, err error)

FindServiceIngressHostname finds Service's Ingress Hostname to be updated and returns the ELB ARN.

func GetInternalIP

func GetInternalIP(node *core_v1.Node) (string, error)

GetInternalIP returns node internal IP

func GetRandomReadySchedulableNode

func GetRandomReadySchedulableNode(cli k8s_client.Interface) (*core_v1.Node, error)

GetRandomReadySchedulableNode gets a single randomly-selected node which is available for running pods on. If there are no available nodes it will return an error.

func IsRetryableAPIError

func IsRetryableAPIError(err error) bool

IsRetryableAPIError verifies whether the error is retryable.

func IsRetryableNetError

func IsRetryableNetError(err error) bool

IsRetryableNetError determines whether the error is a retryable net error.

func ListDaemonSets

func ListDaemonSets(
	lg *zap.Logger,
	c k8s_client.Interface,
	namespace string,
	batchLimit int64,
	batchInterval time.Duration,
	opts ...OpOption) (daemonsets []apps_v1.DaemonSet, err error)

func ListNamespaces

func ListNamespaces(c k8s_client.Interface) ([]core_v1.Namespace, error)

ListNamespaces returns list of existing namespace names.

func ListNodes

func ListNodes(cli k8s_client.Interface) ([]core_v1.Node, error)

ListNodes returns list of cluster nodes.

func ListNodesWithOptions

func ListNodesWithOptions(cli k8s_client.Interface, listOpts meta_v1.ListOptions) ([]core_v1.Node, error)

ListNodesWithOptions lists the cluster nodes using the provided options.

func ListPods

func ListPods(
	lg *zap.Logger,
	c k8s_client.Interface,
	namespace string,
	batchLimit int64,
	batchInterval time.Duration,
	opts ...OpOption) (pods []core_v1.Pod, err error)

func ListSecrets

func ListSecrets(lg *zap.Logger, cli k8s_client.Interface, namespace string, batchLimit int64, batchInterval time.Duration) (ss []core_v1.Secret, err error)

ListSecrets returns list of cluster nodes.

func ListStorageClass

func ListStorageClass(
	lg *zap.Logger,
	c k8s_client.Interface,
	batchLimit int64,
	batchInterval time.Duration,
	opts ...OpOption) (storageclass []storage_v1.StorageClass, err error)

func NewBusyBoxPod

func NewBusyBoxPod(name, command string) *v1.Pod

func PodRunning

func PodRunning(c k8s_client.Interface, podName, namespace string) wait.ConditionFunc

3

func RetryFunction

func RetryFunction(f func() error, options ...*ApiCallOptions) wait.ConditionFunc

RetryFunction opaques given function into retryable function.

func RetryWithExponentialBackOff

func RetryWithExponentialBackOff(fn wait.ConditionFunc) error

RetryWithExponentialBackOff a utility for retrying the given function with exponential backoff.

func WaitForCronJobCompletes

func WaitForCronJobCompletes(
	ctx context.Context,
	lg *zap.Logger,
	logWriter io.Writer,
	stopc chan struct{},
	c k8s_client.Interface,
	initialWait time.Duration,
	pollInterval time.Duration,
	namespace string,
	jobName string,
	targetCompletes int,
	opts ...OpOption) (cronJob *batch_v1beta1.CronJob, pods []core_v1.Pod, err error)

WaitForCronJobCompletes waits for all CronJob completion, by counting the number of pods in the namespace.

func WaitForDaemonSetCompletes

func WaitForDaemonSetCompletes(
	ctx context.Context,
	lg *zap.Logger,
	logWriter io.Writer,
	stopc chan struct{},
	c k8s_client.Interface,
	initialWait time.Duration,
	pollInterval time.Duration,
	namespace string,
	daemonsetName string,
	opts ...OpOption) (dp *apps_v1.DaemonSet, err error)

WaitForDaemonSetCompletes waits till target replicas are ready in the Deployment.

func WaitForDeploymentAvailables

func WaitForDeploymentAvailables(
	ctx context.Context,
	lg *zap.Logger,
	logWriter io.Writer,
	stopc chan struct{},
	c k8s_client.Interface,
	initialWait time.Duration,
	pollInterval time.Duration,
	namespace string,
	deploymentName string,
	targetAvailableReplicas int32,
	opts ...OpOption) (dp *apps_v1.Deployment, err error)

WaitForDeploymentAvailables waits till target replicas are ready in the Deployment.

func WaitForJobCompletes

func WaitForJobCompletes(
	ctx context.Context,
	lg *zap.Logger,
	logWriter io.Writer,
	stopc chan struct{},
	c k8s_client.Interface,
	initialWait time.Duration,
	pollInterval time.Duration,
	namespace string,
	jobName string,
	targetCompletes int,
	opts ...OpOption) (job *batch_v1.Job, pods []core_v1.Pod, err error)

WaitForJobCompletes waits for all Job completion, by counting the number of pods in the namespace.

func WaitForPodCondition

func WaitForPodCondition(lg *zap.Logger, c k8s_client.Interface, ns, podName, desc string, timeout time.Duration, condition podCondition) error

WaitForPodCondition waits a pods to be matched to the given condition.

func WaitForPodRunningInNamespace

func WaitForPodRunningInNamespace(c k8s_client.Interface, pod *v1.Pod) error

1 WaitForPodRunningInNamespace waits default amount of time (podStartTimeout) for the specified pod to become running. Returns an error if timeout occurs first, or pod goes in to failed state.

func WaitForPodSuccessInNamespaceTimeout

func WaitForPodSuccessInNamespaceTimeout(lg *zap.Logger, c k8s_client.Interface, podName, namespace string, timeout time.Duration) error

WaitForPodSuccessInNamespaceTimeout returns nil if the pod reached state success, or an error if it reached failure or ran too long.

func WaitForServiceIngressHostname

func WaitForServiceIngressHostname(
	lg *zap.Logger,
	c k8s_client.Interface,
	namespace string,
	svcName string,
	stopc chan struct{},
	waitDur time.Duration,
	accountID string,
	region string,
	opts ...OpOption) (hostName string, elbARN string, elbName string, err error)

WaitForServiceIngressHostname waits for Service's Ingress Hostname to be updated and returns the ELB ARN.

func WaitTimeoutForPodRunningInNamespace

func WaitTimeoutForPodRunningInNamespace(c k8s_client.Interface, podName, namespace string, timeout time.Duration) error

2 WaitTimeoutForPodRunningInNamespace waits the given timeout duration for the specified pod to become running.

Types

type ApiCallOptions

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

ApiCallOptions describes how api call errors should be treated, i.e. which errors should be allowed (ignored) and which should be retried.

func Allow

func Allow(allowErrorPredicate func(error) bool) *ApiCallOptions

Allow creates an ApiCallOptions that allows (ignores) errors matching the given predicate.

func Retry

func Retry(retryErrorPredicate func(error) bool) *ApiCallOptions

Retry creates an ApiCallOptions that retries errors matching the given predicate.

type Client

type Client interface {
	// KubernetesClient returns a new kubernetes client set.
	KubernetesClient() k8s_client.Interface
	// APIExtensionsClient returns a new apiextensions client set.
	APIExtensionsClient() apiextensions_apiserver_client.Interface
	Config() Config
}

Client defines Kubernetes client interface.

func New

func New(cfg *Config) (Client, error)

New returns the new client interface.

type Config

type Config struct {
	Logger *zap.Logger

	// KubectlDownloadURL is the URL for downloading kubectl.
	KubectlDownloadURL string
	// KubectlPath is the kubectl path.
	KubectlPath string
	// KubeconfigPath is the kubeconfig path to load.
	KubeconfigPath string
	// KubeconfigContext is the kubeconfig context.
	KubeconfigContext string

	// EKS defines EKS-specific configuration.
	EKS *EKS

	// Clients is the number of kubernetes clients to create.
	// Default is 1.
	Clients int
	// ClientQPS is the QPS for kubernetes client.
	// To use while talking with kubernetes apiserver.
	//
	// Kubernetes client DefaultQPS is 5.
	// Kubernetes client DefaultBurst is 10.
	// ref. https://github.com/kubernetes/kubernetes/blob/4d0e86f0b8d1eae00a202009858c8739e4c9402e/staging/src/k8s.io/client-go/rest/config.go#L43-L46
	//
	// kube-apiserver default inflight requests limits are:
	// FLAG: --max-mutating-requests-inflight="200"
	// FLAG: --max-requests-inflight="400"
	// ref. https://github.com/kubernetes/kubernetes/blob/4d0e86f0b8d1eae00a202009858c8739e4c9402e/staging/src/k8s.io/apiserver/pkg/server/config.go#L300-L301
	//
	ClientQPS float32
	// ClientBurst is the burst for kubernetes client.
	// To use while talking with kubernetes apiserver
	//
	// Kubernetes client DefaultQPS is 5.
	// Kubernetes client DefaultBurst is 10.
	// ref. https://github.com/kubernetes/kubernetes/blob/4d0e86f0b8d1eae00a202009858c8739e4c9402e/staging/src/k8s.io/client-go/rest/config.go#L43-L46
	//
	// kube-apiserver default inflight requests limits are:
	// FLAG: --max-mutating-requests-inflight="200"
	// FLAG: --max-requests-inflight="400"
	// ref. https://github.com/kubernetes/kubernetes/blob/4d0e86f0b8d1eae00a202009858c8739e4c9402e/staging/src/k8s.io/apiserver/pkg/server/config.go#L300-L301
	//
	ClientBurst int
	// ClientTimeout is the client timeout.
	ClientTimeout time.Duration
}

Config defines Kubernetes configuration.

type EKS

type EKS struct {
	// Region is used for EKS auth provider configuration.
	Region string
	// ClusterName is the EKS cluster name.
	// Used for EKS auth provider configuration.
	ClusterName string
	// ClusterAPIServerEndpoint is the EKS kube-apiserver endpoint.
	// Use for kubeconfig.
	ClusterAPIServerEndpoint string
	// ClusterCADecoded is the cluster CA base64-decoded.
	// Use for kubeconfig.
	ClusterCADecoded string
}

EKS defines EKS-specific client configuration and its states.

type Op

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

Op represents a Kubernetes client operation.

type OpOption

type OpOption func(*Op)

OpOption configures Kubernetes client operations.

func WithFieldSelector

func WithFieldSelector(s string) OpOption

WithFieldSelector configures field selector for list operations.

func WithForceDelete

func WithForceDelete(forceDelete bool) OpOption

WithForceDelete configures force delete. Useful for namespace deletion. ref. https://github.com/kubernetes/kubernetes/issues/60807

func WithForceDeleteFunc

func WithForceDeleteFunc(forceDeleteFunc func()) OpOption

WithForceDeleteFunc configures force delete. Useful for namespace deletion. ref. https://github.com/kubernetes/kubernetes/issues/60807

func WithLabelSelector

func WithLabelSelector(s string) OpOption

WithLabelSelector configures label selector for list operations.

func WithPodFunc

func WithPodFunc(f func(core_v1.Pod)) OpOption

WithPodFunc configures function to be called for pod.

func WithQueryFunc

func WithQueryFunc(f func()) OpOption

WithQueryFunc configures query function to be called in retry func.

type Spinner

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

func (Spinner) Restart

func (s Spinner) Restart()

func (Spinner) Stop

func (s Spinner) Stop()

Jump to

Keyboard shortcuts

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