-
Notifications
You must be signed in to change notification settings - Fork 100
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
fix: use metav1.Object instead of runtime.Object for more ergonomic annotation handling #144
Conversation
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) { |
There was a problem hiding this comment.
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).
There was a problem hiding this 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.
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
For the refactor which allow calling I think by this way we can make some improvement(avoiding the |
5dbaac1
to
cd6937a
Compare
Sounds good. Please fix the compile error and then LGTM. |
…nnotation handling Signed-off-by: kevin1689 <[email protected]>
cd6937a
to
eab1527
Compare
Done. The compile error has been fixed by replace |
@kevin1689-cloud thanks! |
…nnotation-handling
related to #99