kubernetesk8s.io/kubernetes/pkg/controller/framework Index | Examples | Files

package framework

import "k8s.io/kubernetes/pkg/controller/framework"

Package framework implements all the grunt work involved in running a simple controller.

Example

Code:play 

package main

import (
	"fmt"
	"time"

	"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
	"github.com/GoogleCloudPlatform/kubernetes/pkg/client/cache"
	"github.com/GoogleCloudPlatform/kubernetes/pkg/controller/framework"
	"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
	"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
)

func main() {
	// source simulates an apiserver object endpoint.
	source := framework.NewFakeControllerSource()

	// This will hold the downstream state, as we know it.
	downstream := cache.NewStore(framework.DeletionHandlingMetaNamespaceKeyFunc)

	// This will hold incoming changes. Note how we pass downstream in as a
	// KeyLister, that way resync operations will result in the correct set
	// of update/delete deltas.
	fifo := cache.NewDeltaFIFO(cache.MetaNamespaceKeyFunc, nil, downstream)

	// Let's do threadsafe output to get predictable test results.
	deletionCounter := make(chan string, 1000)

	cfg := &framework.Config{
		Queue:            fifo,
		ListerWatcher:    source,
		ObjectType:       &api.Pod{},
		FullResyncPeriod: time.Millisecond * 100,
		RetryOnError:     false,

		// Let's implement a simple controller that just deletes
		// everything that comes in.
		Process: func(obj interface{}) error {
			// Obj is from the Pop method of the Queue we make above.
			newest := obj.(cache.Deltas).Newest()

			if newest.Type != cache.Deleted {
				// Update our downstream store.
				err := downstream.Add(newest.Object)
				if err != nil {
					return err
				}

				// Delete this object.
				source.Delete(newest.Object.(runtime.Object))
			} else {
				// Update our downstream store.
				err := downstream.Delete(newest.Object)
				if err != nil {
					return err
				}

				// fifo's KeyOf is easiest, because it handles
				// DeletedFinalStateUnknown markers.
				key, err := fifo.KeyOf(newest.Object)
				if err != nil {
					return err
				}

				// Report this deletion.
				deletionCounter <- key
			}
			return nil
		},
	}

	// Create the controller and run it until we close stop.
	stop := make(chan struct{})
	defer close(stop)
	go framework.New(cfg).Run(stop)

	// Let's add a few objects to the source.
	testIDs := []string{"a-hello", "b-controller", "c-framework"}
	for _, name := range testIDs {
		// Note that these pods are not valid-- the fake source doesn't
		// call validation or anything.
		source.Add(&api.Pod{ObjectMeta: api.ObjectMeta{Name: name}})
	}

	// Let's wait for the controller to process the things we just added.
	outputSet := util.StringSet{}
	for i := 0; i < len(testIDs); i++ {
		outputSet.Insert(<-deletionCounter)
	}

	for _, key := range outputSet.List() {
		fmt.Println(key)
	}
}

Output:

a-hello
b-controller
c-framework

Index

Examples

Functions

func DeletionHandlingMetaNamespaceKeyFunc

func DeletionHandlingMetaNamespaceKeyFunc(obj interface{}) (string, error)

DeletionHandlingMetaNamespaceKeyFunc checks for cache.DeletedFinalStateUnknown objects before calling cache.MetaNamespaceKeyFunc.

Types

type Config

type Config struct {
	// The queue for your objects; either a cache.FIFO or
	// a cache.DeltaFIFO. Your Process() function should accept
	// the output of this Oueue's Pop() method.
	cache.Queue

	// Something that can list and watch your objects.
	cache.ListerWatcher

	// Something that can process your objects.
	Process ProcessFunc

	// The type of your objects.
	ObjectType runtime.Object

	// Reprocess everything at least this often.
	// Note that if it takes longer for you to clear the queue than this
	// period, you will end up processing items in the order determined
	// by cache.FIFO.Replace(). Currently, this is random. If this is a
	// problem, we can change that replacement policy to append new
	// things to the end of the queue instead of replacing the entire
	// queue.
	FullResyncPeriod time.Duration

	// If true, when Process() returns an error, re-enqueue the object.
	// TODO: add interface to let you inject a delay/backoff or drop
	//       the object completely if desired. Pass the object in
	//       question to this interface as a parameter.
	RetryOnError bool
}

Config contains all the settings for a Controller.

type Controller

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

Controller is a generic controller framework.

func New

func New(c *Config) *Controller

New makes a new Controller from the given Config.

func NewInformer

func NewInformer(
	lw cache.ListerWatcher,
	objType runtime.Object,
	resyncPeriod time.Duration,
	h ResourceEventHandler,
) (cache.Store, *Controller)

NewInformer returns a cache.Store and a controller for populating the store while also providing event notifications. You should only used the returned cache.Store for Get/List operations; Add/Modify/Deletes will cause the event notifications to be faulty.

Parameters:

func (*Controller) Run

func (c *Controller) Run(stopCh <-chan struct{})

Run begins processing items, and will continue until a value is sent down stopCh. It's an error to call Run more than once. Run blocks; call via go.

type FakeControllerSource

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

FakeControllerSource implements listing/watching for testing.

func NewFakeControllerSource

func NewFakeControllerSource() *FakeControllerSource

func (*FakeControllerSource) Add

func (f *FakeControllerSource) Add(obj runtime.Object)

Add adds an object to the set and sends an add event to watchers. obj's ResourceVersion is set.

func (*FakeControllerSource) AddDropWatch

func (f *FakeControllerSource) AddDropWatch(obj runtime.Object)

AddDropWatch adds an object to the set but forgets to send an add event to watchers. obj's ResourceVersion is set.

func (*FakeControllerSource) Change

func (f *FakeControllerSource) Change(e watch.Event, watchProbability float64)

Change records the given event (setting the object's resource version) and sends a watch event with the specified probability.

func (*FakeControllerSource) Delete

func (f *FakeControllerSource) Delete(lastValue runtime.Object)

Delete deletes an object from the set and sends a delete event to watchers. obj's ResourceVersion is set.

func (*FakeControllerSource) DeleteDropWatch

func (f *FakeControllerSource) DeleteDropWatch(lastValue runtime.Object)

DeleteDropWatch deletes an object from the set but forgets to send a delete event to watchers. obj's ResourceVersion is set.

func (*FakeControllerSource) List

List returns a list object, with its resource version set.

func (*FakeControllerSource) Modify

func (f *FakeControllerSource) Modify(obj runtime.Object)

Modify updates an object in the set and sends a modified event to watchers. obj's ResourceVersion is set.

func (*FakeControllerSource) ModifyDropWatch

func (f *FakeControllerSource) ModifyDropWatch(obj runtime.Object)

ModifyDropWatch updates an object in the set but forgets to send a modify event to watchers. obj's ResourceVersion is set.

func (*FakeControllerSource) Watch

func (f *FakeControllerSource) Watch(resourceVersion string) (watch.Interface, error)

Watch returns a watch, which will be pre-populated with all changes after resourceVersion.

type ProcessFunc

type ProcessFunc func(obj interface{}) error

ProcessFunc processes a single object.

type ResourceEventHandler

type ResourceEventHandler interface {
	OnAdd(obj interface{})
	OnUpdate(oldObj, newObj interface{})
	OnDelete(obj interface{})
}

ResourceEventHandler can handle notifications for events that happen to a resource. The events are informational only, so you can't return an error.

type ResourceEventHandlerFuncs

type ResourceEventHandlerFuncs struct {
	AddFunc    func(obj interface{})
	UpdateFunc func(oldObj, newObj interface{})
	DeleteFunc func(obj interface{})
}

ResourceEventHandlerFuncs is an adaptor to let you easily specify as many or as few of the notification functions as you want while still implementing ResourceEventHandler.

func (ResourceEventHandlerFuncs) OnAdd

func (r ResourceEventHandlerFuncs) OnAdd(obj interface{})

OnAdd calls AddFunc if it's not nil.

func (ResourceEventHandlerFuncs) OnDelete

func (r ResourceEventHandlerFuncs) OnDelete(obj interface{})

OnDelete calls DeleteFunc if it's not nil.

func (ResourceEventHandlerFuncs) OnUpdate

func (r ResourceEventHandlerFuncs) OnUpdate(oldObj, newObj interface{})

OnUpdate calls UpdateFunc if it's not nil.

Source Files

controller.go doc.go fake_controller_source.go

Version
v0.17.0
Published
May 12, 2015
Platform
darwin/amd64
Imports
12 packages
Last checked
34 seconds ago

Tools for package owners.