forked from kubernetes-sigs/apiserver-runtime
/
jsonfile.go
63 lines (60 loc) · 2.14 KB
/
jsonfile.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
// Package filepath provides filepath storage related utilities.
package filepath
import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/apiserver/pkg/registry/generic"
"k8s.io/apiserver/pkg/registry/rest"
"k8s.io/apiserver/pkg/server/storage"
"k8s.io/apiserver/pkg/storage/storagebackend"
"github.com/zachaller/apiserver-runtime/pkg/builder/resource"
builderrest "github.com/zachaller/apiserver-runtime/pkg/builder/rest"
)
// NewJSONFilepathStorageProvider use local host path as persistent layer storage:
//
// - For namespaced-scoped resources: the resource will be written under the root-path in
// the following structure:
//
// -- (root-path) --- /namespace1/ --- resource1
// | |
// | --- resource2
// |
// --- /namespace2/ --- resource3
//
// - For cluster-scoped resources, there will be no mid-layer folders for namespaces:
//
// -- (root-path) --- resource1
// |
// --- resource2
// |
// --- resource3
//
// An example of storing example resource to local filepath will be:
//
// builder.APIServer.
// WithResourceAndHandler(&v1alpha1.ExampleResource{},
// jsonfile.NewJsonFileStorageProvider(&v1alpha1.ExampleResource{}, /*the root file-path*/ "data")).
// Build()
func NewJSONFilepathStorageProvider(obj resource.Object, rootPath string) builderrest.ResourceHandlerProvider {
return func(scheme *runtime.Scheme, getter generic.RESTOptionsGetter) (rest.Storage, error) {
gr := obj.GetGroupVersionResource().GroupResource()
codec, _, err := storage.NewStorageCodec(storage.StorageCodecConfig{
StorageMediaType: runtime.ContentTypeJSON,
StorageSerializer: serializer.NewCodecFactory(scheme),
StorageVersion: scheme.PrioritizedVersionsForGroup(obj.GetGroupVersionResource().Group)[0],
MemoryVersion: scheme.PrioritizedVersionsForGroup(obj.GetGroupVersionResource().Group)[0],
Config: storagebackend.Config{}, // useless fields..
})
if err != nil {
return nil, err
}
return NewFilepathREST(
gr,
codec,
rootPath,
obj.NamespaceScoped(),
obj.New,
obj.NewList,
), nil
}
}