controllerutil

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2018 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package controllerutil contains utility functions for working with and implementing Controllers.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetControllerReference

func SetControllerReference(owner, object v1.Object, scheme *runtime.Scheme) error

SetControllerReference sets owner as a Controller OwnerReference on owned. This is used for garbage collection of the owned object and for reconciling the owner object on changes to owned (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.

Types

type AlreadyOwnedError

type AlreadyOwnedError struct {
	Object v1.Object
	Owner  v1.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 added in v0.1.3

type MutateFn func(existing runtime.Object) error

MutateFn is a function which mutates the existing object into it's desired state.

type OperationResult added in v0.1.3

type OperationResult string

OperationResult is the action result of a CreateOrUpdate 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"
)

func CreateOrUpdate added in v0.1.3

func CreateOrUpdate(ctx context.Context, c client.Client, obj runtime.Object, f MutateFn) (OperationResult, error)

CreateOrUpdate creates or updates the given object obj in the Kubernetes cluster. The object's desired state should be reconciled with the existing state using the passed in ReconcileFn. obj must be a struct pointer so that obj can be updated with the content returned by the Server.

It returns the executed operation and an error.

Example

This example creates or updates an existing deployment

package main

import (
	"context"

	appsv1 "k8s.io/api/apps/v1"
	corev1 "k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/runtime"

	"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
	logf "sigs.k8s.io/controller-runtime/pkg/runtime/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
	deployment := &appsv1.Deployment{ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "default"}}

	op, err := controllerutil.CreateOrUpdate(context.TODO(), c, deployment, func(existing runtime.Object) error {
		deploy := existing.(*appsv1.Deployment)

		// 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{
					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)
	}
}
Output:

Jump to

Keyboard shortcuts

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