This repository has been archived by the owner on Aug 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into storage_new
- Loading branch information
Showing
11 changed files
with
263 additions
and
406 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.