Scopes: Add Filtering for ScopeDashoardBinding and Update Prometheus for ScopeFilterOperator Changes (#85284)

This commit is contained in:
Kyle Brandt 2024-03-27 11:39:55 -04:00 committed by GitHub
parent 7e1cd4397a
commit 136f8e6f0c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 61 additions and 21 deletions

View File

@ -20,8 +20,8 @@ var ScopeResourceInfo = common.NewResourceInfo(GROUP, VERSION,
func() runtime.Object { return &ScopeList{} },
)
var ScopeDashboardResourceInfo = common.NewResourceInfo(GROUP, VERSION,
"scopedashboards", "scopedashboard", "ScopeDashboard",
var ScopeDashboardBindingResourceInfo = common.NewResourceInfo(GROUP, VERSION,
"scopedashboardbindings", "scopedashboardbinding", "ScopeDashboardBinding",
func() runtime.Object { return &ScopeDashboardBinding{} },
func() runtime.Object { return &ScopeDashboardBindingList{} },
)

View File

@ -4,6 +4,11 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
/*
Please keep pkg/promlib/models/query.go and pkg/promlib/models/scope.go in sync
with this file until this package is out of the grafana/grafana module.
*/
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
type Scope struct {
metav1.TypeMeta `json:",inline"`

View File

@ -80,11 +80,22 @@ type ScopeSpec struct {
// ScopeFilter is a hand copy of the ScopeFilter struct from pkg/apis/scope/v0alpha1/types.go
// to avoid import (temp fix)
type ScopeFilter struct {
Key string `json:"key"`
Value string `json:"value"`
Operator string `json:"operator"`
Key string `json:"key"`
Value string `json:"value"`
Operator FilterOperator `json:"operator"`
}
// FilterOperator is a hand copy of the ScopeFilter struct from pkg/apis/scope/v0alpha1/types.go
type FilterOperator string
// Hand copy of enum from pkg/apis/scope/v0alpha1/types.go
const (
FilterOperatorEquals FilterOperator = "equals"
FilterOperatorNotEquals FilterOperator = "not-equals"
FilterOperatorRegexMatch FilterOperator = "regex-match"
FilterOperatorRegexNotMatch FilterOperator = "regex-not-match"
)
// Internal interval and range variables
const (
varInterval = "$__interval"

View File

@ -63,13 +63,13 @@ func scopeFiltersToMatchers(filters []ScopeFilter) ([]*labels.Matcher, error) {
for _, f := range filters {
var mt labels.MatchType
switch f.Operator {
case "=":
case FilterOperatorEquals:
mt = labels.MatchEqual
case "!=":
case FilterOperatorNotEquals:
mt = labels.MatchNotEqual
case "=~":
case FilterOperatorRegexMatch:
mt = labels.MatchRegexp
case "!~":
case FilterOperatorRegexNotMatch:
mt = labels.MatchNotRegexp
default:
return nil, fmt.Errorf("unknown operator %q", f.Operator)

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.ScopeDashboardBindingResourceInfo.GroupVersionKind(),
func(label, value string) (string, string, error) {
fieldSet := SelectableScopeDashboardBindingFields(&scope.ScopeDashboardBinding{})
for key := range fieldSet {
if label == key {
return label, value, nil
}
}
return "", "", fmt.Errorf("field label not supported for %s: %s", scope.ScopeDashboardBindingResourceInfo.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"
@ -87,7 +103,7 @@ func (b *ScopeAPIBuilder) GetAPIGroupInfo(
apiGroupInfo := genericapiserver.NewDefaultAPIGroupInfo(scope.GROUP, scheme, metav1.ParameterCodec, codecs)
scopeResourceInfo := scope.ScopeResourceInfo
scopeDashboardResourceInfo := scope.ScopeDashboardResourceInfo
scopeDashboardResourceInfo := scope.ScopeDashboardBindingResourceInfo
storage := map[string]rest.Storage{}
@ -97,7 +113,7 @@ func (b *ScopeAPIBuilder) GetAPIGroupInfo(
}
storage[scopeResourceInfo.StoragePath()] = scopeStorage
scopeDashboardStorage, err := newScopeDashboardStorage(scheme, optsGetter)
scopeDashboardStorage, err := newScopeDashboardBindingStorage(scheme, optsGetter)
if err != nil {
return nil, err
}

View File

@ -62,10 +62,10 @@ func newScopeStorage(scheme *runtime.Scheme, optsGetter generic.RESTOptionsGette
return &storage{Store: store}, nil
}
func newScopeDashboardStorage(scheme *runtime.Scheme, optsGetter generic.RESTOptionsGetter) (*storage, error) {
func newScopeDashboardBindingStorage(scheme *runtime.Scheme, optsGetter generic.RESTOptionsGetter) (*storage, error) {
strategy := grafanaregistry.NewStrategy(scheme)
resourceInfo := scope.ScopeDashboardResourceInfo
resourceInfo := scope.ScopeDashboardBindingResourceInfo
store := &genericregistry.Store{
NewFunc: resourceInfo.NewFunc,
NewListFunc: resourceInfo.NewListFunc,
@ -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.ScopeDashboardBinding); ok {
return labels.Set(s.Labels), SelectableScopeDashboardBindingFields(s), nil
}
return nil, nil, fmt.Errorf("not a scope or ScopeDashboardBinding 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 SelectableScopeDashboardBindingFields(obj *scope.ScopeDashboardBinding) fields.Set {
return generic.MergeFieldsSets(generic.ObjectMetaFieldsSet(&obj.ObjectMeta, false), fields.Set{
"spec.scope": obj.Spec.Scope,
})
}