filters

package
v0.6.2 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	LabelFilterName    = "label"
	AgeFilterName      = "age"
	KorLabelFilterName = "korlabel"
)

Variables

This section is empty.

Functions

func AgeFilter

func AgeFilter(object runtime.Object, opts *Options) bool

AgeFilter is a filter that filters out resources by age

func HasExcludedLabel

func HasExcludedLabel(resourcelabels map[string]string, excludeSelector []string) (bool, error)

HasExcludedLabel parses the excluded selector into a label selector object

func HasIncludedAge

func HasIncludedAge(creationTime metav1.Time, filterOpts *Options) (bool, error)

HasIncludedAge checks if a resource has an age that matches the included criteria specified by the filter options A resource is considered to have an included age if its age (measured from the last modified time) is within the range specified by older-than and newer-than flags. If older-than or newer-than is zero, no age limit is applied. If both flags are set, an error is returned.

func KorLabelFilter

func KorLabelFilter(object runtime.Object, opts *Options) bool

KorLabelFilter is a filter that filters out resources that are ["kor/used"] != "true"

func LabelFilter

func LabelFilter(object runtime.Object, opts *Options) bool

LabelFilter is a filter that filters out resources by label

Types

type FilterFunc

type FilterFunc func(object runtime.Object, opts *Options) bool

FilterFunc is a filter that is a function If the resource is legal, return true example: deployment.Spec.Replicas > 0; return true meta.GetLabels()["kor/used"] == "true"; return true

type Framework

type Framework interface {
	// Run runs all the filters in the framework
	// If the resource is legal, return true
	Run(opts *Options, disable ...string) (bool, error)
	// AddFilter adds a filter to the framework
	AddFilter(name string, f FilterFunc) Framework
	// SetRegistry sets the registry of the framework
	SetRegistry(r Registry) Framework
	// SetObject sets the object of the framework
	SetObject(object runtime.Object) Framework
	// RunFilter runs a filter in the framework
	// name not found, return true
	RunFilter(name string, opts *Options) (bool, error)
}

Framework is a filter framework

func NewNormalFramework

func NewNormalFramework(r Registry) Framework

type Options

type Options struct {
	// OlderThan is the minimum age of the resources to be considered unused
	OlderThan string
	// NewerThan is the maximum age of the resources to be considered unused
	NewerThan string
	// ExcludeLabels is a label selector to exclude resources with matching labels
	// IncludeLabels conflicts with it, and when setting IncludeLabels, ExcludeLabels is ignored and set to empty
	ExcludeLabels []string
	// IncludeLabels is a label selector to include resources with matching labels
	IncludeLabels string
	// ExcludeNamespaces is a namespace selector to exclude resources in matching namespaces
	// IncludeNamespaces conflicts with it, and when setting IncludeNamespaces, ExcludeNamespaces is ignored and set to empty
	ExcludeNamespaces []string
	// IncludeNamespaces is a namespace selector to include resources in matching namespaces
	IncludeNamespaces []string
	// contains filtered or unexported fields
}

Options represents the flags and options for filtering unused Kubernetes resources, such as pods, services, or configmaps. A resource is considered unused if it meets the following conditions:

  • Its age (measured from the last modified time) is within the range specified by older-than and newer-than flags. If older-than or newer-than is zero, no age limit is applied. If both flags are set, an error is returned.
  • Its size (measured in bytes) is within the range specified by MinSize and MaxSize flags. If MinSize or MaxSize is zero, no size limit is applied.
  • It does not have any labels that match the ExcludeLabels flag. The ExcludeLabels flag supports '=', '==', and '!=' operators, and multiple label pairs can be separated by commas. For example, -l key1=value1,key2!=value2.

func NewFilterOptions

func NewFilterOptions() *Options

NewFilterOptions returns a new FilterOptions instance with default values

func (*Options) Modify

func (o *Options) Modify()

Modify modifies the options

func (*Options) Namespaces

func (o *Options) Namespaces(clientset kubernetes.Interface) []string

Namespaces returns the namespaces, only called once

func (*Options) Validate

func (o *Options) Validate() error

Validate makes sure provided values for FilterOptions are valid

type Registry

type Registry map[string]FilterFunc

Registry is a collection of all available filters. The framework uses a

func NewDefaultRegistry

func NewDefaultRegistry() Registry

func (Registry) Merge

func (r Registry) Merge(in Registry) error

func (Registry) Register

func (r Registry) Register(name string, filter FilterFunc) error

func (Registry) Unregister

func (r Registry) Unregister(name string) error

Jump to

Keyboard shortcuts

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