filter

package
v0.14.1 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2022 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package filter contains methods for selecting and filtering lists of components and objects.

Example (Select)
package main

import (
	"fmt"
	"log"

	bundle "github.com/GoogleCloudPlatform/k8s-cluster-bundle/pkg/apis/bundle/v1alpha1"
	"github.com/GoogleCloudPlatform/k8s-cluster-bundle/pkg/converter"
	"github.com/GoogleCloudPlatform/k8s-cluster-bundle/pkg/filter"
	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

var componentsYAMLs = `
components:
- spec:
    componentName: zap
    objects:
    - apiVersion: v1
      kind: Pod
      metadata:
        name: zap-pod
        labels:
          component: zork
        annotations:
          foo: bar
        namespace: kube-system
    - apiVersion: v1
      kind: Pod
      metadata:
        name: zip-pod
        labels:
          component: zork
        annotations:
          foo: baz
        namespace: default
- spec:
    componentName: bog
    objects:
    - apiVersion: v1
      kind: Pod
      metadata:
        name: bog-pod-1
        labels:
          component: bork
        annotations:
          foof: yar
        namespace: kube-system
    - apiVersion: v1
      kind: Pod
      metadata: 
        name: bog-pod-2
        namespace: default
- spec:
    componentName: nog
    objects:
    - apiVersion: v1beta1
      kind: Pod
      metadata:
        name: nog-pod
        labels:
          component: nork
        annotations:
          foof: narf
        namespace: kube
- spec:
    componentName: zog
    objects:
    - apiVersion: v1
      kind: Pod
      metadata:
        name: zog-pod
        namespace: kube-system
    - apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: zog-dep
        labels:
          component: zork
        annotations:
          zoof: zarf
        namespace: zube
- spec:
    componentName: kog
    objects:
    - apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: kog-dep
        namespace: kube-system`

func componentName(c *bundle.Component) string {
	return c.Spec.ComponentName
}

type component struct {
	name    string
	objects []string
}

func toComponent(comp *bundle.Component) component {
	c := component{name: componentName(comp)}
	for _, obj := range comp.Spec.Objects {
		c.objects = append(c.objects, obj.GetName())
	}
	return c
}

func toComponentList(components []*bundle.Component) []component {
	out := make([]component, len(components))
	for i, c := range components {
		out[i] = toComponent(c)
	}
	return out
}

func main() {
	bundle, err := converter.FromYAMLString(componentsYAMLs).ToBundle()
	if err != nil {
		log.Fatalf("could not convert from yaml to bundle: %v", err)
	}
	c1 := filter.Select(bundle.Components, filter.ComponentFieldMatchIn([]string{"zap", "zog"}, componentName))
	fmt.Println("c1:", toComponentList(c1))

	c2 := filter.Select(bundle.Components, filter.Or(
		filter.ObjectFieldMatchIn([]string{"bog-pod-1"}, (*unstructured.Unstructured).GetName),
		filter.ComponentFieldMatchIn([]string{"nog"}, componentName)))
	fmt.Println("c2:", toComponentList(c2))

	c3 := filter.Select(bundle.Components, filter.And(
		filter.ObjectFieldMatchIn([]string{"Pod"}, (*unstructured.Unstructured).GetKind),
		filter.ObjectFieldMatchIn([]string{"kube-system"}, (*unstructured.Unstructured).GetNamespace)))
	fmt.Println("c3:", toComponentList(c3))
}
Output:

c1: [{zap [zap-pod zip-pod]} {zog [zog-pod zog-dep]}]
c2: [{bog [bog-pod-1 bog-pod-2]} {nog [nog-pod]}]
c3: [{zap [zap-pod zip-pod]} {bog [bog-pod-1 bog-pod-2]} {zog [zog-pod zog-dep]}]
Example (SelectObjects)
bundle, err := converter.FromYAMLString(componentsYAMLs).ToBundle()
if err != nil {
	log.Fatalf("could not convert from yaml to bundle: %v", err)
}
// If the match is on a component level then all objects are selected for that component.
c1 := filter.SelectObjects(bundle.Components, filter.ComponentFieldMatchIn([]string{"zap", "zog"}, componentName))
fmt.Println("c1:", toComponentList(c1))

// Only the objects that match are returned on the component.
c2 := filter.SelectObjects(bundle.Components, filter.Or(
	filter.ObjectFieldMatchIn([]string{"zap-pod"}, (*unstructured.Unstructured).GetName),
	filter.ComponentFieldMatchIn([]string{"nog"}, componentName)))
fmt.Println("c2:", toComponentList(c2))

// Only pods that are not in kube-system namespace are returned.
c3 := filter.SelectObjects(bundle.Components, filter.And(
	filter.ObjectFieldMatchIn([]string{"Pod"}, (*unstructured.Unstructured).GetKind),
	filter.Not(filter.ObjectFieldMatchIn([]string{"kube-system"}, (*unstructured.Unstructured).GetNamespace))))
fmt.Println("c3:", toComponentList(c3))
Output:

c1: [{zap [zap-pod zip-pod]} {zog [zog-pod zog-dep]}]
c2: [{zap [zap-pod]} {nog [nog-pod]}]
c3: [{zap [zip-pod]} {bog [bog-pod-2]} {nog [nog-pod]}]

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func MatchesComponent added in v0.11.0

func MatchesComponent(c *bundle.Component, o *Options) bool

MatchesComponent returns true if the conditions match a Component.

func MatchesObject added in v0.11.0

func MatchesObject(obj *unstructured.Unstructured, o *Options) bool

MatchesObject returns true if the conditions match an object.

func Select added in v0.13.0

func Select(components []*bundle.Component, predicate ComponentPredicate) []*bundle.Component

Select takes a list of components and a predicate and returns the components that match the predicate.

func SelectObjects added in v0.13.0

func SelectObjects(components []*bundle.Component, predicate ComponentPredicate) []*bundle.Component

SelectObjects returns components with only the objects that match the predicate.

Types

type ComponentPredicate added in v0.13.0

type ComponentPredicate func(*bundle.Component) bool

ComponentPredicate is a func that returns true for components that match criteria and false otherwise.

func And added in v0.13.0

func And(componentPredicates ...ComponentPredicate) ComponentPredicate

And returns true if every ComponentPredicate function returns true and returns false otherwise.

func ComponentFieldMatchIn added in v0.13.0

func ComponentFieldMatchIn(matchList []string, fieldGetter func(*bundle.Component) string) ComponentPredicate

ComponentFieldMatchIn takes a []string and see if the field matches one of those.

func Not added in v0.13.0

Not returns a ComponentPredicate that negates it's result.

func ObjectFieldMatchIn added in v0.13.0

func ObjectFieldMatchIn(matchList []string, objectGetter func(*unstructured.Unstructured) string) ComponentPredicate

ObjectFieldMatchIn takes a []string and sees if any object in the component matches one of those.

func Or added in v0.13.0

func Or(componentPredicates ...ComponentPredicate) ComponentPredicate

Or returns true if any ComponentPredicate function returns true and returns false otherwise.

type Filter added in v0.6.1

type Filter struct{}

Filter filters the components and objects to produce a new set of components.

func NewFilter added in v0.6.2

func NewFilter() *Filter

NewFilter creates a new Filter.

func (*Filter) FilterComponents added in v0.11.0

func (f *Filter) FilterComponents(data []*bundle.Component, o *Options) []*bundle.Component

FilterComponents removes components based on the ObjectMeta properties of the components, returning a new cluster bundle with just filtered components. Filtering for components doesn't take into account the properties of the object-children of the components. This is the opposite matching from SelectComponents.

func (*Filter) FilterObjects added in v0.11.0

func (f *Filter) FilterObjects(data []*unstructured.Unstructured, o *Options) []*unstructured.Unstructured

FilterObjects removes objects based on the ObjectMeta properties of the objects, returning a new list with just filtered objects. This performs the opposite match from SelectObjects.

func (*Filter) PartitionComponents added in v0.11.0

func (f *Filter) PartitionComponents(data []*bundle.Component, o *Options) ([]*bundle.Component, []*bundle.Component)

PartitionComponents splits the components into matched and not matched sets. PartitionComponents ignores the InvertMatch option, since both matched and unmatched objects are returned. Thus, the options to partition are always treated as options for matching objects.

func (*Filter) PartitionObjects added in v0.9.0

func (f *Filter) PartitionObjects(data []*unstructured.Unstructured, o *Options) ([]*unstructured.Unstructured, []*unstructured.Unstructured)

PartitionObjects splits the objects into matched and not matched sets. PartitionObjects ignores the KeepOnly option, since both matched and unmatched objects are returned. Thus, the options to partition are always treated as options for matching objects.

func (*Filter) SelectComponents added in v0.11.0

func (f *Filter) SelectComponents(data []*bundle.Component, o *Options) []*bundle.Component

SelectComponents picks components based on the ObjectMeta properties of the components, returning a new cluster bundle with just filtered components. Filtering for components doesn't take into account the properties of the object-children of the components. This performs the opposte matching from FilterComponents.

func (*Filter) SelectObjects added in v0.11.0

func (f *Filter) SelectObjects(data []*unstructured.Unstructured, o *Options) []*unstructured.Unstructured

SelectObjects picks objects based on the ObjectMeta properties of the objects, returning a new list with just filtered objects.

type Options

type Options struct {
	// Kinds represent the Kinds to filter on. Can either be unqualified ("Deployment")
	// or qualified ("apps/v1beta1,Pod"). Qualified kinds are often called
	// GroupVersionKind in the Kubernetes Schema.
	Kinds []string

	// Names represent the names to filter on. For objects, this is the
	// metadata.name field. For components, this is the ComponentName.
	Names []string

	// Annotations contain key/value pairs to filter on. An empty string value matches
	// all annotation-values for a particular key.
	Annotations map[string]string

	// Labels contain key/value pairs to filter on. An empty string value matches
	// all label-values for a particular key.
	Labels map[string]string

	// Namespaces to filter on.
	Namespaces []string

	// InvertMatch indicates wether to return the opposite match.
	InvertMatch bool
}

Options for filtering bundles. By default, if any of the options match, then the relevant component or object is removed. If InvertMatch is set, then the objects are kept instead of removed.

func OptionsFromObjectSelector added in v0.11.0

func OptionsFromObjectSelector(sel *bundle.ObjectSelector) *Options

OptionsFromObjectSelector creates an Options Object from a

Jump to

Keyboard shortcuts

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