Documentation ¶
Overview ¶
Package testcluster wraps the Kubernetes library for common test operations. It also provides a TestCluster abstraction for interacting with clusters.
Index ¶
- Constants
- Variables
- func GetIPFromService(service *v13.Service) string
- func MaybeSetContainerResources(pod *v13.Pod, containerName string, requests ContainerResourcesRequest) (*v13.Pod, error)
- func WithTestClusterConstructor(ctx context.Context, ...) context.Context
- type AcceleratorType
- type ContainerResourcesRequest
- type Namespace
- func (n *Namespace) Cleanup(ctx context.Context) error
- func (n *Namespace) GetPersistentVolume(name, size string) *v13.PersistentVolumeClaim
- func (n *Namespace) GetService(name string, spec v13.ServiceSpec) *v13.Service
- func (n *Namespace) NewAlpinePod(name, image string, cmd []string) *v13.Pod
- func (n *Namespace) NewPod(name string) *v13.Pod
- func (n *Namespace) Reset(ctx context.Context) error
- type RuntimeType
- type TestCluster
- func (t *TestCluster) Cluster() *testpb.Cluster
- func (t *TestCluster) ConfigureDaemonSetForRuntimeTestNodepool(ds *appsv1.DaemonSet) error
- func (t *TestCluster) ConfigurePodForClientNodepool(pod *v13.Pod) (*v13.Pod, error)
- func (t *TestCluster) ConfigurePodForRuntimeTestNodepool(pod *v13.Pod) (*v13.Pod, error)
- func (t *TestCluster) ConfigurePodForTertiaryNodepool(pod *v13.Pod) (*v13.Pod, error)
- func (t *TestCluster) ContainerCluster() (*cspb.Cluster, error)
- func (t *TestCluster) ContainerDurationSecondsByName(ctx context.Context, pod *v13.Pod, containerName string) (time.Duration, error)
- func (t *TestCluster) CreateDaemonset(ctx context.Context, ds *appsv1.DaemonSet) (*appsv1.DaemonSet, error)
- func (t *TestCluster) CreatePersistentVolume(ctx context.Context, volume *v13.PersistentVolumeClaim) (*v13.PersistentVolumeClaim, error)
- func (t *TestCluster) CreatePod(ctx context.Context, pod *v13.Pod) (*v13.Pod, error)
- func (t *TestCluster) CreateService(ctx context.Context, service *v13.Service) (*v13.Service, error)
- func (t *TestCluster) DeleteDaemonset(ctx context.Context, ds *appsv1.DaemonSet) error
- func (t *TestCluster) DeletePersistentVolume(ctx context.Context, volume *v13.PersistentVolumeClaim) error
- func (t *TestCluster) DeletePod(ctx context.Context, pod *v13.Pod) error
- func (t *TestCluster) DeleteService(ctx context.Context, service *v13.Service) error
- func (t *TestCluster) GetGVisorRuntimeLabelMap() map[string]string
- func (t *TestCluster) GetGVisorRuntimeToleration() v13.Toleration
- func (t *TestCluster) GetLogReader(ctx context.Context, pod *v13.Pod, opts v13.PodLogOptions) (io.ReadCloser, error)
- func (t *TestCluster) GetName() string
- func (t *TestCluster) GetPod(ctx context.Context, pod *v13.Pod) (*v13.Pod, error)
- func (t *TestCluster) GetPodsInDaemonSet(ctx context.Context, ds *appsv1.DaemonSet) ([]v13.Pod, error)
- func (t *TestCluster) HasGVisorTestRuntime(ctx context.Context) (bool, error)
- func (t *TestCluster) ListNodes(ctx context.Context) (*v13.NodeList, error)
- func (t *TestCluster) ListPods(ctx context.Context, namespace string) (*v13.PodList, error)
- func (t *TestCluster) ListServices(ctx context.Context, namespace string) (*v13.ServiceList, error)
- func (t *TestCluster) Namespace(namespace string) *Namespace
- func (t *TestCluster) OverrideTestNodepoolRuntime(testRuntime RuntimeType)
- func (t *TestCluster) ReadPodLogs(ctx context.Context, pod *v13.Pod) (string, error)
- func (t *TestCluster) RuntimeTestNodepoolIsARM() bool
- func (t *TestCluster) StreamDaemonSetLogs(ctx context.Context, ds *appsv1.DaemonSet, opts v13.PodLogOptions, ...) error
- func (t *TestCluster) WaitForDaemonset(ctx context.Context, ds *appsv1.DaemonSet) error
- func (t *TestCluster) WaitForPodCompleted(ctx context.Context, pod *v13.Pod) error
- func (t *TestCluster) WaitForPodRunning(ctx context.Context, pod *v13.Pod) error
- func (t *TestCluster) WaitForServiceReady(ctx context.Context, service *v13.Service) error
Constants ¶
const ( RuntimeTypeGVisor = RuntimeType("gvisor") RuntimeTypeUnsandboxed = RuntimeType("runc") RuntimeTypeGVisorNvidia = RuntimeType("gvisor-nvidia") RuntimeTypeGVisorTPU = RuntimeType("gvisor-tpu") RuntimeTypeUnsandboxedNvidia = RuntimeType("runc-nvidia") RuntimeTypeUnsandboxedTPU = RuntimeType("runc-tpu") )
List of known runtime types.
const ( // NamespaceDefault is the default namespace for Kubernetes. NamespaceDefault = v13.NamespaceDefault // NamespaceSanity is used for objects that are part of sanity checks. NamespaceSanity = "sanity" // NamespaceBenchmark is used for objects that are part of benchmarks. NamespaceBenchmark = "benchmark" )
Common namespace names.
const ( // TestRuntimeNodepoolName is the value that marks a "test-runtime-nodepool", or a nodepool where // w/ the runtime under test. TestRuntimeNodepoolName = "test-runtime-nodepool" // ClientNodepoolName is the value that marks a client nodepool. Usually this is a plain GKE // nodepool ClientNodepoolName = "client-nodepool" // TertiaryNodepoolName is the value that marks the tertiary nodepool. // This could either be a plain GKE nodepool or could be gVisor-enabled, // as configured during test range creation. TertiaryNodepoolName = "tertiary-nodepool" )
Nodepool names.
const ( // NodePoolTypeKey is the key to mark a nodepool as a "test-runtime-nodepool" or a "client-nodepool" NodePoolTypeKey = "nodepool-type" // NodepoolRuntimeKey is the key to mark the runtime used by a nodepool. NodepoolRuntimeKey = "runtime" // NodepoolNumAcceleratorsKey is the key to mark the number of accelerators in a nodepool. NodepoolNumAcceleratorsKey = "num-accelerators" // NodepoolTPUTopologyKey is the key to mark the TPU topology used by a nodepool. NodepoolTPUTopologyKey = "tpu-topology" // Name of the nodepool key used in Pod.Spec.NodeSelector. NodePoolSelectorKey = "cloud.google.com/gke-nodepool" // Name of the TPU accelerator key used in Pod.Spec.NodeSelector. NodepoolTPUAcceleratorSelectorKey = "cloud.google.com/gke-tpu-accelerator" // Name of the TPU topology key used in Pod.Spec.NodeSelector. NodepoolTPUTopologySelectorKey = "cloud.google.com/gke-tpu-topology" )
Nodepool keys.
const ( AcceleratorTypeTeslaT4GPU = AcceleratorType("nvidia-tesla-t4") AcceleratorTypeA100GPU = AcceleratorType("nvidia-tesla-a100") AcceleratorTypeL4GPU = AcceleratorType("nvidia-tesla-l4") AcceleratorTypeV4PodTPU = AcceleratorType("tpu-v4-pod") )
List of supported GPUs.
Variables ¶
var ( // DefaultMachineType is the default machine type to use for specs and create-default. DefaultMachineType = "n2-standard-4" // DefaultNvidiaMachineType is the default machine type for nvidia. DefaultNvidiaMachineType = "n1-standard-4" // TPUAcceleratorMachineTypeMap maps TPU types to the machine type to use. TPUAcceleratorMachineTypeMap = map[AcceleratorType]string{ AcceleratorTypeV4PodTPU: "ct4p-hightpu-4t", } )
Default machine types.
Functions ¶
func GetIPFromService ¶
GetIPFromService returns the IP on a service.
func MaybeSetContainerResources ¶
func MaybeSetContainerResources(pod *v13.Pod, containerName string, requests ContainerResourcesRequest) (*v13.Pod, error)
MaybeSetContainerResources sets container resources if flags are given. Sets both the resource limits and requests as container runtimes honor them differently.
Types ¶
type ContainerResourcesRequest ¶
type ContainerResourcesRequest struct { CPUResources string // CPUResources to request. Note: Will be overridden by flag above. MemoryResources string // MemoryResources to request. Note: Will be overridden by flag above. GPU bool }
ContainerResourcesRequest holds arguments to set requested resource on a container.
type Namespace ¶
type Namespace struct { Namespace string // contains filtered or unexported fields }
Namespace represents a Kubernetes object namespace. It can contain pods or services or other Kubernetes objects. It is useful in tests that create multiple objects and want to ensure their mutual destruction, as well as for re-running tests and ensuring that the objects from past runs are cleaned up properly on the next run.
func (*Namespace) GetPersistentVolume ¶
func (n *Namespace) GetPersistentVolume(name, size string) *v13.PersistentVolumeClaim
GetPersistentVolume gets a persistent volume spec for benchmarks.
func (*Namespace) GetService ¶
GetService gets a service spec for benchmarks.
func (*Namespace) NewAlpinePod ¶
NewAlpinePod returns an alpine pod template.
type RuntimeType ¶
type RuntimeType string
RuntimeType is a supported runtime for the test nodepool.
func (RuntimeType) ApplyNodepool ¶
func (t RuntimeType) ApplyNodepool(nodepool *cspb.NodePool, accelType AcceleratorType, accelShape string, accelRes string)
ApplyNodepool modifies the nodepool to configure it to use the runtime.
func (RuntimeType) ApplyPodSpec ¶
func (t RuntimeType) ApplyPodSpec(podSpec *v13.PodSpec)
ApplyPodSpec modifies a PodSpec to use this runtime.
type TestCluster ¶
type TestCluster struct {
// contains filtered or unexported fields
}
TestCluster wraps clusters with their individual ClientSets so that helper methods can be called.
func NewTestCluster ¶
NewTestCluster returns a new TestCluster client.
func NewTestClusterWithClient ¶
func NewTestClusterWithClient(cluster *testpb.Cluster, client kubernetes.Interface) *TestCluster
NewTestClusterWithClient returns a new TestCluster client with a given client.
func (*TestCluster) Cluster ¶
func (t *TestCluster) Cluster() *testpb.Cluster
Cluster returns the underlying cluster proto for tests.
func (*TestCluster) ConfigureDaemonSetForRuntimeTestNodepool ¶
func (t *TestCluster) ConfigureDaemonSetForRuntimeTestNodepool(ds *appsv1.DaemonSet) error
ConfigureDaemonSetForRuntimeTestNodepool configures the DaemonSet to run on the test runtime.
func (*TestCluster) ConfigurePodForClientNodepool ¶
ConfigurePodForClientNodepool configures the pod to run on the client nodepool.
func (*TestCluster) ConfigurePodForRuntimeTestNodepool ¶
ConfigurePodForRuntimeTestNodepool configures the pod to run on the test runtime.
func (*TestCluster) ConfigurePodForTertiaryNodepool ¶
ConfigurePodForTertiaryNodepool configures the pod to run on the tertiary nodepool.
func (*TestCluster) ContainerCluster ¶
func (t *TestCluster) ContainerCluster() (*cspb.Cluster, error)
ContainerCluster returns the underlying container cluster proto.
func (*TestCluster) ContainerDurationSecondsByName ¶
func (t *TestCluster) ContainerDurationSecondsByName(ctx context.Context, pod *v13.Pod, containerName string) (time.Duration, error)
ContainerDurationSecondsByName gets the runtime of a container reported by the kubelet by name. The kubelet reports runtime at second granularity.
func (*TestCluster) CreateDaemonset ¶
func (t *TestCluster) CreateDaemonset(ctx context.Context, ds *appsv1.DaemonSet) (*appsv1.DaemonSet, error)
CreateDaemonset creates a daemonset with default options.
func (*TestCluster) CreatePersistentVolume ¶
func (t *TestCluster) CreatePersistentVolume(ctx context.Context, volume *v13.PersistentVolumeClaim) (*v13.PersistentVolumeClaim, error)
CreatePersistentVolume creates a persistent volume.
func (*TestCluster) CreateService ¶
func (t *TestCluster) CreateService(ctx context.Context, service *v13.Service) (*v13.Service, error)
CreateService is a helper method to create a service in a cluster.
func (*TestCluster) DeleteDaemonset ¶
DeleteDaemonset deletes a daemonset from this cluster.
func (*TestCluster) DeletePersistentVolume ¶
func (t *TestCluster) DeletePersistentVolume(ctx context.Context, volume *v13.PersistentVolumeClaim) error
DeletePersistentVolume deletes a persistent volume.
func (*TestCluster) DeleteService ¶
DeleteService is a helper to delete a given service.
func (*TestCluster) GetGVisorRuntimeLabelMap ¶
func (t *TestCluster) GetGVisorRuntimeLabelMap() map[string]string
GetGVisorRuntimeLabelMap returns the gVisor runtime key-value pair used on gVisor-runtime-enabled nodes.
func (*TestCluster) GetGVisorRuntimeToleration ¶
func (t *TestCluster) GetGVisorRuntimeToleration() v13.Toleration
GetGVisorRuntimeToleration returns a pod scheduling toleration that allows the pod to schedule on gVisor-runtime-enabled nodes.
func (*TestCluster) GetLogReader ¶
func (t *TestCluster) GetLogReader(ctx context.Context, pod *v13.Pod, opts v13.PodLogOptions) (io.ReadCloser, error)
GetLogReader gets an io.ReadCloser from which logs can be read. It is the caller's responsibility to close it.
func (*TestCluster) GetName ¶
func (t *TestCluster) GetName() string
GetName returns this cluster's name.
func (*TestCluster) GetPodsInDaemonSet ¶
func (t *TestCluster) GetPodsInDaemonSet(ctx context.Context, ds *appsv1.DaemonSet) ([]v13.Pod, error)
GetPodsInDaemonSet returns the list of pods of the given DaemonSet.
func (*TestCluster) HasGVisorTestRuntime ¶
func (t *TestCluster) HasGVisorTestRuntime(ctx context.Context) (bool, error)
HasGVisorTestRuntime returns whether the test nodes in this cluster use the gVisor runtime.
func (*TestCluster) ListServices ¶
func (t *TestCluster) ListServices(ctx context.Context, namespace string) (*v13.ServiceList, error)
ListServices is a helper method to List services in a cluster.
func (*TestCluster) Namespace ¶
func (t *TestCluster) Namespace(namespace string) *Namespace
Namespace returns a new namespace in this cluster.
func (*TestCluster) OverrideTestNodepoolRuntime ¶
func (t *TestCluster) OverrideTestNodepoolRuntime(testRuntime RuntimeType)
OverrideTestNodepoolRuntime overrides the runtime used for pods running on the test nodepool. If unset, the test nodepool's default runtime is used.
func (*TestCluster) ReadPodLogs ¶
ReadPodLogs reads logs from a pod.
func (*TestCluster) RuntimeTestNodepoolIsARM ¶
func (t *TestCluster) RuntimeTestNodepoolIsARM() bool
RuntimeTestNodepoolIsARM returns true if the runtime undertest nodepool is an ARM nodepool.
func (*TestCluster) StreamDaemonSetLogs ¶
func (t *TestCluster) StreamDaemonSetLogs(ctx context.Context, ds *appsv1.DaemonSet, opts v13.PodLogOptions, fn func(context.Context, v13.Pod, io.ReadCloser) error) error
StreamDaemonSetLogs streams the contents of a container from the given DaemonSet. The callback function is called once per node that the DaemonSet schedules on, with the reader corresponding to that node. The callback function is expected to close the reader. StreamDaemonSetLogs returns once the DaemonSet is ready everywhere that it is meant to be scheduled.
func (*TestCluster) WaitForDaemonset ¶
WaitForDaemonset waits until a daemonset has propagated containers across the affected nodes.
func (*TestCluster) WaitForPodCompleted ¶
WaitForPodCompleted is a helper method to wait for a pod to be completed.
func (*TestCluster) WaitForPodRunning ¶
WaitForPodRunning is a helper method to wait for a pod to be running.
func (*TestCluster) WaitForServiceReady ¶
WaitForServiceReady waits until a service is ready.