k8s

package
v0.13.28 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2019 License: Apache-2.0 Imports: 36 Imported by: 0

Documentation

Overview

Package k8s provides common functionalities for interacting with a Kubernetes cluster in the context of infrastructure testing.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddConfigContextForServiceAccountE added in v0.13.24

func AddConfigContextForServiceAccountE(
	t *testing.T,
	kubectlOptions *KubectlOptions,
	contextName string,
	serviceAccountName string,
	token string,
) error

AddConfigContextForServiceAccountE will add a new config context that binds the ServiceAccount auth token to the Kubernetes cluster of the current config context.

func AreAllNodesReady

func AreAllNodesReady(t *testing.T) bool

AreAllNodesReady checks if all nodes are ready in the Kubernetes cluster targeted by the current config context

func AreAllNodesReadyE

func AreAllNodesReadyE(t *testing.T) (bool, error)

AreAllNodesReadyE checks if all nodes are ready in the Kubernetes cluster targeted by the current config context. If false, returns an error indicating the reason.

func CanIDo added in v0.13.24

func CanIDo(t *testing.T, options *KubectlOptions, action authv1.ResourceAttributes) bool

CanIDo returns whether or not the provided action is allowed by the client configured by the provided kubectl option. This will fail if there are any errors accessing the kubernetes API (but not if the action is denied).

func CanIDoE added in v0.13.24

func CanIDoE(t *testing.T, options *KubectlOptions, action authv1.ResourceAttributes) (bool, error)

CanIDoE returns whether or not the provided action is allowed by the client configured by the provided kubectl option. This will an error if there are problems accessing the kubernetes API (but not if the action is simply denied).

func CopyHomeKubeConfigToTemp added in v0.13.24

func CopyHomeKubeConfigToTemp(t *testing.T) string

CopyHomeKubeConfigToTemp will copy the kubeconfig in the home directory to a temp file. This will fail the test if there are any errors.

func CopyHomeKubeConfigToTempE added in v0.13.24

func CopyHomeKubeConfigToTempE(t *testing.T) (string, error)

CopyHomeKubeConfigToTemp will copy the kubeconfig in the home directory to a temp file.

func CreateNamespace added in v0.13.16

func CreateNamespace(t *testing.T, options *KubectlOptions, namespaceName string)

CreateNamespace will create a new Kubernetes namespace on the cluster targeted by the provided options. This will fail the test if there is an error in creating the namespace.

func CreateNamespaceE added in v0.13.16

func CreateNamespaceE(t *testing.T, options *KubectlOptions, namespaceName string) error

CreateNamespaceE will create a new Kubernetes namespace on the cluster targeted by the provided options.

func CreateServiceAccount added in v0.13.22

func CreateServiceAccount(t *testing.T, options *KubectlOptions, serviceAccountName string)

CreateServiceAccount will create a new service account resource in the provided namespace with the given name. The namespace used is the one provided in the KubectlOptions. This will fail the test if there is an error.

func CreateServiceAccountE added in v0.13.22

func CreateServiceAccountE(t *testing.T, options *KubectlOptions, serviceAccountName string) error

CreateServiceAccountE will create a new service account resource in the provided namespace with the given name. The namespace used is the one provided in the KubectlOptions.

func DeleteConfigContextE

func DeleteConfigContextE(t *testing.T, contextName string) error

DeleteConfigContextE will remove the context specified at the provided name, and remove any clusters and authinfos that are orphaned as a result of it. The config path is either specified in the environment variable KUBECONFIG or at the user's home directory under `.kube/config`.

func DeleteConfigContextWithPathE

func DeleteConfigContextWithPathE(t *testing.T, kubeConfigPath string, contextName string) error

DeleteConfigContextWithPathE will remove the context specified at the provided name, and remove any clusters and authinfos that are orphaned as a result of it.

func DeleteNamespace added in v0.13.16

func DeleteNamespace(t *testing.T, options *KubectlOptions, namespaceName string)

DeleteNamespace will delete the requested namespace from the Kubernetes cluster targeted by the provided options. This will fail the test if there is an error in creating the namespace.

func DeleteNamespaceE added in v0.13.16

func DeleteNamespaceE(t *testing.T, options *KubectlOptions, namespaceName string) error

DeleteNamespaceE will delete the requested namespace from the Kubernetes cluster targeted by the provided options.

func GetAvailablePort added in v0.13.28

func GetAvailablePort(t *testing.T) int

GetAvailablePort retrieves an available port on the host machine. This delegates the port selection to the golang net library by starting a server and then checking the port that the server is using. This will fail the test if it could not find an avilable port.

func GetAvailablePortE added in v0.13.28

func GetAvailablePortE(t *testing.T) (int, error)

GetAvailablePortE retrieves an available port on the host machine. This delegates the port selection to the golang net library by starting a server and then checking the port that the server is using.

func GetKubeConfigPathE

func GetKubeConfigPathE(t *testing.T) (string, error)

GetKubeConfigPathE determines which file path to use as the kubectl config path

func GetKubernetesClientE

func GetKubernetesClientE(t *testing.T) (*kubernetes.Clientset, error)

GetKubernetesClientE returns a Kubernetes API client that can be used to make requests.

func GetKubernetesClientFromOptionsE added in v0.13.16

func GetKubernetesClientFromOptionsE(t *testing.T, options *KubectlOptions) (*kubernetes.Clientset, error)

GetKubernetesClientFromOptionsE returns a Kubernetes API client given a configured KubectlOptions object.

func GetNamespace added in v0.13.16

func GetNamespace(t *testing.T, options *KubectlOptions, namespaceName string) corev1.Namespace

GetNamespace will query the Kubernetes cluster targeted by the provided options for the requested namespace. This will fail the test if there is an error in creating the namespace.

func GetNamespaceE added in v0.13.16

func GetNamespaceE(t *testing.T, options *KubectlOptions, namespaceName string) (corev1.Namespace, error)

GetNamespaceE will query the Kubernetes cluster targeted by the provided options for the requested namespace.

func GetNodes

func GetNodes(t *testing.T) []corev1.Node

GetNodes queries Kubernetes for information about the worker nodes registered to the cluster. If anything goes wrong, the function will automatically fail the test.

func GetNodesE

func GetNodesE(t *testing.T) ([]corev1.Node, error)

GetNodesE queries Kubernetes for information about the worker nodes registered to the cluster.

func GetPod added in v0.13.21

func GetPod(t *testing.T, options *KubectlOptions, podName string) *corev1.Pod

GetPod returns a Kubernetes pod resource in the provided namespace with the given name. This will fail the test if there is an error.

func GetPodE added in v0.13.21

func GetPodE(t *testing.T, options *KubectlOptions, podName string) (*corev1.Pod, error)

GetPodE returns a Kubernetes pod resource in the provided namespace with the given name.

func GetReadyNodes

func GetReadyNodes(t *testing.T) []corev1.Node

GetReadyNodes queries Kubernetes for information about the worker nodes registered to the cluster and only returns those that are in the ready state. If anything goes wrong, the function will automatically fail the test.

func GetReadyNodesE

func GetReadyNodesE(t *testing.T) ([]corev1.Node, error)

GetReadyNodesE queries Kubernetes for information about the worker nodes registered to the cluster and only returns those that are in the ready state.

func GetSecret added in v0.13.22

func GetSecret(t *testing.T, options *KubectlOptions, secretName string) *corev1.Secret

GetSecret returns a Kubernetes secret resource in the provided namespace with the given name. The namespace used is the one provided in the KubectlOptions. This will fail the test if there is an error.

func GetSecretE added in v0.13.22

func GetSecretE(t *testing.T, options *KubectlOptions, secretName string) (*corev1.Secret, error)

GetSecret returns a Kubernetes secret resource in the provided namespace with the given name. The namespace used is the one provided in the KubectlOptions.

func GetService added in v0.13.16

func GetService(t *testing.T, options *KubectlOptions, serviceName string) *corev1.Service

GetService returns a Kubernetes service resource in the provided namespace with the given name. This will fail the test if there is an error.

func GetServiceAccount added in v0.13.22

func GetServiceAccount(t *testing.T, options *KubectlOptions, serviceAccountName string) *corev1.ServiceAccount

GetServiceAccount returns a Kubernetes service account resource in the provided namespace with the given name. The namespace used is the one provided in the KubectlOptions. This will fail the test if there is an error.

func GetServiceAccountAuthToken added in v0.13.24

func GetServiceAccountAuthToken(t *testing.T, kubectlOptions *KubectlOptions, serviceAccountName string) string

GetServiceAccountWithAuthTokenE will retrieve the ServiceAccount token from the cluster so it can be used to authenticate requests as that ServiceAccount. This will fail the test if there is an error.

func GetServiceAccountAuthTokenE added in v0.13.24

func GetServiceAccountAuthTokenE(t *testing.T, kubectlOptions *KubectlOptions, serviceAccountName string) (string, error)

GetServiceAccountWithAuthTokenE will retrieve the ServiceAccount token from the cluster so it can be used to authenticate requests as that ServiceAccount.

func GetServiceAccountE added in v0.13.22

func GetServiceAccountE(t *testing.T, options *KubectlOptions, serviceAccountName string) (*corev1.ServiceAccount, error)

GetServiceAccount returns a Kubernetes service account resource in the provided namespace with the given name. The namespace used is the one provided in the KubectlOptions.

func GetServiceE added in v0.13.16

func GetServiceE(t *testing.T, options *KubectlOptions, serviceName string) (*corev1.Service, error)

GetServiceE returns a Kubernetes service resource in the provided namespace with the given name.

func GetServiceEndpoint added in v0.13.16

func GetServiceEndpoint(t *testing.T, service *corev1.Service, servicePort int) string

GetServiceEndpoint will return the service access point. If the service endpoint is not ready, will fail the test immediately.

func GetServiceEndpointE added in v0.13.16

func GetServiceEndpointE(t *testing.T, service *corev1.Service, servicePort int) (string, error)

GetServiceEndpointE will return the service access point using the following logic:

  • For ClusterIP service type, return the URL that maps to ClusterIP and Service Port
  • For NodePort service type, identify the public IP of the node (if it exists, otherwise return the bound hostname), and the assigned node port for the provided service port, and return the URL that maps to node ip and node port.
  • For LoadBalancer service type, return the publicly accessible hostname of the load balancer.
  • All other service types are not supported.

func IsNodeReady

func IsNodeReady(node corev1.Node) bool

IsNodeReady takes a Kubernetes Node information object and checks if the Node is in the ready state.

func IsPodAvailable added in v0.13.21

func IsPodAvailable(pod *corev1.Pod) bool

IsPodAvailable returns true if the pod is running.

func IsServiceAvailable added in v0.13.16

func IsServiceAvailable(service *corev1.Service) bool

IsServiceAvailable returns true if the service endpoint is ready to accept traffic.

func KubeConfigPathFromHomeDirE added in v0.13.14

func KubeConfigPathFromHomeDirE() (string, error)

KubeConfigPathFromHomeDirE returns a string to the default Kubernetes config path in the home directory. This will error if the home directory can not be determined.

func KubectlApply added in v0.13.16

func KubectlApply(t *testing.T, options *KubectlOptions, configPath string)

KubectlApply will take in a file path and apply it to the cluster targeted by KubectlOptions. If there are any errors, fail the test immediately.

func KubectlApplyE added in v0.13.16

func KubectlApplyE(t *testing.T, options *KubectlOptions, configPath string) error

KubectlApplyE will take in a file path and apply it to the cluster targeted by KubectlOptions.

func KubectlApplyFromString added in v0.13.16

func KubectlApplyFromString(t *testing.T, options *KubectlOptions, configData string)

KubectlApplyFromString will take in a kubernetes resource config as a string and apply it on the cluster specified by the provided kubectl options.

func KubectlApplyFromStringE added in v0.13.16

func KubectlApplyFromStringE(t *testing.T, options *KubectlOptions, configData string) error

KubectlApplyFromStringE will take in a kubernetes resource config as a string and apply it on the cluster specified by the provided kubectl options. If it fails, this will return the error.

func KubectlDelete added in v0.13.16

func KubectlDelete(t *testing.T, options *KubectlOptions, configPath string)

KubectlDelete will take in a file path and delete it from the cluster targeted by KubectlOptions. If there are any errors, fail the test immediately.

func KubectlDeleteE added in v0.13.16

func KubectlDeleteE(t *testing.T, options *KubectlOptions, configPath string) error

KubectlDeleteE will take in a file path and delete it from the cluster targeted by KubectlOptions.

func KubectlDeleteFromString added in v0.13.16

func KubectlDeleteFromString(t *testing.T, options *KubectlOptions, configData string)

KubectlDeleteFromString will take in a kubernetes resource config as a string and delete it on the cluster specified by the provided kubectl options.

func KubectlDeleteFromStringE added in v0.13.16

func KubectlDeleteFromStringE(t *testing.T, options *KubectlOptions, configData string) error

KubectlDeleteFromStringE will take in a kubernetes resource config as a string and delete it on the cluster specified by the provided kubectl options. If it fails, this will return the error.

func ListPods added in v0.13.28

func ListPods(t *testing.T, options *KubectlOptions, filters metav1.ListOptions) []corev1.Pod

ListPods will look for pods in the given namespace that match the given filters and return them. This will fail the test if there is an error.

func ListPodsE added in v0.13.28

func ListPodsE(t *testing.T, options *KubectlOptions, filters metav1.ListOptions) ([]corev1.Pod, error)

ListPodsE will look for pods in the given namespace that match the given filters and return them.

func ListServices added in v0.13.28

func ListServices(t *testing.T, options *KubectlOptions, filters metav1.ListOptions) []corev1.Service

ListServices will look for services in the given namespace that match the given filters and return them. This will fail the test if there is an error.

func ListServicesE added in v0.13.28

func ListServicesE(t *testing.T, options *KubectlOptions, filters metav1.ListOptions) ([]corev1.Service, error)

ListServicesE will look for services in the given namespace that match the given filters and return them.

func LoadApiClientConfigE added in v0.13.16

func LoadApiClientConfigE(configPath string, contextName string) (*restclient.Config, error)

LoadApiClientConfig will load a ClientConfig object from a file path that points to a location on disk containing a kubectl config, with the requested context loaded.

func LoadConfigFromPath

func LoadConfigFromPath(path string) clientcmd.ClientConfig

LoadConfigFromPath will load a ClientConfig object from a file path that points to a location on disk containing a kubectl config.

func RemoveOrphanedClusterAndAuthInfoConfig

func RemoveOrphanedClusterAndAuthInfoConfig(config *api.Config)

RemoveOrphanedClusterAndAuthInfoConfig will remove all configurations related to clusters and users that have no contexts associated with it

func RunKubectl added in v0.13.16

func RunKubectl(t *testing.T, options *KubectlOptions, args ...string)

RunKubectl will call kubectl using the provided options and args, failing the test on error.

func RunKubectlAndGetOutputE added in v0.13.16

func RunKubectlAndGetOutputE(t *testing.T, options *KubectlOptions, args ...string) (string, error)

RunKubectlAndGetOutputE will call kubectl using the provided options and args, returning the output of stdout and stderr.

func RunKubectlE added in v0.13.16

func RunKubectlE(t *testing.T, options *KubectlOptions, args ...string) error

RunKubectlE will call kubectl using the provided options and args.

func StoreConfigToTempFile added in v0.13.16

func StoreConfigToTempFile(t *testing.T, configData string) string

StoreConfigToTempFile will store the provided config data to a temporary file created on the os and return the filename.

func StoreConfigToTempFileE added in v0.13.16

func StoreConfigToTempFileE(t *testing.T, configData string) (string, error)

StoreConfigToTempFileE will store the provided config data to a temporary file created on the os and return the filename, or error.

func UpsertConfigContext added in v0.13.24

func UpsertConfigContext(config *api.Config, contextName string, clusterName string, userName string)

UpsertConfigContext will update or insert a new context to the provided config, binding the provided cluster to the provided user.

func WaitUntilAllNodesReady

func WaitUntilAllNodesReady(t *testing.T, retries int, sleepBetweenRetries time.Duration)

WaitUntilAllNodesReady continuously polls the Kubernetes cluster until all nodes in the cluster reach the ready state, or runs out of retries. Will fail the test immediately if it times out.

func WaitUntilAllNodesReadyE

func WaitUntilAllNodesReadyE(t *testing.T, retries int, sleepBetweenRetries time.Duration) error

WaitUntilAllNodesReadyE continuously polls the Kubernetes cluster until all nodes in the cluster reach the ready state, or runs out of retries.

func WaitUntilNumPodsCreated added in v0.13.28

func WaitUntilNumPodsCreated(
	t *testing.T,
	options *KubectlOptions,
	filters metav1.ListOptions,
	desiredCount int,
	retries int,
	sleepBetweenRetries time.Duration,
)

WaitUntilNumPodsCreated waits until the desired number of pods are created that match the provided filter. This will retry the check for the specified amount of times, sleeping for the provided duration between each try. This will fail the test if the retry times out.

func WaitUntilNumPodsCreatedE added in v0.13.28

func WaitUntilNumPodsCreatedE(
	t *testing.T,
	options *KubectlOptions,
	filters metav1.ListOptions,
	desiredCount int,
	retries int,
	sleepBetweenRetries time.Duration,
) error

WaitUntilNumPodsCreatedE waits until the desired number of pods are created that match the provided filter. This will retry the check for the specified amount of times, sleeping for the provided duration between each try.

func WaitUntilPodAvailable added in v0.13.21

func WaitUntilPodAvailable(t *testing.T, options *KubectlOptions, podName string, retries int, sleepBetweenRetries time.Duration)

WaitUntilPodAvailable waits until the pod is running, retrying the check for the specified amount of times, sleeping for the provided duration between each try. This will fail the test if there is an error or if the check times out.

func WaitUntilPodAvailableE added in v0.13.21

func WaitUntilPodAvailableE(t *testing.T, options *KubectlOptions, podName string, retries int, sleepBetweenRetries time.Duration) error

WaitUntilPodAvailableE waits until the pod is running, retrying the check for the specified amount of times, sleeping for the provided duration between each try.

func WaitUntilServiceAvailable added in v0.13.16

func WaitUntilServiceAvailable(t *testing.T, options *KubectlOptions, serviceName string, retries int, sleepBetweenRetries time.Duration)

WaitUntilServiceAvailable waits until the service endpoint is ready to accept traffic.

Types

type DesiredNumberOfPodsNotCreated added in v0.13.28

type DesiredNumberOfPodsNotCreated struct {
	Filter       metav1.ListOptions
	DesiredCount int
}

DesiredNumberOfPodsNotCreated is returned when the number of pods matching a filter condition does not match the desired number of Pods.

func (DesiredNumberOfPodsNotCreated) Error added in v0.13.28

type KubeResourceType added in v0.13.28

type KubeResourceType int

KubeResourceType is an enum representing known resource types that can support port forwarding

const (
	ResourceTypePod KubeResourceType = iota
	ResourceTypeService
)

func (KubeResourceType) String added in v0.13.28

func (resourceType KubeResourceType) String() string

type KubectlOptions added in v0.13.16

type KubectlOptions struct {
	ContextName string
	ConfigPath  string
	Namespace   string
	Env         map[string]string
}

Represents common options necessary to specify for all Kubectl calls

func NewKubectlOptions added in v0.13.16

func NewKubectlOptions(contextName string, configPath string) *KubectlOptions

func (*KubectlOptions) GetConfigPath added in v0.13.28

func (kubectlOptions *KubectlOptions) GetConfigPath(t *testing.T) (string, error)

GetConfigPath will return a sensible default if the config path is not set on the options.

type MalformedNodeID added in v0.13.16

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

MalformedNodeID is returned when a Kubernetes node has a malformed node id scheme

func NewMalformedNodeIDError added in v0.13.16

func NewMalformedNodeIDError(node *corev1.Node) MalformedNodeID

func (MalformedNodeID) Error added in v0.13.16

func (err MalformedNodeID) Error() string

type NoNodesInKubernetes added in v0.13.16

type NoNodesInKubernetes struct{}

NoNodesInKubernetes is returned when the Kubernetes cluster has no nodes registered.

func NewNoNodesInKubernetesError added in v0.13.16

func NewNoNodesInKubernetesError() NoNodesInKubernetes

func (NoNodesInKubernetes) Error added in v0.13.16

func (err NoNodesInKubernetes) Error() string

type NodeHasNoHostname added in v0.13.16

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

NodeHasNoHostname is returned when a Kubernetes node has no discernible hostname

func NewNodeHasNoHostnameError added in v0.13.16

func NewNodeHasNoHostnameError(node *corev1.Node) NodeHasNoHostname

func (NodeHasNoHostname) Error added in v0.13.16

func (err NodeHasNoHostname) Error() string

type PodNotAvailable added in v0.13.21

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

PodNotAvailable is returned when a Kubernetes service is not yet available to accept traffic.

func NewPodNotAvailableError added in v0.13.21

func NewPodNotAvailableError(pod *corev1.Pod) PodNotAvailable

func (PodNotAvailable) Error added in v0.13.21

func (err PodNotAvailable) Error() string

type ServiceAccountTokenNotAvailable added in v0.13.24

type ServiceAccountTokenNotAvailable struct {
	Name string
}

ServiceAccountTokenNotAvailable is returned when a Kubernetes ServiceAccount does not have a token provisioned yet.

func (ServiceAccountTokenNotAvailable) Error added in v0.13.24

type ServiceNotAvailable added in v0.13.16

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

ServiceNotAvailable is returned when a Kubernetes service is not yet available to accept traffic.

func NewServiceNotAvailableError added in v0.13.16

func NewServiceNotAvailableError(service *corev1.Service) ServiceNotAvailable

func (ServiceNotAvailable) Error added in v0.13.16

func (err ServiceNotAvailable) Error() string

type Tunnel added in v0.13.28

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

Tunnel is the main struct that configures and manages port forwading tunnels to Kubernetes resources.

func NewTunnel added in v0.13.28

func NewTunnel(kubectlOptions *KubectlOptions, resourceType KubeResourceType, resourceName string, local int, remote int) *Tunnel

NewTunnel will create a new Tunnel struct. Note that if you use 0 for the local port, an open port on the host system will be selected automatically, and the Tunnel struct will be updated with the selected port.

func (*Tunnel) Close added in v0.13.28

func (tunnel *Tunnel) Close()

Close disconnects a tunnel connection by closing the StopChan, thereby stopping the goroutine.

func (*Tunnel) Endpoint added in v0.13.28

func (tunnel *Tunnel) Endpoint() string

Endpoint returns the tunnel endpoint

func (*Tunnel) ForwardPort added in v0.13.28

func (tunnel *Tunnel) ForwardPort(t *testing.T)

ForwardPort opens a tunnel to a kubernetes resource, as specified by the provided tunnel struct. This will fail the test if there is an error attempting to open the port.

func (*Tunnel) ForwardPortE added in v0.13.28

func (tunnel *Tunnel) ForwardPortE(t *testing.T) error

ForwardPortE opens a tunnel to a kubernetes resource, as specified by the provided tunnel struct.

type UnknownKubeResourceType added in v0.13.28

type UnknownKubeResourceType struct {
	ResourceType KubeResourceType
}

UnknownKubeResourceType is returned if the given resource type does not match the list of known resource types.

func (UnknownKubeResourceType) Error added in v0.13.28

func (err UnknownKubeResourceType) Error() string

type UnknownServicePort added in v0.13.16

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

UnknownServicePort is returned when the given service port is not an exported port of the service.

func NewUnknownServicePortError added in v0.13.16

func NewUnknownServicePortError(service *corev1.Service, port int32) UnknownServicePort

func (UnknownServicePort) Error added in v0.13.16

func (err UnknownServicePort) Error() string

type UnknownServiceType added in v0.13.16

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

UnknownServiceType is returned when a Kubernetes service has a type that is not yet handled by the test functions.

func NewUnknownServiceTypeError added in v0.13.16

func NewUnknownServiceTypeError(service *corev1.Service) UnknownServiceType

func (UnknownServiceType) Error added in v0.13.16

func (err UnknownServiceType) Error() string

Jump to

Keyboard shortcuts

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