Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Commit

Permalink
Merge branch 'master' into storage_new
Browse files Browse the repository at this point in the history
  • Loading branch information
luxas committed Aug 21, 2020
2 parents 0121e59 + 6526276 commit ec8eabb
Show file tree
Hide file tree
Showing 11 changed files with 263 additions and 406 deletions.
37 changes: 0 additions & 37 deletions pkg/filter/all.go

This file was deleted.

81 changes: 0 additions & 81 deletions pkg/filter/idname.go

This file was deleted.

48 changes: 48 additions & 0 deletions pkg/filter/interfaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package filter

import "github.com/weaveworks/libgitops/pkg/runtime"

// ListFilter is an interface for pipe-like list filtering behavior.
type ListFilter interface {
// Filter walks through all objects in obj, assesses whether the object
// matches the filter parameters, and conditionally adds it to the return
// slice or not. This method can be thought of like an UNIX pipe.
Filter(objs ...runtime.Object) ([]runtime.Object, error)
}

// ObjectFilter is an interface for filtering objects one-by-one.
type ObjectFilter interface {
// Filter takes in one object (at once, per invocation), and returns a
// boolean whether the object matches the filter parameters, or not.
Filter(obj runtime.Object) (bool, error)
}

// ObjectToListFilter transforms an ObjectFilter into a ListFilter. If of is nil,
// this function panics.
func ObjectToListFilter(of ObjectFilter) ListFilter {
if of == nil {
panic("programmer error: of ObjectFilter must not be nil in ObjectToListFilter")
}
return &objectToListFilter{of}
}

type objectToListFilter struct {
of ObjectFilter
}

// Filter implements ListFilter, but uses an ObjectFilter for the underlying logic.
func (f objectToListFilter) Filter(objs ...runtime.Object) (retarr []runtime.Object, err error) {
// Walk through all objects
for _, obj := range objs {
// Match them one-by-one against the ObjectFilter
match, err := f.of.Filter(obj)
if err != nil {
return nil, err
}
// If the object matches, include it in the return array
if match {
retarr = append(retarr, obj)
}
}
return
}
67 changes: 38 additions & 29 deletions pkg/filter/name.go
Original file line number Diff line number Diff line change
@@ -1,45 +1,54 @@
package filter

/*
TODO: Revisit if we need this file/package in the future.
import (
"fmt"
"strings"

"github.com/weaveworks/libgitops/pkg/runtime"
"github.com/weaveworks/libgitops/pkg/storage/filterer"
)

// The NameFilter matches Objects by their exact name
// NameFilter implements ObjectFilter and ListOption.
var _ ObjectFilter = NameFilter{}
var _ ListOption = NameFilter{}

// NameFilter is an ObjectFilter that compares runtime.Object.GetName()
// to the Name field by either equality or prefix.
type NameFilter struct {
name string
kind runtime.Kind
// Name matches the object by .metadata.name.
// +required
Name string
// Namespace matches the object by .metadata.namespace. If left as
// an empty string, it is ignored when filtering.
// +optional
Namespace string
// MatchPrefix whether the name (not namespace) matching should be exact, or prefix-based.
// +optional
MatchPrefix bool
}

var _ filterer.MetaFilter = &NameFilter{}
func NewNameFilter(n string) *NameFilter {
return &NameFilter{
name: n,
// Filter implements ObjectFilter
func (f NameFilter) Filter(obj runtime.Object) (bool, error) {
// Require f.Name to always be set.
if len(f.Name) == 0 {
return false, fmt.Errorf("the NameFilter.Name field must not be empty: %w", ErrInvalidFilterParams)
}
}

func (f *NameFilter) FilterMeta(object runtime.Object) (filterer.Match, error) {
if object.GetName() == f.name {
return filterer.NewMatch(object, true), nil
// If f.Namespace is set, and it does not match the object, return false
if len(f.Namespace) > 0 && f.Namespace != obj.GetNamespace() {
return false, nil
}

return nil, nil
}
func (f *NameFilter) SetKind(k runtime.Kind) {
f.kind = k
}
func (f *NameFilter) AmbiguousError(_ []filterer.Match) *filterer.AmbiguousError {
return filterer.NewAmbiguousError("ambiguous %s query: %q matched multiple names", f.kind, f.name)
// If the Name should be matched by the prefix, use strings.HasPrefix
if f.MatchPrefix {
return strings.HasPrefix(obj.GetName(), f.Name), nil
}
// Otherwise, just use an equality check
return f.Name == obj.GetName(), nil
}

func (f *NameFilter) NonexistentError() *filterer.NonexistentError {
return filterer.NewNonexistentError("can't find %s: no name matches for %q", f.kind, f.name)
// ApplyToListOptions implements ListOption, and adds itself converted to
// a ListFilter to ListOptions.Filters.
func (f NameFilter) ApplyToListOptions(target *ListOptions) error {
target.Filters = append(target.Filters, ObjectToListFilter(f))
return nil
}
*/
27 changes: 27 additions & 0 deletions pkg/filter/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package filter

// ListOptions is a generic struct for listing options.
type ListOptions struct {
// Filters contains a chain of ListFilters, which will be processed in order and pipe the
// available objects through before returning.
Filters []ListFilter
}

// ListOption is an interface which can be passed into e.g. List() methods as a variadic-length
// argument list.
type ListOption interface {
// ApplyToListOptions applies the configuration of the current object into a target ListOptions struct.
ApplyToListOptions(target *ListOptions) error
}

// MakeListOptions makes a completed ListOptions struct from a list of ListOption implementations.
func MakeListOptions(opts ...ListOption) (*ListOptions, error) {
o := &ListOptions{}
for _, opt := range opts {
// For every option, apply it into o, and check if there's an error
if err := opt.ApplyToListOptions(o); err != nil {
return nil, err
}
}
return o, nil
}
53 changes: 53 additions & 0 deletions pkg/filter/uid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package filter

import (
"errors"
"fmt"
"strings"

"github.com/weaveworks/libgitops/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
)

var (
// ErrInvalidFilterParams describes an error where invalid parameters were given
// to a filter.
ErrInvalidFilterParams = errors.New("invalid parameters given to filter")
)

// UIDFilter implements ObjectFilter and ListOption.
var _ ObjectFilter = UIDFilter{}
var _ ListOption = UIDFilter{}

// UIDFilter is an ObjectFilter that compares runtime.Object.GetUID() to
// the UID field by either equality or prefix. The UID field is required,
// otherwise ErrInvalidFilterParams is returned.
type UIDFilter struct {
// UID matches the object by .metadata.uid.
// +required
UID types.UID
// MatchPrefix whether the UID-matching should be exact, or prefix-based.
// +optional
MatchPrefix bool
}

// Filter implements ObjectFilter
func (f UIDFilter) Filter(obj runtime.Object) (bool, error) {
// Require f.UID to always be set.
if len(f.UID) == 0 {
return false, fmt.Errorf("the UIDFilter.UID field must not be empty: %w", ErrInvalidFilterParams)
}
// If the UID should be matched by the prefix, use strings.HasPrefix
if f.MatchPrefix {
return strings.HasPrefix(string(obj.GetUID()), string(f.UID)), nil
}
// Otherwise, just use an equality check
return f.UID == obj.GetUID(), nil
}

// ApplyToListOptions implements ListOption, and adds itself converted to
// a ListFilter to ListOptions.Filters.
func (f UIDFilter) ApplyToListOptions(target *ListOptions) error {
target.Filters = append(target.Filters, ObjectToListFilter(f))
return nil
}
3 changes: 3 additions & 0 deletions pkg/runtime/identifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// DefaultNamespace describes the default namespace name used for the system.
const DefaultNamespace = "default"

// Identifyable is an object which can be identified
type Identifyable interface {
// GetIdentifier can return e.g. a "namespace/name" combination, which is not guaranteed
Expand Down
Loading

0 comments on commit ec8eabb

Please sign in to comment.