client

package
v0.0.0-...-79e2c14 Latest Latest
Warning

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

Go to latest
Published: May 2, 2024 License: Apache-2.0 Imports: 30 Imported by: 0

Documentation

Overview

package client provides a client for tests.

This Client can operate on any k8s resource struct type, core or custom, via the runtime.Object interface. As well as the usual Create, Get, Update, Delete it supports Watch and provides some convenience methods for tests.

Most tests should use the singleton client returned by Get(). Avoiding creation of multiple clients saves significant time in a test suite.

Index

Constants

View Source
const (
	// LabelKey is the create label key for the Get() client.
	LabelKey = "test-client"
	// LabelValue is the create label value for the Get() client.
	LabelValue = "true"
)

Variables

View Source
var (
	ErrWatchClosed = errors.New("watch closed")
	ErrTimeout     = errors.New("timeout")
)

Functions

func ClusterLogForwarderReady

func ClusterLogForwarderReady(e watch.Event) (bool, error)

func ClusterLogForwarderValidationFailure

func ClusterLogForwarderValidationFailure(e watch.Event) (bool, error)

ClusterLogForwarderValidationFailure expects condition type "Validation" to be set on the ClusterLogForwarder resource. If no such condition can be found, it returns false, and a nil error (so that c.WaitFor can wait until the condition is set, or time out if the condition is never set). If the condition is set, we expect its message to match clfvalidation.ValidateClusterLoggingDependencyMSG and we expect it to be "True". We also expect the "Ready" condition to be "False". In that case, we return true and no error. In case of the contrary, we return false and an error.

func Deleted

func Deleted(e watch.Event) (bool, error)

Deleted is a condition for Client.WaitFor true when the event is of type Deleted.

func IgnoreAlreadyExists

func IgnoreAlreadyExists(err error) error

IgnoreAlreadyExists returns nil on AlreadyExists errors, other values returned unmodified.

func IgnoreNotFound

func IgnoreNotFound(err error) error

IgnoreNotFound returns nil on NotFound errors, other values returned unmodified.

func IsDaemonSetReady

func IsDaemonSetReady(e watch.Event) (bool, error)

DaemonSetIsReady returns (true, nil) when the pods of the set are ready on all nodes in the cluster and (false, nil) otherwise.

func PodFailed

func PodFailed(e watch.Event) (bool, error)

PodFailed returns (true,nil) when e.Object is a Pod with phase PodFailed. Returns an error if pod reaches any other long-lasting state [failed, succeeded ,running]

func PodRunning

func PodRunning(e watch.Event) (bool, error)

PodRunning returns (true,nil) when e.Object is a Pod with phase PodRunning. Returns an error if pod reaches any other long-lasting state [failed, succeeded ,running]

func PodSucceeded

func PodSucceeded(e watch.Event) (bool, error)

PodSucceeded returns (true,nil) when e.Object is a Pod with phase PodSucceeded. Returns an error if pod reaches any other long-lasting state [failed, succeeded ,running]

func RouteReady

func RouteReady(e watch.Event) (bool, error)

Types

type Client

type Client struct {
	Labels map[string]string
	// contains filtered or unexported fields
}

Client operates on any runtime.Object (core or custom) and has Watch to wait efficiently.

func Get

func Get() *Client

Get returns a process-wide singleton client, created on demand. Uses defaults: DefaultTimeout, LabelKey, LabelValue.

func New

func New(cfg *rest.Config, timeout time.Duration, labels map[string]string) (*Client, error)

New client.

Operations will fail if they take longer than timeout. The Labels will be applied to each object created by the client.

func (*Client) Cfg

func (c *Client) Cfg() *rest.Config

Cfg returns the the client's rest.Config

func (*Client) ContainerLogStream

func (c *Client) ContainerLogStream(namespace, podName, containerName string, follow bool) (io.ReadCloser, error)

ContainerLogStream returns the log stream for a container. Empty containerName means default container.

func (*Client) ContainerLogs

func (c *Client) ContainerLogs(namespace, podName, containerName string) string

ContainerLogs returns the pod logs, or an error string if there is a problem reading them. Empty containerName means default container.

func (*Client) ControllerRuntimeClient

func (c *Client) ControllerRuntimeClient() crclient.Client

ControllerRuntimeClient returns the underlying controller runtime Client

func (*Client) Create

func (c *Client) Create(o crclient.Object) (err error)

Create the cluster resource described by the struct o.

func (*Client) Delete

func (c *Client) Delete(o crclient.Object) (err error)

Delete the of resource with name, namespace and type of o.

func (*Client) Get

func (c *Client) Get(o crclient.Object) (err error)

Get the resource with the name, namespace and type of o, copy it's state into o.

func (*Client) GroupVersionResource

func (c *Client) GroupVersionResource(o crclient.Object) (schema.GroupVersionResource, error)

GroupVersionResource uses the Client's RESTMapping to find the resource name for o.

func (*Client) Host

func (c *Client) Host() string

Host returns the API host or URL used by the client's rest.Config.

func (*Client) List

func (c *Client) List(list crclient.ObjectList, opts ...ListOption) (err error)

List resources, return results in o, which must be an xxxList struct.

func (*Client) Recreate

func (c *Client) Recreate(o crclient.Object) (err error)

Recreate creates o after removing any existing object of the same name and type.

func (*Client) Remove

func (c *Client) Remove(o crclient.Object) (err error)

Remove is like Delete but ignores NotFound errors.

func (*Client) RemoveSync

func (c *Client) RemoveSync(o crclient.Object) (err error)

RemoveSync is like Delete but ignores NotFound errors and deletes in the foreground

func (*Client) Timeout

func (c *Client) Timeout() time.Duration

func (*Client) Update

func (c *Client) Update(o crclient.Object) (err error)

Update the state of resource with name, namespace and type of o using the state in o.

func (*Client) WaitFor

func (c *Client) WaitFor(o client.Object, condition Condition) (err error)

WaitFor watches o until condition() returns true or error, or c.Timeout expires. It is not an error if o does not exist, it will be waited for. o is updated from the last object seen by the watch.

func (*Client) WaitForType

func (c *Client) WaitForType(o client.Object, condition Condition, opts ...ListOption) (err error)

WaitForType watches for events involving objects of the same type as o, until condition() returns true or error, or c.Timeout expires. o is updated from the last object seen by the watch.

func (*Client) Watch

func (c *Client) Watch(gvr schema.GroupVersionResource, opts ...ListOption) (w watch.Interface, err error)

Watch for changes in namespace to objects with the given GroupVersionResource, apply the given list options to the watch.

func (*Client) WatchObject

func (c *Client) WatchObject(o client.Object) (w watch.Interface, err error)

WatchObject returns a watch for changes to a single named object. Note: it is not an error if no such object exists, the watcher will wait for creation.

func (*Client) WatchTypeOf

func (c *Client) WatchTypeOf(o client.Object, opts ...ListOption) (w watch.Interface, err error)

WatchTypeOf returns a watch using the GroupVersionResource of object o.

func (*Client) WithLabels

func (c *Client) WithLabels(labels map[string]string) *Client

WithLabels returns a client that uses c but has a different set of Create Labels.

func (*Client) WithTimeout

func (c *Client) WithTimeout(timeout time.Duration) *Client

WithTimeout returns a client that uses c but has a different timeout.

type Condition

type Condition func(watch.Event) (bool, error)

Condition returns true if an event meets the condition, error if something goes wrong.

type Continue

type Continue = crclient.Continue

type HasLabels

type HasLabels = crclient.HasLabels

type InNamespace

type InNamespace = crclient.InNamespace

type Limit

type Limit = crclient.Limit

type ListOption

type ListOption = crclient.ListOption

List options aliased from crclient

type ListOptions

type ListOptions = crclient.ListOptions

type MatchingFields

type MatchingFields = crclient.MatchingFields

type MatchingFieldsSelector

type MatchingFieldsSelector = crclient.MatchingFieldsSelector

type MatchingLabels

type MatchingLabels = crclient.MatchingLabels

type MatchingLabelsSelector

type MatchingLabelsSelector = crclient.MatchingLabelsSelector

type NamespaceClient

type NamespaceClient struct {
	Test
}

NamespaceClient wraps the singleton test client for use with hack testing

func NewNamespaceClient

func NewNamespaceClient() *NamespaceClient

func (*NamespaceClient) Close

func (t *NamespaceClient) Close()

type Test

type Test struct {
	*Client
	NS *corev1.Namespace
}

Test wraps the singleton test client with setup/teardown and convenience methods for testing.

func ForTest

func ForTest(t *testing.T) *Test

ForTest returns a Test for a testing.T. The client.Test is closed when the test ends.

func NewTest

func NewTest(testOptions ...TestOption) *Test

NewTest creates a new Test, which includes creating a new test namespace.

func (*Test) Close

func (t *Test) Close()

Close removes the test namespace unless called from a failed test.

type TestOption

type TestOption string

TestOption is an option to alter a test in some way

const (
	//UseInfraNamespaceTestOption is the option to hint the test should be run in an infrastructure namespace
	UseInfraNamespaceTestOption TestOption = "useInfraNamespace"

	//DryRunTestOption is a hint to use in testing to not actually create resources against cluster (e.g. unit tests)
	DryRunTestOption TestOption = "dryRun"
)

type TestOptions

type TestOptions []TestOption

func (TestOptions) Include

func (options TestOptions) Include(option TestOption) bool

Jump to

Keyboard shortcuts

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