-
Notifications
You must be signed in to change notification settings - Fork 121
Avoid setting tags if there are no changes, prevent conflict on every resource creation #476
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -351,10 +351,13 @@ func (t *Tagger) Initialize(ctx context.Context, mg xpresource.Managed) error { | |||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| pavedByte, err := setExternalTagsWithPaved(xpresource.GetExternalTags(mg), paved, t.fieldName) | ||||||||||||||||||||||||||||||||||||||||||||
| pavedByte, changed, err := setExternalTagsWithPaved(xpresource.GetExternalTags(mg), paved, t.fieldName) | ||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| if !changed { | ||||||||||||||||||||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| if err := json.Unmarshal(pavedByte, mg); err != nil { | ||||||||||||||||||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -364,21 +367,36 @@ func (t *Tagger) Initialize(ctx context.Context, mg xpresource.Managed) error { | |||||||||||||||||||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| func setExternalTagsWithPaved(externalTags map[string]string, paved *fieldpath.Paved, fieldName string) ([]byte, error) { | ||||||||||||||||||||||||||||||||||||||||||||
| func tagsUpToDate(tags map[string]*string, paved *fieldpath.Paved, tagField string) bool { | ||||||||||||||||||||||||||||||||||||||||||||
| curTags, _ := paved.GetStringObject(tagField) | ||||||||||||||||||||||||||||||||||||||||||||
| for k, v := range tags { | ||||||||||||||||||||||||||||||||||||||||||||
| if curTags[k] != *v { | ||||||||||||||||||||||||||||||||||||||||||||
| return false | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| return true | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+370
to
+378
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Two small safety concerns worth addressing in
🛡️ Suggested nil guard for k, v := range tags {
+ if v == nil {
+ return false
+ }
if curTags[k] != *v {
return false
}
}
📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| func setExternalTagsWithPaved(externalTags map[string]string, paved *fieldpath.Paved, fieldName string) ([]byte, bool, error) { | ||||||||||||||||||||||||||||||||||||||||||||
| tagField := fmt.Sprintf("spec.forProvider.%s", fieldName) | ||||||||||||||||||||||||||||||||||||||||||||
| tags := map[string]*string{ | ||||||||||||||||||||||||||||||||||||||||||||
| xpresource.ExternalResourceTagKeyKind: ptr.To(externalTags[xpresource.ExternalResourceTagKeyKind]), | ||||||||||||||||||||||||||||||||||||||||||||
| xpresource.ExternalResourceTagKeyName: ptr.To(externalTags[xpresource.ExternalResourceTagKeyName]), | ||||||||||||||||||||||||||||||||||||||||||||
| xpresource.ExternalResourceTagKeyProvider: ptr.To(externalTags[xpresource.ExternalResourceTagKeyProvider]), | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| if err := paved.SetValue(fmt.Sprintf("spec.forProvider.%s", fieldName), tags); err != nil { | ||||||||||||||||||||||||||||||||||||||||||||
| return nil, err | ||||||||||||||||||||||||||||||||||||||||||||
| if tagsUpToDate(tags, paved, tagField) { | ||||||||||||||||||||||||||||||||||||||||||||
| return nil, false, nil | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| if err := paved.SetValue(tagField, tags); err != nil { | ||||||||||||||||||||||||||||||||||||||||||||
| return nil, false, err | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| pavedByte, err := paved.MarshalJSON() | ||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||
| return nil, err | ||||||||||||||||||||||||||||||||||||||||||||
| return nil, false, err | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| return pavedByte, nil | ||||||||||||||||||||||||||||||||||||||||||||
| return pavedByte, true, nil | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| type InjectedKey struct { | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
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.
Worth confirming: the subset-check is intentional.
tagsUpToDateonly verifies that the three desired system tags are present with the correct values — it doesn't require the tag field to contain exactly those three keys. This creates a subtle asymmetry:tagsUpToDate = true→ update skipped → user tags are preserved ✅tagsUpToDate = false→paved.SetValue(tagField, tags)replaces the entire field with only the three system tags → extra user tags are lostThe first case is an improvement over the old behavior (which always replaced). But the second case is unchanged and still discards user-defined tags when a system tag needs updating. Is this an acceptable trade-off, or should
setExternalTagsWithPavedbe doing a merge instead of a full replace when it does write? Happy to discuss the intent here — just want to make sure this asymmetry is deliberate.🤖 Prompt for AI Agents