Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add pkg/runtime/resourceconfig.ResourceConfig #678

Merged
merged 2 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions pkg/apiserver/registry/clusterpedia/collectionresources/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
internal "github.com/clusterpedia-io/api/clusterpedia"
"github.com/clusterpedia-io/api/clusterpedia/scheme"
"github.com/clusterpedia-io/api/clusterpedia/v1beta1"
resourceconfigfactory "github.com/clusterpedia-io/clusterpedia/pkg/runtime/resourceconfig/factory"
"github.com/clusterpedia-io/clusterpedia/pkg/storage"
"github.com/clusterpedia-io/clusterpedia/pkg/storageconfig"
"github.com/clusterpedia-io/clusterpedia/pkg/utils"
"github.com/clusterpedia-io/clusterpedia/pkg/utils/negotiation"
"github.com/clusterpedia-io/clusterpedia/pkg/utils/request"
Expand All @@ -48,7 +48,7 @@ func NewREST(serializer runtime.NegotiatedSerializer, factory storage.StorageFac

list := &internal.CollectionResourceList{}
storages := make(map[string]storage.CollectionResourceStorage, len(crs))
configFactory := storageconfig.NewStorageConfigFactory()
configFactory := resourceconfigfactory.New()
for _, cr := range crs {
for irt := range cr.ResourceTypes {
rt := &cr.ResourceTypes[irt]
Expand All @@ -59,9 +59,9 @@ func NewREST(serializer runtime.NegotiatedSerializer, factory storage.StorageFac
}

*rt = internal.CollectionResourceType{
Group: config.StorageGroupResource.Group,
Version: config.StorageVersion.Version,
Resource: config.StorageGroupResource.Resource,
Group: config.StorageResource.Group,
Version: config.StorageResource.Version,
Resource: config.StorageResource.Resource,
}
}
}
Expand Down
17 changes: 8 additions & 9 deletions pkg/kube_state_metrics/metrics_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@ import (
"k8s.io/kube-state-metrics/v2/pkg/optin"
"k8s.io/kube-state-metrics/v2/pkg/options"

resourceconfigfactory "github.com/clusterpedia-io/clusterpedia/pkg/runtime/resourceconfig/factory"
"github.com/clusterpedia-io/clusterpedia/pkg/runtime/scheme"
"github.com/clusterpedia-io/clusterpedia/pkg/storageconfig"
)

var (
storageConfigFactory = storageconfig.NewStorageConfigFactory()
hubGVRs = make(map[schema.GroupVersionResource]schema.GroupVersionResource)
resourceConfigFactory = resourceconfigfactory.New()
hubGVRs = make(map[schema.GroupVersionResource]schema.GroupVersionResource)
)

func init() {
for gvr := range generators {
config, err := storageConfigFactory.NewLegacyResourceConfig(gvr.GroupResource(), false)
memory, err := resourceConfigFactory.MemoryResource(gvr)
if err != nil {
panic(err)
}
hubGVRs[config.StorageGroupResource.WithVersion(config.MemoryVersion.Version)] = gvr
hubGVRs[memory] = gvr
}
}

Expand Down Expand Up @@ -101,11 +101,10 @@ func (builder *MetricsStoreBuilder) GetMetricStore(cluster string, resource sche
return nil
}

config, err := storageConfigFactory.NewLegacyResourceConfig(resource.GroupResource(), false)
hub, err := resourceConfigFactory.MemoryResource(resource)
if err != nil {
return nil
}
hub := config.StorageGroupResource.WithVersion(config.MemoryVersion.Version)
metricsGVR, ok := hubGVRs[hub]
if !ok {
return nil
Expand Down Expand Up @@ -134,11 +133,11 @@ func (builder *MetricsStoreBuilder) GetMetricStore(cluster string, resource sche
return obj, nil
}

hobj, err := scheme.LegacyResourceScheme.ConvertToVersion(obj.(runtime.Object), config.MemoryVersion)
hobj, err := scheme.LegacyResourceScheme.ConvertToVersion(obj.(runtime.Object), hub.GroupVersion())
if err != nil {
return nil, err
}
if metricsGVR.GroupVersion() == config.MemoryVersion {
if metricsGVR.GroupVersion() == hub.GroupVersion() {
return hobj, nil
}
return scheme.LegacyResourceScheme.ConvertToVersion(hobj, metricsGVR.GroupVersion())
Expand Down
24 changes: 13 additions & 11 deletions pkg/kubeapiserver/restmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ import (
"github.com/clusterpedia-io/clusterpedia/pkg/kubeapiserver/discovery"
"github.com/clusterpedia-io/clusterpedia/pkg/kubeapiserver/printers"
"github.com/clusterpedia-io/clusterpedia/pkg/kubeapiserver/resourcerest"
resourceconfigfactory "github.com/clusterpedia-io/clusterpedia/pkg/runtime/resourceconfig/factory"
"github.com/clusterpedia-io/clusterpedia/pkg/runtime/scheme"
unstructuredscheme "github.com/clusterpedia-io/clusterpedia/pkg/runtime/scheme/unstructured"
"github.com/clusterpedia-io/clusterpedia/pkg/storage"
"github.com/clusterpedia-io/clusterpedia/pkg/storageconfig"
)

type RESTManager struct {
serializer runtime.NegotiatedSerializer
storageFactory storage.StorageFactory
resourcetSorageConfig *storageconfig.StorageConfigFactory
resourceConfigFactory *resourceconfigfactory.ResourceConfigFactory
equivalentResourceRegistry runtime.EquivalentResourceMapper

lock sync.Mutex
Expand Down Expand Up @@ -87,7 +87,7 @@ func NewRESTManager(serializer runtime.NegotiatedSerializer, storageMediaType st
manager := &RESTManager{
serializer: serializer,
storageFactory: storageFactory,
resourcetSorageConfig: storageconfig.NewStorageConfigFactory(),
resourceConfigFactory: resourceconfigfactory.New(),
equivalentResourceRegistry: runtime.NewEquivalentResourceRegistry(),
requestVerbs: requestVerbs,
}
Expand Down Expand Up @@ -270,12 +270,13 @@ func (m *RESTManager) addRESTResourceInfosLocked(addedInfos map[schema.GroupVers
}

func (m *RESTManager) genLegacyResourceRESTStorage(gvr schema.GroupVersionResource, kind string, namespaced bool) (*resourcerest.RESTStorage, error) {
storageConfig, err := m.resourcetSorageConfig.NewLegacyResourceConfig(gvr.GroupResource(), namespaced)
resourceConfig, err := m.resourceConfigFactory.NewLegacyResourceConfig(gvr.GroupResource(), namespaced)
if err != nil {
return nil, err
}

resourceStorage, err := m.storageFactory.NewResourceStorage(storageConfig)
config := &storage.ResourceStorageConfig{ResourceConfig: *resourceConfig}
resourceStorage, err := m.storageFactory.NewResourceStorage(config)
if err != nil {
return nil, err
}
Expand All @@ -284,11 +285,11 @@ func (m *RESTManager) genLegacyResourceRESTStorage(gvr schema.GroupVersionResour
DefaultQualifiedResource: gvr.GroupResource(),

NewFunc: func() runtime.Object {
obj, _ := scheme.LegacyResourceScheme.New(storageConfig.MemoryVersion.WithKind(kind))
obj, _ := scheme.LegacyResourceScheme.New(resourceConfig.MemoryResource.GroupVersion().WithKind(kind))
return obj
},
NewListFunc: func() runtime.Object {
obj, _ := scheme.LegacyResourceScheme.New(storageConfig.MemoryVersion.WithKind(kind + "List"))
obj, _ := scheme.LegacyResourceScheme.New(resourceConfig.MemoryResource.GroupVersion().WithKind(kind + "List"))
return obj
},

Expand All @@ -297,25 +298,26 @@ func (m *RESTManager) genLegacyResourceRESTStorage(gvr schema.GroupVersionResour
}

func (m *RESTManager) genUnstructuredRESTStorage(gvr schema.GroupVersionResource, kind string, namespaced bool) (*resourcerest.RESTStorage, error) {
storageConfig, err := m.resourcetSorageConfig.NewUnstructuredConfig(gvr, namespaced)
resourceConfig, err := m.resourceConfigFactory.NewUnstructuredConfig(gvr, namespaced)
if err != nil {
return nil, err
}

resourceStorage, err := m.storageFactory.NewResourceStorage(storageConfig)
config := &storage.ResourceStorageConfig{ResourceConfig: *resourceConfig}
resourceStorage, err := m.storageFactory.NewResourceStorage(config)
if err != nil {
return nil, err
}

return &resourcerest.RESTStorage{
NewFunc: func() runtime.Object {
obj := &unstructured.Unstructured{}
obj.SetGroupVersionKind(storageConfig.MemoryVersion.WithKind(kind))
obj.SetGroupVersionKind(resourceConfig.MemoryResource.GroupVersion().WithKind(kind))
return obj
},
NewListFunc: func() runtime.Object {
obj := &unstructured.UnstructuredList{}
obj.SetGroupVersionKind(storageConfig.MemoryVersion.WithKind(kind + "List"))
obj.SetGroupVersionKind(resourceConfig.MemoryResource.GroupVersion().WithKind(kind + "List"))
return obj
},

Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
package storageconfig
package factory

import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer/versioning"
"k8s.io/apiserver/pkg/server/resourceconfig"
serverresourceconfig "k8s.io/apiserver/pkg/server/resourceconfig"
serverstorage "k8s.io/apiserver/pkg/server/storage"
apisstorage "k8s.io/kubernetes/pkg/apis/storage"

"github.com/clusterpedia-io/clusterpedia/pkg/runtime/resourceconfig"
"github.com/clusterpedia-io/clusterpedia/pkg/runtime/scheme"
"github.com/clusterpedia-io/clusterpedia/pkg/storage"
)

type StorageConfigFactory struct {
type ResourceConfigFactory struct {
legacyResourceEncodingConfig serverstorage.ResourceEncodingConfig

cohabitatingResources map[schema.GroupResource][]schema.GroupResource
}

func NewStorageConfigFactory() *StorageConfigFactory {
func New() *ResourceConfigFactory {
resources := []schema.GroupVersionResource{
apisstorage.Resource("csistoragecapacities").WithVersion("v1beta1"),
}

resourceEncodingConfig := serverstorage.NewDefaultResourceEncodingConfig(scheme.LegacyResourceScheme)
resourceEncodingConfig = resourceconfig.MergeResourceEncodingConfigs(resourceEncodingConfig, resources)
resourceEncodingConfig = serverresourceconfig.MergeResourceEncodingConfigs(resourceEncodingConfig, resources)

factory := &StorageConfigFactory{
factory := &ResourceConfigFactory{
legacyResourceEncodingConfig: resourceEncodingConfig,
cohabitatingResources: make(map[schema.GroupResource][]schema.GroupResource),
}
Expand All @@ -45,28 +45,54 @@ func NewStorageConfigFactory() *StorageConfigFactory {
}

/*
func (f *StorageConfigFactory) addCohabitatingResources(groupResources ...schema.GroupResource) {
func (f *ResourceConfigFactory) addCohabitatingResources(groupResources ...schema.GroupResource) {
for _, groupResource := range groupResources {
f.cohabitatingResources[groupResource] = groupResources
}
}
*/

func (g *StorageConfigFactory) GetStorageGroupResource(groupResource schema.GroupResource) schema.GroupResource {
func (g *ResourceConfigFactory) getStorageGroupResource(groupResource schema.GroupResource) schema.GroupResource {
if len(g.cohabitatingResources[groupResource]) != 0 {
return g.cohabitatingResources[groupResource][0]
}
return groupResource
}

func (g *StorageConfigFactory) NewConfig(gvr schema.GroupVersionResource, namespaced bool) (*storage.ResourceStorageConfig, error) {
func (g *ResourceConfigFactory) MemoryResource(gvr schema.GroupVersionResource) (schema.GroupVersionResource, error) {
if scheme.LegacyResourceScheme.IsGroupRegistered(gvr.Group) {
return gvr, nil
}
gr := gvr.GroupResource()
memoryVersion, err := g.legacyResourceEncodingConfig.InMemoryEncodingFor(gr)
if err != nil {
return schema.GroupVersionResource{}, err
}
return gr.WithVersion(memoryVersion.Version), nil
}

func (g *ResourceConfigFactory) StorageResource(gvr schema.GroupVersionResource) (schema.GroupVersionResource, error) {
if scheme.LegacyResourceScheme.IsGroupRegistered(gvr.Group) {
return gvr, nil
}

gr := gvr.GroupResource()
chosenStorageResource := g.getStorageGroupResource(gr)
storageVersion, err := g.legacyResourceEncodingConfig.StorageEncodingFor(chosenStorageResource)
if err != nil {
return schema.GroupVersionResource{}, err
}
return chosenStorageResource.WithVersion(storageVersion.Version), nil
}

func (g *ResourceConfigFactory) NewConfig(gvr schema.GroupVersionResource, namespaced bool) (*resourceconfig.ResourceConfig, error) {
if scheme.LegacyResourceScheme.IsGroupRegistered(gvr.Group) {
return g.NewLegacyResourceConfig(gvr.GroupResource(), namespaced)
}
return g.NewUnstructuredConfig(gvr, namespaced)
}

func (g *StorageConfigFactory) NewUnstructuredConfig(gvr schema.GroupVersionResource, namespaced bool) (*storage.ResourceStorageConfig, error) {
func (g *ResourceConfigFactory) NewUnstructuredConfig(gvr schema.GroupVersionResource, namespaced bool) (*resourceconfig.ResourceConfig, error) {
version := gvr.GroupVersion()
codec := versioning.NewCodec(
scheme.UnstructuredCodecs,
Expand All @@ -79,18 +105,18 @@ func (g *StorageConfigFactory) NewUnstructuredConfig(gvr schema.GroupVersionReso
version,
"unstructuredObjectStorage",
)
return &storage.ResourceStorageConfig{
GroupResource: gvr.GroupResource(),
StorageGroupResource: gvr.GroupResource(),
Codec: codec,
StorageVersion: version,
MemoryVersion: version,
Namespaced: namespaced,
return &resourceconfig.ResourceConfig{
Namespaced: namespaced,
GroupResource: gvr.GroupResource(),

StorageResource: gvr,
MemoryResource: gvr,
Codec: codec,
}, nil
}

func (g *StorageConfigFactory) NewLegacyResourceConfig(gr schema.GroupResource, namespaced bool) (*storage.ResourceStorageConfig, error) {
chosenStorageResource := g.GetStorageGroupResource(gr)
func (g *ResourceConfigFactory) NewLegacyResourceConfig(gr schema.GroupResource, namespaced bool) (*resourceconfig.ResourceConfig, error) {
chosenStorageResource := g.getStorageGroupResource(gr)

storageVersion, err := g.legacyResourceEncodingConfig.StorageEncodingFor(chosenStorageResource)
if err != nil {
Expand All @@ -112,12 +138,12 @@ func (g *StorageConfigFactory) NewLegacyResourceConfig(gr schema.GroupResource,
return nil, err
}

return &storage.ResourceStorageConfig{
GroupResource: gr,
StorageGroupResource: chosenStorageResource,
Codec: codec,
StorageVersion: codecConfig.StorageVersion,
MemoryVersion: memoryVersion,
Namespaced: namespaced,
return &resourceconfig.ResourceConfig{
Namespaced: namespaced,
GroupResource: gr,

StorageResource: chosenStorageResource.WithVersion(storageVersion.Version),
MemoryResource: gr.WithVersion(memoryVersion.Version),
Codec: codec,
}, nil
}
16 changes: 16 additions & 0 deletions pkg/runtime/resourceconfig/resource_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package resourceconfig

import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)

type ResourceConfig struct {
Namespaced bool
GroupResource schema.GroupResource

StorageResource schema.GroupVersionResource
MemoryResource schema.GroupVersionResource

Codec runtime.Codec
}
Loading
Loading