Documentation
¶
Overview ¶
Package capi provides a Go client for interacting with Cluster API (CAPI) resources.
This package implements a comprehensive client for managing Kubernetes clusters through the Cluster API, supporting operations on clusters, machines, machine deployments, and nodes across multiple infrastructure providers.
Key Components ¶
Client: The main client struct that provides methods for all CAPI operations. It wraps both the standard Kubernetes client and the controller-runtime client for optimal interaction with CAPI resources.
Providers: Support for multiple infrastructure providers including AWS, Azure, GCP, and vSphere. The client can automatically detect the provider type from cluster resources.
Basic Usage ¶
// Create a new CAPI client
client, err := capi.NewClient("") // Uses default kubeconfig
if err != nil {
log.Fatal(err)
}
// List all clusters
clusters, err := client.ListClusters(ctx, "default")
if err != nil {
log.Fatal(err)
}
// Get cluster status
status, err := client.GetClusterStatus(ctx, "default", "my-cluster")
if err != nil {
log.Fatal(err)
}
Cluster Operations ¶
The package provides comprehensive cluster management capabilities:
- Create, update, and delete clusters
- Scale control plane and worker nodes
- Upgrade Kubernetes versions
- Pause and resume cluster reconciliation
- Move clusters between management clusters
- Backup cluster configurations
Machine Operations ¶
Machine-level operations include:
- List and get machine details
- Delete machines with proper draining
- Trigger machine remediation
- Create and manage machine deployments
- Scale machine deployments
- Perform rolling updates
Node Operations ¶
The package also provides limited node operations:
- Cordon and uncordon nodes
- Drain nodes (partial implementation)
- Get node status and information
Note: Full node operations require access to the workload cluster, which may not always be available from the management cluster context.
Error Handling ¶
All methods return detailed errors that can be inspected for specific failure conditions. The package uses fmt.Errorf with %w for error wrapping, allowing errors to be unwrapped and inspected.
Thread Safety ¶
The Client struct and its methods are thread-safe and can be used concurrently from multiple goroutines.
Index ¶
- func FormatClusterInfo(status *ClusterStatus) string
- func GetControlPlaneStatus(kcp *controlplanev1.KubeadmControlPlane) string
- func GetMachinePhase(machine *clusterv1.Machine) string
- type BackupClusterOptions
- type Client
- func (c *Client) BackupCluster(ctx context.Context, opts BackupClusterOptions) (string, error)
- func (c *Client) CordonNode(ctx context.Context, opts NodeOperationOptions) error
- func (c *Client) CreateCluster(ctx context.Context, opts CreateClusterOptions) (*clusterv1.Cluster, error)
- func (c *Client) CreateMachineDeployment(ctx context.Context, opts CreateMachineDeploymentOptions) (*clusterv1.MachineDeployment, error)
- func (c *Client) DeleteCluster(ctx context.Context, namespace, name string) error
- func (c *Client) DeleteMachine(ctx context.Context, opts DeleteMachineOptions) error
- func (c *Client) DrainNode(ctx context.Context, opts NodeOperationOptions) error
- func (c *Client) FindClustersByLabelValue(ctx context.Context, namespace string, searchTerm string) (*clusterv1.ClusterList, error)
- func (c *Client) GetCluster(ctx context.Context, namespace, name string) (*clusterv1.Cluster, error)
- func (c *Client) GetClusterHealth(ctx context.Context, namespace, name string) (*ClusterHealthStatus, error)
- func (c *Client) GetClusterStatus(ctx context.Context, namespace, name string) (*ClusterStatus, error)
- func (c *Client) GetClusterStatusFromList(ctx context.Context, cluster *clusterv1.Cluster, machines []clusterv1.Machine) (*ClusterStatus, error)
- func (c *Client) GetCtrlClient() client.Client
- func (c *Client) GetInfrastructureResource(ctx context.Context, ref *client.ObjectKey, into client.Object) error
- func (c *Client) GetK8sClient() kubernetes.Interface
- func (c *Client) GetKubeadmControlPlane(ctx context.Context, namespace, name string) (*controlplanev1.KubeadmControlPlane, error)
- func (c *Client) GetKubeconfig(ctx context.Context, namespace, clusterName string) (string, error)
- func (c *Client) GetMachine(ctx context.Context, namespace, name string) (*clusterv1.Machine, error)
- func (c *Client) GetMachineDeployment(ctx context.Context, namespace, name string) (*clusterv1.MachineDeployment, error)
- func (c *Client) GetMachineSet(ctx context.Context, namespace, name string) (*clusterv1.MachineSet, error)
- func (c *Client) GetNodeStatus(ctx context.Context, opts NodeOperationOptions) (*corev1.Node, error)
- func (c *Client) GetProviderForCluster(ctx context.Context, namespace, clusterName string) (Provider, error)
- func (c *Client) InitializeProviders() error
- func (c *Client) IsClusterReady(ctx context.Context, namespace, name string) (bool, error)
- func (c *Client) ListClusters(ctx context.Context, namespace string, labelSelector map[string]string) (*clusterv1.ClusterList, error)
- func (c *Client) ListKubeadmControlPlanes(ctx context.Context, namespace string) (*controlplanev1.KubeadmControlPlaneList, error)
- func (c *Client) ListMachineDeployments(ctx context.Context, namespace, clusterName string) (*clusterv1.MachineDeploymentList, error)
- func (c *Client) ListMachineSets(ctx context.Context, namespace, clusterName string) (*clusterv1.MachineSetList, error)
- func (c *Client) ListMachines(ctx context.Context, namespace, clusterName string) (*clusterv1.MachineList, error)
- func (c *Client) MoveCluster(ctx context.Context, opts MoveClusterOptions) (string, error)
- func (c *Client) PauseCluster(ctx context.Context, namespace, name string) error
- func (c *Client) RemediateMachine(ctx context.Context, opts RemediateMachineOptions) error
- func (c *Client) ResumeCluster(ctx context.Context, namespace, name string) error
- func (c *Client) RolloutMachineDeployment(ctx context.Context, opts RolloutMachineDeploymentOptions) error
- func (c *Client) ScaleCluster(ctx context.Context, namespace, clusterName, target string, replicas int, ...) error
- func (c *Client) ScaleControlPlane(ctx context.Context, namespace, name string, replicas int32) error
- func (c *Client) ScaleMachineDeployment(ctx context.Context, namespace, name string, replicas int32) error
- func (c *Client) SetClients(k8sClient kubernetes.Interface, ctrlClient client.Client)
- func (c *Client) UpdateCluster(ctx context.Context, opts UpdateClusterOptions) (*clusterv1.Cluster, error)
- func (c *Client) UpdateMachineDeployment(ctx context.Context, opts UpdateMachineDeploymentOptions) (*clusterv1.MachineDeployment, error)
- func (c *Client) UpgradeCluster(ctx context.Context, opts UpgradeClusterOptions) error
- func (c *Client) WaitForClusterReady(ctx context.Context, namespace, name string) error
- type ClusterHealthStatus
- type ClusterStatus
- type CreateClusterOptions
- type CreateMachineDeploymentOptions
- type DeleteMachineOptions
- type MoveClusterOptions
- type NodeOperationOptions
- type Provider
- type RemediateMachineOptions
- type RolloutMachineDeploymentOptions
- type UpdateClusterOptions
- type UpdateMachineDeploymentOptions
- type UpgradeClusterOptions
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FormatClusterInfo ¶
func FormatClusterInfo(status *ClusterStatus) string
FormatClusterInfo formats cluster information for display
func GetControlPlaneStatus ¶
func GetControlPlaneStatus(kcp *controlplanev1.KubeadmControlPlane) string
GetControlPlaneStatus returns the status of a KubeadmControlPlane
func GetMachinePhase ¶
GetMachinePhase returns a human-readable phase for a machine
Types ¶
type BackupClusterOptions ¶
type BackupClusterOptions struct {
Namespace string
Name string
IncludeSecrets bool
OutputFormat string // yaml or json
}
BackupClusterOptions contains options for backing up a cluster
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client provides access to CAPI resources in a Kubernetes cluster
func (*Client) BackupCluster ¶
BackupCluster creates a backup of cluster resources
func (*Client) CordonNode ¶
func (c *Client) CordonNode(ctx context.Context, opts NodeOperationOptions) error
CordonNode cordons or uncordons a node
func (*Client) CreateCluster ¶
func (c *Client) CreateCluster(ctx context.Context, opts CreateClusterOptions) (*clusterv1.Cluster, error)
CreateCluster creates a new CAPI cluster with basic configuration
func (*Client) CreateMachineDeployment ¶
func (c *Client) CreateMachineDeployment(ctx context.Context, opts CreateMachineDeploymentOptions) (*clusterv1.MachineDeployment, error)
CreateMachineDeployment creates a new CAPI MachineDeployment
func (*Client) DeleteCluster ¶
DeleteCluster deletes a CAPI cluster
func (*Client) DeleteMachine ¶
func (c *Client) DeleteMachine(ctx context.Context, opts DeleteMachineOptions) error
DeleteMachine deletes a CAPI machine
func (*Client) DrainNode ¶
func (c *Client) DrainNode(ctx context.Context, opts NodeOperationOptions) error
DrainNode safely drains a node
func (*Client) FindClustersByLabelValue ¶ added in v0.0.43
func (c *Client) FindClustersByLabelValue(ctx context.Context, namespace string, searchTerm string) (*clusterv1.ClusterList, error)
FindClustersByLabelValue searches for clusters where any label value matches the given search term. This is useful when users refer to clusters by a human-friendly identifier stored in labels (e.g. a "friendly-name" or "cluster-id" label) rather than the Kubernetes resource name.
func (*Client) GetCluster ¶
func (c *Client) GetCluster(ctx context.Context, namespace, name string) (*clusterv1.Cluster, error)
GetCluster retrieves a specific cluster
func (*Client) GetClusterHealth ¶
func (c *Client) GetClusterHealth(ctx context.Context, namespace, name string) (*ClusterHealthStatus, error)
GetClusterHealth checks the health of a cluster
func (*Client) GetClusterStatus ¶
func (c *Client) GetClusterStatus(ctx context.Context, namespace, name string) (*ClusterStatus, error)
GetClusterStatus retrieves comprehensive status information for a cluster
func (*Client) GetClusterStatusFromList ¶ added in v0.0.43
func (c *Client) GetClusterStatusFromList(ctx context.Context, cluster *clusterv1.Cluster, machines []clusterv1.Machine) (*ClusterStatus, error)
GetClusterStatusFromList computes status for an already-fetched cluster using pre-fetched machines. This avoids the per-cluster Get and ListMachines calls that GetClusterStatus performs, making it suitable for bulk listing.
func (*Client) GetCtrlClient ¶
GetCtrlClient returns the controller-runtime client
func (*Client) GetInfrastructureResource ¶
func (c *Client) GetInfrastructureResource(ctx context.Context, ref *client.ObjectKey, into client.Object) error
GetInfrastructureResource retrieves an infrastructure-specific resource as unstructured
func (*Client) GetK8sClient ¶
func (c *Client) GetK8sClient() kubernetes.Interface
GetK8sClient returns the standard Kubernetes client
func (*Client) GetKubeadmControlPlane ¶
func (c *Client) GetKubeadmControlPlane(ctx context.Context, namespace, name string) (*controlplanev1.KubeadmControlPlane, error)
GetKubeadmControlPlane retrieves the KubeadmControlPlane for a cluster
func (*Client) GetKubeconfig ¶
GetKubeconfig retrieves the kubeconfig for a workload cluster
func (*Client) GetMachine ¶
func (c *Client) GetMachine(ctx context.Context, namespace, name string) (*clusterv1.Machine, error)
GetMachine retrieves a specific machine
func (*Client) GetMachineDeployment ¶
func (c *Client) GetMachineDeployment(ctx context.Context, namespace, name string) (*clusterv1.MachineDeployment, error)
GetMachineDeployment retrieves a specific machine deployment
func (*Client) GetMachineSet ¶
func (c *Client) GetMachineSet(ctx context.Context, namespace, name string) (*clusterv1.MachineSet, error)
GetMachineSet retrieves a specific MachineSet
func (*Client) GetNodeStatus ¶
func (c *Client) GetNodeStatus(ctx context.Context, opts NodeOperationOptions) (*corev1.Node, error)
GetNodeStatus gets the status of a node in the workload cluster
func (*Client) GetProviderForCluster ¶
func (c *Client) GetProviderForCluster(ctx context.Context, namespace, clusterName string) (Provider, error)
GetProviderForCluster determines which infrastructure provider a cluster is using
func (*Client) InitializeProviders ¶
InitializeProviders adds all provider schemes to the client
func (*Client) IsClusterReady ¶
IsClusterReady checks if a cluster is fully ready
func (*Client) ListClusters ¶
func (c *Client) ListClusters(ctx context.Context, namespace string, labelSelector map[string]string) (*clusterv1.ClusterList, error)
ListClusters lists all CAPI clusters in the given namespace
func (*Client) ListKubeadmControlPlanes ¶
func (c *Client) ListKubeadmControlPlanes(ctx context.Context, namespace string) (*controlplanev1.KubeadmControlPlaneList, error)
ListKubeadmControlPlanes lists all KubeadmControlPlanes
func (*Client) ListMachineDeployments ¶
func (c *Client) ListMachineDeployments(ctx context.Context, namespace, clusterName string) (*clusterv1.MachineDeploymentList, error)
ListMachineDeployments lists all machine deployments
func (*Client) ListMachineSets ¶
func (c *Client) ListMachineSets(ctx context.Context, namespace, clusterName string) (*clusterv1.MachineSetList, error)
ListMachineSets lists all MachineSets in a namespace
func (*Client) ListMachines ¶
func (c *Client) ListMachines(ctx context.Context, namespace, clusterName string) (*clusterv1.MachineList, error)
ListMachines lists all machines for a given cluster
func (*Client) MoveCluster ¶
MoveCluster prepares a cluster for migration to another management cluster Note: This is a simplified implementation that exports the cluster resources
func (*Client) PauseCluster ¶
PauseCluster pauses reconciliation for a cluster by adding the cluster.x-k8s.io/paused annotation
func (*Client) RemediateMachine ¶
func (c *Client) RemediateMachine(ctx context.Context, opts RemediateMachineOptions) error
RemediateMachine triggers machine health check remediation by annotating the machine
func (*Client) ResumeCluster ¶
ResumeCluster resumes reconciliation for a cluster by removing the cluster.x-k8s.io/paused annotation
func (*Client) RolloutMachineDeployment ¶
func (c *Client) RolloutMachineDeployment(ctx context.Context, opts RolloutMachineDeploymentOptions) error
RolloutMachineDeployment triggers a rolling update of a MachineDeployment
func (*Client) ScaleCluster ¶
func (c *Client) ScaleCluster(ctx context.Context, namespace, clusterName, target string, replicas int, machineDeploymentName string) error
ScaleCluster scales either control plane or worker nodes of a cluster
func (*Client) ScaleControlPlane ¶
func (c *Client) ScaleControlPlane(ctx context.Context, namespace, name string, replicas int32) error
ScaleControlPlane scales a KubeadmControlPlane to the specified number of replicas
func (*Client) ScaleMachineDeployment ¶
func (c *Client) ScaleMachineDeployment(ctx context.Context, namespace, name string, replicas int32) error
ScaleMachineDeployment scales a MachineDeployment to the specified number of replicas
func (*Client) SetClients ¶ added in v0.0.26
func (c *Client) SetClients(k8sClient kubernetes.Interface, ctrlClient client.Client)
SetClients sets the Kubernetes and controller-runtime clients (for testing)
func (*Client) UpdateCluster ¶
func (c *Client) UpdateCluster(ctx context.Context, opts UpdateClusterOptions) (*clusterv1.Cluster, error)
UpdateCluster updates a CAPI cluster's metadata
func (*Client) UpdateMachineDeployment ¶
func (c *Client) UpdateMachineDeployment(ctx context.Context, opts UpdateMachineDeploymentOptions) (*clusterv1.MachineDeployment, error)
UpdateMachineDeployment updates a MachineDeployment's configuration
func (*Client) UpgradeCluster ¶
func (c *Client) UpgradeCluster(ctx context.Context, opts UpgradeClusterOptions) error
UpgradeCluster upgrades a CAPI cluster to a new Kubernetes version
type ClusterHealthStatus ¶
type ClusterHealthStatus struct {
Healthy bool
ControlPlaneReady bool
WorkersReady bool
InfraReady bool
Issues []string
Warnings []string
}
ClusterHealthStatus represents the health status of a cluster
type ClusterStatus ¶
type ClusterStatus struct {
Name string
Namespace string
Phase string
Ready bool
ControlPlaneReady bool
InfraReady bool
Version string
Provider Provider
TotalMachines int
ReadyMachines int
Conditions clusterv1.Conditions
Paused bool
Labels map[string]string
Annotations map[string]string
}
ClusterStatus represents the status of a CAPI cluster
type CreateClusterOptions ¶
type CreateClusterOptions struct {
Name string
Namespace string
InfraProvider string
KubernetesVersion string
ControlPlaneCount int32
WorkerCount int32
Region string
InstanceType string
}
CreateClusterOptions contains options for creating a new cluster
type CreateMachineDeploymentOptions ¶
type CreateMachineDeploymentOptions struct {
Namespace string
Name string
ClusterName string
Replicas int32
InfrastructureRef corev1.ObjectReference
BootstrapConfigRef corev1.ObjectReference
Version string
Labels map[string]string
NodeDrainTimeout *metav1.Duration
MinReadySeconds int32
}
CreateMachineDeploymentOptions contains options for creating a machine deployment
type DeleteMachineOptions ¶
DeleteMachineOptions contains options for deleting a machine
type MoveClusterOptions ¶
type MoveClusterOptions struct {
Namespace string
Name string
TargetKubeconfig string
TargetNamespace string
DryRun bool
}
MoveClusterOptions contains options for moving a cluster
type NodeOperationOptions ¶
type NodeOperationOptions struct {
Namespace string
MachineName string
NodeName string
// For drain operations
GracePeriodSeconds *int32
IgnoreDaemonSets bool
DeleteLocalData bool
Force bool
// For cordon operations
Uncordon bool
}
NodeOperationOptions contains options for node operations
type Provider ¶
type Provider string
Provider represents an infrastructure provider
func DetermineProvider ¶ added in v0.0.43
DetermineProvider returns the infrastructure provider for a cluster based on its InfrastructureRef.Kind. Unlike GetProviderForCluster, this is a pure function that requires no API call.
type RemediateMachineOptions ¶
RemediateMachineOptions contains options for remediating a machine
type RolloutMachineDeploymentOptions ¶
RolloutMachineDeploymentOptions contains options for triggering a rollout
type UpdateClusterOptions ¶
type UpdateClusterOptions struct {
Namespace string
Name string
Labels map[string]string
Annotations map[string]string
}
UpdateClusterOptions contains options for updating a cluster
type UpdateMachineDeploymentOptions ¶
type UpdateMachineDeploymentOptions struct {
Namespace string
Name string
Version *string
Replicas *int32
Labels map[string]string
Annotations map[string]string
MinReadySeconds *int32
NodeDrainTimeout *metav1.Duration
}
UpdateMachineDeploymentOptions contains options for updating a machine deployment