-
Notifications
You must be signed in to change notification settings - Fork 250
resourceapply: Allow removing run-level labels from namespaces #727
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
resourceapply: Allow removing run-level labels from namespaces #727
Conversation
|
|
||
| // allow removing runlevels since the metadata are otherwise merged | ||
| _, requiredRunLevelPresent := required.ObjectMeta.Labels["run-level"] | ||
| if _, ok := existingCopy.ObjectMeta.Labels["run-level"]; ok != requiredRunLevelPresent && !requiredRunLevelPresent { |
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.
the add case works?
ok && !requiredRunLevelPresent is simpler
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.
The add case is handled by the EnsureObjectMeta() function above.
Good simplification, I modified the condition in latest revision.
57bdb65 to
148d711
Compare
148d711 to
57bdb65
Compare
57bdb65 to
1f5fc32
Compare
| func ApplyConfigMap(client coreclientv1.ConfigMapsGetter, recorder events.Recorder, required *corev1.ConfigMap) (*corev1.ConfigMap, bool, error) { | ||
| existing, err := client.ConfigMaps(required.Namespace).Get(required.Name, metav1.GetOptions{}) | ||
| if apierrors.IsNotFound(err) { | ||
| resourcemerge.CleanMetaLabelsAnnotations(&required.ObjectMeta) |
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.
mutating the input is bad. We need a deep copy (or shallow copy of at least the root and ObjectMeta).
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.
I went with the DeepCopy() should we ever want to mutate any other fields of the actual object as well
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.
performance here does not matter anyway
1f5fc32 to
d477ea7
Compare
| actual, err := client.Namespaces().Create(required) | ||
| reportCreateEvent(recorder, required, err) | ||
| requiredCopy := required.DeepCopy() | ||
| resourcemerge.CleanMetaLabelsAnnotations(&requiredCopy.ObjectMeta) |
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.
how about:
actual, err := client.Namespaces().Create(WithCleanLabelsAndAnnotations(requiredCopy))`
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.
done
d477ea7 to
c6fa45c
Compare
If an annotation/label key ends with "-", remove that that key (ending "-"s are trimmed) from the existing map instead.
c6fa45c to
3a6456f
Compare
| requiredCopy := required.DeepCopy() | ||
| actual, err := client.Namespaces(). | ||
| Create(resourcemerge.WithCleanLabelsAndAnnotations(requiredCopy).(*corev1.Namespace)) | ||
| reportCreateEvent(recorder, requiredCopy, err) |
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.
report requiredCopy or actual or cleaned version?
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.
actual could be nil here in case of an error, so I guess the cleaned version should be reported. In C I would be sure that requiredCopy == cleaned, not sure if Go does additional magic to the pointer being passed to WithCleanLabelsAndAnnotations 🤔
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.
Yes, they are equal
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.
I don't care much which one you print, just decide conciously :)
| // if "required" map contains a key with "-" as suffix, remove that | ||
| // key from the existing map instead of replacing the value | ||
| if strings.HasSuffix(k, "-") { | ||
| delete(*existing, strings.TrimRight(k, "-")) |
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.
Intentional to remove arbitrary many - here?
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.
yes, you need to remove the trailing - to get the actual name of the field you're trying to remove:
original: run-level: 0
required: run-level-:
This is different in case of https://github.com/openshift/library-go/pull/727/files#diff-3567eb2cd10c84b30387be04408ab7b2R27 where the object being cleaned is the object that's actually getting created.
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.
I meant that TrimRight also removes --- from foo---.
|
/lgtm |
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: stlaz, sttts The full list of commands accepted by this bot can be found here. The pull request process is described here DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |
resourceapply: Allow removing run-level labels from namespaces
A couple of components were using run-level labels on their
namespaces even though they did not have to. Make it possible
to remove this label with the library-go functions while not
changing the other labels/annotations in case these are maintained
automatically by a different controller.