Skip to content

Commit 13af8f2

Browse files
authored
Merge pull request kubernetes#80160 from wojtek-t/propagate_error_from_cacher_creation
Propagate error from creating cacher and storage decorators up
2 parents ab8506f + ee13be2 commit 13af8f2

File tree

12 files changed

+129
-51
lines changed

12 files changed

+129
-51
lines changed

pkg/registry/core/pod/rest/log_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ import (
3030
func TestPodLogValidates(t *testing.T) {
3131
config, server := registrytest.NewEtcdStorage(t, "")
3232
defer server.Terminate(t)
33-
s, destroyFunc := generic.NewRawStorage(config)
33+
s, destroyFunc, err := generic.NewRawStorage(config)
34+
if err != nil {
35+
t.Fatalf("Unexpected error: %v", err)
36+
}
3437
defer destroyFunc()
3538
store := &genericregistry.Store{
3639
Storage: genericregistry.DryRunnableStorage{Storage: s},
@@ -46,7 +49,7 @@ func TestPodLogValidates(t *testing.T) {
4649
for _, tc := range testCases {
4750
_, err := logRest.Get(genericapirequest.NewDefaultContext(), "test", tc)
4851
if !errors.IsInvalid(err) {
49-
t.Fatalf("unexpected error: %v", err)
52+
t.Fatalf("Unexpected error: %v", err)
5053
}
5154
}
5255
}

pkg/registry/core/service/allocator/storage/storage.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ var _ rangeallocation.RangeRegistry = &Etcd{}
6161
// NewEtcd returns an allocator that is backed by Etcd and can manage
6262
// persisting the snapshot state of allocation after each allocation is made.
6363
func NewEtcd(alloc allocator.Snapshottable, baseKey string, resource schema.GroupResource, config *storagebackend.Config) *Etcd {
64-
storage, d := generic.NewRawStorage(config)
64+
storage, d, err := generic.NewRawStorage(config)
65+
if err != nil {
66+
panic(err) // TODO: Propagate error up
67+
}
6568

6669
// TODO : Remove RegisterStorageCleanup below when PR
6770
// https://github.com/kubernetes/kubernetes/pull/50690

pkg/registry/core/service/ipallocator/storage/storage_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ func newStorage(t *testing.T) (*etcd3testing.EtcdTestServer, ipallocator.Interfa
4747
etcd := allocatorstore.NewEtcd(mem, "/ranges/serviceips", api.Resource("serviceipallocations"), etcdStorage)
4848
return etcd
4949
})
50-
s, d := generic.NewRawStorage(etcdStorage)
50+
s, d, err := generic.NewRawStorage(etcdStorage)
51+
if err != nil {
52+
t.Fatalf("Couldn't create storage: %v", err)
53+
}
5154
destroyFunc := func() {
5255
d()
5356
server.Terminate(t)

pkg/registry/extensions/controller/storage/storage_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ import (
3434
func newStorage(t *testing.T) (*ScaleREST, *etcd3testing.EtcdTestServer, storage.Interface, factory.DestroyFunc) {
3535
etcdStorage, server := registrytest.NewEtcdStorage(t, "")
3636
restOptions := generic.RESTOptions{StorageConfig: etcdStorage, Decorator: generic.UndecoratedStorage, DeleteCollectionWorkers: 1, ResourcePrefix: "controllers"}
37-
s, d := generic.NewRawStorage(etcdStorage)
37+
s, d, err := generic.NewRawStorage(etcdStorage)
38+
if err != nil {
39+
t.Fatalf("Couldn't create storage: %v", err)
40+
}
3841
destroyFunc := func() {
3942
d()
4043
server.Terminate(t)

staging/src/k8s.io/apiserver/pkg/registry/generic/BUILD

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ go_library(
2323
"//staging/src/k8s.io/apiserver/pkg/storage:go_default_library",
2424
"//staging/src/k8s.io/apiserver/pkg/storage/storagebackend:go_default_library",
2525
"//staging/src/k8s.io/apiserver/pkg/storage/storagebackend/factory:go_default_library",
26-
"//vendor/k8s.io/klog:go_default_library",
2726
],
2827
)
2928

staging/src/k8s.io/apiserver/pkg/registry/generic/registry/storage_factory.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,15 @@ func StorageWithCacher(capacity int) generic.StorageDecorator {
3939
newFunc func() runtime.Object,
4040
newListFunc func() runtime.Object,
4141
getAttrsFunc storage.AttrFunc,
42-
triggerFunc storage.TriggerPublisherFunc) (storage.Interface, factory.DestroyFunc) {
42+
triggerFunc storage.TriggerPublisherFunc) (storage.Interface, factory.DestroyFunc, error) {
4343

44-
s, d := generic.NewRawStorage(storageConfig)
44+
s, d, err := generic.NewRawStorage(storageConfig)
45+
if err != nil {
46+
return s, d, err
47+
}
4548
if capacity <= 0 {
4649
klog.V(5).Infof("Storage caching is disabled for %T", newFunc())
47-
return s, d
50+
return s, d, nil
4851
}
4952
if klog.V(5) {
5053
klog.Infof("Storage caching is enabled for %T with capacity %v", newFunc(), capacity)
@@ -64,7 +67,10 @@ func StorageWithCacher(capacity int) generic.StorageDecorator {
6467
TriggerPublisherFunc: triggerFunc,
6568
Codec: storageConfig.Codec,
6669
}
67-
cacher := cacherstorage.NewCacherFromConfig(cacherConfig)
70+
cacher, err := cacherstorage.NewCacherFromConfig(cacherConfig)
71+
if err != nil {
72+
return nil, func() {}, err
73+
}
6874
destroyFunc := func() {
6975
cacher.Stop()
7076
d()
@@ -75,7 +81,7 @@ func StorageWithCacher(capacity int) generic.StorageDecorator {
7581
// merges as that shuts down storage properly
7682
RegisterStorageCleanup(destroyFunc)
7783

78-
return cacher, destroyFunc
84+
return cacher, destroyFunc, nil
7985
}
8086
}
8187

staging/src/k8s.io/apiserver/pkg/registry/generic/registry/store.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1310,7 +1310,8 @@ func (e *Store) CompleteWithOptions(options *generic.StoreOptions) error {
13101310

13111311
if e.Storage.Storage == nil {
13121312
e.Storage.Codec = opts.StorageConfig.Codec
1313-
e.Storage.Storage, e.DestroyFunc = opts.Decorator(
1313+
var err error
1314+
e.Storage.Storage, e.DestroyFunc, err = opts.Decorator(
13141315
opts.StorageConfig,
13151316
prefix,
13161317
keyFunc,
@@ -1319,6 +1320,9 @@ func (e *Store) CompleteWithOptions(options *generic.StoreOptions) error {
13191320
attrFunc,
13201321
triggerFunc,
13211322
)
1323+
if err != nil {
1324+
return err
1325+
}
13221326
e.StorageVersioner = opts.StorageConfig.EncodeVersioner
13231327

13241328
if opts.CountMetricPollPeriod > 0 {

staging/src/k8s.io/apiserver/pkg/registry/generic/registry/store_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1561,7 +1561,10 @@ func newTestGenericStoreRegistry(t *testing.T, scheme *runtime.Scheme, hasCacheE
15611561
NewListFunc: func() runtime.Object { return &example.PodList{} },
15621562
Codec: sc.Codec,
15631563
}
1564-
cacher := cacherstorage.NewCacherFromConfig(config)
1564+
cacher, err := cacherstorage.NewCacherFromConfig(config)
1565+
if err != nil {
1566+
t.Fatalf("Couldn't create cacher: %v", err)
1567+
}
15651568
d := destroyFunc
15661569
s = cacher
15671570
destroyFunc = func() {

staging/src/k8s.io/apiserver/pkg/registry/generic/storage_decorator.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"k8s.io/apiserver/pkg/storage"
2222
"k8s.io/apiserver/pkg/storage/storagebackend"
2323
"k8s.io/apiserver/pkg/storage/storagebackend/factory"
24-
"k8s.io/klog"
2524
)
2625

2726
// StorageDecorator is a function signature for producing a storage.Interface
@@ -33,7 +32,7 @@ type StorageDecorator func(
3332
newFunc func() runtime.Object,
3433
newListFunc func() runtime.Object,
3534
getAttrsFunc storage.AttrFunc,
36-
trigger storage.TriggerPublisherFunc) (storage.Interface, factory.DestroyFunc)
35+
trigger storage.TriggerPublisherFunc) (storage.Interface, factory.DestroyFunc, error)
3736

3837
// UndecoratedStorage returns the given a new storage from the given config
3938
// without any decoration.
@@ -44,17 +43,13 @@ func UndecoratedStorage(
4443
newFunc func() runtime.Object,
4544
newListFunc func() runtime.Object,
4645
getAttrsFunc storage.AttrFunc,
47-
trigger storage.TriggerPublisherFunc) (storage.Interface, factory.DestroyFunc) {
46+
trigger storage.TriggerPublisherFunc) (storage.Interface, factory.DestroyFunc, error) {
4847
return NewRawStorage(config)
4948
}
5049

5150
// NewRawStorage creates the low level kv storage. This is a work-around for current
5251
// two layer of same storage interface.
5352
// TODO: Once cacher is enabled on all registries (event registry is special), we will remove this method.
54-
func NewRawStorage(config *storagebackend.Config) (storage.Interface, factory.DestroyFunc) {
55-
s, d, err := factory.Create(*config)
56-
if err != nil {
57-
klog.Fatalf("Unable to create storage backend: config (%v), err (%v)", config, err)
58-
}
59-
return s, d
53+
func NewRawStorage(config *storagebackend.Config) (storage.Interface, factory.DestroyFunc, error) {
54+
return factory.Create(*config)
6055
}

staging/src/k8s.io/apiserver/pkg/storage/cacher/cacher.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,13 +291,13 @@ type Cacher struct {
291291
// NewCacherFromConfig creates a new Cacher responsible for servicing WATCH and LIST requests from
292292
// its internal cache and updating its cache in the background based on the
293293
// given configuration.
294-
func NewCacherFromConfig(config Config) *Cacher {
294+
func NewCacherFromConfig(config Config) (*Cacher, error) {
295295
stopCh := make(chan struct{})
296296
obj := config.NewFunc()
297297
// Give this error when it is constructed rather than when you get the
298298
// first watch item, because it's much easier to track down that way.
299299
if err := runtime.CheckCodec(config.Codec, obj); err != nil {
300-
panic("storage codec doesn't seem to match given type: " + err.Error())
300+
return nil, fmt.Errorf("storage codec doesn't seem to match given type: %v", err)
301301
}
302302

303303
clock := clock.RealClock{}
@@ -363,7 +363,7 @@ func NewCacherFromConfig(config Config) *Cacher {
363363
)
364364
}()
365365

366-
return cacher
366+
return cacher, nil
367367
}
368368

369369
func (c *Cacher) startCaching(stopChannel <-chan struct{}) {

0 commit comments

Comments
 (0)