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

Enhance YurtHub's caching ability #225

Merged
merged 1 commit into from
Apr 21, 2021

Conversation

qclc
Copy link
Member

@qclc qclc commented Feb 23, 2021

Ⅰ. Describe what this PR does

  1. Replace the original serializer.WithoutConversionCodecFactory with a custom WithVersionCodecFactory so that the GVK information can be preserved during deserialization.
  2. Delete both the resourceToKindMap and the resourceToListKindMap
  3. On the basis of the existing NegotiatedSerializer increase a new unstructuredNegotiatedSerializer, used to decode the structure of the unregistered Custom Resources.

Ⅱ. Does this pull request fix one issue?

fixes #162
This PR extends the ability to cache CR resources, removing the restriction that only resources in resourceToKindMap and resourceToListKindMap can be cached.

Ⅲ. List the added test cases (unit test/integration test) if any, please explain if no tests are needed.

  1. Unit tests for CR caching and getting have been added to cache_manager_test.go;

Ⅳ. Describe how to verify it

Ⅴ. Special notes for reviews

@rambohe-ch
Copy link
Member

@qclc Very much appreciate for your work that refactor cache manager in yurthub component.
Would you fix the error of github actions at first?
and please merge all commits in only one commit.

@qclc
Copy link
Member Author

qclc commented Feb 23, 2021

@qclc Very much appreciate for your work that refactor cache manager in yurthub component.
Would you fix the error of github actions at first?
and please merge all commits in only one commit.

Thanks for your suggestions, I will fix the error and merge all commits.

@qclc qclc force-pushed the deleteResourceToKind branch 2 times, most recently from 6c0d780 to 9aaa238 Compare February 23, 2021 07:53
@qclc qclc changed the title Delete resource to kind Enhance Yuthub's caching capabilities Mar 15, 2021
@qclc qclc changed the title Enhance Yuthub's caching capabilities Enhance YurtHub's caching ability Mar 15, 2021
pkg/yurthub/cachemanager/cache_manager.go Show resolved Hide resolved
} else {
tmp := new(unstructured.UnstructuredList)
tmp.SetGroupVersionKind(listGvk)
listObj = tmp
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about delete tmp var and like that:

listObj = new(unstructured.UnstructuredList)
listObj.GetObjectKind().SetGroupVersionKind(listGvk)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code is already updated, thanks for review

tmp := new(unstructured.UnstructuredList)
tmp.SetGroupVersionKind(listGvk)
listObj = tmp
}
if err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about move this if statement after 163 line(listObj, err = scheme.Scheme.New(listGvk))?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code is already updated, thanks for review

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe comment is confusing, The if err !=nil statement is only need by listObj, err = scheme.Scheme.New(listGvk), so move into the following if statement.

	if scheme.Scheme.Recognizes(listGvk) {
		listObj, err = scheme.Scheme.New(listGvk)
                if err != nil {
                    ....
                }

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Thanks for reminding me

// Decode does not do conversion. It keeps the gvk during deserialization.
func (d WithVersionDecoder) Decode(data []byte, defaults *schema.GroupVersionKind, into runtime.Object) (runtime.Object, *schema.GroupVersionKind, error) {
obj, gvk, err := d.Decoder.Decode(data, defaults, into)
return obj, gvk, err
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about return d.Decoder.Decode(data, defaults, into) directly ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code is already updated, thanks for review

pkg/yurthub/cachemanager/cache_manager.go Show resolved Hide resolved
pkg/yurthub/cachemanager/cache_manager.go Show resolved Hide resolved
mediaTypes := sm.NegotiatedSerializer.SupportedMediaTypes()
func (sm *SerializerManager) CreateSerializers(contentType, group, version, apiPrefix string) (*rest.Serializers, error) {
var mediaTypes []runtime.SerializerInfo
if apiPrefix == "api" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there are some normal gvk with apiPrefix="api" and contentType="xxx.protobuf.xxx", and CreateSerializers can not handle them.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my suggestion: use gvr + restMapper to get gvk, so select serializer based on gvk.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code is already updated, thanks for review

@@ -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])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about move Interpret out of for loop, so run Interpret only one time.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code is already updated, thanks for review

@qclc qclc force-pushed the deleteResourceToKind branch 2 times, most recently from 6171e97 to 1491e3f Compare March 16, 2021 15:38
@@ -248,20 +221,21 @@ 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, "", "v1", "events")
Copy link
Member

@rambohe-ch rambohe-ch Mar 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need a serializer for WatchEvent, so maybe need to use cm.serializerManager.CreateSerializers(reqContentType, info.APIGroup, info.APIVersion, "watchevent") to create serializer.

and before making a pr we need to build a yurthub image and make sure the new image can work no problem.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code is already updated, thanks for review

// our resources.
for _, gv := range defaultGroupVersions {
for kind := range scheme.KnownTypes(gv) {
scope := meta.RESTScopeNamespace
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that scopes for all gvk are meta.RESTScopeNamespace is not make sense.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code is already updated, thanks for review

@qclc qclc force-pushed the deleteResourceToKind branch 4 times, most recently from 352e7a4 to 1301a90 Compare March 29, 2021 16:27
ns: "default",
kind: "CronTab",
},
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about add a namespaced=false test case?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test cases is already added, thanks for review.

@@ -0,0 +1,117 @@
package cachemanager
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add OpenYurt license info

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

License info is already added, thanks for review.

handlers := hl.GetHandlers(gvk)
if handlers != nil {
var newobj runtime.Object
newobj = obj
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

newobj is redundant, how about use obj directly?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code is already updated, thanks for review

var newobj runtime.Object
newobj = obj
for _, handler := range handlers {
tmp, err := handler.ServeObject(newobj)
Copy link
Member

@rambohe-ch rambohe-ch Apr 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tmp is the same as newobj

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code is already updated, thanks for review

}

//SelectAndProcess determines whether an object meets the filter criteria and processes it if it does
func (hl *HandlerLayer) SelectAndProcess(obj runtime.Object) (runtime.Object, bool, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The caller of SelectAndProcess is not found?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code is already updated, change SelectAndProcess to Filter function

type HandlerLayer struct {
sync.Mutex
handlers map[schema.GroupVersionKind][]Handler
selectors map[schema.GroupVersionKind]*storage.SelectionPredicate
Copy link
Member

@rambohe-ch rambohe-ch Apr 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use SelectionPredicate is a good idea, but I think that we need to filter object not only based on labels and fields. How about define a common filter function? like Filter(obj runtime.Object) bool, and SelectionPredicate is only a specific case.

Copy link
Member Author

@qclc qclc Apr 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The storage.AttrFunc of SelectionPredicate is used to extract the corresponding field data from the actual Object, which is defined by the user. It can basically meet the matching of all fields of runtime.Object. The newly added hanler_layer_test.go has concrete examples to refer to

Copy link
Member

@rambohe-ch rambohe-ch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@rambohe-ch rambohe-ch merged commit 6aef72b into openyurtio:master Apr 21, 2021
@qclc qclc mentioned this pull request Sep 14, 2021
6 tasks
@qclc qclc mentioned this pull request Nov 12, 2021
6 tasks
MrGirl pushed a commit to MrGirl/openyurt that referenced this pull request Mar 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[feature request]all resources can be cached by yurt-hub
2 participants