controller

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2018 License: Apache-2.0 Imports: 23 Imported by: 0

Documentation

Overview

The controller package provides libraries for creating controllers.

Example
package main

import (
	"flag"
	"log"

	"github.com/kubernetes-sigs/kubebuilder/pkg/config"
	"github.com/kubernetes-sigs/kubebuilder/pkg/controller"
	"github.com/kubernetes-sigs/kubebuilder/pkg/inject/run"

	corev1 "k8s.io/api/core/v1"
)

func main() {
	// Step 1: Register informers to Watch for Pod events
	flag.Parse()
	informerFactory := config.GetKubernetesInformersOrDie()
	if err := controller.AddInformerProvider(&corev1.Pod{}, informerFactory.Core().V1().Pods()); err != nil {
		log.Fatalf("Could not set informer %v", err)
	}

	// Step 2: Create a new Pod controller to reconcile Pods changes using the default
	// Reconcile function to print messages on events
	podController := &controller.GenericController{}
	if err := podController.Watch(&corev1.Pod{}); err != nil {
		log.Fatalf("%v", err)
	}
	controller.AddController(podController)

	// Step 3: RunInformersAndControllers all informers and controllers
	controller.RunInformersAndControllers(run.CreateRunArguments())
}
Output:

Example (Second)
package main

import (
	"flag"
	"log"

	"github.com/kubernetes-sigs/kubebuilder/pkg/config"
	"github.com/kubernetes-sigs/kubebuilder/pkg/controller"
	"github.com/kubernetes-sigs/kubebuilder/pkg/controller/types"
	"github.com/kubernetes-sigs/kubebuilder/pkg/inject/run"
	corev1 "k8s.io/api/core/v1"
	corev1informer "k8s.io/client-go/informers/core/v1"
	corev1lister "k8s.io/client-go/listers/core/v1"
)

func main() {
	// Step 1: Register informers to Watch for Pod events
	flag.Parse()
	informerFactory := config.GetKubernetesInformersOrDie()
	if err := controller.AddInformerProvider(&corev1.Pod{}, informerFactory.Core().V1().Pods()); err != nil {
		log.Fatalf("Could not set informer %v", err)
	}

	// Step 2: Create a new Pod controller to reconcile Pods changes using the default
	// Reconcile function to print messages on events
	podController := &Controller{
		podlister: controller.GetInformerProvider(&corev1.Pod{}).(corev1informer.PodInformer).Lister(),
	}
	genericController := &controller.GenericController{
		Name:      "PodController",
		Reconcile: podController.Reconcile,
	}
	if err := genericController.Watch(&corev1.Pod{}); err != nil {
		log.Fatalf("%v", err)
	}
	controller.AddController(genericController)

	// Step 3: RunInformersAndControllers all informers and controllers
	controller.RunInformersAndControllers(run.CreateRunArguments())
}

type Controller struct {
	podlister corev1lister.PodLister
}

func (c *Controller) Reconcile(key types.ReconcileKey) error {
	pod, err := c.podlister.Pods(key.Namespace).Get(key.Name)
	if err != nil {
		log.Printf("Failed to reconcile Pod %+v\n", err)
		return err
	}
	log.Printf("Reconcile Pod %+v\n", pod)
	return nil
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// DefaultManager is the ControllerManager used by the package functions
	DefaultManager = &defaultManager
)
View Source
var (
	// DefaultReconcileFn is used by GenericController if Reconcile is not set
	DefaultReconcileFn = func(k types.ReconcileKey) error {
		log.Printf("No ReconcileFn defined - skipping %+v", k)
		return nil
	}
)

Functions

func AddController

func AddController(controller *GenericController)

AddController registers a new controller to be run.

Example
package main

import (
	"log"

	"github.com/kubernetes-sigs/kubebuilder/pkg/controller"
	"github.com/kubernetes-sigs/kubebuilder/pkg/controller/types"
	"k8s.io/api/core/v1"
)

func main() {
	podController := &controller.GenericController{
		Reconcile: func(key types.ReconcileKey) error {
			log.Printf("Reconciling Pod %v\n", key)
			return nil
		},
	}
	if err := podController.Watch(&v1.Pod{}); err != nil {
		log.Fatalf("%v", err)
	}
	controller.AddController(podController)
}
Output:

func AddInformerProvider

func AddInformerProvider(object metav1.Object, informerProvider informers.InformerProvider) error

AddInformerProvider registers a new shared SharedIndexInformer under the object type. SharedIndexInformer will be RunInformersAndControllers by calling RunInformersAndControllers on the ControllerManager.

Example
package main

import (
	"flag"
	"log"

	"github.com/kubernetes-sigs/kubebuilder/pkg/config"
	"github.com/kubernetes-sigs/kubebuilder/pkg/controller"

	corev1 "k8s.io/api/core/v1"
)

func main() {
	flag.Parse()
	informerFactory := config.GetKubernetesInformersOrDie()

	// Register informers to Watch for Pod events
	if err := controller.AddInformerProvider(&corev1.Pod{}, informerFactory.Core().V1().Pods()); err != nil {
		log.Fatalf("Could not set informer %v", err)
	}
}
Output:

func GetInformerProvider

func GetInformerProvider(object metav1.Object) informers.InformerProvider

GetInformerProvider returns the InformerProvider for the object type. Use this to get Listers for objects.

Example
package main

import (
	"flag"
	"log"

	"github.com/kubernetes-sigs/kubebuilder/pkg/controller"
	corev1 "k8s.io/api/core/v1"
	"k8s.io/client-go/informers/core/v1"
)

func main() {
	// Get the registered PodInformer
	flag.Parse()
	provider := controller.GetInformerProvider(&corev1.Pod{})
	podinformer, ok := provider.(v1.PodInformer)
	if !ok {
		log.Fatalf("Expected PodInformer for Pod, got %T", podinformer)
	}
	lister := podinformer.Lister()
	lister.Pods("default").Get("pod-name")
}
Output:

func RunInformersAndControllers

func RunInformersAndControllers(options run.RunArguments)

RunInformersAndControllers runs all of the informers and controllers

Example
package main

import (
	"github.com/kubernetes-sigs/kubebuilder/pkg/controller"
	"github.com/kubernetes-sigs/kubebuilder/pkg/inject/run"
)

func main() {
	// RunInformersAndControllers all registered informers and controllers
	controller.RunInformersAndControllers(run.CreateRunArguments())
}
Output:

Types

type ControllerManager

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

ControllerManager registers shared informers and controllers

Example
package main

import (
	"github.com/kubernetes-sigs/kubebuilder/pkg/controller"
)

func main() {
	// Create a new empty ControllerManager for managing Informers and Controllers
	var _ = &controller.ControllerManager{}
}
Output:

func (*ControllerManager) AddController

func (m *ControllerManager) AddController(controller *GenericController)

AddController registers a new controller to be run..

func (*ControllerManager) AddInformerProvider

func (m *ControllerManager) AddInformerProvider(object metav1.Object, informerProvider informers.InformerProvider) error

AddInformerProvider registers a new shared SharedIndexInformer under the object type. SharedIndexInformer will be RunInformersAndControllers by calling RunInformersAndControllers on the ControllerManager.

func (*ControllerManager) GetController

func (m *ControllerManager) GetController(name string) *GenericController

GetController returns a registered controller with the name

func (*ControllerManager) GetInformer

func (m *ControllerManager) GetInformer(object metav1.Object) cache.SharedInformer

GetInformer returns the Informer for an object

func (*ControllerManager) GetInformerProvider

func (m *ControllerManager) GetInformerProvider(object metav1.Object) informers.InformerProvider

GetInformerProvider returns the InformerProvider for the object type

func (*ControllerManager) RunInformersAndControllers

func (m *ControllerManager) RunInformersAndControllers(options run.RunArguments)

RunInformersAndControllers starts the registered informers and controllers. Sets options.Parallelism to 1 if it is lt 1 Creates a new channel for options.Stop if it is nil

func (*ControllerManager) String

func (m *ControllerManager) String() string

String prints the registered shared informers

type GenericController

type GenericController struct {
	// Name is the name of the controller
	Name string

	// Reconcile implements the controller business logic.
	Reconcile types.ReconcileFn

	// informerProvider contains the registry of shared informers to use.
	InformerRegistry informers.InformerGetter

	BeforeReconcile func(key types.ReconcileKey)
	AfterReconcile  func(key types.ReconcileKey, err error)
	// contains filtered or unexported fields
}

GenericController watches event sources and invokes a Reconcile function

Example
package main

import (
	"flag"
	"fmt"
	"log"

	"github.com/kubernetes-sigs/kubebuilder/pkg/config"
	"github.com/kubernetes-sigs/kubebuilder/pkg/controller"
	"github.com/kubernetes-sigs/kubebuilder/pkg/controller/eventhandlers"
	"github.com/kubernetes-sigs/kubebuilder/pkg/controller/types"
	"github.com/kubernetes-sigs/kubebuilder/pkg/inject/run"

	appsv1 "k8s.io/api/apps/v1"

	corev1 "k8s.io/api/core/v1"
)

func main() {
	// Step 1: Register informers with the ControllerManager to Watch for Pod and ReplicaSet events
	flag.Parse()
	informerFactory := config.GetKubernetesInformersOrDie()
	if err := controller.AddInformerProvider(&corev1.Pod{}, informerFactory.Core().V1().Pods()); err != nil {
		log.Fatalf("Could not set informer %v", err)
	}
	if err := controller.AddInformerProvider(&appsv1.ReplicaSet{}, informerFactory.Apps().V1().ReplicaSets()); err != nil {
		log.Fatalf("Could not set informer %v", err)
	}

	// Step 2.1: Create a new Pod controller to reconcile Pods changes
	podController := &controller.GenericController{
		Reconcile: func(key types.ReconcileKey) error {
			fmt.Printf("Reconciling Pod %v\n", key)
			return nil
		},
	}
	if err := podController.Watch(&corev1.Pod{}); err != nil {
		log.Fatalf("%v", err)
	}
	controller.AddController(podController)

	// Step 2.2: Create a new ReplicaSet controller to reconcile ReplicaSet changes
	rsController := &controller.GenericController{
		Reconcile: func(key types.ReconcileKey) error {
			fmt.Printf("Reconciling ReplicaSet %v\n", key)
			return nil
		},
	}

	fn := func(k types.ReconcileKey) (interface{}, error) {
		return informerFactory.Apps().V1().ReplicaSets().Lister().ReplicaSets(k.Namespace).Get(k.Name)
	}
	if err := rsController.WatchControllerOf(&corev1.Pod{}, eventhandlers.Path{fn}); err != nil {
		log.Fatalf("%v", err)
	}
	if err := rsController.Watch(&appsv1.ReplicaSet{}); err != nil {
		log.Fatalf("%v", err)
	}
	controller.AddController(rsController)

	// Step 3: RunInformersAndControllers all informers and controllers
	controller.RunInformersAndControllers(run.CreateRunArguments())
}
Output:

func (*GenericController) GetMetrics

func (gc *GenericController) GetMetrics() metrics.Metrics

GetMetrics returns metrics about the queue processing

func (*GenericController) Watch

Watch watches objects matching obj's type and enqueues their keys to be reconcild.

Example
package main

import (
	"flag"
	"fmt"
	"log"

	"github.com/kubernetes-sigs/kubebuilder/pkg/config"
	"github.com/kubernetes-sigs/kubebuilder/pkg/controller"
	"github.com/kubernetes-sigs/kubebuilder/pkg/controller/types"
	"github.com/kubernetes-sigs/kubebuilder/pkg/inject/run"
	"k8s.io/api/core/v1"
)

func main() {
	// One time setup for program
	flag.Parse()
	informerFactory := config.GetKubernetesInformersOrDie()
	if err := controller.AddInformerProvider(&v1.Pod{}, informerFactory.Core().V1().Pods()); err != nil {
		log.Fatalf("Could not set informer %v", err)
	}

	// Per-controller setup
	c := &controller.GenericController{
		Name: "Foo",
		Reconcile: func(key types.ReconcileKey) error {
			fmt.Printf("Reconciling Pod %s\n", key)
			return nil
		},
	}
	if err := c.Watch(&v1.Pod{}); err != nil {
		log.Fatalf("%v", err)
	}
	controller.AddController(c)

	// One time for program
	controller.RunInformersAndControllers(run.CreateRunArguments())
}
Output:

func (*GenericController) WatchChannel

func (gc *GenericController) WatchChannel(source <-chan string) error

WatchChannel enqueues object keys read from the channel.

Example
package main

import (
	"fmt"
	"log"

	"github.com/kubernetes-sigs/kubebuilder/pkg/controller"
	"github.com/kubernetes-sigs/kubebuilder/pkg/controller/types"
	"github.com/kubernetes-sigs/kubebuilder/pkg/inject/run"
)

func main() {
	podkeys := make(chan string)

	c := &controller.GenericController{
		Reconcile: func(key types.ReconcileKey) error {
			fmt.Printf("Reconciling Pod %s\n", key)
			return nil
		},
	}
	if err := c.WatchChannel(podkeys); err != nil {
		log.Fatalf("%v", err)
	}
	controller.AddController(c)
	controller.RunInformersAndControllers(run.CreateRunArguments())

	podkeys <- "namespace/pod-name"
}
Output:

func (*GenericController) WatchControllerOf

func (gc *GenericController) WatchControllerOf(obj metav1.Object, path eventhandlers.Path,
	p ...predicates.Predicate) error

WatchControllerOf reconciles the controller of the object type being watched. e.g. If the controller created a Pod, watch the Pod for events and invoke the controller reconcile function. Uses path to lookup the ancestors. Will lookup each ancestor in the path until it gets to the root and then reconcile this key.

Example: Deployment controller creates a ReplicaSet. ReplicaSet controller creates a Pod. Deployment controller wants to have its reconcile method called for Pod events for any Pods it created (transitively). - Pod event occurs - find owners references - Lookup the Pod parent ReplicaSet by using the first path element (compare UID to ref) - Lookup the ReplicaSet parent Deployment by using the second path element (compare UID to ref) - Enqueue reconcile for Deployment namespace/name

This could be implemented as: WatchControllerOf(&corev1.Pod, eventhandlers.Path{FnToLookupReplicaSetByNamespaceName, FnToLookupDeploymentByNamespaceName })

Example
package main

import (
	"flag"
	"fmt"
	"log"

	"github.com/kubernetes-sigs/kubebuilder/pkg/config"
	"github.com/kubernetes-sigs/kubebuilder/pkg/controller"
	"github.com/kubernetes-sigs/kubebuilder/pkg/controller/eventhandlers"
	"github.com/kubernetes-sigs/kubebuilder/pkg/controller/types"
	"github.com/kubernetes-sigs/kubebuilder/pkg/inject/run"

	corev1 "k8s.io/api/core/v1"
)

func main() {
	// One time setup for program
	flag.Parse()
	informerFactory := config.GetKubernetesInformersOrDie()
	if err := controller.AddInformerProvider(&corev1.Pod{}, informerFactory.Core().V1().Pods()); err != nil {
		log.Fatalf("Could not set informer %v", err)
	}

	// Per-controller setup
	c := &controller.GenericController{
		Reconcile: func(key types.ReconcileKey) error {
			fmt.Printf("Reconciling Pod %s\n", key)
			return nil
		},
	}
	fn := func(k types.ReconcileKey) (interface{}, error) {
		return informerFactory.Apps().V1().ReplicaSets().Lister().ReplicaSets(k.Namespace).Get(k.Name)
	}
	err := c.WatchControllerOf(&corev1.Pod{}, eventhandlers.Path{fn})
	if err != nil {
		log.Fatalf("%v", err)
	}
	controller.AddController(c)

	// One time for program
	controller.RunInformersAndControllers(run.CreateRunArguments())
}
Output:

func (*GenericController) WatchEvents

func (gc *GenericController) WatchEvents(obj metav1.Object, provider types.HandleFnProvider) error

WatchEvents watches objects matching obj's type and uses the functions from provider to handle events.

Example
package main

import (
	"flag"
	"fmt"
	"log"

	"github.com/kubernetes-sigs/kubebuilder/pkg/config"
	"github.com/kubernetes-sigs/kubebuilder/pkg/controller"
	"github.com/kubernetes-sigs/kubebuilder/pkg/controller/eventhandlers"
	"github.com/kubernetes-sigs/kubebuilder/pkg/controller/types"
	"github.com/kubernetes-sigs/kubebuilder/pkg/inject/run"

	corev1 "k8s.io/api/core/v1"
	"k8s.io/client-go/tools/cache"
	"k8s.io/client-go/util/workqueue"
)

func main() {
	// One time setup for program
	flag.Parse()
	informerFactory := config.GetKubernetesInformersOrDie()
	if err := controller.AddInformerProvider(&corev1.Pod{}, informerFactory.Core().V1().Pods()); err != nil {
		log.Fatalf("Could not set informer %v", err)

	}

	// Per-controller setup
	c := &controller.GenericController{
		Reconcile: func(key types.ReconcileKey) error {
			fmt.Printf("Reconciling Pod %s\n", key)
			return nil
		},
	}
	err := c.WatchEvents(&corev1.Pod{},
		// This function returns the callbacks that will be invoked for events
		func(q workqueue.RateLimitingInterface) cache.ResourceEventHandler {
			// This function implements the same functionality as GenericController.Watch
			return cache.ResourceEventHandlerFuncs{
				AddFunc:    func(obj interface{}) { q.AddRateLimited(eventhandlers.MapToSelf(obj)) },
				UpdateFunc: func(old, obj interface{}) { q.AddRateLimited(eventhandlers.MapToSelf(obj)) },
				DeleteFunc: func(obj interface{}) { q.AddRateLimited(eventhandlers.MapToSelf(obj)) },
			}
		})
	if err != nil {
		log.Fatalf("%v", err)
	}
	controller.AddController(c)

	// One time for program
	controller.RunInformersAndControllers(run.CreateRunArguments())
}
Output:

func (*GenericController) WatchTransformationKeyOf

func (gc *GenericController) WatchTransformationKeyOf(obj metav1.Object, mapFn eventhandlers.ObjToReconcileKey,
	p ...predicates.Predicate) error

WatchTransformationKeyOf watches objects matching obj's type and enqueues the key returned by mapFn.

Example
package main

import (
	"flag"
	"fmt"
	"log"
	"strings"

	"github.com/kubernetes-sigs/kubebuilder/pkg/config"
	"github.com/kubernetes-sigs/kubebuilder/pkg/controller"
	"github.com/kubernetes-sigs/kubebuilder/pkg/controller/types"
	"github.com/kubernetes-sigs/kubebuilder/pkg/inject/run"

	appsv1 "k8s.io/api/apps/v1"

	corev1 "k8s.io/api/core/v1"
)

func main() {
	// One time setup for program
	flag.Parse()
	informerFactory := config.GetKubernetesInformersOrDie()
	if err := controller.AddInformerProvider(&corev1.Pod{}, informerFactory.Core().V1().Pods()); err != nil {
		log.Fatalf("Could not set informer %v", err)
	}
	if err := controller.AddInformerProvider(&appsv1.ReplicaSet{}, informerFactory.Apps().V1().ReplicaSets()); err != nil {
		log.Fatalf("Could not set informer %v", err)
	}

	// Per-controller setup
	c := &controller.GenericController{
		Reconcile: func(key types.ReconcileKey) error {
			fmt.Printf("Reconciling Pod %s\n", key)
			return nil
		},
	}
	err := c.Watch(&appsv1.ReplicaSet{})
	if err != nil {
		log.Fatalf("%v", err)
	}
	err = c.WatchTransformationKeyOf(&corev1.Pod{},
		func(i interface{}) types.ReconcileKey {
			p, ok := i.(*corev1.Pod)
			if !ok {
				return types.ReconcileKey{}
			}

			// Find multiple parents based off the name
			return types.ReconcileKey{p.Namespace, strings.Split(p.Name, "-")[0]}
		},
	)
	if err != nil {
		log.Fatalf("%v", err)
	}
	controller.AddController(c)

	// One time for program
	controller.RunInformersAndControllers(run.CreateRunArguments())
}
Output:

func (*GenericController) WatchTransformationKeysOf

func (gc *GenericController) WatchTransformationKeysOf(obj metav1.Object, mapFn eventhandlers.ObjToReconcileKeys,
	p ...predicates.Predicate) error

WatchTransformationKeysOf watches objects matching obj's type and enqueues the keys returned by mapFn.

Example
package main

import (
	"flag"
	"fmt"
	"log"
	"strings"

	"github.com/kubernetes-sigs/kubebuilder/pkg/config"
	"github.com/kubernetes-sigs/kubebuilder/pkg/controller"
	"github.com/kubernetes-sigs/kubebuilder/pkg/controller/types"
	"github.com/kubernetes-sigs/kubebuilder/pkg/inject/run"

	appsv1 "k8s.io/api/apps/v1"

	corev1 "k8s.io/api/core/v1"
)

func main() {
	// One time setup for program
	flag.Parse()
	informerFactory := config.GetKubernetesInformersOrDie()
	if err := controller.AddInformerProvider(&corev1.Pod{}, informerFactory.Core().V1().Pods()); err != nil {
		log.Fatalf("Could not set informer %v", err)
	}
	if err := controller.AddInformerProvider(&appsv1.ReplicaSet{}, informerFactory.Apps().V1().ReplicaSets()); err != nil {
		log.Fatalf("Could not set informer %v", err)
	}

	// Per-controller setup
	c := &controller.GenericController{
		Reconcile: func(key types.ReconcileKey) error {
			fmt.Printf("Reconciling Pod %s\n", key)
			return nil
		},
	}
	err := c.Watch(&appsv1.ReplicaSet{})
	if err != nil {
		log.Fatalf("%v", err)
	}
	err = c.WatchTransformationKeysOf(&corev1.Pod{},
		func(i interface{}) []types.ReconcileKey {
			p, ok := i.(*corev1.Pod)
			if !ok {
				return []types.ReconcileKey{}
			}

			// Find multiple parents based off the name
			return []types.ReconcileKey{
				{p.Namespace, strings.Split(p.Name, "-")[0] + "-parent-1"},
				{p.Namespace, strings.Split(p.Name, "-")[0] + "-parent-2"},
			}
		},
	)
	if err != nil {
		log.Fatalf("%v", err)
	}
	controller.AddController(c)

	// One time for program
	controller.RunInformersAndControllers(run.CreateRunArguments())
}
Output:

func (*GenericController) WatchTransformationOf

func (gc *GenericController) WatchTransformationOf(obj metav1.Object, mapFn eventhandlers.ObjToKey,
	p ...predicates.Predicate) error

WatchTransformationOf watches objects matching obj's type and enqueues the key returned by mapFn.

Example
package main

import (
	"flag"
	"fmt"
	"log"
	"strings"

	"github.com/kubernetes-sigs/kubebuilder/pkg/config"
	"github.com/kubernetes-sigs/kubebuilder/pkg/controller"
	"github.com/kubernetes-sigs/kubebuilder/pkg/controller/types"
	"github.com/kubernetes-sigs/kubebuilder/pkg/inject/run"

	appsv1 "k8s.io/api/apps/v1"

	corev1 "k8s.io/api/core/v1"
)

func main() {
	// One time setup for program
	flag.Parse()
	informerFactory := config.GetKubernetesInformersOrDie()
	if err := controller.AddInformerProvider(&corev1.Pod{}, informerFactory.Core().V1().Pods()); err != nil {
		log.Fatalf("Could not set informer %v", err)
	}
	if err := controller.AddInformerProvider(&appsv1.ReplicaSet{}, informerFactory.Apps().V1().ReplicaSets()); err != nil {
		log.Fatalf("Could not set informer %v", err)
	}

	// Per-controller setup
	c := &controller.GenericController{
		Reconcile: func(key types.ReconcileKey) error {
			fmt.Printf("Reconciling Pod %s\n", key)
			return nil
		},
	}
	err := c.Watch(&appsv1.ReplicaSet{})
	if err != nil {
		log.Fatalf("%v", err)
	}
	err = c.WatchTransformationOf(&corev1.Pod{},
		func(i interface{}) string {
			p, ok := i.(*corev1.Pod)
			if !ok {
				return ""
			}

			// Find the parent key based on the name
			return p.Namespace + "/" + strings.Split(p.Name, "-")[0]
		},
	)
	if err != nil {
		log.Fatalf("%v", err)
	}
	controller.AddController(c)

	// One time for program
	controller.RunInformersAndControllers(run.CreateRunArguments())
}
Output:

func (*GenericController) WatchTransformationsOf

func (gc *GenericController) WatchTransformationsOf(obj metav1.Object, mapFn eventhandlers.ObjToKeys,
	p ...predicates.Predicate) error

WatchTransformationsOf watches objects matching obj's type and enqueues the keys returned by mapFn.

Example
package main

import (
	"flag"
	"fmt"
	"log"
	"strings"

	"github.com/kubernetes-sigs/kubebuilder/pkg/config"
	"github.com/kubernetes-sigs/kubebuilder/pkg/controller"
	"github.com/kubernetes-sigs/kubebuilder/pkg/controller/types"
	"github.com/kubernetes-sigs/kubebuilder/pkg/inject/run"

	appsv1 "k8s.io/api/apps/v1"

	corev1 "k8s.io/api/core/v1"
)

func main() {
	// One time setup for program
	flag.Parse()
	informerFactory := config.GetKubernetesInformersOrDie()
	if err := controller.AddInformerProvider(&corev1.Pod{}, informerFactory.Core().V1().Pods()); err != nil {
		log.Fatalf("Could not set informer %v", err)
	}
	if err := controller.AddInformerProvider(&appsv1.ReplicaSet{}, informerFactory.Apps().V1().ReplicaSets()); err != nil {
		log.Fatalf("Could not set informer %v", err)
	}

	// Per-controller setup
	c := &controller.GenericController{
		Reconcile: func(key types.ReconcileKey) error {
			fmt.Printf("Reconciling Pod %s\n", key)
			return nil
		},
	}
	err := c.Watch(&appsv1.ReplicaSet{})
	if err != nil {
		log.Fatalf("%v", err)
	}
	err = c.WatchTransformationsOf(&corev1.Pod{},
		func(i interface{}) []string {
			p, ok := i.(*corev1.Pod)
			if !ok {
				return []string{}
			}

			// Find multiple parents based off the name
			return []string{
				p.Namespace + "/" + strings.Split(p.Name, "-")[0] + "-parent-1",
				p.Namespace + "/" + strings.Split(p.Name, "-")[0] + "-parent-2",
			}
		},
	)
	if err != nil {
		log.Fatalf("%v", err)
	}
	controller.AddController(c)

	// One time for program
	controller.RunInformersAndControllers(run.CreateRunArguments())
}
Output:

Directories

Path Synopsis
The handlefunctions defines mapping and event handling functions for controllers
The handlefunctions defines mapping and event handling functions for controllers
The informers defines a registry for sharing informers
The informers defines a registry for sharing informers
The metrics package defines controller runtime metrics
The metrics package defines controller runtime metrics
The test package contains fake informers for testing controllers
The test package contains fake informers for testing controllers
The types package declares types used by the controller package
The types package declares types used by the controller package

Jump to

Keyboard shortcuts

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