syncer

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2020 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (

	// ErrIgnore when returned the syncer ignores it and returns nil
	ErrIgnore = fmt.Errorf("ignored error")
)

Functions

func Sync

func Sync(ctx context.Context, syncer Interface, recorder record.EventRecorder) error

Sync mutates the subject of the syncer interface using controller-runtime CreateOrUpdate method, when obj is not nil. It takes care of setting owner references and recording kubernetes events where appropriate

Types

type Interface

type Interface interface {
	// Object returns the object for which sync applies
	Object() interface{}

	// GetObject returns the object for which sync applies
	// Deprecated: use github.com/presslabs/controller-util/syncer.Object() instead
	GetObject() interface{}

	// Owner returns the object owner or nil if object does not have one
	ObjectOwner() runtime.Object

	// GetOwner returns the object owner or nil if object does not have one
	// Deprecated: use github.com/presslabs/controller-util/syncer.ObjectOwner() instead
	GetOwner() runtime.Object

	// Sync persists data into the external store
	Sync(context.Context) (SyncResult, error)
}

Interface represents a syncer. A syncer persists an object (known as subject), into a store (kubernetes apiserver or generic stores) and records kubernetes events

func NewExternalSyncer

func NewExternalSyncer(name string, owner runtime.Object, obj interface{},
	syncFn func(context.Context, interface{}) (controllerutil.OperationResult, error)) Interface

NewExternalSyncer creates a new syncer which syncs a generic object persisting it's state into and external store The name is used for logging and event emitting purposes and should be an valid go identifier in upper camel case. (eg. GiteaRepo)

func NewObjectSyncer

func NewObjectSyncer(name string, owner, obj runtime.Object, c client.Client, scheme *runtime.Scheme,
	syncFn controllerutil.MutateFn) Interface

NewObjectSyncer creates a new kubernetes.Object syncer for a given object with an owner and persists data using controller-runtime's CreateOrUpdate. The name is used for logging and event emitting purposes and should be an valid go identifier in upper camel case. (eg. MysqlStatefulSet)

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"
	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/client-go/kubernetes/scheme"
	"k8s.io/client-go/tools/record"
	logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"

	"github.com/charlesld/controller-util/syncer"
)

var (
	recorder record.EventRecorder
	owner    runtime.Object
	log      = logf.Log.WithName("controllerutil-examples")
)

func NewDeploymentSyncer(owner runtime.Object) syncer.Interface {
	deploy := &appsv1.Deployment{
		ObjectMeta: metav1.ObjectMeta{
			Name:      "example",
			Namespace: "default",
		},
	}

	// c is client.Client
	return syncer.NewObjectSyncer("ExampleDeployment", owner, deploy, c, scheme.Scheme, 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
	})
}

func main() {
	// recorder is record.EventRecorder
	// owner is the owner for the syncer subject

	deploymentSyncer := NewDeploymentSyncer(owner)
	err := syncer.Sync(context.TODO(), deploymentSyncer, recorder)
	if err != nil {
		log.Error(err, "unable to sync")
	}
}
Output:

type ObjectSyncer added in v0.1.1

type ObjectSyncer struct {
	Owner  runtime.Object
	Obj    runtime.Object
	SyncFn controllerutil.MutateFn
	Name   string
	Client client.Client
	Scheme *runtime.Scheme
	// contains filtered or unexported fields
}

ObjectSyncer is a syncer.Interface for syncing kubernetes.Objects only by passing a SyncFn

func (*ObjectSyncer) GetObject added in v0.1.1

func (s *ObjectSyncer) GetObject() interface{}

GetObject returns the ObjectSyncer subject Deprecated: use github.com/presslabs/controller-util/syncer.Object() instead

func (*ObjectSyncer) GetOwner added in v0.1.1

func (s *ObjectSyncer) GetOwner() runtime.Object

GetOwner returns the ObjectSyncer owner Deprecated: use github.com/presslabs/controller-util/syncer.ObjectOwner() instead

func (*ObjectSyncer) Object added in v1.0.0

func (s *ObjectSyncer) Object() interface{}

Object returns the ObjectSyncer subject

func (*ObjectSyncer) ObjectOwner added in v1.0.0

func (s *ObjectSyncer) ObjectOwner() runtime.Object

ObjectOwner returns the ObjectSyncer owner

func (*ObjectSyncer) ObjectType added in v1.0.0

func (s *ObjectSyncer) ObjectType() string

ObjectType returns the type of the ObjectSyncer subject

func (*ObjectSyncer) ObjectWithoutSecretData added in v1.0.0

func (s *ObjectSyncer) ObjectWithoutSecretData() interface{}

ObjectWithoutSecretData returns the ObjectSyncer subject without secret data

func (*ObjectSyncer) OwnerType added in v1.0.0

func (s *ObjectSyncer) OwnerType() string

OwnerType returns the type of the ObjectSyncer owner

func (*ObjectSyncer) PreviousWithoutSecretData added in v1.0.0

func (s *ObjectSyncer) PreviousWithoutSecretData() interface{}

PreviousWithoutSecretData returns the ObjectSyncer previous subject without secret data

func (*ObjectSyncer) Sync added in v0.1.1

func (s *ObjectSyncer) Sync(ctx context.Context) (SyncResult, error)

Sync does the actual syncing and implements the syncer.Inteface Sync method

type SyncResult

type SyncResult struct {
	Operation    controllerutil.OperationResult
	EventType    string
	EventReason  string
	EventMessage string
}

SyncResult is a result of an Sync call

func (*SyncResult) SetEventData

func (r *SyncResult) SetEventData(eventType, reason, message string)

SetEventData sets event data on an SyncResult

type WithoutOwner

type WithoutOwner struct{}

WithoutOwner partially implements implements the syncer interface for the case the subject has no owner

func (*WithoutOwner) GetOwner

func (*WithoutOwner) GetOwner() runtime.Object

GetOwner implementation of syncer interface for the case the subject has no owner

Jump to

Keyboard shortcuts

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