@@ -21,11 +21,15 @@ import (
21
21
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
22
22
extensionsapiserver "k8s.io/apiextensions-apiserver/pkg/apiserver"
23
23
"k8s.io/apimachinery/pkg/runtime"
24
+ "k8s.io/apimachinery/pkg/runtime/schema"
24
25
utilerrors "k8s.io/apimachinery/pkg/util/errors"
25
26
utilnet "k8s.io/apimachinery/pkg/util/net"
26
27
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
28
+ "k8s.io/apiserver/pkg/admission"
27
29
genericapifilters "k8s.io/apiserver/pkg/endpoints/filters"
28
30
genericapiserver "k8s.io/apiserver/pkg/server"
31
+ genericoptions "k8s.io/apiserver/pkg/server/options"
32
+ "k8s.io/apiserver/pkg/storage/storagebackend"
29
33
utilfeature "k8s.io/apiserver/pkg/util/feature"
30
34
"k8s.io/apiserver/pkg/util/notfoundhandler"
31
35
"k8s.io/apiserver/pkg/util/webhook"
@@ -45,6 +49,7 @@ import (
45
49
"github.com/superproj/onex/cmd/onex-apiserver/app/options"
46
50
"github.com/superproj/onex/internal/controlplane"
47
51
controlplaneapiserver "github.com/superproj/onex/internal/controlplane/apiserver"
52
+ "github.com/superproj/onex/internal/controlplane/storage"
48
53
generatedopenapi "github.com/superproj/onex/pkg/generated/openapi"
49
54
"github.com/superproj/onex/pkg/version"
50
55
)
@@ -55,9 +60,80 @@ func init() {
55
60
utilruntime .Must (logsapi .AddFeatureGates (utilfeature .DefaultMutableFeatureGate ))
56
61
}
57
62
63
+ type Option func (* options.ServerRunOptions )
64
+ type RegisterFunc func (plugins * admission.Plugins )
65
+
66
+ // WithLegacyCode returns an Option that sets the external group versions in the ServerRunOptions.
67
+ func WithEtcdOptions (prefix string , versions ... schema.GroupVersion ) Option {
68
+ return func (s * options.ServerRunOptions ) {
69
+ codec := legacyscheme .Codecs .LegacyCodec (versions ... )
70
+ s .RecommendedOptions .Etcd = genericoptions .NewEtcdOptions (storagebackend .NewDefaultConfig (prefix , codec ))
71
+ // Note: DefaultStorageMediaType needs to be reset here.
72
+ s .RecommendedOptions .Etcd .DefaultStorageMediaType = "application/vnd.kubernetes.protobuf"
73
+ /*
74
+ s.RecommendedOptions.Etcd.StorageConfig.EncodeVersioner = runtime.NewMultiGroupVersioner(
75
+ v1beta1.SchemeGroupVersion,
76
+ schema.GroupKind{Group: v1beta1.GroupName},
77
+ )
78
+ */
79
+ for _ , version := range versions {
80
+ controlplane .AddStableAPIGroupVersionsEnabledByDefault (version )
81
+ }
82
+ }
83
+ }
84
+
85
+ // WithAdmissionPlugin returns an Option function that adds an admission plugin to the recommended plugin order list
86
+ // and registers the plugin using the provided RegisterFunc in the ServerRunOptions.
87
+ func WithAdmissionPlugin (name string , registerFunc RegisterFunc ) Option {
88
+ return func (s * options.ServerRunOptions ) {
89
+ // Note: Need to add this to the RecommendedPluginOrder list.
90
+ s .RecommendedOptions .Admission .RecommendedPluginOrder = append (s .RecommendedOptions .Admission .RecommendedPluginOrder , name )
91
+ registerFunc (s .RecommendedOptions .Admission .Plugins )
92
+ }
93
+ }
94
+
95
+ // WithAdmissionInitializers returns an Option function that sets the external admission initializers in the ServerRunOptions.
96
+ func WithAdmissionInitializers (initializer func (c * genericapiserver.RecommendedConfig ) ([]admission.PluginInitializer , error )) Option {
97
+ return func (s * options.ServerRunOptions ) {
98
+ s .RecommendedOptions .ExternalAdmissionInitializers = initializer
99
+ }
100
+ }
101
+
102
+ // WithPostStartHook returns an Option function that sets the external post-start hook with the given name in the ServerRunOptions.
103
+ func WithPostStartHook (name string , hook genericapiserver.PostStartHookFunc ) Option {
104
+ return func (s * options.ServerRunOptions ) {
105
+ s .ExternalPostStartHooks [name ] = hook
106
+ }
107
+ }
108
+
109
+ // WithSharedInformerFactory returns an Option function that sets the external SharedInformerFactory in the ServerRunOptions.
110
+ func WithSharedInformerFactory (informers controlplane.ExternalSharedInformerFactory ) Option {
111
+ return func (s * options.ServerRunOptions ) {
112
+ s .ExternalVersionedInformers = informers
113
+ }
114
+ }
115
+
116
+ // WithRESTStorageProviders returns an Option function that sets the external REST storage providers in the ServerRunOptions.
117
+ func WithRESTStorageProviders (providers ... storage.RESTStorageProvider ) Option {
118
+ return func (s * options.ServerRunOptions ) {
119
+ s .ExternalRESTStorageProviders = providers
120
+ }
121
+ }
122
+
123
+ // WithAlternateDNS returns an Option function that sets the alternate DNS configurations in the ServerRunOptions.
124
+ func WithAlternateDNS (dns ... string ) Option {
125
+ return func (s * options.ServerRunOptions ) {
126
+ s .AlternateDNS = dns
127
+ }
128
+ }
129
+
58
130
// NewAPIServerCommand creates a *cobra.Command object with default parameters.
59
- func NewAPIServerCommand () * cobra.Command {
131
+ func NewAPIServerCommand (serverRunOptions ... Option ) * cobra.Command {
60
132
s := options .NewServerRunOptions ()
133
+ for _ , opt := range serverRunOptions {
134
+ opt (s )
135
+ }
136
+
61
137
cmd := & cobra.Command {
62
138
Use : appName ,
63
139
Short : "Launch a onex API server" ,
@@ -198,7 +274,7 @@ func CreateOneXAPIServerConfig(opts options.CompletedOptions) (
198
274
) {
199
275
proxyTransport := CreateProxyTransport ()
200
276
201
- genericConfig , _ , kubeVersionedInformers , storageFactory , err := controlplaneapiserver .BuildGenericConfig (
277
+ genericConfig , _ , kubeSharedInformers , storageFactory , err := controlplaneapiserver .BuildGenericConfig (
202
278
opts .CompletedOptions ,
203
279
[]* runtime.Scheme {legacyscheme .Scheme , extensionsapiserver .Scheme , aggregatorscheme .Scheme },
204
280
generatedopenapi .GetOpenAPIDefinitions ,
@@ -217,10 +293,15 @@ func CreateOneXAPIServerConfig(opts options.CompletedOptions) (
217
293
EventTTL : opts .EventTTL ,
218
294
EnableLogsSupport : opts .EnableLogsHandler ,
219
295
ProxyTransport : proxyTransport ,
220
- MasterCount : opts .MasterCount ,
221
- VersionedInformers : opts .SharedInformerFactory ,
296
+ //ExternalGroupResources: opts.ExternalGroupResources,
297
+ ExternalRESTStorageProviders : opts .ExternalRESTStorageProviders ,
298
+ MasterCount : opts .MasterCount ,
299
+ //VersionedInformers: opts.SharedInformerFactory,
222
300
// Here we will use the config file of "onex" to create a client-go informers.
223
- KubeVersionedInformers : kubeVersionedInformers ,
301
+ KubeVersionedInformers : kubeSharedInformers ,
302
+ InternalVersionedInformers : opts .InternalVersionedInformers ,
303
+ ExternalVersionedInformers : opts .ExternalVersionedInformers ,
304
+ ExternalPostStartHooks : opts .ExternalPostStartHooks ,
224
305
},
225
306
}
226
307
@@ -232,7 +313,7 @@ func CreateOneXAPIServerConfig(opts options.CompletedOptions) (
232
313
// build peer proxy config only if peer ca file exists
233
314
if opts .PeerCAFile != "" {
234
315
config .ExtraConfig .PeerProxy , err = controlplaneapiserver .BuildPeerProxy (
235
- kubeVersionedInformers ,
316
+ kubeSharedInformers ,
236
317
genericConfig .StorageVersionManager ,
237
318
opts .ProxyClientCertFile ,
238
319
opts .ProxyClientKeyFile ,
@@ -268,7 +349,7 @@ func CreateOneXAPIServerConfig(opts options.CompletedOptions) (
268
349
}
269
350
*/
270
351
271
- serviceResolver := buildServiceResolver (opts .EnableAggregatorRouting , genericConfig .LoopbackClientConfig .Host , kubeVersionedInformers )
352
+ serviceResolver := buildServiceResolver (opts .EnableAggregatorRouting , genericConfig .LoopbackClientConfig .Host , kubeSharedInformers )
272
353
273
354
return config , serviceResolver , nil
274
355
}
0 commit comments