resourcekeeper

package
v1.12.0-alpha.2 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: Apache-2.0 Imports: 30 Imported by: 0

Documentation

Overview

Package resourcekeeper applies an owner's resources, records them in ResourceTrackers and garbage-collects the ones it no longer renders.

It knows no owner kind. An owner implements resourcetracker.Tracked (pkg/appkeeper for Applications, and a Component in the spoke agent), supplies its apply-time Policies, and fills the Options hooks for whatever the library cannot do for it: revision collection, dependency order, the requester's identity, metrics.

Together with pkg/resourcetracker, pkg/utils/apply and pkg/kubeutil it is meant to move to its own module, so it may depend only on those and on the API packages (apis/core.oam.dev, pkg/oam, pkg/utils/errors). Three tests in boundary_test.go enforce that: the imports, the weight of the API packages, and that no identifier here names the Application.

Migration: owner labels

Objects written before owner.oam.dev/* labels carry app.oam.dev/* only. Five pieces carry that upgrade, each marked "Migration: owner labels" where it sits. All five can go once the oldest supported release writes owner labels:

  • resourceKeeper.labelTrackers, which labels an owner's trackers when it loads them;
  • the Stamp in resourceKeeper.StateKeep, so an idle owner's resources are relabelled;
  • applicationTracked.ControlledBy's fall back to app.oam.dev/* (pkg/appkeeper);
  • applicationTracked.Release stripping app.oam.dev/* as well;
  • apply.Config.LegacyControlledBy, so other kinds see an unlabelled resource as its Application's rather than as unowned.

Index

Constants

This section is empty.

Variables

View Source
var (
	// AllowCrossNamespaceResource indicates whether application can apply resources into other namespaces
	AllowCrossNamespaceResource = true
	// AllowResourceTypes if not empty, application can only apply resources with specified types
	AllowResourceTypes = ""
)
View Source
var (
	// MarkWithProbability optimize ResourceTracker gc for legacy resource by reducing the frequency of outdated rt check
	MarkWithProbability = 0.1
)
View Source
var MaxDispatchConcurrent = 10

MaxDispatchConcurrent is the max dispatch concurrent number

Functions

func DeleteManagedResource

func DeleteManagedResource(ctx context.Context, cli client.Client, mr v1beta1.ManagedResource, obj *unstructured.Unstructured, owner resourcetracker.Tracked, garbageCollectPolicy *v1alpha1.GarbageCollectPolicySpec) error

DeleteManagedResource lets owner go of a resource it manages: if others share it, owner leaves the sharer list and control passes to the next sharer; if it is to be kept (skip-GC, an orphaning owner, or the garbage-collect policy), owner's marks are removed; otherwise it is deleted. garbageCollectPolicy may be nil.

func IsResourceManaged

func IsResourceManaged(manifest *unstructured.Unstructured, owner resourcetracker.Tracked) bool

IsResourceManaged reports whether a live resource is still the owner's: it controls it, it is a sharer of it, or the object has no resource version (an API response rather than a stored object, e.g. from vela-prism) and the owner is not orphaning its resources.

func UpdateSharedManagedResourceOwner added in v1.7.0

func UpdateSharedManagedResourceOwner(ctx context.Context, cli client.Client, manifest *unstructured.Unstructured, sharedBy string) error

UpdateSharedManagedResourceOwner hands a shared resource to the first remaining sharer: it records the sharer list and labels the resource as that sharer's (see resourcetracker.LabelsForKey), dropping any owner.oam.dev/* labels of the previous owner.

func WithFailedRun

func WithFailedRun(ctx context.Context, failed bool) context.Context

WithFailedRun records whether the owner's current run failed (for an Application, its workflow), so a garbage-collect policy with continueOnFailure still collects.

Types

type CollectState

type CollectState struct {
	// Trackers are the owner's trackers as loaded, after this pass marked inactive ones.
	Trackers resourcetracker.Trackers
	// InUseComponents returns the components with resources in live or unfinished trackers.
	// It walks the whole resource cache, so it is computed only if the hook asks for it.
	InUseComponents func() map[string]bool
	// RevisionLimit is the revision limit set by RevisionLimitGCOption.
	RevisionLimit int
	// DisableRevisionGC and DisableComponentRevisionGC reflect DisableRevisionGCOption
	// and DisableGCComponentRevisionOption.
	DisableRevisionGC, DisableComponentRevisionGC bool
}

CollectState is what the keeper knows at the end of a garbage-collection pass, for Options.Collect.

type CreatorOption added in v1.4.0

type CreatorOption struct {
	Creator string
}

CreatorOption set the creator of the resource

func (CreatorOption) ApplyToDispatchConfig added in v1.4.0

func (option CreatorOption) ApplyToDispatchConfig(cfg *dispatchConfig)

ApplyToDispatchConfig apply change to dispatch config

type DeleteOption

type DeleteOption interface {
	ApplyToDeleteConfig(*deleteConfig)
}

DeleteOption option for delete

type DependencyGCOption added in v1.4.0

type DependencyGCOption struct{}

DependencyGCOption recycle the resource in the order of reverse dependency

func (DependencyGCOption) ApplyToGCConfig added in v1.4.0

func (option DependencyGCOption) ApplyToGCConfig(cfg *gcConfig)

ApplyToGCConfig apply change to gc config

type DisableGCComponentRevisionOption

type DisableGCComponentRevisionOption struct{}

DisableGCComponentRevisionOption disable the component revision gc process this option should be switched on when application workflow is suspending/terminating

func (DisableGCComponentRevisionOption) ApplyToGCConfig

func (option DisableGCComponentRevisionOption) ApplyToGCConfig(cfg *gcConfig)

ApplyToGCConfig apply change to gc config

type DisableLegacyGCOption

type DisableLegacyGCOption struct{}

DisableLegacyGCOption disable garbage collect legacy resourcetrackers

func (DisableLegacyGCOption) ApplyToGCConfig

func (option DisableLegacyGCOption) ApplyToGCConfig(cfg *gcConfig)

ApplyToGCConfig apply change to gc config

type DisableMarkStageGCOption

type DisableMarkStageGCOption struct{}

DisableMarkStageGCOption disable the mark stage in gc process (no rt will be marked to be deleted) this option should be switched on when application workflow is suspending/terminating since workflow is not finished so outdated versions should be kept

func (DisableMarkStageGCOption) ApplyToGCConfig

func (option DisableMarkStageGCOption) ApplyToGCConfig(cfg *gcConfig)

ApplyToGCConfig apply change to gc config

type DisableRevisionGCOption

type DisableRevisionGCOption struct{}

DisableRevisionGCOption disables the owner kind's revision garbage collection (see Options.Collect)

func (DisableRevisionGCOption) ApplyToGCConfig

func (option DisableRevisionGCOption) ApplyToGCConfig(cfg *gcConfig)

ApplyToGCConfig apply change to gc config

type DispatchOption

type DispatchOption interface {
	ApplyToDispatchConfig(*dispatchConfig)
}

DispatchOption option for dispatch

type GCOption

type GCOption interface {
	ApplyToGCConfig(*gcConfig)
}

GCOption option for gc

type GarbageCollectStrategyOption

type GarbageCollectStrategyOption v1alpha1.GarbageCollectStrategy

GarbageCollectStrategyOption apply garbage collect strategy to resourcetracker recording

func (GarbageCollectStrategyOption) ApplyToDeleteConfig

func (option GarbageCollectStrategyOption) ApplyToDeleteConfig(cfg *deleteConfig)

ApplyToDeleteConfig apply change to delete config

func (GarbageCollectStrategyOption) ApplyToDispatchConfig

func (option GarbageCollectStrategyOption) ApplyToDispatchConfig(cfg *dispatchConfig)

ApplyToDispatchConfig apply change to dispatch config

type MetaOnlyOption

type MetaOnlyOption struct{}

MetaOnlyOption record only meta part in resourcetracker, which disables the configuration-drift-prevention

func (MetaOnlyOption) ApplyToDispatchConfig

func (option MetaOnlyOption) ApplyToDispatchConfig(cfg *dispatchConfig)

ApplyToDispatchConfig apply change to dispatch config

type MetadataRemoval added in v1.11.1

type MetadataRemoval struct {
	Labels      map[string]string
	Annotations map[string]string
}

MetadataRemoval names labels and annotations to take off resources: a key and the value it must still have, so a key someone has changed since is left alone.

func (MetadataRemoval) Empty

func (r MetadataRemoval) Empty() bool

Empty reports whether there is nothing to remove.

type NamespaceAdmissionHandler added in v1.3.0

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

NamespaceAdmissionHandler defines the handler to validate if the resource namespace is valid to be dispatch/delete

func (*NamespaceAdmissionHandler) Validate added in v1.3.0

Validate check if cross namespace is available

type Options

type Options struct {
	// ApplyOnce applies every resource once and never updates it. Default false
	// (KubeVela: ApplyOnce).
	ApplyOnce func() bool
	// PreDispatchDryRun dry-runs a dispatch before recording and applying it. Default
	// true (KubeVela: PreDispatchDryRun).
	PreDispatchDryRun func() bool
	// Requester returns ctx acting as whoever the owner acts for, so applies and deletes
	// run with their permissions. Default: ctx unchanged, the keeper's own identity
	// (KubeVela: the user recorded on the Application).
	Requester func(context.Context) context.Context
	// ObserveStage records how long a garbage-collection stage took. Default: nothing
	// (KubeVela: the reconcile stage duration histogram).
	ObserveStage func(stage string, took time.Duration)
	// LegacyGarbageCollect removes trackers from an older tracker scheme. The keeper runs it
	// during garbage collection once the owner has a current tracker, or while the owner is
	// being deleted. Default: nothing (KubeVela: pre-v1.2 Application trackers).
	LegacyGarbageCollect func(context.Context) error
	// Collect is garbage collection the owner's kind needs beyond the keeper's own, run at
	// the end of each pass. Default: nothing (KubeVela: Application and component revisions).
	Collect func(context.Context, CollectState) error
	// Dependents returns the components that depend on a component, so dependency-ordered
	// garbage collection deletes dependents first. Default: none, so dependency order is
	// plain order (KubeVela: from the Application's dependsOn, inputs and outputs).
	Dependents func(component string) []string
}

Options holds the behaviour a keeper's creator decides. Switches are functions, evaluated on each use, so a creator can back them with feature gates that change at runtime. A nil field takes the default documented on it, which matches KubeVela's default. pkg/appkeeper fills these from KubeVela's gates, auth and metrics.

type PassiveGCOption

type PassiveGCOption struct{}

PassiveGCOption disable the active gc for outdated versions. Old versioned resourcetracker will not be recycled except all of their managed resources have already been deleted or controlled by later resourcetrackers.

func (PassiveGCOption) ApplyToGCConfig

func (option PassiveGCOption) ApplyToGCConfig(cfg *gcConfig)

ApplyToGCConfig apply change to gc config

type Policies

type Policies struct {
	ApplyOnce      *v1alpha1.ApplyOncePolicySpec
	GarbageCollect *v1alpha1.GarbageCollectPolicySpec
	SharedResource *v1alpha1.SharedResourcePolicySpec
	TakeOver       *v1alpha1.TakeOverPolicySpec
	ReadOnly       *v1alpha1.ReadOnlyPolicySpec
	ResourceUpdate *v1alpha1.ResourceUpdatePolicySpec
}

Policies are the apply-time policies a keeper honours. An Application carries them in spec.policies; other owners pass them in directly (Component.spec.apply).

type ResourceAdmissionHandler added in v1.3.0

type ResourceAdmissionHandler interface {
	Validate(ctx context.Context, manifests []*unstructured.Unstructured) error
}

ResourceAdmissionHandler defines the handler to validate the admission of resource operation

type ResourceKeeper

type ResourceKeeper interface {
	Dispatch(context.Context, []*unstructured.Unstructured, []apply.ApplyOption, ...DispatchOption) error
	Delete(context.Context, []*unstructured.Unstructured, ...DeleteOption) error
	GarbageCollect(context.Context, ...GCOption) (bool, []v1beta1.ManagedResource, error)
	StateKeep(context.Context) error
	ContainsResources([]*unstructured.Unstructured) bool
	ManifestMetadataChanged([]*unstructured.Unstructured) bool
	RefreshRecordedMetadata(ctx context.Context, labels, annotations map[string]string, removed MetadataRemoval) (bool, error)
	PolicyContextHash() (string, bool)
	SetPolicyContextHash(ctx context.Context, hash string) error
	ComponentResources(component string) []v1beta1.ManagedResource
	// PruneComponentResources deletes what a component no longer renders. See
	// the implementation for why this is not part of Dispatch.
	PruneComponentResources(context.Context, string, []*unstructured.Unstructured) ([]v1beta1.ManagedResource, error)

	// GetAppliedResources returns the current applied resources from the ResourceTracker.
	GetAppliedResources() []common.ClusterObjectReference
}

ResourceKeeper handler for dispatching and deleting resources

func New

func New(ctx context.Context, cli client.Client, tracked resourcetracker.Tracked, policies Policies, opts Options) (_ ResourceKeeper, err error)

New creates a keeper for an owner, given its Tracked (see resourcetracker.Tracked). What differs by kind comes from the Tracked and from Options (see pkg/appkeeper for Applications).

type ResourceTypeAdmissionHandler added in v1.3.0

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

ResourceTypeAdmissionHandler defines the handler to validate if the resource type is valid to be dispatch/delete

func (*ResourceTypeAdmissionHandler) Validate added in v1.3.0

Validate check if resource type is valid

type RevisionLimitGCOption

type RevisionLimitGCOption int

RevisionLimitGCOption is the maximum number of revisions that will be maintained

func (RevisionLimitGCOption) ApplyToGCConfig

func (option RevisionLimitGCOption) ApplyToGCConfig(cfg *gcConfig)

ApplyToGCConfig apply change to gc config

type SkipGCOption added in v1.5.0

type SkipGCOption struct{}

SkipGCOption marks the recorded resource to skip gc

func (SkipGCOption) ApplyToDeleteConfig added in v1.5.0

func (option SkipGCOption) ApplyToDeleteConfig(cfg *deleteConfig)

ApplyToDeleteConfig apply change to delete config

func (SkipGCOption) ApplyToDispatchConfig added in v1.5.0

func (option SkipGCOption) ApplyToDispatchConfig(cfg *dispatchConfig)

ApplyToDispatchConfig apply change to dispatch config

type UseRootOption

type UseRootOption struct{}

UseRootOption let the recording and management of target resource belongs to the RootRT instead of VersionedRT. This will let the resource be alive as long as the application is still alive.

func (UseRootOption) ApplyToDeleteConfig

func (option UseRootOption) ApplyToDeleteConfig(cfg *deleteConfig)

ApplyToDeleteConfig apply change to delete config

func (UseRootOption) ApplyToDispatchConfig

func (option UseRootOption) ApplyToDispatchConfig(cfg *dispatchConfig)

ApplyToDispatchConfig apply change to dispatch config

Jump to

Keyboard shortcuts

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