Refactor REST storage to use generic defaults

Signed-off-by: Monis Khan <mkhan@redhat.com>
This commit is contained in:
Monis Khan
2016-11-30 21:09:56 -05:00
parent 809d259d68
commit a6bafbacbf
145 changed files with 920 additions and 1679 deletions

View File

@@ -22,12 +22,10 @@ go_library(
"//pkg/apis/autoscaling/validation:go_default_library",
"//pkg/apis/meta/v1:go_default_library",
"//pkg/labels:go_default_library",
"//pkg/registry/cachesize:go_default_library",
"//pkg/registry/core/controller:go_default_library",
"//pkg/registry/generic:go_default_library",
"//pkg/registry/generic/registry:go_default_library",
"//pkg/runtime:go_default_library",
"//pkg/storage:go_default_library",
],
)

View File

@@ -28,12 +28,10 @@ import (
"k8s.io/kubernetes/pkg/apis/autoscaling/validation"
metav1 "k8s.io/kubernetes/pkg/apis/meta/v1"
"k8s.io/kubernetes/pkg/labels"
"k8s.io/kubernetes/pkg/registry/cachesize"
"k8s.io/kubernetes/pkg/registry/core/controller"
"k8s.io/kubernetes/pkg/registry/generic"
genericregistry "k8s.io/kubernetes/pkg/registry/generic/registry"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/storage"
)
// ControllerStorage includes dummy storage for Replication Controllers and for Scale subresource.
@@ -43,8 +41,8 @@ type ControllerStorage struct {
Scale *ScaleREST
}
func NewStorage(opts generic.RESTOptions) ControllerStorage {
controllerREST, statusREST := NewREST(opts)
func NewStorage(optsGetter generic.RESTOptionsGetter) ControllerStorage {
controllerREST, statusREST := NewREST(optsGetter)
controllerRegistry := controller.NewRegistry(controllerREST)
return ControllerStorage{
@@ -59,57 +57,25 @@ type REST struct {
}
// NewREST returns a RESTStorage object that will work against replication controllers.
func NewREST(opts generic.RESTOptions) (*REST, *StatusREST) {
prefix := "/" + opts.ResourcePrefix
newListFunc := func() runtime.Object { return &api.ReplicationControllerList{} }
storageInterface, dFunc := opts.Decorator(
opts.StorageConfig,
cachesize.GetWatchCacheSizeByResource(cachesize.Controllers),
&api.ReplicationController{},
prefix,
controller.Strategy,
newListFunc,
controller.GetAttrs,
storage.NoTriggerPublisher,
)
func NewREST(optsGetter generic.RESTOptionsGetter) (*REST, *StatusREST) {
store := &genericregistry.Store{
NewFunc: func() runtime.Object { return &api.ReplicationController{} },
// NewListFunc returns an object capable of storing results of an etcd list.
NewListFunc: newListFunc,
// Produces a path that etcd understands, to the root of the resource
// by combining the namespace in the context with the given prefix
KeyRootFunc: func(ctx api.Context) string {
return genericregistry.NamespaceKeyRootFunc(ctx, prefix)
},
// Produces a path that etcd understands, to the resource by combining
// the namespace in the context with the given prefix
KeyFunc: func(ctx api.Context, name string) (string, error) {
return genericregistry.NamespaceKeyFunc(ctx, prefix, name)
},
// Retrieve the name field of a replication controller
NewFunc: func() runtime.Object { return &api.ReplicationController{} },
NewListFunc: func() runtime.Object { return &api.ReplicationControllerList{} },
ObjectNameFunc: func(obj runtime.Object) (string, error) {
return obj.(*api.ReplicationController).Name, nil
},
// Used to match objects based on labels/fields for list and watch
PredicateFunc: controller.MatchController,
QualifiedResource: api.Resource("replicationcontrollers"),
EnableGarbageCollection: opts.EnableGarbageCollection,
DeleteCollectionWorkers: opts.DeleteCollectionWorkers,
// Used to validate controller creation
CreateStrategy: controller.Strategy,
// Used to validate controller updates
UpdateStrategy: controller.Strategy,
DeleteStrategy: controller.Strategy,
Storage: storageInterface,
DestroyFunc: dFunc,
}
options := &generic.StoreOptions{RESTOptions: optsGetter, AttrFunc: controller.GetAttrs}
if err := store.CompleteWithOptions(options); err != nil {
panic(err) // TODO: Propagate error up
}
statusStore := *store
statusStore.UpdateStrategy = controller.StatusStrategy

View File

@@ -40,7 +40,12 @@ const (
func newStorage(t *testing.T) (ControllerStorage, *etcdtesting.EtcdTestServer) {
etcdStorage, server := registrytest.NewEtcdStorage(t, "")
restOptions := generic.RESTOptions{StorageConfig: etcdStorage, Decorator: generic.UndecoratedStorage, DeleteCollectionWorkers: 1}
restOptions := generic.RESTOptions{
StorageConfig: etcdStorage,
Decorator: generic.UndecoratedStorage,
DeleteCollectionWorkers: 1,
ResourcePrefix: "replicationcontrollers",
}
storage := NewStorage(restOptions)
return storage, server
}