Skip to content

Commit

Permalink
resolve comments 2, clusterpedia-io#345
Browse files Browse the repository at this point in the history
Co-authored-by: duanmeng <duanmeng_yewu@cmss.chinamobile.com>
Co-authored-by: wuyingjun <wuyingjun_yewu@cmss.chinamobile.com>
Co-authored-by: hanweisen <hanweisen_yewu@cmss.chinamobile.com>
Signed-off-by: zhangyongxi <zhangyongxi_yewu@cmss.chinamobile.com>
  • Loading branch information
4 people authored and 张永曦 committed Sep 19, 2022
1 parent 1cbff16 commit db89c0c
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 13 deletions.
8 changes: 8 additions & 0 deletions pkg/apiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package apiserver

import (
"context"
"fmt"
"net/http"

metainternal "k8s.io/apimachinery/pkg/apis/meta/internalversion"
Expand Down Expand Up @@ -98,6 +99,13 @@ func (cfg *Config) Complete() CompletedConfig {
}

func (config completedConfig) New() (*ClusterPediaServer, error) {
if config.ClientConfig == nil {
return nil, fmt.Errorf("CompletedConfig.New() called with config.ClientConfig == nil")
}
if config.StorageFactory == nil {
return nil, fmt.Errorf("CompletedConfig.New() called with config.StorageFactory == nil")
}

discoveryClient, err := discovery.NewDiscoveryClientForConfig(config.ClientConfig)
if err != nil {
return nil, err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func NewREST(serializer runtime.NegotiatedSerializer, factory storage.StorageFac
for irt := range cr.ResourceTypes {
rt := &cr.ResourceTypes[irt]
if rt.Resource != "" {
config, err := configFactory.NewConfig(rt.GroupResource().WithVersion(""))
config, err := configFactory.NewConfig(rt.GroupResource().WithVersion(""), false)
if err != nil {
continue
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/kubeapiserver/restmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ func (m *RESTManager) addRESTResourceInfosLocked(addedInfos map[schema.GroupVers
}

func (m *RESTManager) genLegacyResourceRESTStorage(gvr schema.GroupVersionResource, kind string) (*resourcerest.RESTStorage, error) {
storageConfig, err := m.resourcetSorageConfig.NewLegacyResourceConfig(gvr.GroupResource())
storageConfig, err := m.resourcetSorageConfig.NewLegacyResourceConfig(gvr.GroupResource(), false)
if err != nil {
return nil, err
}
Expand All @@ -291,7 +291,7 @@ func (m *RESTManager) genLegacyResourceRESTStorage(gvr schema.GroupVersionResour
}

func (m *RESTManager) genCustomResourceRESTStorage(gvr schema.GroupVersionResource, kind string) (*resourcerest.RESTStorage, error) {
storageConfig, err := m.resourcetSorageConfig.NewCustomResourceConfig(gvr)
storageConfig, err := m.resourcetSorageConfig.NewCustomResourceConfig(gvr, false)
if err != nil {
return nil, err
}
Expand Down
12 changes: 7 additions & 5 deletions pkg/kubeapiserver/storageconfig/storageconfig_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ func (g *StorageConfigFactory) GetStorageGroupResource(groupResource schema.Grou
return groupResource
}

func (g *StorageConfigFactory) NewConfig(gvr schema.GroupVersionResource) (*storage.ResourceStorageConfig, error) {
func (g *StorageConfigFactory) NewConfig(gvr schema.GroupVersionResource, namespaced bool) (*storage.ResourceStorageConfig, error) {
if resourcescheme.LegacyResourceScheme.IsGroupRegistered(gvr.Group) {
return g.NewLegacyResourceConfig(gvr.GroupResource())
return g.NewLegacyResourceConfig(gvr.GroupResource(), namespaced)
}
return g.NewCustomResourceConfig(gvr)
return g.NewCustomResourceConfig(gvr, namespaced)
}

func (g *StorageConfigFactory) NewCustomResourceConfig(gvr schema.GroupVersionResource) (*storage.ResourceStorageConfig, error) {
func (g *StorageConfigFactory) NewCustomResourceConfig(gvr schema.GroupVersionResource, namespaced bool) (*storage.ResourceStorageConfig, error) {
version := gvr.GroupVersion()
codec := versioning.NewCodec(
resourcescheme.UnstructuredCodecs,
Expand All @@ -85,10 +85,11 @@ func (g *StorageConfigFactory) NewCustomResourceConfig(gvr schema.GroupVersionRe
Codec: codec,
StorageVersion: version,
MemoryVersion: version,
Namespaced: namespaced,
}, nil
}

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

storageVersion, err := g.legacyResourceEncodingConfig.StorageEncodingFor(chosenStorageResource)
Expand Down Expand Up @@ -117,5 +118,6 @@ func (g *StorageConfigFactory) NewLegacyResourceConfig(gr schema.GroupResource)
Codec: codec,
StorageVersion: codecConfig.StorageVersion,
MemoryVersion: memoryVersion,
Namespaced: namespaced,
}, nil
}
3 changes: 1 addition & 2 deletions pkg/synchromanager/clustersynchro/cluster_synchro.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,8 @@ func (s *ClusterSynchro) setSyncResources() {
if _, ok := s.storageResourceSynchros.Load(storageGVR); ok {
continue
}
resourceConfig := config.storageConfig

resourceStorage, err := s.storage.NewResourceStorage(resourceConfig)
resourceStorage, err := s.storage.NewResourceStorage(config.storageConfig)
if err != nil {
klog.ErrorS(err, "Failed to create resource storage", "cluster", s.name, "storage resource", storageGVR)
updateSyncConditions(storageGVR, clusterv1alpha2.ResourceSyncStatusPending, "SynchroCreateFailed", fmt.Sprintf("new resource storage failed: %s", err))
Expand Down
4 changes: 1 addition & 3 deletions pkg/synchromanager/clustersynchro/resource_negotiator.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,14 @@ func (negotiator *ResourceNegotiator) NegotiateSyncResources(syncResources []clu
Reason: "SynchroCreating",
}

storageConfig, err := negotiator.resourceStorageConfig.NewConfig(syncGVR)

storageConfig, err := negotiator.resourceStorageConfig.NewConfig(syncGVR, apiResource.Namespaced)
if err != nil {
syncCondition.Reason = "SynchroCreateFailed"
syncCondition.Message = fmt.Sprintf("new resource storage config failed: %s", err)
groupResourceStatus.addSyncCondition(syncGVR, syncCondition)
continue
}

storageConfig.Namespaced = apiResource.Namespaced
storageGVR := storageConfig.StorageGroupResource.WithVersion(storageConfig.StorageVersion.Version)
syncCondition.StorageVersion = storageGVR.Version
if syncGR != storageConfig.StorageGroupResource {
Expand Down

0 comments on commit db89c0c

Please sign in to comment.