util

package
v1.12.0 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2026 License: Apache-2.0 Imports: 37 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AggregateIpsToRanges added in v1.9.0

func AggregateIpsToRanges(ips []string) ([]string, error)

AggregateIpsToRanges converts a list of individual IPs into ranges to reduce API calls. Examples:

  • IPv4: ["10.200.1.200", "10.200.1.201", "10.200.1.202"] -> ["10.200.1.200-202"]
  • IPv6: ["2001:db8::1", "2001:db8::2", "2001:db8::3"] -> ["2001:db8::1-2001:db8::3"]

Uses net.ParseIP for validation and supports both IPv4 and IPv6.

func AreMapsEqual added in v1.7.0

func AreMapsEqual[K comparable, V comparable](a, b map[K]V) bool

func BoolToShellString

func BoolToShellString(b bool) string

func CheckTolerations

func CheckTolerations(taints []corev1.Taint, tolerations []corev1.Toleration, ignoreTaints []string) bool

CheckTolerations Check if the given taints can be tolerated by the given tolerations.

func ConvertToUnstructured

func ConvertToUnstructured[T runtime.Object](obj T) (*unstructured.Unstructured, error)

ConvertToUnstructured converts any typed object (e.g. corev1.Node, corev1.Pod) to an unstructured.Unstructured.

func ExpandIpRanges added in v1.9.0

func ExpandIpRanges(ipRanges []string) ([]string, error)

ExpandIpRanges expands IP range strings into individual IP addresses. Supports multiple formats:

  • Single IP: "10.200.1.200"
  • Full range: "10.200.1.200-10.200.1.203"
  • Short form (IPv4 only): "10.200.1.200-203"
  • IPv6 ranges: "2001:db8::1-2001:db8::4"

Uses net.ParseIP for robust validation and supports both IPv4 and IPv6.

func FileExists

func FileExists(filename string) bool

func GeneratePassword

func GeneratePassword(length int) string

func GetHash added in v1.9.0

func GetHash(s string, n int) string

Generates SHA-256 hash and takes the first n characters

func GetKubeField

func GetKubeField(obj *unstructured.Unstructured, fieldPath string) (interface{}, error)

func GetKubeFieldValue

func GetKubeFieldValue[T any](obj *unstructured.Unstructured, fieldPath string) (T, error)

GetKubeFieldValue converts the retrieved field to any specified type.

func GetKubeObjectFieldValue

func GetKubeObjectFieldValue[T any, K runtime.Object](obj K, fieldPath string) (T, error)

GetKubeObjectFieldValue combines conversion and field extraction. It accepts any runtime.Object (like corev1.Node or corev1.Pod) and returns the field value of the specified type.

func GetKubernetesVersion added in v1.8.2

func GetKubernetesVersion(restConfig *rest.Config) (string, error)

func GetMinValue

func GetMinValue(a, b int) int

func GetNonZeroOrDefault added in v1.11.0

func GetNonZeroOrDefault(val, defaultVal int) int

func GetOperatorDeployment added in v1.8.3

func GetOperatorDeployment(ctx context.Context, k8sClient crclient.Client) (*appsv1.Deployment, error)

func GetPodNamespace

func GetPodNamespace() (string, error)

func HashStruct

func HashStruct(s any) (string, error)

HashStruct generates a hash of a struct NOTE: This function returns an error if the struct contains maps. For structs with maps, use HashableMap instead.

func HumanReadableIops

func HumanReadableIops(iops float64) string

func HumanReadableSize

func HumanReadableSize(bytes int64) string

HumanReadableSize converts a size in bytes to a human-readable string with appropriate size units (B, KiB, MiB, GiB, TiB)

func HumanReadableThroughput

func HumanReadableThroughput(bytesPerSecond float64) string

func Int32Ref

func Int32Ref(i int32) *int32

func IsEqualConfigMapData

func IsEqualConfigMapData(cm1, cm2 *v1.ConfigMap) bool

func IsIpv6

func IsIpv6(ip string) bool

func MapMissingItems

func MapMissingItems(originalMap, newMap map[string]string) map[string]string

MapMissingItems returns a map containing the items that are in the newMap but not in the originalMap

func MapOrdered

func MapOrdered[K cmp.Ordered, V any](m map[K]V) iter.Seq2[K, V]

func MergeMaps

func MergeMaps(originalMap, newMap map[string]string) map[string]string

func NodeSelectorMatchesNode

func NodeSelectorMatchesNode(nodeSelector map[string]string, node *v1.Node) bool

func RemoveKeysStartingWithPrefix added in v1.10.1

func RemoveKeysStartingWithPrefix(originalMap map[string]string, prefix string) map[string]string

func SanitizeK8sName added in v1.10.3

func SanitizeK8sName(name string) string

SanitizeK8sName replaces characters not allowed in DNS-1035 labels (dots) with hyphens.

func SendGetRequest

func SendGetRequest(ctx context.Context, url string, options RequestOptions) (*http.Response, error)

func SendJsonRequest

func SendJsonRequest(ctx context.Context, url string, jsonData []byte, options RequestOptions) (*http.Response, error)

func Shuffle

func Shuffle[T any](slice []T)

Shuffle shuffles any slice in place using Go generics.

func SliceEquals

func SliceEquals[T comparable](a, b []T) bool

func StringPtrOrDefault added in v1.9.0

func StringPtrOrDefault(ptr *string, defaultValue string) string

StringPtrOrDefault returns the default value if the pointer is nil, otherwise returns the dereferenced value

func TolerationsEqual added in v1.9.0

func TolerationsEqual(a, b corev1.Toleration) bool

TolerationsEqual checks if two tolerations are equal

func TolerationsEqualExceptSeconds added in v1.9.0

func TolerationsEqualExceptSeconds(a, b corev1.Toleration) bool

TolerationsEqualExceptSeconds checks if two tolerations are equal ignoring tolerationSeconds

Types

type ConfigurationError

type ConfigurationError struct {
	Err     error
	Message string
}

func (*ConfigurationError) Error

func (e *ConfigurationError) Error() string

type Exec

type Exec interface {
	Exec(ctx context.Context, command []string) (stdout, stderr bytes.Buffer, err error)
	ExecNamed(ctx context.Context, name string, command []string) (stdout, stderr bytes.Buffer, err error)
	ExecSensitive(ctx context.Context, name string, command []string) (stdout, stderr bytes.Buffer, err error)
}

func NewExecInPod

func NewExecInPod(restClient rest.Interface, cfg *rest.Config, pod *v1.Pod) (Exec, error)

func NewExecInPodByName

func NewExecInPodByName(restClient rest.Interface, cfg *rest.Config, pod *v1.Pod, containerName string, timeout *time.Duration) (Exec, error)

func NewExecInPodWithTimeout

func NewExecInPodWithTimeout(restClient rest.Interface, cfg *rest.Config, pod *v1.Pod, timeout *time.Duration) (Exec, error)

func NewExecWithConfig

func NewExecWithConfig(restClient rest.Interface, cfg *rest.Config, pod types.NamespacedName, timeout *time.Duration, containerName, nodeName string) (Exec, error)

type HashableMap

type HashableMap struct {
	Entries []hashMapEntry
}

HashableMap is a struct that can replace a map in order to be hashed Create a new HashableMap instance with NewHashableMap from a map[string]string and use it in place of a map

func NewHashableMap

func NewHashableMap(m map[string]string) *HashableMap

func (*HashableMap) Equals

func (hm *HashableMap) Equals(other *HashableMap) bool

Allows to compare two HashableMap instances

type PodExec

type PodExec struct {
	RestClient rest.Interface
	RestConfig *rest.Config
	Pod        types.NamespacedName

	ContainerName string
	// node name is provided for debugging purposes only, it is not used for anything else
	NodeName string
	// contains filtered or unexported fields
}

func (*PodExec) Exec

func (e *PodExec) Exec(ctx context.Context, command []string) (stdout, stderr bytes.Buffer, err error)

Exec executes a command in a pod. Logs input and output if exit code != 0. Should be used in rare cases as might reveal sensitive data.

func (*PodExec) ExecNamed

func (e *PodExec) ExecNamed(ctx context.Context, name string, command []string) (stdout, stderr bytes.Buffer, err error)

ExecNamed executes a command in a pod. Logs input and output if exit code != 0. However, provides a name for the span.

func (*PodExec) ExecSensitive

func (e *PodExec) ExecSensitive(ctx context.Context, name string, command []string) (stdout, stderr bytes.Buffer, err error)

type RequestOptions

type RequestOptions struct {
	AuthHeader string
}

Jump to

Keyboard shortcuts

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