smartrequeue

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2025 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Example (ControllerUsage)

This example shows how to use the SmartRequeue package in a Kubernetes controller.

package main

import (
	"fmt"
	"time"

	ctrl "sigs.k8s.io/controller-runtime"
	"sigs.k8s.io/controller-runtime/pkg/client"

	"github.com/openmcp-project/controller-utils/pkg/controller/smartrequeue"
)

func main() {
	// Create a store with min and max requeue intervals
	store := smartrequeue.NewStore(5*time.Second, 10*time.Minute, 2.0)

	// In your controller's Reconcile function:
	reconcileFunction := func(_ ctrl.Request) (ctrl.Result, error) {
		// Create a dummy object representing what you'd get from the client
		var obj client.Object // In real code: Get this from the client

		// Get the Entry for this specific object
		entry := store.For(obj)

		// Determine the state of the external resource...
		inProgress := false  // This would be determined by your logic
		errOccurred := false // This would be determined by your logic

		// nolint:gocritic
		if errOccurred {
			// Handle error case
			err := fmt.Errorf("something went wrong")
			return entry.Error(err)
		} else if inProgress {
			// Resource is changing - check back soon
			return entry.Reset()
		} else {
			// Resource is stable - gradually back off
			return entry.Backoff()
		}
	}

	// Call the reconcile function
	_, _ = reconcileFunction(ctrl.Request{})
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewContext

func NewContext(ctx context.Context, entry *Entry) context.Context

NewContext creates a new context with the given Entry. This is a utility function for passing Entry instances through context.

Types

type Entry

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

Entry is used to manage the requeue logic for a specific object. It holds the next duration to requeue and the store it belongs to.

func FromContext

func FromContext(ctx context.Context) *Entry

FromContext retrieves the Entry from the context, if it exists. Returns nil if no Entry is found in the context.

func (*Entry) Backoff

func (e *Entry) Backoff() (ctrl.Result, error)

Backoff returns a Result and increments the interval for the next iteration.

func (*Entry) Error

func (e *Entry) Error(err error) (ctrl.Result, error)

Error resets the duration to the minInterval and returns an empty Result and the error so that the controller-runtime can handle the exponential backoff for errors.

func (*Entry) Never

func (e *Entry) Never() (ctrl.Result, error)

Never deletes the entry from the store and returns an empty Result.

func (*Entry) Reset

func (e *Entry) Reset() (ctrl.Result, error)

Reset resets the duration to the minInterval and returns a Result with that interval.

type Store

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

Store is used to manage requeue entries for different objects. It holds a map of entries indexed by a key that uniquely identifies the object.

func NewStore

func NewStore(minInterval, maxInterval time.Duration, multiplier float32) *Store

NewStore creates a new Store with the specified minimum and maximum intervals and a multiplier for the exponential backoff logic.

func (*Store) Clear

func (s *Store) Clear()

Clear removes all entries from the store (mainly useful for testing).

func (*Store) For

func (s *Store) For(obj client.Object) *Entry

For gets or creates an Entry for the specified object.

Jump to

Keyboard shortcuts

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