pkg

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2026 License: Apache-2.0 Imports: 19 Imported by: 34

Documentation

Index

Constants

View Source
const (
	EnvApplicationName      = "APPLICATION_NAME"
	EnvApplicationNamespace = "APPLICATION_NAMESPACE"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ApplicationDependencyContainer added in v0.7.0

type ApplicationDependencyContainer interface {
	GetHTTPClient(options ...HTTPOption) HTTPClient

	GetRegistryClient(repo string, options ...RegistryOption) (RegistryClient, error)
	MustGetRegistryClient(repo string, options ...RegistryOption) RegistryClient

	GetClock() clockwork.Clock
}

type ApplicationHookConfig added in v0.8.0

type ApplicationHookConfig struct {
	Metadata HookMetadata
	Schedule []ScheduleConfig

	Kubernetes []ApplicationKubernetesConfig

	// OnStartup runs hook on application startup
	// Attention! During the startup you don't have snapshots available
	// use native KubeClient to fetch resources
	OnStartup         *OrderedConfig
	OnBeforeHelm      *OrderedConfig
	OnAfterHelm       *OrderedConfig
	OnAfterDeleteHelm *OrderedConfig

	AllowFailure bool
	Queue        string

	Settings *HookConfigSettings
}

ApplicationHookConfig defines the configuration for an application hook.

func (*ApplicationHookConfig) AsApplicationHookConfig added in v0.8.0

func (cfg *ApplicationHookConfig) AsApplicationHookConfig() (*ApplicationHookConfig, bool)

AsApplicationHookConfig implements HookConfigLike.

func (*ApplicationHookConfig) AsHookConfig added in v0.8.0

func (cfg *ApplicationHookConfig) AsHookConfig() (*HookConfig, bool)

AsHookConfig implements HookConfigLike.

func (*ApplicationHookConfig) GetMetadata added in v0.8.0

func (cfg *ApplicationHookConfig) GetMetadata() HookMetadata

GetMetadata implements HookConfigLike.

func (*ApplicationHookConfig) GetQueue added in v0.8.0

func (cfg *ApplicationHookConfig) GetQueue() string

GetQueue implements HookConfigLike.

func (*ApplicationHookConfig) Validate added in v0.8.0

func (cfg *ApplicationHookConfig) Validate() error

Validate checks the ApplicationHookConfig for errors.

type ApplicationHookInput added in v0.7.0

type ApplicationHookInput struct {
	Snapshots Snapshots

	Instance Instance

	Values           PatchableValuesCollector
	PatchCollector   NamespacedPatchCollector
	MetricsCollector MetricsCollector

	DC ApplicationDependencyContainer

	Logger Logger
}

ApplicationHookInput provides context and utilities for application hook execution.

type ApplicationKubernetesConfig added in v0.8.0

type ApplicationKubernetesConfig struct {
	// Name is a key in snapshots map.
	Name string
	// APIVersion of objects. "v1" is used if not set.
	APIVersion string
	// Kind of objects.
	Kind string
	// NameSelector used to subscribe on object by its name.
	NameSelector *NameSelector
	// LabelSelector used to subscribe on objects by matching their labels.
	LabelSelector *metav1.LabelSelector
	// FieldSelector used to subscribe on objects by matching specific fields (the list of fields is narrow, see shell-operator documentation).
	FieldSelector *FieldSelector
	// ExecuteHookOnEvents is true by default. Set to false if only snapshot update is needed.
	ExecuteHookOnEvents *bool
	// ExecuteHookOnSynchronization is true by default. Set to false if only snapshot update is needed.
	ExecuteHookOnSynchronization *bool
	// WaitForSynchronization is true by default. Set to false if beforeHelm is not required this snapshot on start.
	WaitForSynchronization *bool
	// JqFilter filters results from kubernetes objects.
	JqFilter string
	// AllowFailure allows the hook to fail without stopping execution.
	AllowFailure *bool

	ResynchronizationPeriod string
}

ApplicationKubernetesConfig defines a subscription to Kubernetes objects for application hooks. Application hooks automatically work in the application's namespace, so NamespaceSelector is not allowed.

func (*ApplicationKubernetesConfig) Validate added in v0.8.0

func (cfg *ApplicationKubernetesConfig) Validate() error

Validate checks the ApplicationKubernetesConfig for errors.

type Config added in v0.8.0

Config is a type constraint for hook configuration types (used in generics).

type DependencyContainer

type DependencyContainer interface {
	GetHTTPClient(options ...HTTPOption) HTTPClient

	GetK8sClient(options ...KubernetesOption) (KubernetesClient, error)
	MustGetK8sClient(options ...KubernetesOption) KubernetesClient
	GetClientConfig() (*rest.Config, error)

	GetRegistryClient(repo string, options ...RegistryOption) (RegistryClient, error)
	MustGetRegistryClient(repo string, options ...RegistryOption) RegistryClient

	GetClock() clockwork.Clock
}

Container with external dependencies Avoid using dependencies, if you can, because of it cost

type Error

type Error struct {
	Message string
	Code    int
}

func (*Error) Error

func (e *Error) Error() string

type FieldSelector

type FieldSelector struct {
	MatchExpressions []FieldSelectorRequirement
}

FieldSelector filters objects by field values.

type FieldSelectorRequirement

type FieldSelectorRequirement struct {
	Field    string
	Operator string
	Value    string
}

FieldSelectorRequirement defines a single field selector condition.

type HTTPClient

type HTTPClient interface {
	Do(req *http.Request) (*http.Response, error)
}

type HTTPOption

type HTTPOption interface {
	Apply(optsApplier HTTPOptionApplier)
}

type HTTPOptionApplier

type HTTPOptionApplier interface {
	WithTimeout(t time.Duration)
	WithInsecureSkipVerify()
	WithAdditionalCACerts(certs [][]byte)
	WithTLSServerName(name string)
}

type Hook

type Hook[C Config, T Input] struct {
	Config   C
	HookFunc HookFunc[T]
}

Hook represents a generic hook with configuration and execution function.

type HookConfig

type HookConfig struct {
	Metadata HookMetadata
	Schedule []ScheduleConfig

	Kubernetes []KubernetesConfig

	// OnStartup runs hook on module/global startup
	// Attention! During the startup you don't have snapshots available
	// use native KubeClient to fetch resources
	OnStartup         *OrderedConfig
	OnBeforeHelm      *OrderedConfig
	OnAfterHelm       *OrderedConfig
	OnAfterDeleteHelm *OrderedConfig

	AllowFailure bool
	Queue        string

	Settings *HookConfigSettings
}

HookConfig defines the configuration for a module hook.

func (*HookConfig) AsApplicationHookConfig added in v0.8.0

func (cfg *HookConfig) AsApplicationHookConfig() (*ApplicationHookConfig, bool)

AsApplicationHookConfig implements HookConfigLike.

func (*HookConfig) AsHookConfig added in v0.8.0

func (cfg *HookConfig) AsHookConfig() (*HookConfig, bool)

AsHookConfig implements HookConfigLike.

func (*HookConfig) GetMetadata added in v0.8.0

func (cfg *HookConfig) GetMetadata() HookMetadata

GetMetadata implements HookConfigLike.

func (*HookConfig) GetQueue added in v0.8.0

func (cfg *HookConfig) GetQueue() string

GetQueue implements HookConfigLike.

func (*HookConfig) Validate

func (cfg *HookConfig) Validate() error

Validate checks the HookConfig for errors.

type HookConfigInterface added in v0.8.0

type HookConfigInterface interface {
	GetMetadata() HookMetadata
	GetQueue() string
	// AsHookConfig returns the config as *HookConfig if it is a module hook config.
	AsHookConfig() (*HookConfig, bool)
	// AsApplicationHookConfig returns the config as *ApplicationHookConfig if it is an application hook config.
	AsApplicationHookConfig() (*ApplicationHookConfig, bool)
}

HookConfigInterface is implemented by *HookConfig and *ApplicationHookConfig. It provides type-safe access to common config fields and explicit conversion to concrete types, avoiding unsafe type assertions on any.

type HookConfigSettings

type HookConfigSettings struct {
	ExecutionMinInterval time.Duration
	ExecutionBurst       int
}

HookConfigSettings contains rate limiting settings for hook execution.

type HookFunc added in v0.7.0

type HookFunc[T Input] func(ctx context.Context, input T) error

HookFunc is the function signature for hook execution logic.

type HookInput

type HookInput struct {
	Snapshots Snapshots

	Values           PatchableValuesCollector
	ConfigValues     PatchableValuesCollector
	PatchCollector   PatchCollector
	MetricsCollector MetricsCollector

	DC DependencyContainer

	Logger Logger
}

HookInput provides context and utilities for module hook execution.

type HookMetadata

type HookMetadata struct {
	// Name is the hook's unique identifier
	Name string
	// Path is the file path where the hook is defined
	Path string
}

HookMetadata contains identifying information for a hook.

type Input added in v0.7.0

type Input interface {
	*HookInput | *ApplicationHookInput
}

Input is a type constraint for hook input types.

type Instance added in v0.7.0

type Instance interface {
	// Name returns application instance name
	Name() string
	// Namespace returns application instance namespace
	Namespace() string
}

Instance provides access to application instance metadata.

type KubernetesClient

type KubernetesClient interface {
	client.Client
	Dynamic() dynamic.Interface
}

type KubernetesConfig

type KubernetesConfig struct {
	// Name is a key in snapshots map.
	Name string
	// APIVersion of objects. "v1" is used if not set.
	APIVersion string
	// Kind of objects.
	Kind string
	// NameSelector used to subscribe on object by its name.
	NameSelector *NameSelector
	// NamespaceSelector used to subscribe on objects in namespaces.
	NamespaceSelector *NamespaceSelector
	// LabelSelector used to subscribe on objects by matching their labels.
	LabelSelector *metav1.LabelSelector
	// FieldSelector used to subscribe on objects by matching specific fields (the list of fields is narrow, see shell-operator documentation).
	FieldSelector *FieldSelector
	// ExecuteHookOnEvents is true by default. Set to false if only snapshot update is needed.
	ExecuteHookOnEvents *bool
	// ExecuteHookOnSynchronization is true by default. Set to false if only snapshot update is needed.
	ExecuteHookOnSynchronization *bool
	// WaitForSynchronization is true by default. Set to false if beforeHelm is not required this snapshot on start.
	WaitForSynchronization *bool
	// JqFilter filters results from kubernetes objects.
	JqFilter string
	// AllowFailure allows the hook to fail without stopping execution.
	AllowFailure *bool

	ResynchronizationPeriod string
}

KubernetesConfig defines a subscription to Kubernetes objects for module hooks.

func (*KubernetesConfig) Validate

func (cfg *KubernetesConfig) Validate() error

Validate checks the KubernetesConfig for errors.

type KubernetesOption

type KubernetesOption interface {
	Apply(optsApplier KubernetesOptionApplier)
}

type KubernetesOptionApplier

type KubernetesOptionApplier interface {
	WithSchemeBuilder(builder runtime.SchemeBuilder)
}

type Logger

type Logger interface {
	Debug(msg string, args ...any)
	DebugContext(ctx context.Context, msg string, args ...any)

	Error(msg string, args ...any)
	ErrorContext(ctx context.Context, msg string, args ...any)

	Fatal(msg string, args ...any)

	Info(msg string, args ...any)
	InfoContext(ctx context.Context, msg string, args ...any)

	Log(ctx context.Context, level slog.Level, msg string, args ...any)
	LogAttrs(ctx context.Context, level slog.Level, msg string, attrs ...slog.Attr)

	Warn(msg string, args ...any)
	WarnContext(ctx context.Context, msg string, args ...any)

	Enabled(ctx context.Context, level slog.Level) bool
	With(args ...any) *log.Logger
	WithGroup(name string) *log.Logger
	Named(name string) *log.Logger
	SetLevel(level log.Level)
	SetOutput(w io.Writer)
	GetLevel() log.Level
	Handler() slog.Handler
}

type MetricCollectorOption

type MetricCollectorOption interface {
	Apply(op MetricCollectorOptionApplier)
}

type MetricCollectorOptionApplier

type MetricCollectorOptionApplier interface {
	WithGroup(group string)
}

type MetricsCollector

type MetricsCollector interface {
	// Inc increments the specified Counter metric
	Inc(name string, labels map[string]string, opts ...MetricCollectorOption)
	// Add adds custom value for the specified Counter metric
	Add(name string, value float64, labels map[string]string, opts ...MetricCollectorOption)
	// Set specifies the custom value for the Gauge metric
	Set(name string, value float64, labels map[string]string, opts ...MetricCollectorOption)
	// Expire marks metric's group as expired
	Expire(group string)
}

type NameSelector

type NameSelector struct {
	MatchNames []string
}

NameSelector filters objects by name.

type NamespaceSelector

type NamespaceSelector struct {
	NameSelector  *NameSelector
	LabelSelector *metav1.LabelSelector
}

NamespaceSelector filters namespaces for object subscription.

type NamespacedPatchCollector added in v0.7.0

type NamespacedPatchCollector interface {
	// Create creates the object in the cluster.
	Create(object runtime.Object)
	// CreateIfNotExists creates the object only if it does not already exist.
	CreateIfNotExists(object runtime.Object)
	// CreateOrUpdate creates the object if it does not exist, or updates it if it does.
	CreateOrUpdate(object runtime.Object)

	// Delete removes the object using foreground cascading deletion.
	// The API server adds the "foregroundDeletion" finalizer and sets deletionTimestamp.
	// The object remains until the garbage collector deletes all dependents
	// with ownerReference.blockOwnerDeletion=true.
	Delete(apiVersion, kind, name string)
	// DeleteInBackground removes the object immediately while the garbage collector
	// deletes dependents in the background.
	DeleteInBackground(apiVersion, kind, name string)
	// DeleteNonCascading removes the object without deleting its dependents (orphans them).
	DeleteNonCascading(apiVersion, kind, name string)

	// PatchWithJSON applies a RFC6902 JSON Patch to the object.
	// This format requires explicit operations (add, remove, replace, etc.) with paths and values.
	// See https://tools.ietf.org/html/rfc6902 for details.
	PatchWithJSON(jsonPatch any, apiVersion, kind, name string, opts ...PatchCollectorOption)
	// PatchWithMerge applies a RFC7396 JSON Merge Patch to the object.
	// This format merges the patch directly into the object, replacing values at matching paths.
	// See https://tools.ietf.org/html/rfc7396 for details.
	PatchWithMerge(mergePatch any, apiVersion, kind, name string, opts ...PatchCollectorOption)
	// PatchWithJQ mutates the object using a jq filter expression.
	PatchWithJQ(jqfilter, apiVersion, kind, name string, opts ...PatchCollectorOption)

	// Operations returns all collected patch operations.
	Operations() []PatchCollectorOperation
}

type OrderedConfig

type OrderedConfig struct {
	Order uint
}

OrderedConfig specifies execution order for lifecycle hooks.

type OutputMetricsCollector

type OutputMetricsCollector interface {
	MetricsCollector
	Outputer
}

type Outputer

type Outputer interface {
	WriteOutput(writer io.Writer) error
}

type PatchCollector

type PatchCollector interface {
	Outputer

	// object must be Unstructured, map[string]any or runtime.Object
	Create(object any)
	// object must be Unstructured, map[string]any or runtime.Object
	CreateIfNotExists(object any)
	// object must be Unstructured, map[string]any or runtime.Object
	CreateOrUpdate(object any)

	// The object exists in the key-value store until the garbage collector
	// deletes all the dependents whose ownerReference.blockOwnerDeletion=true
	// from the key-value store.  API sever will put the "foregroundDeletion"
	// finalizer on the object, and sets its deletionTimestamp.  This policy is
	// cascading, i.e., the dependents will be deleted with Foreground.
	Delete(apiVersion string, kind string, namespace string, name string)
	// Deletes the object from the key-value store, the garbage collector will
	// delete the dependents in the background.
	DeleteInBackground(apiVersion string, kind string, namespace string, name string)
	// Orphans the dependents.
	DeleteNonCascading(apiVersion string, kind string, namespace string, name string)

	// Deprecated: use PatchWithJSON instead
	JSONPatch(jsonPatch any, apiVersion string, kind string, namespace string, name string, opts ...PatchCollectorOption)
	// Deprecated: use PatchWithMerge instead
	MergePatch(mergePatch any, apiVersion string, kind string, namespace string, name string, opts ...PatchCollectorOption)
	// Deprecated: use PatchWithJQ instead
	JQFilter(jqfilter string, apiVersion string, kind string, namespace string, name string, opts ...PatchCollectorOption)

	// JSONPatch is a PatchType indicating the patch should be interpreted as a RFC6902 JSON Patch.
	// This patch format requires specifying operations, paths, and values explicitly.
	// See https://tools.ietf.org/html/rfc6902 for details.
	PatchWithJSON(jsonPatch any, apiVersion string, kind string, namespace string, name string, opts ...PatchCollectorOption)
	// MergePatch is a PatchType indicating the patch should be interpreted as a RFC7396 JSON Merge Patch.
	// This patch format replaces elements at the object level rather than requiring explicit operations.
	// See https://tools.ietf.org/html/rfc7396 for details.
	PatchWithMerge(mergePatch any, apiVersion string, kind string, namespace string, name string, opts ...PatchCollectorOption)
	// Mutate object with jq query
	PatchWithJQ(jqfilter string, apiVersion string, kind string, namespace string, name string, opts ...PatchCollectorOption)

	Operations() []PatchCollectorOperation
}

type PatchCollectorOperation

type PatchCollectorOperation interface {
	Description() string
}

There are 4 types of operations:

- createOperation to create or update object via Create and Update API calls. Unstructured, map[string]any or runtime.Object is required.

- deleteOperation to delete object via Delete API call

- patchOperation to modify object via Patch API call

- filterOperation to modify object via Get-filter-Update process

type PatchCollectorOption

type PatchCollectorOption interface {
	Apply(PatchCollectorOptionApplier)
}

type PatchCollectorOptionApplier

type PatchCollectorOptionApplier interface {
	WithSubresource(subresource string)
	WithIgnoreMissingObject(ignore bool)
	WithIgnoreHookError(update bool)
}

type PatchableValuesCollector

type PatchableValuesCollector interface {
	ArrayCount(path string) (int, error)
	Exists(path string) bool
	Get(path string) gjson.Result
	GetOk(path string) (gjson.Result, bool)
	GetPatches() []*utils.ValuesPatchOperation
	GetRaw(path string) any
	Remove(path string)
	Set(path string, value any)
}

type ReadableValuesCollector added in v0.6.0

type ReadableValuesCollector interface {
	ArrayCount(path string) (int, error)
	Exists(path string) bool
	Get(path string) gjson.Result
	GetOk(path string) (gjson.Result, bool)
	GetPatches() []*utils.ValuesPatchOperation
	GetRaw(path string) any
}

type RegistryClient

type RegistryClient interface {
	Image(ctx context.Context, tag string) (v1.Image, error)
	Digest(ctx context.Context, tag string) (string, error)
	ListTags(ctx context.Context) ([]string, error)
}

type RegistryOption

type RegistryOption interface {
	Apply(optsApplier RegistryOptionApplier)
}

type RegistryOptionApplier

type RegistryOptionApplier interface {
	WithCA(ca string)
	WithInsecureSchema(insecure bool)
	WithAuth(dockerCfg string)
	WithUserAgent(ua string)
	WithTimeout(timeout time.Duration)
}

type ScheduleConfig

type ScheduleConfig struct {
	Name string
	// Crontab is a schedule config in crontab format. (5 or 6 fields)
	Crontab string
}

ScheduleConfig defines a cron-based schedule for hook execution.

func (*ScheduleConfig) Validate

func (cfg *ScheduleConfig) Validate() error

Validate checks the ScheduleConfig for errors.

type Snapshot

type Snapshot interface {
	UnmarshalTo(v any) error
	// returns pure form of object
	// can contains special symbols
	// to receive string values - use UnmarshalTo method
	String() string
}

type Snapshots

type Snapshots interface {
	Get(key string) []Snapshot
}

Jump to

Keyboard shortcuts

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