/
sqlite_options.go
108 lines (92 loc) · 3.5 KB
/
sqlite_options.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package options
import (
"fmt"
"github.com/spf13/pflag"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apiserver/pkg/registry/generic"
"k8s.io/apiserver/pkg/server"
serverstorage "k8s.io/apiserver/pkg/server/storage"
"k8s.io/apiserver/pkg/storage/storagebackend"
)
//SqliteOptions sqlite as a backend
type SqliteOptions struct {
StorageConfig storagebackend.Config
DefaultStorageMediaType string
}
//NewSqliteOptions create mysql options
func NewSqliteOptions(backendConfig *storagebackend.Config) *SqliteOptions {
sqlite := &SqliteOptions{
StorageConfig: *backendConfig,
DefaultStorageMediaType: "application/json",
}
sqlite.StorageConfig.Type = storagebackend.StorageTypeSqlite
return sqlite
}
//Validate validate mysql input options
func (s *SqliteOptions) Validate() []error {
allErrors := []error{}
if len(s.StorageConfig.Sqlite.DSN) == 0 {
allErrors = append(allErrors, fmt.Errorf("--sqlite-dsn must be specified"))
}
return allErrors
}
// AddFlags adds flags related to mysql storage for a specific APIServer to the specified FlagSet
// you must set storage-backend flag with mysql.
func (s *SqliteOptions) AddFlags(fs *pflag.FlagSet) {
fs.StringVar(&s.StorageConfig.Sqlite.DSN, "sqlite-dsn", s.StorageConfig.Sqlite.DSN, ""+
"specify server to connented backend.eg:./test.db?cache=share&mode=memory, comma separated.")
fs.BoolVar(&s.StorageConfig.Sqlite.Debug, "sqlite-debug", s.StorageConfig.Sqlite.Debug, ""+
"enable sqlite debug mode.")
fs.IntVar(&s.StorageConfig.Sqlite.ListDefaultLimit, "sqlite-default-limit", s.StorageConfig.Sqlite.ListDefaultLimit, ""+
"the default limit for sqlite query.")
}
//ApplyTo apply to server
func (s *SqliteOptions) ApplyTo(c *server.Config) error {
if s == nil {
return nil
}
c.RESTOptionsGetter = &SqliteSimpleRestOptionsFactory{Options: *s}
return nil
}
//ApplyWithStorageFactoryTo apply to storage factory
func (s *SqliteOptions) ApplyWithStorageFactoryTo(factory serverstorage.StorageFactory, c *server.Config) error {
c.RESTOptionsGetter = &sqliteStorageFactoryRestOptionsFactory{Options: *s, StorageFactory: factory}
return nil
}
//SqliteSimpleRestOptionsFactory simple rest options factory
type SqliteSimpleRestOptionsFactory struct {
Options SqliteOptions
}
//GetRESTOptions impl generic.RESTOptions
func (f *SqliteSimpleRestOptionsFactory) GetRESTOptions(resource schema.GroupResource) (generic.RESTOptions, error) {
ret := generic.RESTOptions{
StorageConfig: &storagebackend.ConfigForResource{
Config: f.Options.StorageConfig,
},
Decorator: generic.UndecoratedStorage,
EnableGarbageCollection: false,
DeleteCollectionWorkers: 0,
ResourcePrefix: resource.Group + "/" + resource.Resource,
CountMetricPollPeriod: 0,
}
return ret, nil
}
type sqliteStorageFactoryRestOptionsFactory struct {
Options SqliteOptions
StorageFactory serverstorage.StorageFactory
}
func (f *sqliteStorageFactoryRestOptionsFactory) GetRESTOptions(resource schema.GroupResource) (generic.RESTOptions, error) {
storageConfig, err := f.StorageFactory.NewConfig(resource)
if err != nil {
return generic.RESTOptions{}, fmt.Errorf("unable to find storage destination for %v, due to %v", resource, err.Error())
}
ret := generic.RESTOptions{
StorageConfig: storageConfig,
Decorator: generic.UndecoratedStorage,
DeleteCollectionWorkers: 0,
EnableGarbageCollection: false,
ResourcePrefix: f.StorageFactory.ResourcePrefix(resource),
CountMetricPollPeriod: 0,
}
return ret, nil
}