kubectl

package
v0.0.0-...-6ab3b6d Latest Latest
Warning

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

Go to latest
Published: May 2, 2024 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 KubectlCli,
	resourceType ResourceType,
	resourceName string,
	flags *KubeCliFlags,
) (T, error)

func WaitForResource

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

Types

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 KubectlCli) (*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 KubectlCli

type KubectlCli interface {
	tools.ExternalTool
	// Sets the current working directory
	Cwd(cwd string)
	// Sets the env vars available to the CLI
	SetEnv(env map[string]string)
	// Sets the KUBECONFIG environment variable
	SetKubeConfig(kubeConfig string)
	// Applies one or more files from the specified path
	Apply(ctx context.Context, path string, flags *KubeCliFlags) error
	// Applies manifests from the specified input
	ApplyWithStdIn(ctx context.Context, input string, flags *KubeCliFlags) (*exec.RunResult, error)
	// Applies manifests from the specified file path
	ApplyWithFile(ctx context.Context, filePath string, flags *KubeCliFlags) (*exec.RunResult, error)
	// Views the current k8s configuration including available clusters, contexts & users
	ConfigView(ctx context.Context, merge bool, flatten bool, flags *KubeCliFlags) (*exec.RunResult, error)
	// Sets the k8s context to use for future CLI commands
	ConfigUseContext(ctx context.Context, name string, flags *KubeCliFlags) (*exec.RunResult, error)
	// Creates a new k8s namespace with the specified name
	CreateNamespace(ctx context.Context, name string, flags *KubeCliFlags) (*exec.RunResult, error)
	// Executes a k8s CLI command from the specified arguments and flags
	Exec(ctx context.Context, flags *KubeCliFlags, args ...string) (exec.RunResult, error)
	// Gets the deployment rollout status
	RolloutStatus(ctx context.Context, deploymentName string, flags *KubeCliFlags) (*exec.RunResult, error)
	// Applies the manifests at the specified path using kustomize
	ApplyWithKustomize(ctx context.Context, path string, flags *KubeCliFlags) error
}

Executes commands against the Kubernetes CLI

func NewKubectl

func NewKubectl(commandRunner exec.CommandRunner) KubectlCli

Creates a new K8s CLI instance

type List

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

func GetResources

func GetResources[T any](
	ctx context.Context,
	cli KubectlCli,
	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