Documentation ¶
Overview ¶
Package k8s provides utilities for interacting with Kubernetes (k8s) using the Helm and Kubernetes client libraries.
It imports several packages from the Helm library to work with Helm charts and releases. It also imports the Kubernetes client libraries to interact with the Kubernetes API server.
The package provides functionality for various Kubernetes operations, such as creating and managing resources, handling Helm charts, and managing releases.
It also handles errors and metadata from the Kubernetes API, and can work with unstructured data, which allows it to interact with any Kubernetes resource.
The package uses the context, fmt, os, reflect, regexp, strings, sync, and time standard library packages for various utility functions and data types.
Index ¶
- Variables
- func Get[T Resource](ctx context.Context, name, namespace string) (T, error)
- func Init() error
- func List[T Resource](ctx context.Context, namespace string, options ...ListOption) ([]T, error)
- func Parse[T Resource](r Resource) (T, error)
- func Remove(ctx context.Context, name, namespace, kind string, options ...OperationOption) error
- func Rollout(ctx context.Context, item Resource, options ...OperationOption) error
- func WatchPods(ctx context.Context, namespace string, options ...WatchOption) error
- type API
- type ClusterRole
- type ClusterRoleBinding
- type ConfigMap
- type CronJob
- type CustomResourceDefinition
- type DaemonSet
- type Deployment
- type HorizontalPodAutoscaler
- type Ingress
- type Job
- type Kind
- type ListOption
- type OperationOption
- type PersistentVolume
- type PersistentVolumeClaim
- type Pod
- type PodDisruptionBudget
- type Resource
- type Role
- type RoleBinding
- type Secret
- type Service
- type ServiceAccount
- type StatefulSet
- type StorageClass
- type WatchOption
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var EncodeYAML = func(obj Resource) ([]byte, error) { kind := obj.GetObjectKind().GroupVersionKind().Kind switch kind { case KindDeployment: return encodeYaml[*Deployment](obj) case KindStatefulSet: return encodeYaml[*StatefulSet](obj) case KindConfigMap: return encodeYaml[*ConfigMap](obj) case KindCronJob: return encodeYaml[*CronJob](obj) case KindService: return encodeYaml[*Service](obj) case KindIngress: return encodeYaml[*Ingress](obj) case KindPodDisruptionBudget: return encodeYaml[*PodDisruptionBudget](obj) case KindSecret: return encodeYaml[*Secret](obj) case KindStorageClass: return encodeYaml[*StorageClass](obj) case KindPersistentVolumeClaim: return encodeYaml[*PersistentVolumeClaim](obj) case KindPersistentVolume: return encodeYaml[*PersistentVolume](obj) case KindCustomResourceDefinition: return encodeYaml[*CustomResourceDefinition](obj) case KindServiceAccount: return encodeYaml[*ServiceAccount](obj) case KindClusterRole: return encodeYaml[*ClusterRole](obj) case KindClusterRoleBinding: return encodeYaml[*ClusterRoleBinding](obj) case KindRole: return encodeYaml[*Role](obj) case KindRoleBinding: return encodeYaml[*RoleBinding](obj) case KindDaemonSet: return encodeYaml[*DaemonSet](obj) case KindPod: return encodeYaml[*Pod](obj) case KindJob: return encodeYaml[*Job](obj) case KindHorizontalPodAutoscaler: return encodeYaml[*HorizontalPodAutoscaler](obj) default: return nil, fmt.Errorf("[encode] unsupported kind: %s", kind) } }
EncodeYAML encodes the given resource object into YAML format. It takes a Resource object as input and returns the encoded YAML data as a byte slice. If the kind of the resource is supported, it uses the corresponding encodeYaml function to encode the object. If the kind is not supported, it returns an error with a message indicating the unsupported kind.
var EncodeYAMLAll = func(list []Resource) ([]byte, error) { var buf bytes.Buffer var err error var data []byte for _, item := range list { if data, err = EncodeYAML(item); err != nil { return nil, err } if buf.Len() != 0 { buf.WriteString("\n---\n") } else { buf.WriteString("---\n") } buf.Write(data) } return buf.Bytes(), nil }
EncodeYAMLAll encodes a list of resources into YAML format. It takes a slice of Resource objects as input and returns the encoded YAML data as a byte array. Each resource is encoded separately and separated by "---" in the output. If an error occurs during encoding, it returns nil and the error.
Functions ¶
func Get ¶
Get retrieves a resource of type T from the Kubernetes cluster. It takes a context, the name and namespace of the resource as input parameters. It returns the retrieved resource of type T and an error if any. The function supports various types of resources such as Deployment, StatefulSet, ConfigMap, CronJob, Service, Ingress, etc. If the resource type is not supported, it returns an error indicating the unsupported type.
func Init ¶
func Init() error
Init initializes the k8s client, which throws error if failed to get envrioment variables: KUBECONFIG, or in-cluster config.
func List ¶
List retrieves a list of resources of type T from the specified namespace. It accepts a context, namespace, and optional list options. The function returns a slice of resources of type T and an error. The list options can be used to filter the results. The function supports various resource types, such as Deployment, StatefulSet, ConfigMap, CronJob, Service, Ingress, and more. If the resource type is not supported, the function returns an error.
func Parse ¶
Parse is a generic function that parses a resource into the specified type. It takes a resource of type `Resource` and returns an instance of type `T`. If the resource is of type `unstructured.Unstructured`, it uses the default unstructured converter to convert the resource into the specified type `T`. If the resource is already of type `T`, it returns the resource as is. If the resource is of any other type, it returns a new instance of type `T` and an error indicating that the type casting is unsupported.
Example ¶
var d Deployment var r Resource = &d var err error var parsed *Deployment if parsed, err = Parse[*Deployment](r); err != nil { panic(err) } fmt.Printf("%t\n", parsed != nil)
Output: true
func Remove ¶
func Remove(ctx context.Context, name, namespace, kind string, options ...OperationOption) error
Remove removes a resource from a Kubernetes cluster. It takes the following parameters: - ctx: The context.Context object for the operation. - name: The name of the resource to remove. - namespace: The namespace of the resource. - kind: The kind of the resource to remove. - options: Optional operation options. It returns an error if the removal operation fails. The supported resource kinds are: - Deployment - StatefulSet - ConfigMap - CronJob - Service - Ingress - PodDisruptionBudget - Secret - StorageClass - PersistentVolumeClaim - PersistentVolume - CustomResourceDefinition - ServiceAccount - ClusterRole - ClusterRoleBinding - Role - RoleBinding - DaemonSet - Pod - Job - HorizontalPodAutoscaler If the provided kind is not supported, it returns an error.
func Rollout ¶
func Rollout(ctx context.Context, item Resource, options ...OperationOption) error
Rollout performs a rollout operation on the specified Kubernetes resource. It takes a context, the resource to rollout, and optional operation options. The function returns an error if the rollout operation fails.
Types ¶
type ClusterRole ¶
type ClusterRole = rbacv1.ClusterRole
type ClusterRoleBinding ¶
type ClusterRoleBinding = rbacv1.ClusterRoleBinding
type CustomResourceDefinition ¶
type CustomResourceDefinition = apiextensionsv1.CustomResourceDefinition
type Deployment ¶
type Deployment = appsv1.Deployment
type HorizontalPodAutoscaler ¶
type HorizontalPodAutoscaler = autoscalingv2.HorizontalPodAutoscaler
type Ingress ¶
type Ingress = networkingv1.Ingress
type Kind ¶
type Kind = string
const ( KindDeployment Kind = "Deployment" KindStatefulSet Kind = "StatefulSet" KindConfigMap Kind = "ConfigMap" KindCronJob Kind = "CronJob" KindService Kind = "Service" KindIngress Kind = "Ingress" KindPodDisruptionBudget Kind = "PodDisruptionBudget" KindSecret Kind = "Secret" KindStorageClass Kind = "StorageClass" KindPersistentVolumeClaim Kind = "PersistentVolumeClaim" KindPersistentVolume Kind = "PersistentVolume" KindCustomResourceDefinition Kind = "CustomResourceDefinition" KindServiceAccount Kind = "ServiceAccount" KindClusterRole Kind = "ClusterRole" KindClusterRoleBinding Kind = "ClusterRoleBinding" KindRole Kind = "Role" KindRoleBinding Kind = "RoleBinding" KindDaemonSet Kind = "DaemonSet" KindPod Kind = "Pod" KindJob Kind = "Job" KindHorizontalPodAutoscaler Kind = "HorizontalPodAutoscaler" )
type ListOption ¶
type ListOption func(*listOptions)
func WithRegex ¶
func WithRegex(p *regexp.Regexp) ListOption
WithRegex sets the regular expression pattern for filtering items in a list. It returns a ListOption function that can be used to modify list options.
type OperationOption ¶
type OperationOption func(*operationOptions)
func DoNotRemoveChildren ¶ added in v1.0.14
func DoNotRemoveChildren() OperationOption
func WithWait ¶
func WithWait(duration time.Duration) OperationOption
WithWait sets the duration to wait before performing an operation. It returns an OperationOption that can be used to configure the operation options.
type PersistentVolume ¶
type PersistentVolume = v1.PersistentVolume
type PersistentVolumeClaim ¶
type PersistentVolumeClaim = v1.PersistentVolumeClaim
type PodDisruptionBudget ¶
type PodDisruptionBudget = policyv1.PodDisruptionBudget
type Resource ¶
type Resource interface { GetNamespace() string GetName() string GetObjectKind() schema.ObjectKind DeepCopyObject() runtime.Object }
func DecodeAllYAML ¶
DecodeAllYAML decodes a YAML manifest into a slice of Kubernetes resources. It splits the YAML content into individual documents, trims spaces, and decodes each document into a Kubernetes resource. The decoded resources are returned as a slice. If there is an error during decoding, it returns nil and the error.
func DecodeYAML ¶
DecodeYAML decodes the provided YAML content into an unstructured Kubernetes resource. It returns the decoded resource and any error encountered during decoding.
func GenManifest ¶
func GenManifest(ctx context.Context, chartPath string, values map[string]any) (resources []Resource, manifest string, err error)
GenManifest generates the Kubernetes manifest from a Helm chart and values. It takes a context, the path to the Helm chart, and a map of values as input. It returns a slice of resources and an error.
type RoleBinding ¶
type RoleBinding = rbacv1.RoleBinding
type ServiceAccount ¶
type ServiceAccount = v1.ServiceAccount
type StatefulSet ¶
type StatefulSet = appsv1.StatefulSet
type StorageClass ¶
type StorageClass = storagev1.StorageClass
type WatchOption ¶ added in v1.0.19
type WatchOption func(*watchOptions)
func WithOnAvailable ¶ added in v1.0.19
func WithOnAvailable(f func(*Pod)) WatchOption
func WithOnUnavailable ¶ added in v1.0.19
func WithOnUnavailable(f func(*Pod)) WatchOption
func WithWatchRegex ¶ added in v1.0.19
func WithWatchRegex(pattern *regexp.Regexp) WatchOption