smartrequeue

package
v0.27.0 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2026 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.ReturnError(err)
		} else if inProgress {
			// Resource is changing - check back soon
			return entry.IsProgressing()
		} else {
			// Resource is stable - gradually back off
			return entry.IsStable()
		}
	}

	// 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) IsProgressing added in v0.27.0

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

IsProgressing indicates the resource is actively changing. It resets the backoff to minInterval and requeues after that interval.

func (*Entry) IsStable added in v0.27.0

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

IsStable indicates the resource has reached its desired state. It requeues after the current interval and increases the interval for the next call, implementing exponential backoff.

func (*Entry) ReturnError added in v0.27.0

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

ReturnError resets the backoff to minInterval and returns the given error, delegating backoff handling to controller-runtime.

func (*Entry) StopRequeue added in v0.27.0

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

StopRequeue removes the entry from the store and returns an empty result, stopping further requeues for this object.

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