Skip to content

Commit

Permalink
fix querying local empty list error
Browse files Browse the repository at this point in the history
  • Loading branch information
qclc committed Jun 29, 2021
1 parent 146a744 commit a8a5a24
Show file tree
Hide file tree
Showing 15 changed files with 815 additions and 74 deletions.
4 changes: 4 additions & 0 deletions cmd/yurthub/app/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/openyurtio/openyurt/cmd/yurthub/app/options"
"github.com/openyurtio/openyurt/pkg/projectinfo"
"github.com/openyurtio/openyurt/pkg/yurthub/cachemanager"
"github.com/openyurtio/openyurt/pkg/yurthub/kubernetes/meta"
"github.com/openyurtio/openyurt/pkg/yurthub/kubernetes/serializer"
"github.com/openyurtio/openyurt/pkg/yurthub/storage/factory"

Expand Down Expand Up @@ -53,6 +54,7 @@ type YurtHubConfiguration struct {
HubAgentDummyIfName string
StorageWrapper cachemanager.StorageWrapper
SerializerManager *serializer.SerializerManager
RESTMapperManager *meta.RESTMapperManager
}

// Complete converts *options.YurtHubOptions to *YurtHubConfiguration
Expand All @@ -69,6 +71,7 @@ func Complete(options *options.YurtHubOptions) (*YurtHubConfiguration, error) {
}
storageWrapper := cachemanager.NewStorageWrapper(storageManager)
serializerManager := serializer.NewSerializerManager()
restMapperManager := meta.NewRESTMapperManager(options.DiskCachePath)

hubServerAddr := net.JoinHostPort(options.YurtHubHost, options.YurtHubPort)
proxyServerAddr := net.JoinHostPort(options.YurtHubHost, options.YurtHubProxyPort)
Expand All @@ -94,6 +97,7 @@ func Complete(options *options.YurtHubOptions) (*YurtHubConfiguration, error) {
HubAgentDummyIfName: options.HubAgentDummyIfName,
StorageWrapper: storageWrapper,
SerializerManager: serializerManager,
RESTMapperManager: restMapperManager,
}

return cfg, nil
Expand Down
4 changes: 2 additions & 2 deletions cmd/yurthub/app/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func Run(cfg *config.YurtHubConfiguration, stopCh <-chan struct{}) error {
trace++

klog.Infof("%d. new cache manager with storage wrapper and serializer manager", trace)
cacheMgr, err := cachemanager.NewCacheManager(cfg.StorageWrapper, cfg.SerializerManager)
cacheMgr, err := cachemanager.NewCacheManager(cfg.StorageWrapper, cfg.SerializerManager, cfg.RESTMapperManager)
if err != nil {
klog.Errorf("could not new cache manager, %v", err)
return err
Expand All @@ -128,7 +128,7 @@ func Run(cfg *config.YurtHubConfiguration, stopCh <-chan struct{}) error {
trace++

klog.Infof("%d. new reverse proxy handler for remote servers", trace)
yurtProxyHandler, err := proxy.NewYurtReverseProxyHandler(cfg, cacheMgr, transportManager, healthChecker, certManager, stopCh)
yurtProxyHandler, err := proxy.NewYurtReverseProxyHandler(cfg, cacheMgr, transportManager, healthChecker, certManager, cfg.RESTMapperManager, stopCh)
if err != nil {
klog.Errorf("could not create reverse proxy handler, %v", err)
return err
Expand Down
6 changes: 3 additions & 3 deletions pkg/yurthub/cachemanager/cache_agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestInitCacheAgents(t *testing.T) {
t.Errorf("failed to create disk storage, %v", err)
}
s := NewStorageWrapper(dStorage)
m, _ := NewCacheManager(s, nil)
m, _ := NewCacheManager(s, nil, nil)

// default cache agents in fake store
b, err := s.GetRaw(cacheAgentsKey)
Expand All @@ -52,7 +52,7 @@ func TestInitCacheAgents(t *testing.T) {
// add agents for next init cache
_ = m.UpdateCacheAgents([]string{"agent1"})

_, _ = NewCacheManager(s, nil)
_, _ = NewCacheManager(s, nil, nil)

b2, err := s.GetRaw(cacheAgentsKey)
if err != nil {
Expand Down Expand Up @@ -81,7 +81,7 @@ func TestUpdateCacheAgents(t *testing.T) {
t.Errorf("failed to create disk storage, %v", err)
}
s := NewStorageWrapper(dStorage)
m, _ := NewCacheManager(s, nil)
m, _ := NewCacheManager(s, nil, nil)

testcases := map[string]struct {
desc string
Expand Down
24 changes: 20 additions & 4 deletions pkg/yurthub/cachemanager/cache_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"strings"
"sync"

meta2 "github.com/openyurtio/openyurt/pkg/yurthub/kubernetes/meta"
"github.com/openyurtio/openyurt/pkg/yurthub/kubernetes/serializer"
"github.com/openyurtio/openyurt/pkg/yurthub/storage"
"github.com/openyurtio/openyurt/pkg/yurthub/util"
Expand Down Expand Up @@ -58,6 +59,7 @@ type cacheManager struct {
sync.RWMutex
storage StorageWrapper
serializerManager *serializer.SerializerManager
restMapperManager *meta2.RESTMapperManager
cacheAgents map[string]bool
listSelectorCollector map[string]string
}
Expand All @@ -66,10 +68,12 @@ type cacheManager struct {
func NewCacheManager(
storage StorageWrapper,
serializerMgr *serializer.SerializerManager,
restMapperMgr *meta2.RESTMapperManager,
) (CacheManager, error) {
cm := &cacheManager{
storage: storage,
serializerManager: serializerMgr,
restMapperManager: restMapperMgr,
cacheAgents: make(map[string]bool),
listSelectorCollector: make(map[string]string),
}
Expand Down Expand Up @@ -152,13 +156,14 @@ func (cm *cacheManager) queryListObject(req *http.Request) (runtime.Object, erro
if err != nil {
return nil, err
} else if len(objs) == 0 {
gvk, err = serializer.UnsafeDefaultRESTMapper.KindFor(schema.GroupVersionResource{
gvr := schema.GroupVersionResource{
Group: info.APIGroup,
Version: info.APIVersion,
Resource: info.Resource,
})
if err != nil {
return nil, err
}
_, gvk = cm.restMapperManager.KindFor(gvr)
if gvk.Empty() {
return nil, fmt.Errorf("no matches for %v", gvr)
}
kind = gvk.Kind
} else {
Expand Down Expand Up @@ -349,6 +354,11 @@ func (cm *cacheManager) saveListObject(ctx context.Context, info *apirequest.Req
}.String()
accessor := meta.NewAccessor()

// Verify if DynamicRESTMapper(which store the CRD info) needs to be updated
if err := cm.restMapperManager.UpdateKind(schema.GroupVersionKind{Group: info.APIGroup, Version: info.APIVersion, Kind: kind}); err != nil {
klog.Errorf("failed to update the DynamicRESTMapper %v", err)
}

comp, _ := util.ClientComponentFrom(ctx)
// even if no objects in cloud cluster, we need to
// make up a storage that represents the no resources
Expand Down Expand Up @@ -437,6 +447,12 @@ func (cm *cacheManager) saveOneObject(ctx context.Context, info *apirequest.Requ
return err
}

// Verify if DynamicRESTMapper(which store the CRD info) needs to be updated
gvk := obj.GetObjectKind().GroupVersionKind()
if err := cm.restMapperManager.UpdateKind(gvk); err != nil {
klog.Errorf("failed to update the DynamicRESTMapper %v", err)
}

if err := cm.saveOneObjectWithValidation(key, obj); err != nil {
if err != storage.ErrStorageAccessConflict {
return err
Expand Down
Loading

0 comments on commit a8a5a24

Please sign in to comment.