fix the namespaceScoped of cachers

This commit is contained in:
Chao Xu 2016-01-21 16:59:30 -08:00
parent 2aff6ccf06
commit ebcff4b5e4
24 changed files with 44 additions and 26 deletions

View File

@ -35,8 +35,10 @@ type REST struct {
func NewREST(s storage.Interface, storageDecorator generic.StorageDecorator) *REST { func NewREST(s storage.Interface, storageDecorator generic.StorageDecorator) *REST {
prefix := "/testtype" prefix := "/testtype"
newListFunc := func() runtime.Object { return &testgroup.TestTypeList{} } newListFunc := func() runtime.Object { return &testgroup.TestTypeList{} }
// Usually you should reuse your RESTCreateStrategy.
strategy := &NotNamespaceScoped{}
storageInterface := storageDecorator( storageInterface := storageDecorator(
s, 100, &testgroup.TestType{}, prefix, false, newListFunc) s, 100, &testgroup.TestType{}, prefix, strategy, newListFunc)
store := &etcdgeneric.Etcd{ store := &etcdgeneric.Etcd{
NewFunc: func() runtime.Object { return &testgroup.TestType{} }, NewFunc: func() runtime.Object { return &testgroup.TestType{} },
// NewListFunc returns an object capable of storing results of an etcd list. // NewListFunc returns an object capable of storing results of an etcd list.
@ -63,3 +65,10 @@ func NewREST(s storage.Interface, storageDecorator generic.StorageDecorator) *RE
} }
return &REST{store} return &REST{store}
} }
type NotNamespaceScoped struct {
}
func (*NotNamespaceScoped) NamespaceScoped() bool {
return false
}

View File

@ -118,3 +118,9 @@ func objectMetaAndKind(typer runtime.ObjectTyper, obj runtime.Object) (*api.Obje
} }
return objectMeta, kind, nil return objectMeta, kind, nil
} }
// NamespaceScopedStrategy has a method to tell if the object must be in a namespace.
type NamespaceScopedStrategy interface {
// NamespaceScoped returns if the object must be in a namespace.
NamespaceScoped() bool
}

View File

@ -37,7 +37,7 @@ func NewREST(s storage.Interface, storageDecorator generic.StorageDecorator) *RE
newListFunc := func() runtime.Object { return &api.ConfigMapList{} } newListFunc := func() runtime.Object { return &api.ConfigMapList{} }
storageInterface := storageDecorator( storageInterface := storageDecorator(
s, 100, &api.ConfigMap{}, prefix, false, newListFunc) s, 100, &api.ConfigMap{}, prefix, configmap.Strategy, newListFunc)
store := &etcdgeneric.Etcd{ store := &etcdgeneric.Etcd{
NewFunc: func() runtime.Object { NewFunc: func() runtime.Object {

View File

@ -37,7 +37,7 @@ func NewREST(s storage.Interface, storageDecorator generic.StorageDecorator) (*R
newListFunc := func() runtime.Object { return &api.ReplicationControllerList{} } newListFunc := func() runtime.Object { return &api.ReplicationControllerList{} }
storageInterface := storageDecorator( storageInterface := storageDecorator(
s, 100, &api.ReplicationController{}, prefix, true, newListFunc) s, 100, &api.ReplicationController{}, prefix, controller.Strategy, newListFunc)
store := &etcdgeneric.Etcd{ store := &etcdgeneric.Etcd{
NewFunc: func() runtime.Object { return &api.ReplicationController{} }, NewFunc: func() runtime.Object { return &api.ReplicationController{} },

View File

@ -39,7 +39,7 @@ func NewREST(s storage.Interface, storageDecorator generic.StorageDecorator) (*R
newListFunc := func() runtime.Object { return &extensions.DaemonSetList{} } newListFunc := func() runtime.Object { return &extensions.DaemonSetList{} }
storageInterface := storageDecorator( storageInterface := storageDecorator(
s, 100, &extensions.DaemonSet{}, prefix, false, newListFunc) s, 100, &extensions.DaemonSet{}, prefix, daemonset.Strategy, newListFunc)
store := &etcdgeneric.Etcd{ store := &etcdgeneric.Etcd{
NewFunc: func() runtime.Object { return &extensions.DaemonSet{} }, NewFunc: func() runtime.Object { return &extensions.DaemonSet{} },

View File

@ -62,7 +62,7 @@ func NewREST(s storage.Interface, storageDecorator generic.StorageDecorator) (*R
newListFunc := func() runtime.Object { return &extensions.DeploymentList{} } newListFunc := func() runtime.Object { return &extensions.DeploymentList{} }
storageInterface := storageDecorator( storageInterface := storageDecorator(
s, 100, &extensions.Deployment{}, prefix, false, newListFunc) s, 100, &extensions.Deployment{}, prefix, deployment.Strategy, newListFunc)
store := &etcdgeneric.Etcd{ store := &etcdgeneric.Etcd{
NewFunc: func() runtime.Object { return &extensions.Deployment{} }, NewFunc: func() runtime.Object { return &extensions.Deployment{} },

View File

@ -37,7 +37,7 @@ func NewREST(s storage.Interface, storageDecorator generic.StorageDecorator) *RE
newListFunc := func() runtime.Object { return &api.EndpointsList{} } newListFunc := func() runtime.Object { return &api.EndpointsList{} }
storageInterface := storageDecorator( storageInterface := storageDecorator(
s, 1000, &api.Endpoints{}, prefix, true, newListFunc) s, 1000, &api.Endpoints{}, prefix, endpoint.Strategy, newListFunc)
store := &etcdgeneric.Etcd{ store := &etcdgeneric.Etcd{
NewFunc: func() runtime.Object { return &api.Endpoints{} }, NewFunc: func() runtime.Object { return &api.Endpoints{} },

View File

@ -17,6 +17,7 @@ limitations under the License.
package etcd package etcd
import ( import (
"k8s.io/kubernetes/pkg/api/rest"
"k8s.io/kubernetes/pkg/runtime" "k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/storage" "k8s.io/kubernetes/pkg/storage"
etcdstorage "k8s.io/kubernetes/pkg/storage/etcd" etcdstorage "k8s.io/kubernetes/pkg/storage/etcd"
@ -28,9 +29,9 @@ func StorageWithCacher(
capacity int, capacity int,
objectType runtime.Object, objectType runtime.Object,
resourcePrefix string, resourcePrefix string,
namespaceScoped bool, scopeStrategy rest.NamespaceScopedStrategy,
newListFunc func() runtime.Object) storage.Interface { newListFunc func() runtime.Object) storage.Interface {
return storage.NewCacher( return storage.NewCacher(
storageInterface, capacity, etcdstorage.APIObjectVersioner{}, storageInterface, capacity, etcdstorage.APIObjectVersioner{},
objectType, resourcePrefix, namespaceScoped, newListFunc) objectType, resourcePrefix, scopeStrategy, newListFunc)
} }

View File

@ -17,6 +17,7 @@ limitations under the License.
package generic package generic
import ( import (
"k8s.io/kubernetes/pkg/api/rest"
"k8s.io/kubernetes/pkg/runtime" "k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/storage" "k8s.io/kubernetes/pkg/storage"
) )
@ -28,7 +29,7 @@ type StorageDecorator func(
capacity int, capacity int,
objectType runtime.Object, objectType runtime.Object,
resourcePrefix string, resourcePrefix string,
namespaceScoped bool, scopeStrategy rest.NamespaceScopedStrategy,
newListFunc func() runtime.Object) storage.Interface newListFunc func() runtime.Object) storage.Interface
// Returns given 'storageInterface' without any decoration. // Returns given 'storageInterface' without any decoration.
@ -37,7 +38,7 @@ func UndecoratedStorage(
capacity int, capacity int,
objectType runtime.Object, objectType runtime.Object,
resourcePrefix string, resourcePrefix string,
namespaceScoped bool, scopeStrategy rest.NamespaceScopedStrategy,
newListFunc func() runtime.Object) storage.Interface { newListFunc func() runtime.Object) storage.Interface {
return storageInterface return storageInterface
} }

View File

@ -38,7 +38,7 @@ func NewREST(s storage.Interface, storageDecorator generic.StorageDecorator) (*R
newListFunc := func() runtime.Object { return &extensions.HorizontalPodAutoscalerList{} } newListFunc := func() runtime.Object { return &extensions.HorizontalPodAutoscalerList{} }
storageInterface := storageDecorator( storageInterface := storageDecorator(
s, 100, &extensions.HorizontalPodAutoscaler{}, prefix, false, newListFunc) s, 100, &extensions.HorizontalPodAutoscaler{}, prefix, horizontalpodautoscaler.Strategy, newListFunc)
store := &etcdgeneric.Etcd{ store := &etcdgeneric.Etcd{
NewFunc: func() runtime.Object { return &extensions.HorizontalPodAutoscaler{} }, NewFunc: func() runtime.Object { return &extensions.HorizontalPodAutoscaler{} },

View File

@ -39,7 +39,7 @@ func NewREST(s storage.Interface, storageDecorator generic.StorageDecorator) (*R
newListFunc := func() runtime.Object { return &extensions.IngressList{} } newListFunc := func() runtime.Object { return &extensions.IngressList{} }
storageInterface := storageDecorator( storageInterface := storageDecorator(
s, 100, &extensions.Ingress{}, prefix, false, newListFunc) s, 100, &extensions.Ingress{}, prefix, ingress.Strategy, newListFunc)
store := &etcdgeneric.Etcd{ store := &etcdgeneric.Etcd{
NewFunc: func() runtime.Object { return &extensions.Ingress{} }, NewFunc: func() runtime.Object { return &extensions.Ingress{} },

View File

@ -39,7 +39,7 @@ func NewREST(s storage.Interface, storageDecorator generic.StorageDecorator) (*R
newListFunc := func() runtime.Object { return &extensions.JobList{} } newListFunc := func() runtime.Object { return &extensions.JobList{} }
storageInterface := storageDecorator( storageInterface := storageDecorator(
s, 100, &extensions.Job{}, prefix, false, newListFunc) s, 100, &extensions.Job{}, prefix, job.Strategy, newListFunc)
store := &etcdgeneric.Etcd{ store := &etcdgeneric.Etcd{
NewFunc: func() runtime.Object { return &extensions.Job{} }, NewFunc: func() runtime.Object { return &extensions.Job{} },

View File

@ -37,7 +37,7 @@ func NewREST(s storage.Interface, storageDecorator generic.StorageDecorator) *RE
newListFunc := func() runtime.Object { return &api.LimitRangeList{} } newListFunc := func() runtime.Object { return &api.LimitRangeList{} }
storageInterface := storageDecorator( storageInterface := storageDecorator(
s, 100, &api.LimitRange{}, prefix, true, newListFunc) s, 100, &api.LimitRange{}, prefix, limitrange.Strategy, newListFunc)
store := &etcdgeneric.Etcd{ store := &etcdgeneric.Etcd{
NewFunc: func() runtime.Object { return &api.LimitRange{} }, NewFunc: func() runtime.Object { return &api.LimitRange{} },

View File

@ -53,7 +53,7 @@ func NewREST(s storage.Interface, storageDecorator generic.StorageDecorator) (*R
newListFunc := func() runtime.Object { return &api.NamespaceList{} } newListFunc := func() runtime.Object { return &api.NamespaceList{} }
storageInterface := storageDecorator( storageInterface := storageDecorator(
s, 100, &api.Namespace{}, prefix, true, newListFunc) s, 100, &api.Namespace{}, prefix, namespace.Strategy, newListFunc)
store := &etcdgeneric.Etcd{ store := &etcdgeneric.Etcd{
NewFunc: func() runtime.Object { return &api.Namespace{} }, NewFunc: func() runtime.Object { return &api.Namespace{} },

View File

@ -57,7 +57,7 @@ func NewREST(s storage.Interface, storageDecorator generic.StorageDecorator, con
newListFunc := func() runtime.Object { return &api.NodeList{} } newListFunc := func() runtime.Object { return &api.NodeList{} }
storageInterface := storageDecorator( storageInterface := storageDecorator(
s, 1000, &api.Node{}, prefix, false, newListFunc) s, 1000, &api.Node{}, prefix, node.Strategy, newListFunc)
store := &etcdgeneric.Etcd{ store := &etcdgeneric.Etcd{
NewFunc: func() runtime.Object { return &api.Node{} }, NewFunc: func() runtime.Object { return &api.Node{} },

View File

@ -37,7 +37,7 @@ func NewREST(s storage.Interface, storageDecorator generic.StorageDecorator) (*R
newListFunc := func() runtime.Object { return &api.PersistentVolumeList{} } newListFunc := func() runtime.Object { return &api.PersistentVolumeList{} }
storageInterface := storageDecorator( storageInterface := storageDecorator(
s, 100, &api.PersistentVolume{}, prefix, true, newListFunc) s, 100, &api.PersistentVolume{}, prefix, persistentvolume.Strategy, newListFunc)
store := &etcdgeneric.Etcd{ store := &etcdgeneric.Etcd{
NewFunc: func() runtime.Object { return &api.PersistentVolume{} }, NewFunc: func() runtime.Object { return &api.PersistentVolume{} },

View File

@ -37,7 +37,7 @@ func NewREST(s storage.Interface, storageDecorator generic.StorageDecorator) (*R
newListFunc := func() runtime.Object { return &api.PersistentVolumeClaimList{} } newListFunc := func() runtime.Object { return &api.PersistentVolumeClaimList{} }
storageInterface := storageDecorator( storageInterface := storageDecorator(
s, 100, &api.PersistentVolumeClaim{}, prefix, true, newListFunc) s, 100, &api.PersistentVolumeClaim{}, prefix, persistentvolumeclaim.Strategy, newListFunc)
store := &etcdgeneric.Etcd{ store := &etcdgeneric.Etcd{
NewFunc: func() runtime.Object { return &api.PersistentVolumeClaim{} }, NewFunc: func() runtime.Object { return &api.PersistentVolumeClaim{} },

View File

@ -67,7 +67,7 @@ func NewStorage(
newListFunc := func() runtime.Object { return &api.PodList{} } newListFunc := func() runtime.Object { return &api.PodList{} }
storageInterface := storageDecorator( storageInterface := storageDecorator(
s, 1000, &api.Pod{}, prefix, true, newListFunc) s, 1000, &api.Pod{}, prefix, pod.Strategy, newListFunc)
store := &etcdgeneric.Etcd{ store := &etcdgeneric.Etcd{
NewFunc: func() runtime.Object { return &api.Pod{} }, NewFunc: func() runtime.Object { return &api.Pod{} },

View File

@ -37,7 +37,7 @@ func NewREST(s storage.Interface, storageDecorator generic.StorageDecorator) *RE
newListFunc := func() runtime.Object { return &api.PodTemplateList{} } newListFunc := func() runtime.Object { return &api.PodTemplateList{} }
storageInterface := storageDecorator( storageInterface := storageDecorator(
s, 100, &api.PodTemplate{}, prefix, false, newListFunc) s, 100, &api.PodTemplate{}, prefix, podtemplate.Strategy, newListFunc)
store := &etcdgeneric.Etcd{ store := &etcdgeneric.Etcd{
NewFunc: func() runtime.Object { return &api.PodTemplate{} }, NewFunc: func() runtime.Object { return &api.PodTemplate{} },

View File

@ -37,7 +37,7 @@ func NewREST(s storage.Interface, storageDecorator generic.StorageDecorator) (*R
newListFunc := func() runtime.Object { return &api.ResourceQuotaList{} } newListFunc := func() runtime.Object { return &api.ResourceQuotaList{} }
storageInterface := storageDecorator( storageInterface := storageDecorator(
s, 100, &api.ResourceQuota{}, prefix, true, newListFunc) s, 100, &api.ResourceQuota{}, prefix, resourcequota.Strategy, newListFunc)
store := &etcdgeneric.Etcd{ store := &etcdgeneric.Etcd{
NewFunc: func() runtime.Object { return &api.ResourceQuota{} }, NewFunc: func() runtime.Object { return &api.ResourceQuota{} },

View File

@ -37,7 +37,7 @@ func NewREST(s storage.Interface, storageDecorator generic.StorageDecorator) *RE
newListFunc := func() runtime.Object { return &api.SecretList{} } newListFunc := func() runtime.Object { return &api.SecretList{} }
storageInterface := storageDecorator( storageInterface := storageDecorator(
s, 100, &api.Secret{}, prefix, true, newListFunc) s, 100, &api.Secret{}, prefix, secret.Strategy, newListFunc)
store := &etcdgeneric.Etcd{ store := &etcdgeneric.Etcd{
NewFunc: func() runtime.Object { return &api.Secret{} }, NewFunc: func() runtime.Object { return &api.Secret{} },

View File

@ -37,7 +37,7 @@ func NewREST(s storage.Interface, storageDecorator generic.StorageDecorator) *RE
newListFunc := func() runtime.Object { return &api.ServiceList{} } newListFunc := func() runtime.Object { return &api.ServiceList{} }
storageInterface := storageDecorator( storageInterface := storageDecorator(
s, 100, &api.Service{}, prefix, false, newListFunc) s, 100, &api.Service{}, prefix, service.Strategy, newListFunc)
store := &etcdgeneric.Etcd{ store := &etcdgeneric.Etcd{
NewFunc: func() runtime.Object { return &api.Service{} }, NewFunc: func() runtime.Object { return &api.Service{} },

View File

@ -37,7 +37,7 @@ func NewREST(s storage.Interface, storageDecorator generic.StorageDecorator) *RE
newListFunc := func() runtime.Object { return &api.ServiceAccountList{} } newListFunc := func() runtime.Object { return &api.ServiceAccountList{} }
storageInterface := storageDecorator( storageInterface := storageDecorator(
s, 100, &api.ServiceAccount{}, prefix, true, newListFunc) s, 100, &api.ServiceAccount{}, prefix, serviceaccount.Strategy, newListFunc)
store := &etcdgeneric.Etcd{ store := &etcdgeneric.Etcd{
NewFunc: func() runtime.Object { return &api.ServiceAccount{} }, NewFunc: func() runtime.Object { return &api.ServiceAccount{} },

View File

@ -26,6 +26,7 @@ import (
"k8s.io/kubernetes/pkg/api" "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/meta" "k8s.io/kubernetes/pkg/api/meta"
"k8s.io/kubernetes/pkg/api/rest"
"k8s.io/kubernetes/pkg/client/cache" "k8s.io/kubernetes/pkg/client/cache"
"k8s.io/kubernetes/pkg/conversion" "k8s.io/kubernetes/pkg/conversion"
"k8s.io/kubernetes/pkg/runtime" "k8s.io/kubernetes/pkg/runtime"
@ -115,7 +116,7 @@ func NewCacher(
versioner Versioner, versioner Versioner,
objectType runtime.Object, objectType runtime.Object,
resourcePrefix string, resourcePrefix string,
namespaceScoped bool, scopeStrategy rest.NamespaceScopedStrategy,
newListFunc func() runtime.Object) Interface { newListFunc func() runtime.Object) Interface {
config := CacherConfig{ config := CacherConfig{
CacheCapacity: capacity, CacheCapacity: capacity,
@ -125,7 +126,7 @@ func NewCacher(
ResourcePrefix: resourcePrefix, ResourcePrefix: resourcePrefix,
NewListFunc: newListFunc, NewListFunc: newListFunc,
} }
if namespaceScoped { if scopeStrategy.NamespaceScoped() {
config.KeyFunc = func(obj runtime.Object) (string, error) { config.KeyFunc = func(obj runtime.Object) (string, error) {
return NamespaceKeyFunc(resourcePrefix, obj) return NamespaceKeyFunc(resourcePrefix, obj)
} }