Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Interface ¶
type Interface interface {
// GetObject returns the object for which sync applies
GetObject() interface{}
// GetOwner returns the object owner or nil if object does not have one
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/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
"github.com/presslabs/controller-util/syncer"
)
var (
c client.Client
scheme *runtime.Scheme
recorder record.EventRecorder
owner runtime.Object
log = logf.Log.WithName("controllerutil-examples")
)
func NewDeploymentSyncer(owner runtime.Object) syncer.Interface {
obj := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: "example",
Namespace: "default",
},
}
// c is client.Client
// scheme is *runtime.Scheme
return syncer.NewObjectSyncer("ExampleDeployment", owner, obj, c, scheme, 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{
{
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")
}
}
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