Scopes: Add more BE filtering (field selectors) (#85169)

This commit is contained in:
Kyle Brandt
2024-03-27 09:10:49 -04:00
committed by GitHub
parent d71266b8af
commit 222f93794d
2 changed files with 32 additions and 8 deletions

View File

@@ -54,7 +54,7 @@ func (b *ScopeAPIBuilder) InstallSchema(scheme *runtime.Scheme) error {
err = scheme.AddFieldLabelConversionFunc(
scope.ScopeResourceInfo.GroupVersionKind(),
func(label, value string) (string, string, error) {
fieldSet := SelectableFields(&scope.Scope{})
fieldSet := SelectableScopeFields(&scope.Scope{})
for key := range fieldSet {
if label == key {
return label, value, nil
@@ -67,6 +67,22 @@ func (b *ScopeAPIBuilder) InstallSchema(scheme *runtime.Scheme) error {
return err
}
err = scheme.AddFieldLabelConversionFunc(
scope.ScopeDashboardResourceInfo.GroupVersionKind(),
func(label, value string) (string, string, error) {
fieldSet := SelectableScopeDashboardFields(&scope.ScopeDashboard{})
for key := range fieldSet {
if label == key {
return label, value, nil
}
}
return "", "", fmt.Errorf("field label not supported for %s: %s", scope.ScopeDashboardResourceInfo.GroupVersionKind(), label)
},
)
if err != nil {
return err
}
// Link this version to the internal representation.
// This is used for server-side-apply (PATCH), and avoids the error:
// "no kind is registered for the type"

View File

@@ -101,12 +101,13 @@ func newScopeDashboardStorage(scheme *runtime.Scheme, optsGetter generic.RESTOpt
}
func GetAttrs(obj runtime.Object) (labels.Set, fields.Set, error) {
s, ok := obj.(*scope.Scope)
if !ok {
return nil, nil, fmt.Errorf("not a scope")
if s, ok := obj.(*scope.Scope); ok {
return labels.Set(s.Labels), SelectableScopeFields(s), nil
}
return labels.Set(s.Labels), SelectableFields(s), nil
if s, ok := obj.(*scope.ScopeDashboard); ok {
return labels.Set(s.Labels), SelectableScopeDashboardFields(s), nil
}
return nil, nil, fmt.Errorf("not a scope or scopeDashboard object")
}
// Matcher returns a generic.SelectionPredicate that matches on label and field selectors.
@@ -118,8 +119,15 @@ func Matcher(label labels.Selector, field fields.Selector) apistore.SelectionPre
}
}
func SelectableFields(obj *scope.Scope) fields.Set {
func SelectableScopeFields(obj *scope.Scope) fields.Set {
return generic.MergeFieldsSets(generic.ObjectMetaFieldsSet(&obj.ObjectMeta, false), fields.Set{
"spec.type": obj.Spec.Type,
"spec.type": obj.Spec.Type,
"spec.category": obj.Spec.Category,
})
}
func SelectableScopeDashboardFields(obj *scope.ScopeDashboard) fields.Set {
return generic.MergeFieldsSets(generic.ObjectMetaFieldsSet(&obj.ObjectMeta, false), fields.Set{
"spec.scopeUid": obj.Spec.ScopeUID,
})
}