This repository has been archived by the owner on Jul 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
helpers.go
84 lines (75 loc) · 2.68 KB
/
helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package restoptions
import (
"fmt"
"strings"
kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/registry/generic/registry"
"k8s.io/kubernetes/pkg/storage"
)
// DefaultKeyFunctions sets the default behavior for storage key generation onto a Store.
func DefaultKeyFunctions(store *registry.Store, prefix string, isNamespaced bool) {
if isNamespaced {
if store.KeyRootFunc == nil {
store.KeyRootFunc = func(ctx kapi.Context) string {
return registry.NamespaceKeyRootFunc(ctx, prefix)
}
}
if store.KeyFunc == nil {
store.KeyFunc = func(ctx kapi.Context, name string) (string, error) {
return registry.NamespaceKeyFunc(ctx, prefix, name)
}
}
} else {
if store.KeyRootFunc == nil {
store.KeyRootFunc = func(ctx kapi.Context) string {
return prefix
}
}
if store.KeyFunc == nil {
store.KeyFunc = func(ctx kapi.Context, name string) (string, error) {
return registry.NoNamespaceKeyFunc(ctx, prefix, name)
}
}
}
}
// ApplyOptions updates the given generic storage from the provided rest options
// TODO: remove need for etcdPrefix once Decorator interface is refactored upstream
func ApplyOptions(optsGetter Getter, store *registry.Store, oldIsNamespaced bool, triggerFn storage.TriggerPublisherFunc) error {
if store.QualifiedResource.Empty() {
return fmt.Errorf("store must have a non-empty qualified resource")
}
if store.NewFunc == nil {
return fmt.Errorf("store for %s must have NewFunc set", store.QualifiedResource.String())
}
if store.NewListFunc == nil {
return fmt.Errorf("store for %s must have NewListFunc set", store.QualifiedResource.String())
}
if store.CreateStrategy == nil {
return fmt.Errorf("store for %s must have CreateStrategy set", store.QualifiedResource.String())
}
isNamespaced := store.CreateStrategy.NamespaceScoped()
if isNamespaced != oldIsNamespaced { // TODO(soltysh): oldIsNamespaced should be completely removed in #12541
return fmt.Errorf("CreateStrategy has %v for namespace scope but user specified %v as namespace scope", isNamespaced, oldIsNamespaced)
}
opts, err := optsGetter.GetRESTOptions(store.QualifiedResource)
if err != nil {
return fmt.Errorf("error building RESTOptions for %s store: %v", store.QualifiedResource.String(), err)
}
// Resource prefix must come from the underlying factory
prefix := opts.ResourcePrefix
if !strings.HasPrefix(prefix, "/") {
prefix = "/" + prefix
}
DefaultKeyFunctions(store, prefix, isNamespaced)
store.DeleteCollectionWorkers = opts.DeleteCollectionWorkers
store.Storage, store.DestroyFunc = opts.Decorator(
opts.StorageConfig,
UseConfiguredCacheSize,
store.NewFunc(),
prefix,
store.CreateStrategy,
store.NewListFunc,
triggerFn,
)
return nil
}