operatorkit: github.com/giantswarm/operatorkit/controller Index | Files | Directories

package controller

import "github.com/giantswarm/operatorkit/controller"

Index

Package Files

controller.go crud_resource.go crud_resource_ops.go error.go finalizer.go metric.go patch.go resource_set.go spec.go string_cache.go

Constants

const (
    PrometheusNamespace = "operatorkit"
    PrometheusSubsystem = "controller"
)

func IsExecutionFailed Uses

func IsExecutionFailed(err error) bool

IsExecutionFailed asserts executionFailedError.

func IsInvalidConfig Uses

func IsInvalidConfig(err error) bool

IsInvalidConfig asserts invalidConfigError.

func IsInvalidRESTClient Uses

func IsInvalidRESTClient(err error) bool

IsInvalidRESTClient asserts invalidRESTClientError.

func IsNoResourceSet Uses

func IsNoResourceSet(err error) bool

IsNoResourceSet asserts noResourceSetError.

func IsStatusForbidden Uses

func IsStatusForbidden(err error) bool

IsStatusForbiddenError asserts statusForbiddenError and apimachinery StatusError with StatusReasonForbidden.

func IsTooManyResourceSets Uses

func IsTooManyResourceSets(err error) bool

IsTooManyResourceSets asserts tooManyResourceSetsError.

func ProcessDelete Uses

func ProcessDelete(ctx context.Context, obj interface{}, resources []Resource) error

ProcessDelete is a drop-in for an informer's DeleteFunc. It receives the custom object observed during custom resource watches and anything that implements Resource. ProcessDelete takes care about all necessary reconciliation logic for delete events.

func deleteFunc(obj interface{}) {
    err := c.ProcessDelete(obj, resources)
    if err != nil {
        // error handling here
    }
}

newResourceEventHandler := &cache.ResourceEventHandlerFuncs{
    DeleteFunc:    deleteFunc,
}

func ProcessUpdate Uses

func ProcessUpdate(ctx context.Context, obj interface{}, resources []Resource) error

ProcessUpdate is a drop-in for an informer's UpdateFunc. It receives the new custom object observed during custom resource watches and anything that implements Resource. ProcessUpdate takes care about all necessary reconciliation logic for update events. For complex resources this means state has to be created, deleted and updated eventually, in this order.

func updateFunc(oldObj, newObj interface{}) {
    err := c.ProcessUpdate(newObj, resources)
    if err != nil {
        // error handling here
    }
}

newResourceEventHandler := &cache.ResourceEventHandlerFuncs{
    UpdateFunc:    updateFunc,
}

type CRUDResource Uses

type CRUDResource struct {
    CRUDResourceOps
    // contains filtered or unexported fields
}

CRUDResource allows implementing complex CRUD Resrouces in structured way. Besides that is implements various context features defined in subpackages of the context package.

func NewCRUDResource Uses

func NewCRUDResource(config CRUDResourceConfig) (*CRUDResource, error)

func (*CRUDResource) EnsureCreated Uses

func (r *CRUDResource) EnsureCreated(ctx context.Context, obj interface{}) error

func (*CRUDResource) EnsureDeleted Uses

func (r *CRUDResource) EnsureDeleted(ctx context.Context, obj interface{}) error

type CRUDResourceConfig Uses

type CRUDResourceConfig struct {
    Logger micrologger.Logger
    // Ops is a set of operations used by CRUDResource to implement the
    // Resource interface.
    Ops CRUDResourceOps
}

type CRUDResourceOps Uses

type CRUDResourceOps interface {
    // Name returns the resource's name used for identification.
    Name() string

    // GetCurrentState receives the custom object observed during custom
    // resource watches. Its purpose is to return the current state of the
    // resources being managed by the operator. This can e.g. be some
    // actual data within a configmap as provided by the Kubernetes API.
    // This is not limited to Kubernetes resources though. Another example
    // would be to fetch and return information about Flannel bridges.
    //
    // NOTE GetCurrentState is called on create, delete and update events. When
    // called on create and delete events the provided custom object will be the
    // custom object currently known to the informer. On update events the
    // informer knows about the old and the new custom object. GetCurrentState
    // then receives the new custom object to be able to consume the current state
    // of a system.
    GetCurrentState(ctx context.Context, obj interface{}) (interface{}, error)
    // GetDesiredState receives the custom object observed during custom
    // resource watches. Its purpose is to return the desired state of the
    // resources being managed by the operator. The desired state should
    // always be able to be made up using the information provided by the
    // custom object. This can e.g. be some data within a configmap, how it
    // should be provided by the Kubernetes API. This is not limited to
    // Kubernetes resources though. Another example would be to make up and
    // return information about Flannel bridges, how they should look like
    // on a server host.
    //
    // NOTE GetDesiredState is called on create, delete and update events.
    // When called on create events the provided custom object will be the
    // custom object currently known to the informer. On update events the
    // informer knows about the old and the new custom object.
    // GetDesiredState then receives the new custom object to be able to
    // compute the desired state of a system.
    GetDesiredState(ctx context.Context, obj interface{}) (interface{}, error)

    // NewUpdatePatch is called upon observed custom object change. It receives
    // the observed custom object, the current state as provided by
    // GetCurrentState and the desired state as provided by
    // GetDesiredState. NewUpdatePatch analyses the current and desired
    // state and returns the patch to be applied by Create, Delete, and
    // Update functions. ApplyCreateChange, ApplyDeleteChange, and
    // ApplyUpdateChange are called only when the corresponding patch part
    // was created.
    NewUpdatePatch(ctx context.Context, obj, currentState, desiredState interface{}) (*Patch, error)
    // NewDeletePatch is called upon observed custom object deletion. It
    // receives the deleted custom object, the current state as provided by
    // GetCurrentState and the desired state as provided by
    // GetDesiredState. NewDeletePatch analyses the current and desired
    // state returns the patch to be applied by Create, Delete, and Update
    // functions. ApplyCreateChange, ApplyDeleteChange, and
    // ApplyUpdateChange are called only when the corresponding patch part
    // was created.
    NewDeletePatch(ctx context.Context, obj, currentState, desiredState interface{}) (*Patch, error)

    // ApplyCreateChange receives the new custom object observed during
    // custom resource watches. It also receives the create portion of the
    // Patch provided by NewUpdatePatch or NewDeletePatch.
    // ApplyCreateChange only has to create resources based on its provided
    // input. All other reconciliation logic and state transformation is
    // already done at this point of the reconciliation loop.
    ApplyCreateChange(ctx context.Context, obj, createChange interface{}) error
    // ApplyDeleteChange receives the new custom object observed during
    // custom resource watches. It also receives the delete portion of the
    // Patch provided by NewUpdatePatch or NewDeletePatch.
    // ApplyDeleteChange only has to delete resources based on its provided
    // input. All other reconciliation logic and state transformation is
    // already done at this point of the reconciliation loop.
    ApplyDeleteChange(ctx context.Context, obj, deleteChange interface{}) error
    // ApplyUpdateChange receives the new custom object observed during
    // custom resource watches. It also receives the update portion of the
    // Patch provided by NewUpdatePatch or NewDeletePatch.
    // ApplyUpdateChange has to update resources based on its provided
    // input. All other reconciliation logic and state transformation is
    // already done at this point of the reconciliation loop.
    ApplyUpdateChange(ctx context.Context, obj, updateChange interface{}) error
}

CRUDResourceOps provides set of building blocks of a CRUDResource business logic being reconciled when observing custom objects. The interface provides a guideline for an easier way to follow the rather complex intentions of operators in general.

type Config Uses

type Config struct {
    CRD       *apiextensionsv1beta1.CustomResourceDefinition
    CRDClient *k8scrdclient.CRDClient
    Informer  informer.Interface
    Logger    micrologger.Logger
    // ResourceSets is a list of resource sets. A resource set provides a specific
    // function to initialize the request context and a list of resources to be
    // executed for a reconciliation loop. That way each runtime object being
    // reconciled is executed against a desired list of resources. Since runtime
    // objects may differ in version and/or structure the resource router enables
    // custom inspection before each reconciliation loop. That way the complete
    // list of resources being executed for the received runtime object can be
    // versioned and different resources can be executed depending on the runtime
    // object being reconciled.
    ResourceSets []*ResourceSet
    // RESTClient needs to be configured with a serializer capable of serializing
    // and deserializing the object which is watched by the informer. Otherwise
    // deserialization will fail when trying to add a finalizer.
    //
    // For standard k8s object this is going to be e.g.
    //
    // 		k8sClient.CoreV1().RESTClient()
    //
    // For CRs of giantswarm this is going to be e.g.
    //
    // 		g8sClient.CoreV1alpha1().RESTClient()
    //
    RESTClient rest.Interface

    BackOffFactory func() backoff.Interface
    // Name is the name which the controller uses on finalizers for resources.
    // The name used should be unique in the kubernetes cluster, to ensure that
    // two operators which handle the same resource add two distinct finalizers.
    Name string
}

type Controller Uses

type Controller struct {
    // contains filtered or unexported fields
}

func New Uses

func New(config Config) (*Controller, error)

New creates a new configured operator controller.

func (*Controller) Boot Uses

func (c *Controller) Boot(ctx context.Context)

func (*Controller) Booted Uses

func (c *Controller) Booted() chan struct{}

func (*Controller) DeleteFunc Uses

func (c *Controller) DeleteFunc(obj interface{})

DeleteFunc executes the controller's ProcessDelete function.

func (*Controller) ProcessEvents Uses

func (c *Controller) ProcessEvents(ctx context.Context, deleteChan chan watch.Event, updateChan chan watch.Event, errChan chan error) error

ProcessEvents takes the event channels created by the operatorkit informer and executes the controller's event functions accordingly.

func (*Controller) UpdateFunc Uses

func (c *Controller) UpdateFunc(oldObj, newObj interface{})

UpdateFunc executes the controller's ProcessUpdate function.

type Patch Uses

type Patch struct {
    // contains filtered or unexported fields
}

Patch is a set of information required in order to reconcile to the desired state. Patch is split into three parts: create, delete and update changes. The parts are passed as arguments to Resource's ApplyCreateChange, ApplyDeleteChange and ApplyUpdateChange functions respectively. Patch changes are guaranteed to be applied in that order (i.e. create, update, delete).

func NewPatch Uses

func NewPatch() *Patch

func (*Patch) SetCreateChange Uses

func (p *Patch) SetCreateChange(create interface{})

func (*Patch) SetDeleteChange Uses

func (p *Patch) SetDeleteChange(delete interface{})

func (*Patch) SetUpdateChange Uses

func (p *Patch) SetUpdateChange(update interface{})

type Resource Uses

type Resource interface {
    // EnsureCreated is called when observed object is created or updated.
    // The object is in state after cration or modification. This method must
    // be idempotent.
    EnsureCreated(ctx context.Context, obj interface{}) error
    // EnsureDeleted is called when observed object is deleted. The object
    // is in last observed state before the deletion. This method must be
    // idempotent.
    EnsureDeleted(ctx context.Context, obj interface{}) error
    // Name returns the resource's name used for identification.
    Name() string
}

Resource is an interface. Resources are the building blocks of the operator's reconciliation logic. Note there can be multiple Resources reconciling the same object in the chain. In that case they are guaranteed to be executed in order one after another.

type ResourceSet Uses

type ResourceSet struct {
    // contains filtered or unexported fields
}

func NewResourceSet Uses

func NewResourceSet(c ResourceSetConfig) (*ResourceSet, error)

func (*ResourceSet) Handles Uses

func (r *ResourceSet) Handles(obj interface{}) bool

func (*ResourceSet) InitCtx Uses

func (r *ResourceSet) InitCtx(ctx context.Context, obj interface{}) (context.Context, error)

func (*ResourceSet) Resources Uses

func (r *ResourceSet) Resources() []Resource

type ResourceSetConfig Uses

type ResourceSetConfig struct {
    // Handles determines if this resource set handles the reconciliation of the
    // object.
    Handles func(obj interface{}) bool
    // InitCtx is to prepare the given context for a single reconciliation loop.
    // Operators can implement common context packages to enable communication
    // between resources. These context packages can be set up within this context
    // initializer function. InitCtx receives the runtime object being reconciled
    // as second argument. Information provided by the runtime object can be used
    // to initialize the context.
    InitCtx func(ctx context.Context, obj interface{}) (context.Context, error)
    // Logger is a usual micrologger instance to emit log messages, if any.
    Logger micrologger.Logger
    // Resources is the list of controller resources being executed on runtime
    // object reconciliation if Handles returns true when asked by the
    // controller. Resources are executed in given order.
    Resources []Resource
}

Directories

PathSynopsis
context/finalizerskeptcontextPackage finalizerskeptcontext stores and accesses the kept in context.Context.
context/reconciliationcanceledcontextPackage reconciliationcanceledcontext stores and accesses the canceled in context.Context.
context/resourcecanceledcontextPackage resourcecanceledcontext stores and accesses the canceled in context.Context.
context/updateallowedcontextPackage updateallowedcontext stores and accesses the update allowed in context.Context.
resource/internal
resource/metricsresource
resource/retryresource

Package controller imports 26 packages (graph) and is imported by 264 packages. Updated 2019-07-10. Refresh now. Tools for package owners.