Documentation ¶
Index ¶
- Variables
- func AddHook[T client.Object](ctx context.Context, cluster *Cluster, hook *Hook[T]) error
- func AddReconciler[T client.Object](ctx context.Context, cluster *Cluster, ...) error
- func Init(cfg ObservabilityConfig)
- type CRDs
- type Client
- func (c *Client[TItem, TList]) Create(ctx context.Context, resource TItem) error
- func (c *Client[TItem, TList]) Delete(ctx context.Context, resource TItem) error
- func (c *Client[TItem, TList]) Get(ctx context.Context, namespace, name string) (TItem, error)
- func (c *Client[TItem, TList]) List(ctx context.Context) (TList, error)
- func (c *Client[TItem, TList]) Update(ctx context.Context, resource TItem, subresources ...Subresource) error
- type Cluster
- type ClusterConfig
- type CustomResource
- type CustomResourceList
- type FieldUndefined
- type Hook
- func (h *Hook[T]) Default(ctx context.Context, obj runtime.Object) error
- func (h *Hook[T]) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error)
- func (h *Hook[T]) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error)
- func (h *Hook[T]) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error)
- type KindType
- type LeaderElectionConfig
- type ListUndefined
- type ObservabilityConfig
- type ReconcileEventType
- type ReconcilerFilterFunc
- type ReconcilerFunc
- type ResourceEventType
- type Subresource
Constants ¶
This section is empty.
Variables ¶
var ( ResourceEventTypeCreated = ResourceEventType(0) ResourceEventTypeUpdated = ResourceEventType(1) ResourceEventTypeDeleted = ResourceEventType(2) )
var ( ReconcileEventTypeCreatedOrUpdated = ReconcileEventType(0) ReconcileEventTypeDeleted = ReconcileEventType(1) )
Functions ¶
func AddHook ¶
AddHook registers a hook with the provided cluster.
Hooks defines admission control functionality for validating and applying default values to resources before CRUD operations occur.
func AddReconciler ¶
func AddReconciler[T client.Object](ctx context.Context, cluster *Cluster, reconcilerFilterFunc ReconcilerFilterFunc, reconcilerFunc ReconcilerFunc[T]) error
AddReconciler causes the specifed ReconcilerFunc to be invoked whenever any resource of type T in the specifed cluster is subject to a modification event: create, update or delete.
An eventFilterFunc can be provided that is used to reduce the scope of the reconciler. The eventFilterFunc is invoked before the ReconcilerFunc and is passed the object being modified and the type of the modification. The reconcilerFunc is only invoked if the eventFilterFunc returns true.
A nil filterFunc value matches all events.
Types ¶
type CRDs ¶
CRDs defines a mapping between a set of one or more structs that each represent a CRD and the k8s API Group and Version that they are defined within
type Client ¶
type Client[TItem client.Object, TList client.ObjectList] struct { // contains filtered or unexported fields }
Client can be used to perform various IO operations against resources on a k8s cluster
func ClientFor ¶
func ClientFor[TItem client.Object, TList client.ObjectList](ctx context.Context, cluster *Cluster, cache bool) *Client[TItem, TList]
ClientFor returns a Client that can be used to perform various IO operations against resources on a k8s cluster
If cache is true, the client will use the cache to store and retrieve resources. This is more efficient and should be used by default.
However, as there can be a delay before the latest resource state is available in the cache, some clients may need to disable it in order to retrieve the latest resource state from the cluster.
type Cluster ¶
type Cluster struct {
// contains filtered or unexported fields
}
Cluster represents a k8s cluster. A kapi.Cluster is used to - configure one or more ReconcilerFuncs that are executed when specified k8s cluster resource-change events occur - access a `client` that can be used to perform resource level CRUD operations against a k8s cluster
func NewCluster ¶
func NewCluster(ctx context.Context, cfg ClusterConfig) (*Cluster, error)
NewCluster returns a new kapi.Cluster based on the passed kapi.ClusterConfig
Multiple kapi.Clusters can be created to manage different configurations. However, it is preferable and typical to re-use the same kapi.Cluster where possible as this improves cache efficiency.
For example, where possible, create one kapi.Cluster and add two Reconcilers as opposed to creating two kapi.Clusters with one Reconciler attached to each
type ClusterConfig ¶
type ClusterConfig struct { // TLS defines the directory in which the TLS certificates to use when serving any configured hooks are stored TLS string // DisableCaching disables caching of cluster information locally. // // This canis typically used in scenarios where resources are modified outside of the kapi.Cluster scope or where // resource modifications need to be reflected immediately DisableCaching bool // LeaderElectionConfig defines the configuration for leader election. By default it is disabled. // Enabling leader election allows multiple replicas of a kapi based controller or operator to be deployed to support high availabity LeaderElection LeaderElectionConfig // Namespaces defines the namespaces for which to invoke configured Reconcilers // An empty slice results in applicable Reconcilers being invoked for all namespaces Namespaces []string // CRDs defines any CRDs that the Cluster must recognise CRDs []CRDs }
ClusterConfig defines information about how to interact with a specific k8s cluster
type CustomResource ¶
type CustomResource[TSpec any, TStatus any, TScale any] struct { metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata,omitempty"` Spec TSpec `json:"spec,omitempty"` Status TStatus `json:"status,omitempty"` Scale TScale `json:"scale,omitempty"` }
CustomResource defines a template for a struct that represents a K8s CustomResource with the conventional fields of Spec, Status and Scale. The typical use-case is to embed it in a descriptively named struct that represents the CR itself.
For example, the below defines an CR named ExampleResource that only exposes a Spec field.
ExampleResource struct { kapi.CustomResource[ExampleResourceSpec, kapi.FieldUndefined, kapi.FieldUndefined] } ExampleResourceSpec struct { ExampleData string `json:"exampleData"` }
The conventional CR fields are defined as follows:
- 'Spec' defines the main properties of the resource; its desired state.
- 'Status', typically configured as a subresource in the CustomResourceDefinition, defines the current state.
- 'Scale', typically configured as a subresource in the CustomResourceDefinition, defines the scaling properties of the resource.
Where any of the above fields are not required, they should be set to the type kapi.FieldUndefined
func (*CustomResource[TSpec, TStatus, TScale]) DeepCopyObject ¶
func (e *CustomResource[TSpec, TStatus, TScale]) DeepCopyObject() runtime.Object
type CustomResourceList ¶
type CustomResourceList[T client.Object] struct { metav1.TypeMeta `json:",inline"` metav1.ListMeta `json:"metadata,omitempty"` Items []T `json:"items"` }
CustomResourceList defines a template for the list representation of zero or more CustomResource[T, T, T] items
func (*CustomResourceList[T]) DeepCopyObject ¶
type FieldUndefined ¶
type FieldUndefined *struct{}
FieldUndefined is used to indicate that a CustomResource does not define a particular conventional field
type Hook ¶
type Hook[T client.Object] struct { DefaulterFunc func(ctx context.Context, resource T) error ValidateCreateFunc func(ctx context.Context, resource T) (warnings []string, err error) ValidateUpdateFunc func(ctx context.Context, oldResource, newResource T) (warnings []string, err error) ValidateDeleteFunc func(ctx context.Context, resource T) (warnings []string, err error) }
Hook defines admission control functionality for validating and applying default values to resources before CRUD operations occur.
A hook is typically used for enforcing business rules or setting default values by implementing one or more of the following functions: - ValidateCreateFunc: Validates resources before creation - ValidateUpdateFunc: Validates resources before updates - ValidateDeleteFunc: Validates resources before deletion - DefaulterFunc: Sets default values for new resources Any of these functions can be omitted (left as nil) if the desired behavior is not required.
func (*Hook[T]) ValidateCreate ¶
func (*Hook[T]) ValidateDelete ¶
type KindType ¶
KindType is an interface that is implemented by any type that is based on kapi.CustomResource or kapi.CustomResourceLlist
type LeaderElectionConfig ¶ added in v1.1.0
type LeaderElectionConfig struct { // Enabled enables leader election for kpai based controllers or operators running multiple replicas to support high availabity Enabled bool // LockResource defines the name of the resource that will be used as the lock resource in leader elections. // This should be set to a unique value for the consuming application to avoid conflicts with other application's leader elections LockResource string // Namespace defines the namespace in which the leader election resource will be created. // This can be unset if the cluster.Config namespace is set to one or more namespaces, which will result in the first namespace in the list being used Namespace string }
LeaderElectionConfig defines the configuration for the leader elections of high availablity deployments
type ListUndefined ¶
type ListUndefined struct { CustomResourceList[*CustomResource[FieldUndefined, FieldUndefined, FieldUndefined]] }
ListUndefined is used to indicate that a CustomResource does not have a list form, or the form is not relevant to the current use-case
type ObservabilityConfig ¶
type ObservabilityConfig struct { // Context specifies the context to be used for logs generated by background operations or internal ctrl-runtime activity BackgroundContext context.Context // LogFunc should be set to a context aware, levelled and structured logger implementation. // - level defines the verbosity, where a higher value indicates and increased level of detail. // - message defines a summary of the log event // - attributes is a variadic list of variables that are parsed as kv pairs and used as structured log attributes LogFunc func(ctx context.Context, level int, msg string, attributes ...any) // MetricTimerFunc should be set to a suitable, context aware metric or trace func able to write count and duration metrics with dimensions/attributes MetricTimerFunc func(ctx context.Context, metric string) func(attributes ...string) // NewCorrelationCtx is defines a func that generates a new correlation context for the purposes of logs or tracing. // This is called at the start of each reconciliation allowing activities associated with it to be correlated NewCorrelationCtx func(ctx context.Context) context.Context }
ObservabilityConfig defines the mechanisms by which logs, metrics and traces are processed
type ReconcileEventType ¶
type ReconcileEventType int
ReconcileEventType defines the limited, summarised set of event types that can affect a k8s resource as presented to an invoked ReconcilerFunc
func (ReconcileEventType) String ¶
func (r ReconcileEventType) String() string
type ReconcilerFilterFunc ¶
type ReconcilerFilterFunc func(ResourceEventType, client.Object) bool
An ReconcilerFilterFunc is used to reduce the scope of a reconciler. ReconcilerFilterFuncs are invoked before ReconcilerFuncs and are
passed the object being modified and the type of the modification. The associated ReconcilerFunc is only invoked if the eventFilterFunc returns true.
type ReconcilerFunc ¶
type ReconcilerFunc[T client.Object] func(ctx context.Context, eventType ReconcileEventType, resource T) error
ReconcilerFunc is a generic func that can be added to a kapi.Cluster.
It will be invoked with the relevant resource data whenever a resource is created, updated or deleted. When executing as a result of a delete event, `resource T` will be set to its zero-value
type ResourceEventType ¶
type ResourceEventType int
ResourceEventType defines the types of events that can affect a k8s resource
This is used to limit the invocation of configured ReconcilerFuncs
func (ResourceEventType) String ¶
func (r ResourceEventType) String() string
type Subresource ¶ added in v1.1.1
type Subresource string
Subresource represents a section of a resource that can be modified independently of the resource as a whole
const ( SubresourceStatus Subresource = "status" SubresourceScale Subresource = "scale" )