Skip to content
Framework Packages

Framework Packages

Utility packages from genop-runtime and genop-testing, useful in your own step/hook code and tests, beyond the operators/operators/model API covered elsewhere in these docs.

k8s — Kubernetes utilities

CreateOrUpdate

Creates a resource if it doesn’t exist, or fetches the live object and applies a set of DiffFunctions to it — writing it back only if one of them actually changed something. Nothing is copied by default: pass a DiffFunction for every field you want reconciled, including the spec if you want it kept in sync.

created, updated, err := k8s.CreateOrUpdate(ctx, k8sClient, expected,
    k8s.DiffFunctionLabels[*corev1.Secret],
    k8s.DiffFunctionOwnerReferences[*corev1.Secret],
)

Four DiffFunctions ship built in — DiffFunctionLabels, DiffFunctionAnnotations, DiffFunctionOwnerReferences (all overwrite actual with expected’s value) and DiffFunctionFinalizers (adds expected’s finalizers without removing ones owned by another controller). Write your own for anything else — a DiffFunction[T] is just func(expected, actual T), mutating actual in place.

Owner references, finalizers, GVK

ownerRef, err := k8s.BuildOwnerReference(scheme, parent, true, true) // controller, blockOwnerDeletion

finalizer, err := k8s.BuildFinalizer(scheme, resource) // "<group>/<kind-lowercased>-deletion"

gvk, err := k8s.GetGroupVersionKind(scheme, resource)

BuildFinalizer is what the operator uses internally for its own finalizer — reach for it directly when a step needs the same <group>/<kind>-deletion string to check or set a finalizer on a different object (e.g. a dependency this resource owns).

genop-testing — test helpers (separate module)

CleanupResources

Deletes every object in the given lists, in the given namespace (empty string for cluster-scoped or “all namespaces”). The standard AfterEach in a generated suite_test.go:

ginkgo.AfterEach(func() {
    gomega.Expect(genoptesting.CleanupResources(k8sClient, "", &v1alpha1.DatabaseList{})).To(gomega.Succeed())
})

Gomega matchers (genop-testing/matchers)

ConsistOfConditions(...) / ContainConditions(...) assert on a resource’s .status.conditions, matching on Type, Status, Reason, ObservedGeneration, and an exact Message, a MessageContains substring or a MessageRe regexp — ConsistOfConditions also requires the list length to match exactly:

gomega.Expect(resource.Status.Conditions).To(matchers.ConsistOfConditions(
    matchers.ExpectedCondition{
        Type:   "DatabaseProvisioned",
        Status: metav1.ConditionTrue,
        Reason: model.ConditionReasonCompleted,
    },
    matchers.ExpectedCondition{
        Type:   model.ConditionTypeReady,
        Status: metav1.ConditionTrue,
        Reason: string(model.PhaseRunning),
    },
))

MatchDurationWithJitter(duration, jitterPercentage) asserts a requeue duration falls within the jitter band around a base delay — useful for testing a custom DeletionPolicyFunc or step that sets its own ctrl.Result.RequeueAfter:

gomega.Expect(result.RequeueAfter).To(matchers.MatchDurationWithJitter(30*time.Second, 0.25))