Skip to content

Commit

Permalink
Support CR resource
Browse files Browse the repository at this point in the history
  • Loading branch information
qclc committed Mar 15, 2021
1 parent 0eb7bd3 commit 8e46456
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 42 deletions.
34 changes: 26 additions & 8 deletions pkg/yurthub/cachemanager/cache_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (

"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/watch"
Expand Down Expand Up @@ -145,15 +146,26 @@ func (cm *cacheManager) queryListObject(req *http.Request) (runtime.Object, erro
if err != nil {
return nil, err
}

listKind := objs[0].GetObjectKind().GroupVersionKind().Kind + "List"
var listKind string
if len(objs) == 0 {
klog.Errorf("no %s in cache", info.Resource)
return nil, err
} else {
listKind = objs[0].GetObjectKind().GroupVersionKind().Kind + "List"
}
var listObj runtime.Object
listGvk := schema.GroupVersionKind{
Group: info.APIGroup,
Version: info.APIVersion,
Kind: listKind,
}

listObj, err := scheme.Scheme.New(listGvk)
if scheme.Scheme.Recognizes(listGvk) {
listObj, err = scheme.Scheme.New(listGvk)
} else {
tmp := new(unstructured.UnstructuredList)
tmp.SetGroupVersionKind(listGvk)
listObj = tmp
}
if err != nil {
klog.Errorf("failed to create list object(%v), %v", listGvk, err)
return nil, err
Expand Down Expand Up @@ -215,7 +227,13 @@ func (cm *cacheManager) saveWatchObject(ctx context.Context, info *apirequest.Re

comp, _ := util.ClientComponentFrom(ctx)
reqContentType, _ := util.ReqContentTypeFrom(ctx)
serializers, err := cm.serializerManager.CreateSerializers(reqContentType, info.APIGroup, info.APIVersion)
serializers, err := cm.serializerManager.CreateSerializers(reqContentType, info.APIGroup, info.APIVersion, "api")
if err != nil {
klog.Errorf("failed to create serializers in saveWatchObject, %v", err)
return err
}

embeddedSerializers, err := cm.serializerManager.CreateSerializers(reqContentType, info.APIGroup, info.APIVersion, info.APIPrefix)
if err != nil {
klog.Errorf("failed to create serializers in saveWatchObject, %v", err)
return err
Expand All @@ -227,7 +245,7 @@ func (cm *cacheManager) saveWatchObject(ctx context.Context, info *apirequest.Re
}.String()
accessor := meta.NewAccessor()

d, err := serializer.WatchDecoder(serializers, r)
d, err := serializer.WatchDecoder(serializers, embeddedSerializers, r)
if err != nil {
klog.Errorf("saveWatchObject ended with error, %v", err)
return err
Expand Down Expand Up @@ -301,7 +319,7 @@ func (cm *cacheManager) saveWatchObject(ctx context.Context, info *apirequest.Re
func (cm *cacheManager) saveListObject(ctx context.Context, info *apirequest.RequestInfo, b []byte) error {
reqContentType, _ := util.ReqContentTypeFrom(ctx)
respContentType, _ := util.RespContentTypeFrom(ctx)
serializers, err := cm.serializerManager.CreateSerializers(reqContentType, info.APIGroup, info.APIVersion)
serializers, err := cm.serializerManager.CreateSerializers(reqContentType, info.APIGroup, info.APIVersion, info.APIPrefix)
if err != nil {
klog.Errorf("failed to create serializers in saveListObject, %v", err)
return err
Expand Down Expand Up @@ -380,7 +398,7 @@ func (cm *cacheManager) saveOneObject(ctx context.Context, info *apirequest.Requ
reqContentType, _ := util.ReqContentTypeFrom(ctx)
respContentType, _ := util.RespContentTypeFrom(ctx)

serializers, err := cm.serializerManager.CreateSerializers(reqContentType, info.APIGroup, info.APIVersion)
serializers, err := cm.serializerManager.CreateSerializers(reqContentType, info.APIGroup, info.APIVersion, info.APIPrefix)
if err != nil {
klog.Errorf("failed to create serializers in saveOneObject: %s, %v", util.ReqInfoString(info), err)
return err
Expand Down
12 changes: 10 additions & 2 deletions pkg/yurthub/cachemanager/cache_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func TestCacheResponse(t *testing.T) {
accept string
verb string
path string
apiPrefix string
namespaced bool
expectResult expectData
}{
Expand All @@ -93,6 +94,7 @@ func TestCacheResponse(t *testing.T) {
accept: "application/json",
verb: "GET",
path: "/api/v1/namespaces/default/pods/mypod1",
apiPrefix: "api",
namespaced: true,
expectResult: expectData{
rv: "1",
Expand Down Expand Up @@ -121,6 +123,7 @@ func TestCacheResponse(t *testing.T) {
accept: "application/json",
verb: "GET",
path: "/api/v1/namespaces/default/pods/mypod2",
apiPrefix: "api",
namespaced: true,
expectResult: expectData{
rv: "3",
Expand Down Expand Up @@ -148,6 +151,7 @@ func TestCacheResponse(t *testing.T) {
accept: "application/json",
verb: "GET",
path: "/api/v1/nodes/mynode1",
apiPrefix: "api",
namespaced: false,
expectResult: expectData{
rv: "4",
Expand All @@ -174,6 +178,7 @@ func TestCacheResponse(t *testing.T) {
accept: "application/json",
verb: "GET",
path: "/api/v1/nodes/mynode2",
apiPrefix: "api",
namespaced: false,
expectResult: expectData{
rv: "6",
Expand All @@ -187,7 +192,7 @@ func TestCacheResponse(t *testing.T) {
resolver := newTestRequestInfoResolver()
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
encoder, err := serializerM.CreateSerializers(tt.accept, tt.group, tt.version)
encoder, err := serializerM.CreateSerializers(tt.accept, tt.group, tt.version, tt.apiPrefix)
if err != nil {
t.Fatalf("could not create serializer, %v", err)
}
Expand Down Expand Up @@ -503,6 +508,7 @@ func TestCacheResponseForList(t *testing.T) {
accept string
verb string
path string
apiPrefix string
namespaced bool
expectResult expectData
}{
Expand Down Expand Up @@ -561,6 +567,7 @@ func TestCacheResponseForList(t *testing.T) {
accept: "application/json",
verb: "GET",
path: "/api/v1/namespaces/default/pods",
apiPrefix: "api",
namespaced: true,
expectResult: expectData{
data: map[string]struct{}{
Expand Down Expand Up @@ -632,6 +639,7 @@ func TestCacheResponseForList(t *testing.T) {
accept: "application/json",
verb: "GET",
path: "/api/v1/nodes",
apiPrefix: "api",
namespaced: false,
expectResult: expectData{
data: map[string]struct{}{
Expand All @@ -648,7 +656,7 @@ func TestCacheResponseForList(t *testing.T) {
resolver := newTestRequestInfoResolver()
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
encoder, err := serializerM.CreateSerializers(tt.accept, tt.group, tt.version)
encoder, err := serializerM.CreateSerializers(tt.accept, tt.group, tt.version, tt.apiPrefix)
if err != nil {
t.Fatalf("could not create serializer, %v", err)
}
Expand Down
28 changes: 25 additions & 3 deletions pkg/yurthub/cachemanager/storage_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/openyurtio/openyurt/pkg/yurthub/storage"
"github.com/openyurtio/openyurt/pkg/yurthub/util"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer/json"
"k8s.io/client-go/kubernetes/scheme"
Expand Down Expand Up @@ -113,8 +114,18 @@ func (sw *storageWrapper) Get(key string) (runtime.Object, error) {
} else if len(b) == 0 {
return nil, nil
}

obj, gvk, err := sw.backendSerializer.Decode(b, nil, nil)
//get the gvk from json data
gvk, err := json.DefaultMetaFactory.Interpret(b)
if err != nil {
return nil, err
}
var UnstructuredObj runtime.Object
if scheme.Scheme.Recognizes(*gvk) {
UnstructuredObj = nil
} else {
UnstructuredObj = new(unstructured.Unstructured)
}
obj, gvk, err := sw.backendSerializer.Decode(b, nil, UnstructuredObj)
if err != nil {
klog.Errorf("could not decode %v for %s, %v", gvk, key, err)
return nil, err
Expand Down Expand Up @@ -145,7 +156,18 @@ func (sw *storageWrapper) List(key string) ([]runtime.Object, error) {
}

for i := range bb {
obj, gvk, err := sw.backendSerializer.Decode(bb[i], nil, nil)
//get the gvk from json data
gvk, err := json.DefaultMetaFactory.Interpret(bb[i])
if err != nil {
return nil, err
}
var UnstructuredObj runtime.Object
if scheme.Scheme.Recognizes(*gvk) {
UnstructuredObj = nil
} else {
UnstructuredObj = new(unstructured.Unstructured)
}
obj, gvk, err := sw.backendSerializer.Decode(bb[i], nil, UnstructuredObj)
if err != nil {
klog.Errorf("could not decode %v for %s, %v", gvk, key, err)
continue
Expand Down
Loading

0 comments on commit 8e46456

Please sign in to comment.