kube

package
v0.0.0-...-d3873ad Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: Apache-2.0 Imports: 28 Imported by: 0

Documentation

Overview

Package kube contains kubernetes utility functions for tests.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoMatchingPods indicates a selector didn't match any pods.
	ErrNoMatchingPods = errors.New("no pods match selector")

	// ErrTooManyMatchingPods indicates a selector should have matched
	// fewer pods than were selected.
	ErrTooManyMatchingPods = errors.New("too many pods match selector")

	// ErrTooFewMatchingPods indicates a selector should have matched
	// more pods than were selected.
	ErrTooFewMatchingPods = errors.New("too few pods match selector")
)
View Source
var TypedClientScheme = runtime.NewScheme()

TypedClientScheme is the scheme specific to our typed object client.

Functions

func ExitCode

func ExitCode(err error) (int, bool)

ExitCode returns an non-successful exit code value for a given error, and a boolean set to true if the exit code is based on the error itself.

func PodIsReady

func PodIsReady(pod *corev1.Pod) bool

PodIsReady returns true if a pod is running and containers are ready.

func WaitForAllPodReady

func WaitForAllPodReady(
	ctx context.Context, tc *TestClient, fo PodFetchOptions) error

WaitForAllPodReady will for all pods selected to be ready, up to the deadline specified by the context, if the context lacks a deadline the call will block indefinitely. Pods are selected using the PodFetchOptions.

func WaitForAnyPodExists

func WaitForAnyPodExists(
	ctx context.Context, tc *TestClient, fo PodFetchOptions) error

WaitForAnyPodExists will wait for a pod to exist, up to the deadline specified by the context, if the context lacks a deadline the call will block indefinitely. Pods are selected using the PodFetchOptions.

func WaitForAnyPodReady

func WaitForAnyPodReady(
	ctx context.Context, tc *TestClient, fo PodFetchOptions) error

WaitForAnyPodReady will wait for a pod to be ready, up to the deadline specified by the context, if the context lacks a deadline the call will block indefinitely. Pods are selected using the PodFetchOptions.

Types

type APIObject

type APIObject interface {
	metav1.Object
	runtime.Object
}

APIObject can be used to stand in for any API object.

type BufferedCommandHandler

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

BufferedCommandHandler uses byte buffers to save command output.

func NewBufferedCommandHandler

func NewBufferedCommandHandler() *BufferedCommandHandler

NewBufferedCommandHandler returns a new BufferedCommandHandler.

func (*BufferedCommandHandler) GetStderr

func (bpc *BufferedCommandHandler) GetStderr() []byte

GetStderr returns the stderr data.

func (*BufferedCommandHandler) GetStdout

func (bpc *BufferedCommandHandler) GetStdout() []byte

GetStdout returns the stdout data.

func (*BufferedCommandHandler) Stderr

func (bpc *BufferedCommandHandler) Stderr() io.Writer

Stderr returns the stderr writer for this request.

func (*BufferedCommandHandler) Stdout

func (bpc *BufferedCommandHandler) Stdout() io.Writer

Stdout returns the stdout writer for this request.

type CommandHandler

type CommandHandler interface {
	Stdout() io.Writer
	Stderr() io.Writer
}

CommandHandler interfaces provide parameters for running a command on a pod.

type DirectSource

type DirectSource struct {
	Source    io.ReadCloser
	Namespace string
}

DirectSource interfaces are used to specify k8s resources directly from a ReadCloser stream.

func (DirectSource) GetNamespace

func (d DirectSource) GetNamespace() string

GetNamespace returns the specified namespace.

func (DirectSource) Open

func (d DirectSource) Open() (io.ReadCloser, error)

Open returns the source.

func (DirectSource) Rename

func (DirectSource) Rename(n string) string

Rename is currently a no-op for DirectSource.

type FileSource

type FileSource struct {
	Path       string
	Namespace  string
	NameSuffix string
}

FileSource selects a file path and an optional namespace where the file contains one or more k8s resource.

func (FileSource) GetNamespace

func (f FileSource) GetNamespace() string

GetNamespace returns the specified namespace.

func (FileSource) Open

func (f FileSource) Open() (io.ReadCloser, error)

Open returns a file opened for reading.

func (FileSource) Rename

func (f FileSource) Rename(n string) string

Rename adjusts the name of the resource.

type InputSource

type InputSource interface {
	Open() (io.ReadCloser, error)
	GetNamespace() string
	Rename(string) string
}

InputSource interfaces are used to specify k8s resources.

type MutatingInputSource

type MutatingInputSource interface {
	InputSource
	Apply(*unstructured.Unstructured) error
}

MutatingInputSource are input sources that can arbitrarily change the objects read from the input.

type MutatingSource

type MutatingSource struct {
	Source InputSource
	Mutate func(*unstructured.Unstructured) error
}

MutatingSource uses another input source and applies a custom Mutate function to change the objects read from the source.

func (MutatingSource) Apply

Apply mutations to the resource.

func (MutatingSource) GetNamespace

func (m MutatingSource) GetNamespace() string

GetNamespace returns the specified namespace.

func (MutatingSource) Open

func (m MutatingSource) Open() (io.ReadCloser, error)

Open returns the source.

func (MutatingSource) Rename

func (m MutatingSource) Rename(n string) string

Rename adjusts the name of the resource.

type PodCommand

type PodCommand struct {
	Command       []string
	Namespace     string
	PodName       string
	ContainerName string
}

PodCommand identifies a pod and a container and a command to run.

type PodFetchOptions

type PodFetchOptions struct {
	Namespace     string
	LabelSelector string
	MaxFound      int
	MinFound      int
}

PodFetchOptions controls what set of pods will be fetched.

type TestClient

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

TestClient is a helper for doing common things for our tests easily in kubernetes. This aims to help write integration tests.

func NewTestClient

func NewTestClient(kubeconfig string) *TestClient

NewTestClient return a new kube util test client.

func (*TestClient) Clientset

func (tc *TestClient) Clientset() *kubernetes.Clientset

Clientset returns the exact clientset used for this client.

func (*TestClient) CreateFromFile

func (tc *TestClient) CreateFromFile(
	ctx context.Context, src InputSource) ([]types.NamespacedName, error)

CreateFromFile creates new resources given a (yaml) file input. It returns an error if the resource already exists.

func (*TestClient) CreateFromFileIfMissing

func (tc *TestClient) CreateFromFileIfMissing(
	ctx context.Context, src InputSource) ([]types.NamespacedName, error)

CreateFromFileIfMissing creates new resources given a (yaml) file input. It does not return an error if the resource already exists.

func (*TestClient) DeleteResourceMatchingFile

func (tc *TestClient) DeleteResourceMatchingFile(
	ctx context.Context, src InputSource) error

DeleteResourceMatchingFile deletes a resource given a (yaml) file input. The resource with the matching group-version-kind and name will be removed if it exists.

func (*TestClient) DynamicClientset

func (tc *TestClient) DynamicClientset(objkind schema.ObjectKind) (
	dynamic.NamespaceableResourceInterface, error)

DynamicClientset returns a clientset for the unstructured type of whatever object kind you provide. It mainly just hides some of the complexity of setting up the dynamic client.

func (*TestClient) FetchPods

func (tc *TestClient) FetchPods(
	ctx context.Context, fo PodFetchOptions) ([]corev1.Pod, error)

FetchPods returns all available pods matching the PodFetchOptions.

func (*TestClient) TypedObjectClient

func (tc *TestClient) TypedObjectClient() TypedObjectClient

TypedObjectClient returns a client that can be easily used with go object types that are not native to kubernetes.

type TestExec

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

TestExec is used to execute CommandRequests.

func NewTestExec

func NewTestExec(tclient *TestClient) *TestExec

NewTestExec returns a new TestExec pointer.

func (*TestExec) Call

func (te *TestExec) Call(pc PodCommand, ch CommandHandler) error

Call executes the requested command on the pod.

type TypedObjectClient

type TypedObjectClient interface {
	Get(ctx context.Context, name types.NamespacedName, obj APIObject) error
	Create(ctx context.Context, obj APIObject) error
	Delete(ctx context.Context, obj APIObject) error
	Update(ctx context.Context, obj APIObject) error
}

TypedObjectClient can be used to interact with non-core typed objects dynamically. Don't forget to register your types with our scheme.

Jump to

Keyboard shortcuts

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