basecontroller

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2023 License: EUPL-1.2 Imports: 14 Imported by: 0

Documentation

Overview

Package basecontroller

Index

Constants

View Source
const (
	ReasonResourceSynced         = "Synced"
	ReasonErrResourceExists      = "ErrResourceExists"
	MessageErrResourceExists     = "Resource %q already exists and is not managed by this controller"
	MessageResourceSyncedCreated = "%q synced successfully (created)"
	MessageResourceSyncedUpdated = "%q synced successfully (updated)"
	MessageResourceSyncedDeleted = "%q synced successfully (deleted)"
)

Message constants used with events

Variables

This section is empty.

Functions

func CreateRecorder

func CreateRecorder(
	kubeClientset kubeclientset.Interface,
	addToScheme func(s *runtime.Scheme) error,
	controllerName string) record.EventRecorder

CreateRecorder creates an event-broadcaster and and event-recorder, and returns the event-recorder. Add kumori types to the default Kubernetes Scheme so Events can be logged for kumori types. Example of use:

import (
	 kubeclientset "k8s.io/client-go/kubernetes"
  myscheme "my-controller/pkg/generated/clientset/versioned/scheme"
)
controllerName = "mycontroller"
recorder = base.CreateRecorder(kubeClientset, myscheme.AddToScheme, controllerName),

Types

type BaseController

type BaseController struct {

	// Property "IBaseController" is required for abstract methods. See:
	// https://medium.com/@adrianwit/abstract-class-reinvented-with-go-4a7326525034
	IBaseController

	// Just a description-name of our controller
	Name string

	// Used for set the OwnerRef.Kind property
	Kind string

	// WorkQueue is used to queue work to be processed instead of performing it as
	// soon as a change happens, thatthat rate limits items being added
	// to the queue.
	Workqueue workqueue.RateLimitingInterface

	// Recorder is an event recorder for recording Event resources to the
	// Kubernetes API.
	Recorder record.EventRecorder

	// SyncedFunctions are bool function that can be used to determine if an
	// informer has already synced.
	SyncedFunctions []cache.InformerSynced

	// PrimarySharedInformer is the informer related to the primary resource.
	// A SharedInformer maintains a local cache, that is eventually consistent
	// with the authoritative state. We use its add/delete/modify event notifications
	PrimarySharedInformers []cache.SharedIndexInformer

	// SecondarySharedInformers are the informers related to the secondary resources.
	SecondarySharedInformers []cache.SharedIndexInformer
}

BaseController is the base to implement a controller working in the way described in:

https://github.com/kubernetes/community/blob/master/contributors/devel/sig-api-machinery/controllers.md
https://github.com/kubernetes/sample-controller

It means:

  • There is a primary resource, that you'll be updating Status for.
  • There are secondary resources are resources that you'll be creating / updating / deleting
  • All the secondary resources created will have the OwnerRef metadata with the primary resource

The controller using this base should embed it and implement the abtract methods (SyncHandler for primary resources and HandleSencondary for secondary resources).

func (*BaseController) EnqueueObject

func (c *BaseController) EnqueueObject(obj interface{})

EnqueueObject takes a primary resource and converts it into a namespace/name string (the key) which is then put onto the work queue

func (*BaseController) GetObject

func (c *BaseController) GetObject(objSrc interface{}) (objDst metav1.Object, ownerRefName string, err error)

GetObject returns, using a generic interface{} reference, the secondary resource and its owner name.

func (*BaseController) Run

func (c *BaseController) Run(threadiness int, stopCh <-chan struct{}, errCh chan<- error)

Run will set up the event handlers for types we are interested in, as well as syncing informer caches and starting workers. It will block until stopCh is closed, at which point it will shutdown the workqueue and wait for workers to finish processing their current work items.

type IBaseController

type IBaseController interface {

	// Public methods
	GetName() string
	Run(int, <-chan struct{}, chan<- error)

	// Abstract methods: Must be implemented in children class.
	// Note1: they must be public, so uppercase is mandatory
	// Note2: if they are not implemented in children class, then a "fatal error:
	// stack overflow" will be occur
	SyncHandler(string) error
	HandleSecondary(obj interface{})

	// "False" public methods: because golang limitations, these methods must be
	// publics because they will be used from the abstract methods implementation
	GetObject(interface{}) (metav1.Object, string, error)
	EnqueueObject(interface{})
	// contains filtered or unexported methods
}

IBaseController is the interface of BaseController struct

Jump to

Keyboard shortcuts

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