package controllerutil
import "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
Package controllerutil contains utility functions for working with and implementing Controllers.
Index ¶
- func AddFinalizer(o client.Object, finalizer string) (finalizersUpdated bool)
- func ContainsFinalizer(o client.Object, finalizer string) bool
- func HasControllerReference(object metav1.Object) bool
- func HasOwnerReference(ownerRefs []metav1.OwnerReference, obj client.Object, scheme *runtime.Scheme) (bool, error)
- func RemoveControllerReference(owner, object metav1.Object, scheme *runtime.Scheme) error
- func RemoveFinalizer(o client.Object, finalizer string) (finalizersUpdated bool)
- func RemoveOwnerReference(owner, object metav1.Object, scheme *runtime.Scheme) error
- func SetControllerReference(owner, controlled metav1.Object, scheme *runtime.Scheme, opts ...OwnerReferenceOption) error
- func SetOwnerReference(owner, object metav1.Object, scheme *runtime.Scheme, opts ...OwnerReferenceOption) error
- type AlreadyOwnedError
- type MutateFn
- type OperationResult
- func CreateOrPatch(ctx context.Context, c client.Client, obj client.Object, f MutateFn) (OperationResult, error)
- func CreateOrUpdate(ctx context.Context, c client.Client, obj client.Object, f MutateFn) (OperationResult, error)
- type OwnerReferenceOption
Examples ¶
Functions ¶
func AddFinalizer ¶
AddFinalizer accepts an Object and adds the provided finalizer if not present. It returns an indication of whether it updated the object's list of finalizers.
func ContainsFinalizer ¶
ContainsFinalizer checks an Object that the provided finalizer is present.
func HasControllerReference ¶
HasControllerReference returns true if the object has an owner ref with controller equal to true
func HasOwnerReference ¶
func HasOwnerReference(ownerRefs []metav1.OwnerReference, obj client.Object, scheme *runtime.Scheme) (bool, error)
HasOwnerReference returns true if the owners list contains an owner reference that matches the object's group, kind, and name.
func RemoveControllerReference ¶
RemoveControllerReference removes an owner reference where the controller equals true
func RemoveFinalizer ¶
RemoveFinalizer accepts an Object and removes the provided finalizer if present. It returns an indication of whether it updated the object's list of finalizers.
func RemoveOwnerReference ¶
RemoveOwnerReference is a helper method to make sure the given object removes an owner reference to the object provided. This allows you to remove the owner to establish a new owner of the object in a subsequent call.
func SetControllerReference ¶
func SetControllerReference(owner, controlled metav1.Object, scheme *runtime.Scheme, opts ...OwnerReferenceOption) error
SetControllerReference sets owner as a Controller OwnerReference on controlled. This is used for garbage collection of the controlled object and for reconciling the owner object on changes to controlled (with a Watch + EnqueueRequestForOwner). Since only one OwnerReference can be a controller, it returns an error if there is another OwnerReference with Controller flag set.
func SetOwnerReference ¶
func SetOwnerReference(owner, object metav1.Object, scheme *runtime.Scheme, opts ...OwnerReferenceOption) error
SetOwnerReference is a helper method to make sure the given object contains an object reference to the object provided. This allows you to declare that owner has a dependency on the object without specifying it as a controller. If a reference to the same object already exists, it'll be overwritten with the newly provided version.
Types ¶
type AlreadyOwnedError ¶
type AlreadyOwnedError struct { Object metav1.Object Owner metav1.OwnerReference }
AlreadyOwnedError is an error returned if the object you are trying to assign a controller reference is already owned by another controller Object is the subject and Owner is the reference for the current owner.
func (*AlreadyOwnedError) Error ¶
func (e *AlreadyOwnedError) Error() string
type MutateFn ¶
type MutateFn func() error
MutateFn is a function which mutates the existing object into its desired state.
type OperationResult ¶
type OperationResult string
OperationResult is the action result of a CreateOrUpdate or CreateOrPatch call.
const ( // OperationResultNone means that the resource has not been changed. OperationResultNone OperationResult = "unchanged" // OperationResultCreated means that a new resource is created. OperationResultCreated OperationResult = "created" // OperationResultUpdated means that an existing resource is updated. OperationResultUpdated OperationResult = "updated" // OperationResultUpdatedStatus means that an existing resource and its status is updated. OperationResultUpdatedStatus OperationResult = "updatedStatus" // OperationResultUpdatedStatusOnly means that only an existing status is updated. OperationResultUpdatedStatusOnly OperationResult = "updatedStatusOnly" )
func CreateOrPatch ¶
func CreateOrPatch(ctx context.Context, c client.Client, obj client.Object, f MutateFn) (OperationResult, error)
CreateOrPatch attempts to fetch the given object from the Kubernetes cluster. If the object didn't exist, MutateFn will be called, and it will be created. If the object did exist, MutateFn will be called, and if it changed the object, it will be patched. Otherwise, it will be left unchanged. The executed operation (and an error) will be returned.
WARNING: If the MutateFn resets a value on obj that has a default value, CreateOrPatch will *always* perform a patch. This is because when the object is fetched from the API server, the value will have taken on the default value, and the check for equality will fail. For example, Deployments must have a Replicas value set. If the MutateFn sets a Deployment's Replicas to nil, then it will never match with the object returned from the API server, which defaults the value to 1.
WARNING: CreateOrPatch assumes that no values have been set on obj aside from the Name/Namespace. Values other than Name and Namespace that existed on obj may be overwritten by the corresponding values in the object returned from the Kubernetes API server. When this happens, the Patch will not work as expected.
Note: changes to any sub-resource other than status will be ignored. Changes to the status sub-resource will only be applied if the object already exist. To change the status on object creation, the easiest way is to requeue the object in the controller if OperationResult is OperationResultCreated
func CreateOrUpdate ¶
func CreateOrUpdate(ctx context.Context, c client.Client, obj client.Object, f MutateFn) (OperationResult, error)
CreateOrUpdate attempts to fetch the given object from the Kubernetes cluster. If the object didn't exist, MutateFn will be called, and it will be created. If the object did exist, MutateFn will be called, and if it changed the object, it will be updated. Otherwise, it will be left unchanged. The executed operation (and an error) will be returned.
WARNING: If the MutateFn resets a value on obj that has a default value, CreateOrUpdate will *always* perform an update. This is because when the object is fetched from the API server, the value will have taken on the default value, and the check for equality will fail. For example, Deployments must have a Replicas value set. If the MutateFn sets a Deployment's Replicas to nil, then it will never match with the object returned from the API server, which defaults the value to 1.
WARNING: CreateOrUpdate assumes that no values have been set on obj aside from the Name/Namespace. Values other than Name and Namespace that existed on obj may be overwritten by the corresponding values in the object returned from the Kubernetes API server. When this happens, the Update will not work as expected.
Note: changes made by MutateFn to any sub-resource (status...), will be
discarded.
This example creates or updates an existing deployment.
Code:play
Example¶
package main
import (
"context"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
logf "sigs.k8s.io/controller-runtime/pkg/log"
)
var (
log = logf.Log.WithName("controllerutil-examples")
)
// This example creates or updates an existing deployment.
func main() {
// c is client.Client
// Create or Update the deployment default/foo
deploy := &appsv1.Deployment{ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "default"}}
op, err := controllerutil.CreateOrUpdate(context.TODO(), c, deploy, func() error {
// Deployment selector is immutable so we set this value only if
// a new object is going to be created
if deploy.ObjectMeta.CreationTimestamp.IsZero() {
deploy.Spec.Selector = &metav1.LabelSelector{
MatchLabels: map[string]string{"foo": "bar"},
}
}
// update the Deployment pod template
deploy.Spec.Template = corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"foo": "bar",
},
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "busybox",
Image: "busybox",
},
},
},
}
return nil
})
if err != nil {
log.Error(err, "Deployment reconcile failed")
} else {
log.Info("Deployment successfully reconciled", "operation", op)
}
}
type OwnerReferenceOption ¶
type OwnerReferenceOption func(*metav1.OwnerReference)
OwnerReferenceOption is a function that can modify a `metav1.OwnerReference`.
func WithBlockOwnerDeletion ¶
func WithBlockOwnerDeletion(blockOwnerDeletion bool) OwnerReferenceOption
WithBlockOwnerDeletion allows configuring the BlockOwnerDeletion field on the `metav1.OwnerReference`.
Source Files ¶
controllerutil.go doc.go
- Version
- v0.21.0 (latest)
- Published
- May 20, 2025
- Platform
- linux/amd64
- Imports
- 12 packages
- Last checked
- 25 minutes ago –
Tools for package owners.