utils

package
v0.0.13 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2025 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DetermineClusterProvider

func DetermineClusterProvider(contextName string) (string, error)

DetermineClusterProvider attempts to identify the cloud provider (e.g., AWS, Azure, GCP) of a Kubernetes cluster by inspecting the `providerID` of the first node. It uses `kubectl get nodes -o jsonpath={.items[0].spec.providerID}`. If `providerID` is not available, it falls back to `determineProviderFromLabels`. - contextName: The Kubernetes context to use. If empty, the current context is used. Returns the determined provider name (e.g., "aws") or "unknown", and an error if `kubectl` fails.

func GetCurrentKubeContext

func GetCurrentKubeContext() (string, error)

GetCurrentKubeContext retrieves the name of the currently active Kubernetes context using `kubectl config current-context`. Returns the context name (trimmed of whitespace) and an error if the command fails.

func GetNodeStatusClientGo

func GetNodeStatusClientGo(kubeContext string) (readyNodes int, totalNodes int, err error)

GetNodeStatusClientGo retrieves the number of ready and total nodes in a cluster using client-go. - kubeContext: The Kubernetes context to target. Returns the count of ready nodes, total nodes, and an error if any occurs.

func LoginToKubeCluster

func LoginToKubeCluster(clusterName string) (stdout string, stderr string, err error)

LoginToKubeCluster executes `tsh kube login <clusterName>` to authenticate with a Teleport Kubernetes cluster. It captures and returns the standard output and standard error from the command. Note: This function currently passes os.Stdin to the command, which might cause issues if `tsh` prompts for interactive input (e.g., 2FA) in a non-interactive environment like the TUI. - clusterName: The name of the Teleport Kubernetes cluster to log into. Returns the stdout string, stderr string, and an error if the command execution fails.

func StartPortForwardClientGo

func StartPortForwardClientGo(
	kubeContext string,
	namespace string,
	serviceArg string,
	portString string,
	pfLabel string,
	sendUpdate SendUpdateFunc,
) (chan struct{}, string, error)

StartPortForwardClientGo establishes a port-forward to a Kubernetes pod using the client-go library. This function handles the entire setup: parsing ports, loading Kubernetes configuration for the specified context, creating a clientset, resolving the service/pod name to a target pod, constructing the port-forwarding URL, and finally, creating and starting the port forwarder in a new goroutine. It returns a channel that can be used to stop the port-forwarding process, an initial status message indicating the setup attempt, and any error encountered during the synchronous part of the setup. Asynchronous updates (status changes, logs, errors, readiness) are sent via the provided `sendUpdate` callback.

Parameters: - kubeContext: The name of the Kubernetes context to use. - namespace: The Kubernetes namespace where the target service/pod resides. - serviceArg: A string specifying the target, e.g., "service/my-service" or "pod/my-pod". - portString: The port mapping, e.g., "localPort:remotePort" (e.g., "8080:80"). - pfLabel: A user-friendly label for this port-forward, used in updates sent via `sendUpdate`. - sendUpdate: The callback function (SendUpdateFunc) for sending asynchronous updates.

Returns: - chan struct{}: A channel that, when closed, signals the port-forwarding goroutine to stop. - string: An initial status message (e.g., "Initializing...") from the synchronous setup phase. - error: Any error that occurred during the synchronous setup before the goroutine was started.

func SwitchKubeContext

func SwitchKubeContext(contextName string) error

SwitchKubeContext changes the active Kubernetes context to the specified context name using `kubectl config use-context <contextName>`. - contextName: The name of the Kubernetes context to switch to. Returns an error if the command fails, including the command's output in the error message.

Types

type ClusterInfo

type ClusterInfo struct {
	ManagementClusters []string            // A list of standalone management cluster names.
	WorkloadClusters   map[string][]string // A map where the key is a management cluster name,

}

ClusterInfo holds structured information about Kubernetes clusters, as parsed from `tsh kube ls` output. It differentiates between management clusters and their associated workload clusters.

func GetClusterInfo

func GetClusterInfo() (*ClusterInfo, error)

GetClusterInfo executes the `tsh kube ls` command and parses its output to populate a ClusterInfo struct. The parsing logic attempts to distinguish between management clusters (e.g., "ceres") and workload clusters (e.g., "ceres-bobcat") based on naming conventions (presence of a hyphen). It returns a pointer to the populated ClusterInfo struct and an error if `tsh kube ls` fails or parsing encounters issues.

type SendUpdateFunc

type SendUpdateFunc func(status, outputLog string, isError, isReady bool)

SendUpdateFunc defines a callback function signature used by port-forwarding logic to send status updates (including log messages, errors, and readiness status) back to the caller, typically the TUI, for display and state management.

Jump to

Keyboard shortcuts

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