Skip to content

Commit

Permalink
fix:notify template list filtered by TenantID always get empty result
Browse files Browse the repository at this point in the history
  • Loading branch information
foshantiger authored and choujimmy committed Dec 6, 2019
1 parent 37ee8e3 commit 9459951
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/notify/registry/template/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func NewStorage(optsGetter genericregistry.RESTOptionsGetter, notifyClient *noti
NewFunc: func() runtime.Object { return &notify.Template{} },
NewListFunc: func() runtime.Object { return &notify.TemplateList{} },
DefaultQualifiedResource: notify.Resource("templates"),
PredicateFunc: templatestrategy.MatchTemplate,
ReturnDeletedObject: true,

CreateStrategy: strategy,
Expand All @@ -57,6 +58,7 @@ func NewStorage(optsGetter genericregistry.RESTOptionsGetter, notifyClient *noti
}
options := &genericregistry.StoreOptions{
RESTOptions: optsGetter,
AttrFunc: templatestrategy.GetAttrs,
}

if err := store.CompleteWithOptions(options); err != nil {
Expand Down
37 changes: 37 additions & 0 deletions pkg/notify/registry/template/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,15 @@ package template

import (
"context"
"fmt"

"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
genericregistry "k8s.io/apiserver/pkg/registry/generic"
"k8s.io/apiserver/pkg/registry/rest"
"k8s.io/apiserver/pkg/storage"
"k8s.io/apiserver/pkg/storage/names"
notifyinternalclient "tkestack.io/tke/api/client/clientset/internalversion/typed/notify/internalversion"
"tkestack.io/tke/api/notify"
Expand Down Expand Up @@ -113,3 +119,34 @@ func (Strategy) Canonicalize(obj runtime.Object) {
func (s *Strategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
return ValidateTemplateUpdate(obj.(*notify.Template), old.(*notify.Template), s.notifyClient)
}

// GetAttrs returns labels and fields of a given object for filtering purposes.
func GetAttrs(obj runtime.Object) (labels.Set, fields.Set, error) {
template, ok := obj.(*notify.Template)
if !ok {
return nil, nil, fmt.Errorf("not a template")
}
return labels.Set(template.ObjectMeta.Labels), ToSelectableFields(template), nil
}

// MatchTemplate returns a generic matcher for a given label and field selector.
func MatchTemplate(label labels.Selector, field fields.Selector) storage.SelectionPredicate {
return storage.SelectionPredicate{
Label: label,
Field: field,
GetAttrs: GetAttrs,
IndexFields: []string{
"spec.tenantID",
"metadata.name",
},
}
}

// ToSelectableFields returns a field set that represents the object
func ToSelectableFields(template *notify.Template) fields.Set {
objectMetaFieldsSet := genericregistry.ObjectMetaFieldsSet(&template.ObjectMeta, false)
specificFieldsSet := fields.Set{
"spec.tenantID": template.Spec.TenantID,
}
return genericregistry.MergeFieldsSets(objectMetaFieldsSet, specificFieldsSet)
}

0 comments on commit 9459951

Please sign in to comment.