Skip to content

Conversation

@stlaz
Copy link
Contributor

@stlaz stlaz commented Mar 6, 2020

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.

@openshift-ci-robot openshift-ci-robot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Mar 6, 2020

// 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 {
Copy link
Contributor

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

Copy link
Contributor Author

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.

@stlaz stlaz force-pushed the ns_apply_runlevels branch 3 times, most recently from 57bdb65 to 148d711 Compare March 6, 2020 14:03
@openshift-ci-robot openshift-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Mar 6, 2020
@stlaz stlaz force-pushed the ns_apply_runlevels branch from 148d711 to 57bdb65 Compare March 6, 2020 14:10
@openshift-ci-robot openshift-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Mar 6, 2020
@stlaz stlaz force-pushed the ns_apply_runlevels branch from 57bdb65 to 1f5fc32 Compare March 6, 2020 14:34
@openshift-ci-robot openshift-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Mar 6, 2020
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)
Copy link
Contributor

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).

Copy link
Contributor Author

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

Copy link
Contributor

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

@stlaz stlaz force-pushed the ns_apply_runlevels branch from 1f5fc32 to d477ea7 Compare March 6, 2020 15:39
actual, err := client.Namespaces().Create(required)
reportCreateEvent(recorder, required, err)
requiredCopy := required.DeepCopy()
resourcemerge.CleanMetaLabelsAnnotations(&requiredCopy.ObjectMeta)
Copy link
Contributor

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))`

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@stlaz stlaz force-pushed the ns_apply_runlevels branch from d477ea7 to c6fa45c Compare March 6, 2020 16:01
If an annotation/label key ends with "-", remove that that key (ending
"-"s are trimmed) from the existing map instead.
@stlaz stlaz force-pushed the ns_apply_runlevels branch from c6fa45c to 3a6456f Compare March 6, 2020 16:08
requiredCopy := required.DeepCopy()
actual, err := client.Namespaces().
Create(resourcemerge.WithCleanLabelsAndAnnotations(requiredCopy).(*corev1.Namespace))
reportCreateEvent(recorder, requiredCopy, err)
Copy link
Contributor

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?

Copy link
Contributor Author

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 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, they are equal

Copy link
Contributor

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, "-"))
Copy link
Contributor

@sttts sttts Mar 9, 2020

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?

Copy link
Contributor Author

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.

Copy link
Contributor

@sttts sttts Mar 9, 2020

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---.

@sttts
Copy link
Contributor

sttts commented Mar 9, 2020

/lgtm
/approve

@openshift-ci-robot openshift-ci-robot added the lgtm Indicates that a PR is ready to be merged. label Mar 9, 2020
@openshift-ci-robot
Copy link

[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

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-ci-robot openshift-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Mar 9, 2020
@openshift-merge-robot openshift-merge-robot merged commit d18a5ca into openshift:master Mar 9, 2020
bertinatto pushed a commit to bertinatto/library-go that referenced this pull request Jul 2, 2020
resourceapply: Allow removing run-level labels from namespaces
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. lgtm Indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants