Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create default storage selection functions #15594

Merged
merged 3 commits into from
Aug 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions pkg/authorization/registry/clusterpolicy/etcd/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@ func NewREST(optsGetter restoptions.Getter) (*REST, error) {
NewFunc: func() runtime.Object { return &authorizationapi.ClusterPolicy{} },
NewListFunc: func() runtime.Object { return &authorizationapi.ClusterPolicyList{} },
QualifiedResource: authorizationapi.Resource("clusterpolicies"),
PredicateFunc: clusterpolicy.Matcher,

CreateStrategy: clusterpolicy.Strategy,
UpdateStrategy: clusterpolicy.Strategy,
DeleteStrategy: clusterpolicy.Strategy,
}

options := &generic.StoreOptions{RESTOptions: optsGetter, AttrFunc: clusterpolicy.GetAttrs}
options := &generic.StoreOptions{RESTOptions: optsGetter}
if err := store.CompleteWithOptions(options); err != nil {
return nil, err
}
Expand Down
23 changes: 0 additions & 23 deletions pkg/authorization/registry/clusterpolicy/strategy.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
package clusterpolicy

import (
"fmt"

"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
apirequest "k8s.io/apiserver/pkg/endpoints/request"
kstorage "k8s.io/apiserver/pkg/storage"
kapi "k8s.io/kubernetes/pkg/api"

authorizationapi "github.com/openshift/origin/pkg/authorization/apis/authorization"
Expand Down Expand Up @@ -65,21 +60,3 @@ func (strategy) Validate(ctx apirequest.Context, obj runtime.Object) field.Error
func (strategy) ValidateUpdate(ctx apirequest.Context, obj, old runtime.Object) field.ErrorList {
return validation.ValidateClusterPolicyUpdate(obj.(*authorizationapi.ClusterPolicy), old.(*authorizationapi.ClusterPolicy))
}

// GetAttrs returns labels and fields of a given object for filtering purposes
func GetAttrs(obj runtime.Object) (labels.Set, fields.Set, bool, error) {
policy, ok := obj.(*authorizationapi.ClusterPolicy)
if !ok {
return nil, nil, false, fmt.Errorf("not a ClusterPolicy")
}
return labels.Set(policy.ObjectMeta.Labels), authorizationapi.ClusterPolicyToSelectableFields(policy), policy.Initializers != nil, nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this a behavior change? authorizationapi.ClusterPolicyToSelectableFields is not the same as your generic method?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this a behavior change? authorizationapi.ClusterPolicyToSelectableFields is not the same as your generic method?

Maybe I've been staring at this too long. Don't both map "metadata.name" to the value of Name?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I've been staring at this too long. Don't both map "metadata.name" to the value of Name?

here: https://github.com/openshift/origin/blob/master/pkg/authorization/apis/authorization/fields.go#L7 . There's only one "special" one and I think I plumbed it through properly.

}

// Matcher returns a generic matcher for a given label and field selector.
func Matcher(label labels.Selector, field fields.Selector) kstorage.SelectionPredicate {
return kstorage.SelectionPredicate{
Label: label,
Field: field,
GetAttrs: GetAttrs,
}
}
3 changes: 1 addition & 2 deletions pkg/authorization/registry/clusterpolicybinding/etcd/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,14 @@ func NewREST(optsGetter restoptions.Getter) (*REST, error) {
Copier: kapi.Scheme,
NewFunc: func() runtime.Object { return &authorizationapi.ClusterPolicyBinding{} },
NewListFunc: func() runtime.Object { return &authorizationapi.ClusterPolicyBindingList{} },
PredicateFunc: clusterpolicybinding.Matcher,
QualifiedResource: authorizationapi.Resource("clusterpolicybindings"),

CreateStrategy: clusterpolicybinding.Strategy,
UpdateStrategy: clusterpolicybinding.Strategy,
DeleteStrategy: clusterpolicybinding.Strategy,
}

options := &generic.StoreOptions{RESTOptions: optsGetter, AttrFunc: clusterpolicybinding.GetAttrs}
options := &generic.StoreOptions{RESTOptions: optsGetter}
if err := store.CompleteWithOptions(options); err != nil {
return nil, err
}
Expand Down
23 changes: 0 additions & 23 deletions pkg/authorization/registry/clusterpolicybinding/strategy.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
package clusterpolicybinding

import (
"fmt"

"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
apirequest "k8s.io/apiserver/pkg/endpoints/request"
kstorage "k8s.io/apiserver/pkg/storage"
kapi "k8s.io/kubernetes/pkg/api"

authorizationapi "github.com/openshift/origin/pkg/authorization/apis/authorization"
Expand Down Expand Up @@ -81,21 +76,3 @@ func (strategy) Validate(ctx apirequest.Context, obj runtime.Object) field.Error
func (strategy) ValidateUpdate(ctx apirequest.Context, obj, old runtime.Object) field.ErrorList {
return validation.ValidateClusterPolicyBindingUpdate(obj.(*authorizationapi.ClusterPolicyBinding), old.(*authorizationapi.ClusterPolicyBinding))
}

// GetAttrs returns labels and fields of a given object for filtering purposes
func GetAttrs(obj runtime.Object) (labels.Set, fields.Set, bool, error) {
policyBinding, ok := obj.(*authorizationapi.ClusterPolicyBinding)
if !ok {
return nil, nil, false, fmt.Errorf("not a ClusterPolicyBinding")
}
return labels.Set(policyBinding.ObjectMeta.Labels), authorizationapi.ClusterPolicyBindingToSelectableFields(policyBinding), policyBinding.Initializers != nil, nil
}

// Matcher returns a generic matcher for a given label and field selector.
func Matcher(label labels.Selector, field fields.Selector) kstorage.SelectionPredicate {
return kstorage.SelectionPredicate{
Label: label,
Field: field,
GetAttrs: GetAttrs,
}
}
3 changes: 1 addition & 2 deletions pkg/authorization/registry/policy/etcd/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,14 @@ func NewREST(optsGetter restoptions.Getter) (*REST, error) {
Copier: kapi.Scheme,
NewFunc: func() runtime.Object { return &authorizationapi.Policy{} },
NewListFunc: func() runtime.Object { return &authorizationapi.PolicyList{} },
PredicateFunc: policy.Matcher,
QualifiedResource: authorizationapi.Resource("policies"),

CreateStrategy: policy.Strategy,
UpdateStrategy: policy.Strategy,
DeleteStrategy: policy.Strategy,
}

options := &generic.StoreOptions{RESTOptions: optsGetter, AttrFunc: policy.GetAttrs}
options := &generic.StoreOptions{RESTOptions: optsGetter}
if err := store.CompleteWithOptions(options); err != nil {
return nil, err
}
Expand Down
23 changes: 0 additions & 23 deletions pkg/authorization/registry/policy/strategy.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
package policy

import (
"fmt"

"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
apirequest "k8s.io/apiserver/pkg/endpoints/request"
kstorage "k8s.io/apiserver/pkg/storage"
kapi "k8s.io/kubernetes/pkg/api"

authorizationapi "github.com/openshift/origin/pkg/authorization/apis/authorization"
Expand Down Expand Up @@ -66,21 +61,3 @@ func (strategy) Validate(ctx apirequest.Context, obj runtime.Object) field.Error
func (strategy) ValidateUpdate(ctx apirequest.Context, obj, old runtime.Object) field.ErrorList {
return validation.ValidateLocalPolicyUpdate(obj.(*authorizationapi.Policy), old.(*authorizationapi.Policy))
}

// GetAttrs returns labels and fields of a given object for filtering purposes
func GetAttrs(obj runtime.Object) (labels.Set, fields.Set, bool, error) {
policy, ok := obj.(*authorizationapi.Policy)
if !ok {
return nil, nil, false, fmt.Errorf("not a Policy")
}
return labels.Set(policy.ObjectMeta.Labels), authorizationapi.PolicyToSelectableFields(policy), policy.Initializers != nil, nil
}

// Matcher returns a generic matcher for a given label and field selector.
func Matcher(label labels.Selector, field fields.Selector) kstorage.SelectionPredicate {
return kstorage.SelectionPredicate{
Label: label,
Field: field,
GetAttrs: GetAttrs,
}
}
17 changes: 15 additions & 2 deletions pkg/authorization/registry/policybinding/etcd/etcd.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package etcd

import (
"fmt"

"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apiserver/pkg/registry/generic"
"k8s.io/apiserver/pkg/registry/generic/registry"
"k8s.io/apiserver/pkg/storage"
kapi "k8s.io/kubernetes/pkg/api"

authorizationapi "github.com/openshift/origin/pkg/authorization/apis/authorization"
Expand All @@ -22,17 +26,26 @@ func NewREST(optsGetter restoptions.Getter) (*REST, error) {
NewFunc: func() runtime.Object { return &authorizationapi.PolicyBinding{} },
NewListFunc: func() runtime.Object { return &authorizationapi.PolicyBindingList{} },
QualifiedResource: authorizationapi.Resource("policybindings"),
PredicateFunc: policybinding.Matcher,

CreateStrategy: policybinding.Strategy,
UpdateStrategy: policybinding.Strategy,
DeleteStrategy: policybinding.Strategy,
}

options := &generic.StoreOptions{RESTOptions: optsGetter, AttrFunc: policybinding.GetAttrs}
options := &generic.StoreOptions{RESTOptions: optsGetter,
AttrFunc: storage.AttrFunc(storage.DefaultNamespaceScopedAttr).WithFieldMutation(FieldSetMutator)}
if err := store.CompleteWithOptions(options); err != nil {
return nil, err
}

return &REST{store}, nil
}

func FieldSetMutator(obj runtime.Object, fieldSet fields.Set) error {
policyBinding, ok := obj.(*authorizationapi.PolicyBinding)
if !ok {
return fmt.Errorf("%T not a PolicyBinding", obj)
}
fieldSet["policyRef.namespace"] = policyBinding.PolicyRef.Namespace
return nil
}
23 changes: 0 additions & 23 deletions pkg/authorization/registry/policybinding/strategy.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
package policybinding

import (
"fmt"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
apirequest "k8s.io/apiserver/pkg/endpoints/request"
kstorage "k8s.io/apiserver/pkg/storage"
kapi "k8s.io/kubernetes/pkg/api"

authorizationapi "github.com/openshift/origin/pkg/authorization/apis/authorization"
Expand Down Expand Up @@ -82,24 +77,6 @@ func (strategy) ValidateUpdate(ctx apirequest.Context, obj, old runtime.Object)
return validation.ValidateLocalPolicyBindingUpdate(obj.(*authorizationapi.PolicyBinding), old.(*authorizationapi.PolicyBinding))
}

// GetAttrs returns labels and fields of a given object for filtering purposes
func GetAttrs(obj runtime.Object) (labels.Set, fields.Set, bool, error) {
policyBinding, ok := obj.(*authorizationapi.PolicyBinding)
if !ok {
return nil, nil, false, fmt.Errorf("not a PolicyBinding")
}
return labels.Set(policyBinding.ObjectMeta.Labels), authorizationapi.PolicyBindingToSelectableFields(policyBinding), policyBinding.Initializers != nil, nil
}

// Matcher returns a generic matcher for a given label and field selector.
func Matcher(label labels.Selector, field fields.Selector) kstorage.SelectionPredicate {
return kstorage.SelectionPredicate{
Label: label,
Field: field,
GetAttrs: GetAttrs,
}
}

func NewEmptyPolicyBinding(namespace, policyNamespace, policyBindingName string) *authorizationapi.PolicyBinding {
binding := &authorizationapi.PolicyBinding{}
binding.Name = policyBindingName
Expand Down
57 changes: 0 additions & 57 deletions pkg/client/cache/clusterpolicy.go

This file was deleted.

57 changes: 0 additions & 57 deletions pkg/client/cache/clusterpolicybinding.go

This file was deleted.

Loading