mirror of
https://github.com/kubesphere/kubekey.git
synced 2025-12-26 01:22:51 +00:00
116 lines
3.6 KiB
Go
116 lines
3.6 KiB
Go
/*
|
|
Copyright 2024 The KubeSphere Authors.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package internal
|
|
|
|
import (
|
|
"path/filepath"
|
|
"sync"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
|
"k8s.io/apimachinery/pkg/runtime/serializer/json"
|
|
"k8s.io/apimachinery/pkg/runtime/serializer/versioning"
|
|
apigeneric "k8s.io/apiserver/pkg/registry/generic"
|
|
apistorage "k8s.io/apiserver/pkg/storage"
|
|
cacherstorage "k8s.io/apiserver/pkg/storage/cacher"
|
|
"k8s.io/apiserver/pkg/storage/storagebackend"
|
|
"k8s.io/apiserver/pkg/storage/storagebackend/factory"
|
|
cgtoolscache "k8s.io/client-go/tools/cache"
|
|
|
|
_const "github.com/kubesphere/kubekey/v4/pkg/const"
|
|
)
|
|
|
|
func NewFileRESTOptionsGetter(gv schema.GroupVersion) apigeneric.RESTOptionsGetter {
|
|
return &fileRESTOptionsGetter{
|
|
gv: gv,
|
|
storageConfig: &storagebackend.Config{
|
|
Type: "",
|
|
Prefix: "/",
|
|
Transport: storagebackend.TransportConfig{},
|
|
Codec: newYamlCodec(gv),
|
|
EncodeVersioner: runtime.NewMultiGroupVersioner(gv),
|
|
},
|
|
}
|
|
}
|
|
|
|
func newYamlCodec(gv schema.GroupVersion) runtime.Codec {
|
|
yamlSerializer := json.NewSerializerWithOptions(json.DefaultMetaFactory, _const.Scheme, _const.Scheme, json.SerializerOptions{Yaml: true})
|
|
return versioning.NewDefaultingCodecForScheme(
|
|
_const.Scheme,
|
|
yamlSerializer,
|
|
yamlSerializer,
|
|
gv,
|
|
gv,
|
|
)
|
|
}
|
|
|
|
type fileRESTOptionsGetter struct {
|
|
gv schema.GroupVersion
|
|
storageConfig *storagebackend.Config
|
|
}
|
|
|
|
func (f fileRESTOptionsGetter) GetRESTOptions(resource schema.GroupResource) (apigeneric.RESTOptions, error) {
|
|
prefix := filepath.Join(_const.GetRuntimeDir(), f.gv.Group, f.gv.Version, resource.Resource)
|
|
return apigeneric.RESTOptions{
|
|
StorageConfig: f.storageConfig.ForResource(resource),
|
|
Decorator: func(storageConfig *storagebackend.ConfigForResource, resourcePrefix string,
|
|
keyFunc func(obj runtime.Object) (string, error),
|
|
newFunc func() runtime.Object,
|
|
newListFunc func() runtime.Object,
|
|
getAttrsFunc apistorage.AttrFunc,
|
|
triggerFuncs apistorage.IndexerFuncs,
|
|
indexers *cgtoolscache.Indexers) (apistorage.Interface, factory.DestroyFunc, error) {
|
|
s, d, err := newFileStorage(prefix, resource, storageConfig.Codec, newFunc)
|
|
if err != nil {
|
|
return s, d, err
|
|
}
|
|
|
|
cacherConfig := cacherstorage.Config{
|
|
Storage: s,
|
|
Versioner: apistorage.APIObjectVersioner{},
|
|
GroupResource: storageConfig.GroupResource,
|
|
ResourcePrefix: resourcePrefix,
|
|
KeyFunc: keyFunc,
|
|
NewFunc: newFunc,
|
|
NewListFunc: newListFunc,
|
|
GetAttrsFunc: getAttrsFunc,
|
|
IndexerFuncs: triggerFuncs,
|
|
Indexers: indexers,
|
|
Codec: storageConfig.Codec,
|
|
}
|
|
cacher, err := cacherstorage.NewCacherFromConfig(cacherConfig)
|
|
if err != nil {
|
|
return nil, func() {}, err
|
|
}
|
|
var once sync.Once
|
|
destroyFunc := func() {
|
|
once.Do(func() {
|
|
cacher.Stop()
|
|
d()
|
|
})
|
|
}
|
|
|
|
return cacher, destroyFunc, nil
|
|
},
|
|
EnableGarbageCollection: false,
|
|
DeleteCollectionWorkers: 0,
|
|
ResourcePrefix: prefix,
|
|
CountMetricPollPeriod: 0,
|
|
StorageObjectCountTracker: nil,
|
|
}, nil
|
|
}
|