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

fix: use metav1.Object instead of runtime.Object for more ergonomic annotation handling #144

Merged

Conversation

kevin1689-cloud
Copy link
Contributor

@kevin1689-cloud kevin1689-cloud commented Jul 10, 2023

related to #99

@CLAassistant
Copy link

CLAassistant commented Jul 10, 2023

CLA assistant check
All committers have signed the CLA.

@kevin1689-cloud
Copy link
Contributor Author

Please take a look. Thanks!

@@ -67,7 +58,7 @@ func HasAnnotationKeyValue(obj runtime.Object, key, value string) (bool, error)
// AddAnnotation adds the given annotation key and value to the given objects ObjectMeta,
// and overwrites the annotation value if it already exists.
// Returns true if the object was updated.
func AddAnnotation(obj runtime.Object, key, value string) (bool, error) {
func AddAnnotation(obj metav1.Object, key, value string) (bool, error) {
Copy link
Member

@gary-lgy gary-lgy Jul 10, 2023

Choose a reason for hiding this comment

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

Besides avoiding the meta.Accessor overhead, another point of this refactor is to allow calling AddAnnotation without having to handle the returned error (given the current code paths, the old implementation of AddAnnotation would never return an error anyway).

The following is a possible implementation of AddAnnotation that satisfies the requirements:

func AddAnnotation(obj metav1.Object, key, value string) bool {
	if HasAnnotationKeyValue(obj, key, value) {
		return false
	}
	annotations := obj.GetAnnotations()
	if annotations == nil {
		annotations = make(map[string]string)
	}
	annotations[key] = value
	obj.SetAnnotations(annotations)
	return true
}

Naturally, since the return signature has changed, all the call sites need to be changed (which is a good thing because the old error handling code is merely unnecessary bloat).

Copy link
Member

@gary-lgy gary-lgy left a comment

Choose a reason for hiding this comment

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

Pls see the above comment.

@kevin1689-cloud
Copy link
Contributor Author

Pls see the above comment.

Got it, thanks for the example which is very clear. I will working on that.

@gary-lgy
Copy link
Member

l

Pls see the above comment.

Got it, thanks for the example which is very clear. I will working on that.

Meanwhile, we're in the process of a major refactor (https://github.com/kubewharf/kubeadmiral/tree/refactor/unified-federated-type) and the code on the main branch will be changed significantly by next week. I suggest delaying this fix until the refactor is done, otherwise we will have to do a lot of rebasing. :)

@kevin1689-cloud
Copy link
Contributor Author

kevin1689-cloud commented Jul 11, 2023

Pls see the above comment.

Got it, thanks for the example which is very clear. I will working on that.

Meanwhile, we're in the process of a major refactor (https://github.com/kubewharf/kubeadmiral/tree/refactor/unified-federated-type) and the code on the main branch will be changed significantly by next week. I suggest delaying this fix until the refactor is done, otherwise we will have to do a lot of rebasing. :)

Got it, how about in this pull request we only avoiding the meta.Accessor overhead which only need change two files:

  • pkg/controllers/util/annotation/annotation.go
  • pkg/controllers/util/annotation/annotation_test.go

For the refactor which allow calling AddAnnotation without having to handle the returned error, we delaying it until the major refactor is done. After the major refactor is done, I can create another pull request to remove the returned error of AddAnnotation and change all the call sites.

I think by this way we can make some improvement(avoiding the meta.Accessor overhead) and minimize the rebasing work. What do you think about that? Thanks!

@kevin1689-cloud kevin1689-cloud force-pushed the fix/ergonomic-annotation-handling branch from 5dbaac1 to cd6937a Compare July 11, 2023 04:39
@gary-lgy
Copy link
Member

Pls see the above comment.

Got it, thanks for the example which is very clear. I will working on that.

Meanwhile, we're in the process of a major refactor (refactor/unified-federated-type) and the code on the main branch will be changed significantly by next week. I suggest delaying this fix until the refactor is done, otherwise we will have to do a lot of rebasing. :)

Got it, how about in this pull request we only avoiding the meta.Accessor overhead which only need change two files:

  • pkg/controllers/util/annotation/annotation.go
  • pkg/controllers/util/annotation/annotation_test.go

For the refactor which allow calling AddAnnotation without having to handle the returned error, we delaying it until the major refactor is done. After the major refactor is done, I can create another pull request to remove the returned error of AddAnnotation and change all the call sites.

I think by this way we can make some improvement(avoiding the meta.Accessor overhead) and minimize the rebasing work. What do you think about that? Thanks!

Sounds good. Please fix the compile error and then LGTM.

@kevin1689-cloud kevin1689-cloud force-pushed the fix/ergonomic-annotation-handling branch from cd6937a to eab1527 Compare July 12, 2023 13:08
@kevin1689-cloud
Copy link
Contributor Author

kevin1689-cloud commented Jul 12, 2023

Pls see the above comment.

Got it, thanks for the example which is very clear. I will working on that.

Meanwhile, we're in the process of a major refactor (refactor/unified-federated-type) and the code on the main branch will be changed significantly by next week. I suggest delaying this fix until the refactor is done, otherwise we will have to do a lot of rebasing. :)

Got it, how about in this pull request we only avoiding the meta.Accessor overhead which only need change two files:

  • pkg/controllers/util/annotation/annotation.go
  • pkg/controllers/util/annotation/annotation_test.go

For the refactor which allow calling AddAnnotation without having to handle the returned error, we delaying it until the major refactor is done. After the major refactor is done, I can create another pull request to remove the returned error of AddAnnotation and change all the call sites.
I think by this way we can make some improvement(avoiding the meta.Accessor overhead) and minimize the rebasing work. What do you think about that? Thanks!

Sounds good. Please fix the compile error and then LGTM.

Done. The compile error has been fixed by replace runtime.Object to metav1.Object in the file pkg/controllers/util/sourcefeedback/util.go. Please take a look.

@kevin1689-cloud kevin1689-cloud requested a review from gary-lgy July 12, 2023 13:15
@gary-lgy gary-lgy merged commit b40dfff into kubewharf:main Jul 13, 2023
@gary-lgy
Copy link
Member

@kevin1689-cloud thanks!

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.

3 participants