kubernetes

package
v0.0.0-...-c7702ad Latest Latest
Warning

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

Go to latest
Published: May 7, 2026 License: Apache-2.0 Imports: 32 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var LocalStorageClassGVR = schema.GroupVersionResource{
	Group:    "storage.deckhouse.io",
	Version:  "v1alpha1",
	Resource: "localstorageclasses",
}

Functions

func CreateLVMVolumeGroup

func CreateLVMVolumeGroup(ctx context.Context, kubeconfig *rest.Config, name, nodeName string, blockDeviceNames []string, actualVGName string) error

CreateLVMVolumeGroup creates an LVMVolumeGroup resource for a specific node

func CreateLVMVolumeGroupWithThinPool

func CreateLVMVolumeGroupWithThinPool(ctx context.Context, kubeconfig *rest.Config, name, nodeName string, blockDeviceNames []string, actualVGName string, thinPools []ThinPoolSpec) error

CreateLVMVolumeGroupWithThinPool creates an LVMVolumeGroup resource with thin pools for a specific node

func CreateLocalStorageClass

func CreateLocalStorageClass(ctx context.Context, kubeconfig *rest.Config, cfg LocalStorageClassConfig) error

func CreateNamespaceIfNotExists

func CreateNamespaceIfNotExists(ctx context.Context, config *rest.Config, name string) (*corev1.Namespace, error)

CreateNamespaceIfNotExists creates a namespace if it doesn't exist, or returns the existing one.

func CreateStaticNodeGroup

func CreateStaticNodeGroup(ctx context.Context, config *rest.Config, name string) error

CreateStaticNodeGroup creates a NodeGroup resource with Static nodeType

func DeleteLVMVolumeGroup

func DeleteLVMVolumeGroup(ctx context.Context, kubeconfig *rest.Config, name string) error

DeleteLVMVolumeGroup deletes an LVMVolumeGroup resource by name

func DetachAndDeleteVirtualDisk

func DetachAndDeleteVirtualDisk(ctx context.Context, kubeconfig *rest.Config, namespace, attachmentName, diskName string) error

DetachAndDeleteVirtualDisk deletes the VirtualMachineBlockDeviceAttachment and then the VirtualDisk. Use this for cleanup after a test. Errors are logged but not returned for "not found" (idempotent).

func EnableAndConfigureModules

func EnableAndConfigureModules(ctx context.Context, kubeconfig *rest.Config, clusterDef *config.ClusterDefinition, sshClient ssh.SSHClient) error

EnableAndConfigureModules enables and configures modules based on cluster definition It builds a dependency graph and processes modules level by level using topological sort After configuring each level, it waits for all modules in that level to become Ready before proceeding to the next level

func EnableModulesAndWait

func EnableModulesAndWait(ctx context.Context, kubeconfig *rest.Config, sshClient ssh.SSHClient, clusterDef *config.ClusterDefinition, modules []ModuleSpec, timeout time.Duration) error

EnableModulesAndWait is a convenience function that enables modules and waits for them to become ready in one call.

Parameters:

  • ctx: context for cancellation
  • kubeconfig: kubernetes client config
  • sshClient: SSH client for cluster access
  • clusterDef: cluster definition (can be nil for existing clusters)
  • modules: list of module specifications to enable
  • timeout: maximum time to wait for all modules to become ready

func EnableModulesWithSpecs

func EnableModulesWithSpecs(ctx context.Context, kubeconfig *rest.Config, sshClient ssh.SSHClient, clusterDef *config.ClusterDefinition, modules []ModuleSpec) error

EnableModulesWithSpecs enables and configures the specified modules in the test cluster. It handles dependencies automatically through topological sort and waits for each level of modules to become Ready before proceeding to the next level.

func FindSecretByName

func FindSecretByName(ctx context.Context, kubeconfig *rest.Config, namespace, name string) (string, error)

FindSecretByName finds a secret by name, trying multiple matching strategies This helps with issues where secret names might have hidden Unicode characters 1. Exact match 2. Case-insensitive match 3. Fuzzy match (ignoring common Unicode issues like non-breaking spaces) Returns the actual secret name found (which may differ from the requested name due to Unicode issues)

func FindUnsetEnvVars

func FindUnsetEnvVars(content string) []string

FindUnsetEnvVars finds all ${VAR} patterns in content and returns those that are not set

func GetDefaultStorageClassName

func GetDefaultStorageClassName(ctx context.Context, kubeconfig *rest.Config) (string, error)

GetDefaultStorageClassName returns the name of the current default StorageClass (annotated with storageclass.kubernetes.io/is-default-class=true), or "" if none exists.

func GetNodeTaints

func GetNodeTaints(ctx context.Context, kubeconfig *rest.Config, nodeName string) ([]corev1.Taint, error)

GetNodeTaints returns the taints of the named node.

func GetNodes

func GetNodes(ctx context.Context, kubeconfig *rest.Config) ([]corev1.Node, error)

GetNodes returns the names of all nodes in the cluster.

func GetSecretDataValue

func GetSecretDataValue(ctx context.Context, kubeconfig *rest.Config, namespace, name, key string) (string, error)

GetSecretDataValue retrieves a specific data value from a secret by name It uses FindSecretByName to handle potential Unicode character issues

func GetStorageClass

func GetStorageClass(ctx context.Context, kubeconfig *rest.Config, name string) (*storagev1.StorageClass, error)

GetStorageClass returns the StorageClass with the given name, or (nil, nil) if it does not exist.

func GetVMIPFromBaseCluster

func GetVMIPFromBaseCluster(ctx context.Context, baseKubeconfig *rest.Config, namespace, vmName string) (string, error)

GetVMIPFromBaseCluster returns the IP address of a VirtualMachine in the base cluster (namespace). Used to SSH to the VM (e.g. cloud@ip) from the jump host to run lsblk on nested nodes.

func GetVMPodNodeAndContainerID

func GetVMPodNodeAndContainerID(ctx context.Context, baseConfig *rest.Config, namespace, vmName string) (nodeName, containerID string, err error)

GetVMPodNodeAndContainerID returns the base cluster node name and the first container ID for the Pod that runs the given VM (e.g. virt-launcher-<vmName>-*). Used to run nsenter into the VM container from the base cluster node.

func GetWorkerNodes

func GetWorkerNodes(ctx context.Context, kubeconfig *rest.Config) ([]corev1.Node, error)

GetWorkerNodes returns all worker nodes in the cluster. A worker node is any node that does NOT have the "node-role.kubernetes.io/control-plane" label.

func IsNodeCordoned

func IsNodeCordoned(ctx context.Context, kubeconfig *rest.Config, nodeName string) (bool, error)

IsNodeCordoned checks whether a node has NoSchedule or NoExecute taints that would prevent DaemonSet pods from scheduling.

func LabelNodes

func LabelNodes(ctx context.Context, kubeconfig *rest.Config, nodeNames []string, labelKey, labelValue string) error

LabelNodes adds a label to each of the specified nodes. If a node already has the label with the desired value, it is skipped. Uses retry with re-fetch to handle optimistic concurrency conflicts.

func ListVirtualMachineNames

func ListVirtualMachineNames(ctx context.Context, kubeconfig *rest.Config, namespace string) ([]string, error)

ListVirtualMachineNames returns names of VirtualMachines in the given namespace. Used to pick a VM when attaching a VirtualDisk (e.g. in alwaysUseExisting mode).

func NewClientsetWithRetry

func NewClientsetWithRetry(ctx context.Context, config *rest.Config) (*kubernetes.Clientset, error)

NewClientsetWithRetry creates a new Kubernetes clientset with retry logic for transient network errors. While kubernetes.NewForConfig itself does not make network calls, this wrapper provides a centralized factory with retry that validates the connection by performing a lightweight server version check. This ensures the cluster is reachable before returning the clientset.

func NewDynamicClientWithRetry

func NewDynamicClientWithRetry(ctx context.Context, config *rest.Config) (dynamic.Interface, error)

NewDynamicClientWithRetry creates a new Kubernetes dynamic client with retry logic for transient network errors. Similar to NewClientsetWithRetry, this provides a centralized factory for dynamic clients with built-in retry.

func ResizeList

func ResizeList(ctx context.Context, clientset *kubernetes.Clientset, namespace string, pvcNames []string, newSize string) error

ResizeList resizes multiple PVCs to a new size in parallel

func SetGlobalDefaultStorageClass

func SetGlobalDefaultStorageClass(ctx context.Context, kubeconfig *rest.Config, storageClassName string) error

SetGlobalDefaultStorageClass updates the "global" ModuleConfig to set spec.settings.storageClass to the given name, making it the cluster default.

func WaitForAllPodsReadyInNamespace

func WaitForAllPodsReadyInNamespace(ctx context.Context, kubeconfig *rest.Config, namespace string, timeout time.Duration) error

WaitForAllPodsReadyInNamespace waits for all pods in a namespace to be in Ready condition

func WaitForLVMVolumeGroupDeletion

func WaitForLVMVolumeGroupDeletion(ctx context.Context, kubeconfig *rest.Config, name string, timeout time.Duration) error

WaitForLVMVolumeGroupDeletion waits for an LVMVolumeGroup to be deleted

func WaitForLVMVolumeGroupReady

func WaitForLVMVolumeGroupReady(ctx context.Context, kubeconfig *rest.Config, name string, timeout time.Duration) error

WaitForLVMVolumeGroupReady waits for an LVMVolumeGroup to become Ready

func WaitForLocalStorageClassCreated

func WaitForLocalStorageClassCreated(ctx context.Context, kubeconfig *rest.Config, name string, timeout time.Duration) error

WaitForLocalStorageClassCreated waits for the LocalStorageClass CR status to indicate that the controller has created the corresponding StorageClass.

func WaitForModuleReady

func WaitForModuleReady(ctx context.Context, kubeconfig *rest.Config, moduleName string, timeout time.Duration) error

WaitForModuleReady waits for a module to reach the Ready phase It continues waiting even if the module is temporarily in Error phase, as modules can recover. Only fails if the timeout is exceeded and the module is still not Ready.

func WaitForModulesReady

func WaitForModulesReady(ctx context.Context, kubeconfig *rest.Config, clusterDef *config.ClusterDefinition, timeout time.Duration) error

WaitForModulesReady waits for all modules specified in cluster definition to be ready It builds a dependency graph and waits for modules level by level using topological sort

func WaitForModulesReadyWithSpecs

func WaitForModulesReadyWithSpecs(ctx context.Context, kubeconfig *rest.Config, clusterDef *config.ClusterDefinition, modules []ModuleSpec, timeout time.Duration) error

WaitForModulesReadyWithSpecs waits for the specified modules to become ready. This is typically called after EnableModulesWithSpecs to ensure all modules are operational.

Parameters:

  • ctx: context for cancellation
  • kubeconfig: kubernetes client config
  • clusterDef: cluster definition (can be nil for existing clusters)
  • modules: list of module specifications to wait for
  • timeout: maximum time to wait for all modules

func WaitForNodesLabeled

func WaitForNodesLabeled(ctx context.Context, kubeconfig *rest.Config, nodeNames []string, labelKey, labelValue string) error

WaitForNodesLabeled waits for all specified nodes to have the given label with the expected value. It polls each node in parallel every 10 seconds until all nodes have the label or the context times out. Parameters:

  • ctx: context with timeout/cancellation
  • kubeconfig: Kubernetes REST config
  • nodeNames: list of node names to check
  • labelKey: the label key to look for (e.g., "storage.deckhouse.io/node-ready-for-iscsi")
  • labelValue: the expected label value (e.g., "true")

func WaitForPVCsBound

func WaitForPVCsBound(ctx context.Context, clientset *kubernetes.Clientset, namespace, labelSelector string, expectedCount int, maxAttempts int, interval time.Duration) error

WaitForPVCsBound waits for PVCs matching the label selector to be in Bound state

func WaitForPVCsResized

func WaitForPVCsResized(ctx context.Context, clientset *kubernetes.Clientset, namespace string, pvcNames []string, targetSize string, maxAttempts int, interval time.Duration) error

WaitForPVCsResized waits for PVCs to be resized to the target size

func WaitForPodsStatus

func WaitForPodsStatus(ctx context.Context, clientset *kubernetes.Clientset, namespace, labelSelector, status string, expectedCount int, maxAttempts int, interval time.Duration) error

WaitForPodsStatus waits for pods to reach a specific status

func WaitForStorageClass

func WaitForStorageClass(ctx context.Context, kubeconfig *rest.Config, storageClassName string, timeout time.Duration) error

WaitForStorageClass waits for a storage class to become available

func WaitForStorageClasses

func WaitForStorageClasses(ctx context.Context, kubeconfig *rest.Config, storageClassNames []string, timeout time.Duration) map[string]error

WaitForStorageClasses waits for multiple storage classes to become available in parallel Returns map of storage class names to errors (nil if successful, error if failed/not found)

func WaitForVirtualDiskAttached

func WaitForVirtualDiskAttached(ctx context.Context, kubeconfig *rest.Config, namespace, attachmentName string, pollInterval time.Duration) error

WaitForVirtualDiskAttached waits for the VirtualMachineBlockDeviceAttachment to reach the Attached phase. It polls the attachment status until it's attached or the context is cancelled/times out. The pollInterval parameter specifies how often to check the status (recommended: 10 seconds).

Types

type ApplyClient

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

ApplyClient handles applying YAML manifests to a Kubernetes cluster

func NewApplyClient

func NewApplyClient(config *rest.Config) (*ApplyClient, error)

NewApplyClient creates a new ApplyClient Includes retry logic for transient network errors during client creation

func (*ApplyClient) ApplyYAML

func (c *ApplyClient) ApplyYAML(ctx context.Context, yamlContent string, namespace string) error

ApplyYAML applies YAML manifest(s) to the cluster The yamlContent can contain multiple YAML documents separated by "---" namespace parameter is optional - if empty, uses namespace from manifest or "default"

func (*ApplyClient) CreateYAML

func (c *ApplyClient) CreateYAML(ctx context.Context, yamlContent string, namespace string) error

CreateYAML creates resources from YAML manifest(s) Unlike ApplyYAML, this will fail if resources already exist

func (*ApplyClient) CreateYAMLFromFileWithEnvvars

func (c *ApplyClient) CreateYAMLFromFileWithEnvvars(ctx context.Context, filePath string, namespace string) error

CreateYAMLFromFileWithEnvvars reads a YAML file, validates environment variables, substitutes them, and creates resources Returns error if file cannot be read, any ${VAR} is not set, or resource creation fails

type BlockDevice

type BlockDevice = storage.BlockDeviceInfo

BlockDevice represents a block device in the cluster (re-export for public API)

func GetConsumableBlockDevices

func GetConsumableBlockDevices(ctx context.Context, kubeconfig *rest.Config) ([]BlockDevice, error)

GetConsumableBlockDevices returns all consumable BlockDevices from the cluster

func GetConsumableBlockDevicesByNode

func GetConsumableBlockDevicesByNode(ctx context.Context, kubeconfig *rest.Config, nodeName string) ([]BlockDevice, error)

GetConsumableBlockDevicesByNode returns consumable BlockDevices for a specific node.

type LocalStorageClassConfig

type LocalStorageClassConfig struct {
	Name              string
	LVMVolumeGroups   []string // LVMVolumeGroup resource names
	LVMType           string   // "Thick" or "Thin"
	ThinPoolName      string   // required when LVMType is "Thin"
	ReclaimPolicy     string   // "Delete" or "Retain" (default: "Delete")
	VolumeBindingMode string   // "WaitForFirstConsumer" or "Immediate" (default: "WaitForFirstConsumer")
}

type ModuleSpec

type ModuleSpec struct {
	// Name is the name of the module (e.g., "snapshot-controller", "csi-hpe")
	Name string

	// Version is the module config version (typically 1)
	Version int

	// Enabled indicates whether the module should be enabled
	Enabled bool

	// Settings contains module-specific settings
	Settings map[string]interface{}

	// Dependencies lists module names that must be enabled before this one
	Dependencies []string

	// ModulePullOverride overrides the module pull branch/tag (e.g., "main", "pr123")
	// Only used for dev registries (registries starting with "dev-")
	ModulePullOverride string
}

ModuleSpec defines a module to be enabled in the cluster. This is a simplified version of config.ModuleConfig that provides a clean API for test writers.

type TestClusterResourcesInterface

type TestClusterResourcesInterface interface {
	GetKubeconfig() *rest.Config
	GetSSHClient() ssh.SSHClient
	GetClusterDefinition() *config.ClusterDefinition
}

TestClusterResourcesInterface defines the interface for accessing test cluster resources This avoids circular imports with the cluster package

type ThinPoolSpec

type ThinPoolSpec struct {
	Name            string // Thin pool name
	Size            string // Size of the thin pool (e.g., "50%" or "10Gi")
	AllocationLimit string // Allocation limit (optional)
}

ThinPoolSpec represents a thin pool specification for LVMVolumeGroup

type VirtualDiskAttachmentConfig

type VirtualDiskAttachmentConfig struct {
	// VMName is the name of the VirtualMachine to attach the disk to
	VMName string
	// Namespace is the namespace where the VM and disk resources are located
	Namespace string
	// DiskName is the name for the new VirtualDisk (optional, auto-generated if empty)
	DiskName string
	// DiskSize is the size of the disk (e.g., "200Gi")
	DiskSize string
	// StorageClassName is the storage class to use for the disk
	StorageClassName string
}

VirtualDiskAttachmentConfig holds configuration for attaching a virtual disk to a VM

type VirtualDiskAttachmentResult

type VirtualDiskAttachmentResult struct {
	// DiskName is the name of the created VirtualDisk
	DiskName string
	// AttachmentName is the name of the created VirtualMachineBlockDeviceAttachment
	AttachmentName string
}

VirtualDiskAttachmentResult holds the result of attaching a virtual disk

func AttachVirtualDiskToVM

func AttachVirtualDiskToVM(ctx context.Context, kubeconfig *rest.Config, config VirtualDiskAttachmentConfig) (*VirtualDiskAttachmentResult, error)

AttachVirtualDiskToVM creates a VirtualDisk and attaches it to the specified VM using VirtualMachineBlockDeviceAttachment. The disk is created as a blank disk with the specified size and storage class. Returns the names of created resources for later use (e.g., waiting for attachment or cleanup).

Jump to

Keyboard shortcuts

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