kubectl

package
v0.0.0-...-d10ad41 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2025 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ResourceTypeDeployment ResourceType = "deployment"
	ResourceTypeIngress    ResourceType = "ing"
	ResourceTypeService    ResourceType = "svc"
	KubeConfigEnvVarName   string       = "KUBECONFIG"
)

Variables

View Source
var (
	ErrResourceNotFound = errors.New("cannot find resource")
	ErrResourceNotReady = errors.New("resource is not ready")
)

Functions

func GetResource

func GetResource[T any](
	ctx context.Context,
	cli *Cli,
	resourceType ResourceType,
	resourceName string,
	flags *KubeCliFlags,
) (T, error)

func WaitForResource

func WaitForResource[T comparable](
	ctx context.Context,
	cli *Cli,
	resourceType ResourceType,
	resourceFilter ResourceFilterFn[T],
	readyStatusFilter ResourceFilterFn[T],
) (T, error)

Types

type Cli

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

func NewCli

func NewCli(commandRunner exec.CommandRunner) *Cli

Creates a new K8s CLI instance

func (*Cli) Apply

func (cli *Cli) Apply(ctx context.Context, path string, flags *KubeCliFlags) error

Applies manifests from the specified input

func (*Cli) ApplyWithFile

func (cli *Cli) ApplyWithFile(ctx context.Context, filePath string, flags *KubeCliFlags) (*exec.RunResult, error)

func (*Cli) ApplyWithKustomize

func (cli *Cli) ApplyWithKustomize(ctx context.Context, path string, flags *KubeCliFlags) error

Applies the manifests at the specified path using kustomize

func (*Cli) ApplyWithStdIn

func (cli *Cli) ApplyWithStdIn(ctx context.Context, input string, flags *KubeCliFlags) (*exec.RunResult, error)

func (*Cli) CheckInstalled

func (cli *Cli) CheckInstalled(ctx context.Context) error

Checks whether or not the K8s CLI is installed and available within the PATH

func (*Cli) ConfigUseContext

func (cli *Cli) ConfigUseContext(ctx context.Context, name string, flags *KubeCliFlags) (*exec.RunResult, error)

Sets the k8s context to use for future CLI commands

func (*Cli) ConfigView

func (cli *Cli) ConfigView(
	ctx context.Context,
	merge bool,
	flatten bool,
	flags *KubeCliFlags,
) (*exec.RunResult, error)

Views the current k8s configuration including available clusters, contexts & users

func (*Cli) CreateNamespace

func (cli *Cli) CreateNamespace(ctx context.Context, name string, flags *KubeCliFlags) (*exec.RunResult, error)

Creates a new k8s namespace with the specified name

func (*Cli) Cwd

func (cli *Cli) Cwd(cwd string)

Sets the current working directory

func (*Cli) Exec

func (cli *Cli) Exec(ctx context.Context, flags *KubeCliFlags, args ...string) (exec.RunResult, error)

Executes a k8s CLI command from the specified arguments and flags

func (*Cli) InstallUrl

func (cli *Cli) InstallUrl() string

Returns the installation URL to install the K8s CLI

func (*Cli) Name

func (cli *Cli) Name() string

Gets the name of the Tool

func (*Cli) RolloutStatus

func (cli *Cli) RolloutStatus(
	ctx context.Context,
	deploymentName string,
	flags *KubeCliFlags,
) (*exec.RunResult, error)

Gets the deployment rollout status

func (*Cli) SetEnv

func (cli *Cli) SetEnv(envValues map[string]string)

Sets the env vars available to the CLI

func (*Cli) SetKubeConfig

func (cli *Cli) SetKubeConfig(kubeConfig string)

Sets the KUBECONFIG environment variable

type DeploymentSpec

type DeploymentSpec struct {
	Replicas int `json:"replicas" yaml:"replicas"`
}

type DeploymentStatus

type DeploymentStatus struct {
	AvailableReplicas int `json:"availableReplicas" yaml:"availableReplicas"`
	ReadyReplicas     int `json:"readyReplicas"     yaml:"readyReplicas"`
	Replicas          int `json:"replicas"          yaml:"replicas"`
	UpdatedReplicas   int `json:"updatedReplicas"   yaml:"updatedReplicas"`
}

type DryRunType

type DryRunType string
const (
	DryRunTypeNone DryRunType = "none"
	// If client strategy, only print the object that would be sent
	DryRunTypeClient DryRunType = "client"
	// If server strategy, submit server-side request without persisting the resource.
	DryRunTypeServer DryRunType = "server"
)

type IngressPath

type IngressPath struct {
	Path     string `json:"path"     yaml:"path"`
	PathType string `json:"pathType" yaml:"pathType"`
}

type IngressRule

type IngressRule struct {
	Host *string         `json:"host" yaml:"host"`
	Http IngressRuleHttp `json:"http" yaml:"http"`
}

type IngressRuleHttp

type IngressRuleHttp struct {
	Paths []IngressPath `json:"paths" yaml:"paths"`
}

type IngressSpec

type IngressSpec struct {
	IngressClassName string        `json:"ingressClassName" yaml:"ingressClassName"`
	Tls              []IngressTls  `json:"tls"              yaml:"tls"`
	Rules            []IngressRule `json:"rules"            yaml:"rules"`
}

type IngressStatus

type IngressStatus struct {
	LoadBalancer LoadBalancer `json:"loadBalancer" yaml:"loadBalancer"`
}

type IngressTls

type IngressTls struct {
	Hosts      []string `json:"hosts"      yaml:"hosts"`
	SecretName string   `json:"secretName" yaml:"secretName"`
}

type KubeCliFlags

type KubeCliFlags struct {
	// The namespace to filter the command or create resources
	Namespace string
	// The dry-run type, defaults to empty
	DryRun DryRunType
	// The expected output, typically JSON or YAML
	Output OutputType
}

K8s CLI Fags

type KubeCluster

type KubeCluster struct {
	Name    string          `yaml:"name"`
	Cluster KubeClusterData `yaml:"cluster"`
}

type KubeClusterData

type KubeClusterData struct {
	CertificateAuthorityData string `yaml:"certificate-authority-data"`
	Server                   string `yaml:"server"`
}

type KubeConfig

type KubeConfig struct {
	ApiVersion     string          `yaml:"apiVersion"`
	Clusters       []*KubeCluster  `yaml:"clusters"`
	Contexts       []*KubeContext  `yaml:"contexts"`
	Users          []*KubeUser     `yaml:"users"`
	Kind           string          `yaml:"kind"`
	CurrentContext string          `yaml:"current-context"`
	Preferences    KubePreferences `yaml:"preferences"`
}

func ParseKubeConfig

func ParseKubeConfig(ctx context.Context, raw []byte) (*KubeConfig, error)

Parses the raw bytes into a KubeConfig instance

type KubeConfigManager

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

Manages k8s configurations available to the k8s CLI

func NewKubeConfigManager

func NewKubeConfigManager(cli *Cli) (*KubeConfigManager, error)

Creates a new instance of the KubeConfigManager

func (*KubeConfigManager) AddOrUpdateContext

func (kcm *KubeConfigManager) AddOrUpdateContext(
	ctx context.Context,
	contextName string,
	newKubeConfig *KubeConfig,
) (string, error)

Adds a new or updates an existing KubeConfig in the main kube config

func (*KubeConfigManager) DeleteKubeConfig

func (kcm *KubeConfigManager) DeleteKubeConfig(ctx context.Context, configName string) error

Deletes the KubeConfig with the specified name

func (*KubeConfigManager) MergeConfigs

func (kcm *KubeConfigManager) MergeConfigs(ctx context.Context, newConfigName string, path ...string) (string, error)

Merges the specified kube configs into the kube config This powers the use of the kubectl config set of commands that allow developers to switch between different k8s cluster contexts

func (*KubeConfigManager) SaveKubeConfig

func (kcm *KubeConfigManager) SaveKubeConfig(ctx context.Context, configName string, config *KubeConfig) (string, error)

Saves the KubeConfig to the kube configuration folder with the specified name

type KubeContext

type KubeContext struct {
	Name    string          `yaml:"name"`
	Context KubeContextData `yaml:"context"`
}

type KubeContextData

type KubeContextData struct {
	Cluster   string `yaml:"cluster"`
	Namespace string `yaml:"namespace"`
	User      string `yaml:"user"`
}

type KubePreferences

type KubePreferences map[string]any

type KubeUser

type KubeUser struct {
	Name         string       `yaml:"name"`
	KubeUserData KubeUserData `yaml:"user"`
}

type KubeUserData

type KubeUserData map[string]any

type List

type List[T any] struct {
	Resource
	Items []T `json:"items" yaml:"items"`
}

func GetResources

func GetResources[T any](
	ctx context.Context,
	cli *Cli,
	resourceType ResourceType,
	flags *KubeCliFlags,
) (*List[T], error)

type LoadBalancer

type LoadBalancer struct {
	Ingress []LoadBalancerIngress `json:"ingress" yaml:"ingress"`
}

type LoadBalancerIngress

type LoadBalancerIngress struct {
	Ip string `json:"ip" yaml:"ip"`
}

type OutputType

type OutputType string
const (
	OutputTypeJson OutputType = "json"
	OutputTypeYaml OutputType = "yaml"
)

type Port

type Port struct {
	Port int `json:"port"`
	// The target port can be a valid port number or well known service name like 'redis'
	TargetPort any    `json:"targetPort" yaml:"targetPort"`
	Protocol   string `json:"protocol"   yaml:"protocol"`
}

func (*Port) UnmarshalJSON

func (p *Port) UnmarshalJSON(data []byte) error

type Resource

type Resource struct {
	ApiVersion string           `json:"apiVersion" yaml:"apiVersion"`
	Kind       string           `json:"kind"       yaml:"kind"`
	Metadata   ResourceMetadata `json:"metadata"   yaml:"metadata"`
}

type ResourceFilterFn

type ResourceFilterFn[T comparable] func(resource T) bool

type ResourceMetadata

type ResourceMetadata struct {
	Name        string `json:"name"      yaml:"name"`
	Namespace   string `json:"namespace" yaml:"namespace"`
	Annotations map[string]any
}

type ResourceType

type ResourceType string

type ResourceWithSpec

type ResourceWithSpec[T any, S any] struct {
	Resource
	Spec   T `json:"spec"   yaml:"spec"`
	Status S `json:"status" yaml:"status"`
}

type ServiceSpec

type ServiceSpec struct {
	Type       ServiceType `json:"type"       yaml:"type"`
	ClusterIp  string      `json:"clusterIP"  yaml:"clusterIP"`
	ClusterIps []string    `json:"clusterIPs" yaml:"clusterIPs"`
	Ports      []Port      `json:"ports"      yaml:"ports"`
}

type ServiceStatus

type ServiceStatus struct {
	LoadBalancer LoadBalancer `json:"loadBalancer" yaml:"loadBalancer"`
}

type ServiceType

type ServiceType string
const (
	ServiceTypeClusterIp    ServiceType = "ClusterIP"
	ServiceTypeLoadBalancer ServiceType = "LoadBalancer"
	ServiceTypeNodePort     ServiceType = "NodePort"
	ServiceTypeExternalName ServiceType = "ExternalName"
)

Jump to

Keyboard shortcuts

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