utils

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package utils provides utility functions for key mapping and conversions.

Index

Constants

This section is empty.

Variables

View Source
var AnnotationPrefixes = []string{
	"ingress.kubernetes.io",
	"haproxy.org",
	"haproxy.com",
}

AnnotationPrefixes are the prefixes under which HAProxy Ingress/Service annotations are recognised. An annotation may be used bare (no prefix) or with any of these; e.g. "cr-backend", "haproxy.org/cr-backend" and "haproxy.com/cr-backend" are equivalent.

Functions

func AnnotationKeys added in v1.1.0

func AnnotationKeys(suffix string) []string

AnnotationKeys returns the accepted keys for an annotation suffix: the bare suffix first, then each supported prefix joined with '/'.

func AnnotationValue added in v1.1.0

func AnnotationValue(annotations map[string]string, suffix string) (string, bool)

AnnotationValue returns the value of the first present key among the accepted keys for suffix (bare then prefixed), and whether one was found.

func BackendObjectReferenceToKey

func BackendObjectReferenceToKey(backendRef gatewayv1.BackendObjectReference) string

BackendObjectReferenceToKey converts a BackendObjectReferenceToKey to a unique string key. It handles nil pointers by using a consistent placeholder.

func ClearMap

func ClearMap[K comparable, V any](m map[K]V)

Generic function to clear a map in place

func ComparePointers

func ComparePointers[T cmp.Ordered](a, b *T) int

ComparePointers compares two pointers to any ordered type. It establishes a consistent sort order where nil values come before non-nil values.

It returns:

  • -1 if a < b
  • 0 if a == b (or both are nil)
  • +1 if a > b

func ConvertSliceWithFunc added in v0.8.0

func ConvertSliceWithFunc[U, V any](arg []U, f func(U) V) []V

func ConvertV1Alpha2HostnameToString added in v0.8.0

func ConvertV1Alpha2HostnameToString(hostname v1alpha2.Hostname) string

func GetHostnamesForRouteWithListener added in v0.8.0

func GetHostnamesForRouteWithListener(listenerHostname *string, routeHostnames []string) []string

GetHostnamesForRouteWithListener returns the hostnames that match a listener and a route. The rules are based on the Gateway API specification. If the listener hostname is not set, it matches any route hostname. If the listener hostname is a wildcard, it checks if it matches any route hostname. If the route hostnames are empty, the listener hostname matches the route hostnames.

func GetNamespacedName added in v0.8.0

func GetNamespacedName(name gatewayv1.ObjectName, namespace *gatewayv1.Namespace, defaultNamespace string) types.NamespacedName

func IsSyntheticName added in v1.1.0

func IsSyntheticName(name string) bool

IsSyntheticName reports whether the name was produced by mangleIngressName. The ':' prefix is illegal in a Kubernetes object name, so a real Kubernetes resource can never carry it: this is a tamper-proof origin marker.

func KeyToBackendObjectReference

func KeyToBackendObjectReference(key string) (gatewayv1.BackendObjectReference, error)

KeyToBackendObjectReference converts a string key back to a gatewayv1.BackendObjectReference. It is the inverse of BackendObjectReferenceToKey.

func KeyToParentRef

func KeyToParentRef(key string) (gatewayv1.ParentReference, error)

KeyToParentRef converts a string key back to a gatewayv1.ParentReference. It is the inverse of ParentRefToKey.

func Keys

func Keys[K comparable, V any](src map[K]V) map[K]struct{}

func MangleIngressName added in v1.1.0

func MangleIngressName(ingress *networkingv1.Ingress, suffix string) string

MangleIngressName builds the collision-free name of a synthetic HTTPRoute, e.g. "ing:my-app:0" for the first rule of Ingress "my-app".

func MapToSortedListByCreationTimestamp

func MapToSortedListByCreationTimestamp[T ObjectWithTimestamp](objects map[types.NamespacedName]T) []T

func NamespaceAsString

func NamespaceAsString(ns *gatewayv1.Namespace) string

func ObjectKeyFromNamespacedName

func ObjectKeyFromNamespacedName(s string) (client.ObjectKey, error)

func ParentRefToKey

func ParentRefToKey(parentRef gatewayv1.ParentReference) string

ParentRefToKey converts a ParentReference to a unique string key. It handles nil pointers by using a consistent placeholder.

func ParseNamespacedName

func ParseNamespacedName(s string) (types.NamespacedName, error)

ParseNamespacedName parses a "namespace/name" string into a NamespacedName.

func ParseSyntheticRoute added in v1.1.0

func ParseSyntheticRoute(routeNamespace, name string) (ingress types.NamespacedName, suffix string, isSynthetic bool)

ParseSyntheticRoute is the inverse of mangleIngressName. Given a synthetic route namespace and name, it returns the source Ingress key and the rule suffix. isSynthetic is false when name is not a synthetic route name.

The prefix is stripped first, then the remaining "name:suffix" is cut at the first ':'. The Ingress name is a valid Kubernetes name (so it never contains ':'), making the cut unambiguous even if a suffix ever contained one.

func PointerDefaultValueIfNil added in v0.8.0

func PointerDefaultValueIfNil[T any](arg *T) T

PointerDefaultValueIfNil dereferences a pointer and returns its value. If the pointer is nil, the zero value of T is returned instead.

Example:

x := 42
v := PointerDefaultValueIfNil(&x)  // returns 42
v := PointerDefaultValueIfNil(nil) // returns 0 (zero value of int)

func RouteGroupKindsToString

func RouteGroupKindsToString(routesGK []gatewayv1.RouteGroupKind) string

RouteGroupKindsToString renders Route kinds group-qualified, as "group/Kind".

The group is not decoration: a kind is most often unsupported *because* of it, and rendering the kind alone yields messages that contradict themselves, such as "HTTPRoute is not supported. Supported Route Kinds [HTTPRoute]" for a kind taken from a foreign group. A nil group defaults to the Gateway API group, as everywhere else.

func SetDifference

func SetDifference[K comparable, V any](mapA, mapB map[K]V) map[K]struct{}

SetDifference finds the keys that are in mapA but not in mapB. It works for any map with a comparable key type K and any value type V.

func SetIntersection

func SetIntersection[K comparable, V any](mapA, mapB map[K]V) map[K]struct{}

func SortByCreationTimestamp

func SortByCreationTimestamp[T ObjectWithTimestamp](objects []T)

SortByCreationTimestamp orders objects oldest first, breaking ties on namespace then name.

The tie-break is not decoration: metav1.Time has a one-second granularity, so equal timestamps are the norm as soon as several objects are applied in one go. Comparing the name alone is not enough either, since names are only unique within a namespace. Without the full key the order of equal elements is unspecified, and any decision derived from it — such as which listener wins a port — becomes irreproducible across runs.

The sort is stable so that objects comparing equal on all three keep their input order rather than being permuted.

func StrPtrToString

func StrPtrToString[T stringer](p *T) string

func StringToPtr

func StringToPtr[T stringer](s string) *T

StringToPtr converts a string to a pointer of a string-based type. If the string is the placeholder "_", it returns nil.

Types

type DedicatedGateway added in v1.0.3

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

func NewDedicatedGateway added in v1.0.3

func NewDedicatedGateway(nsName types.NamespacedName) DedicatedGateway

func (DedicatedGateway) Check added in v1.0.3

func (dg DedicatedGateway) Check(gatewayNsName types.NamespacedName) bool

Check reports whether gatewayNsName matches the dedicated Gateway this instance was configured with. If no dedicated Gateway was configured (both name and namespace are empty), it returns true, allowing all gateways through.

type DedicatedNamespaces added in v1.0.3

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

func NewDedicatedNamespaces added in v1.0.3

func NewDedicatedNamespaces(namespaces []string) DedicatedNamespaces

func (DedicatedNamespaces) Check added in v1.0.3

func (dn DedicatedNamespaces) Check(gatewayNsName types.NamespacedName) bool

type Errors

type Errors []error

func (*Errors) Add

func (e *Errors) Add(errs ...error)

func (*Errors) AddErrors

func (e *Errors) AddErrors(errs Errors)

func (*Errors) Result

func (e *Errors) Result() error

type KeyMap

type KeyMap[KEY any, VALUE any] struct {
	// contains filtered or unexported fields
}

func NewKeyMap

func NewKeyMap[KEY any, VALUE any](keygen func(KEY) string) KeyMap[KEY, VALUE]

func (*KeyMap[KEY, VALUE]) Clear

func (m *KeyMap[KEY, VALUE]) Clear()

func (*KeyMap[KEY, VALUE]) Delete

func (m *KeyMap[KEY, VALUE]) Delete(key KEY)

func (*KeyMap[KEY, VALUE]) Exists

func (m *KeyMap[KEY, VALUE]) Exists(key KEY) bool

func (*KeyMap[KEY, VALUE]) Get

func (m *KeyMap[KEY, VALUE]) Get(key KEY) (VALUE, bool)

func (*KeyMap[KEY, VALUE]) Iterate

func (m *KeyMap[KEY, VALUE]) Iterate(callback func(string, VALUE) bool)

func (*KeyMap[KEY, VALUE]) Len

func (m *KeyMap[KEY, VALUE]) Len() int

func (*KeyMap[KEY, VALUE]) Set

func (m *KeyMap[KEY, VALUE]) Set(key KEY, value VALUE)

type ObjectWithTimestamp

type ObjectWithTimestamp interface {
	GetCreationTimestamp() metav1.Time
	GetNamespace() string
	GetName() string
}

Jump to

Keyboard shortcuts

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