Skip to content

fix(metricalarm): sync tags via TagResource/UntagResource on update - #76

Open
mahasiva-amazon wants to merge 2 commits into
aws-controllers-k8s:mainfrom
mahasiva-amazon:fix/metricalarm-tag-sync
Open

fix(metricalarm): sync tags via TagResource/UntagResource on update#76
mahasiva-amazon wants to merge 2 commits into
aws-controllers-k8s:mainfrom
mahasiva-amazon:fix/metricalarm-tag-sync

Conversation

@mahasiva-amazon

@mahasiva-amazon mahasiva-amazon commented Jul 29, 2026

Copy link
Copy Markdown

Fixes aws-controllers-k8s/community#2985

PutMetricAlarm silently ignores the Tags field when updating an existing alarm. As a result, tag changes made to a MetricAlarm CR were never propagated to AWS.

Additionally, DescribeAlarms does not return tags, so tags must be fetched separately via ListTagsForResource on every reconcile.

Solution

This PR fixes both issues:

  1. Read path (sdk_read_many_post_set_output hook, committed in prior commit): Calls ListTagsForResource after every DescribeAlarms to populate Spec.Tags in the observed state, enabling accurate delta detection.

  2. Write path (this commit): Adds a syncTags function in hooks.go that is called from sdkUpdate whenever Spec.Tags differs. It calls TagResource to add/update tags and UntagResource to remove tags no longer in the spec.

  3. Short-circuit: A DifferentExcept("Spec.Tags") guard prevents an unnecessary PutMetricAlarm call when the delta is tags-only.

Files Changed

  • generator.yaml — adds sdk_update_pre_build_request hook for MetricAlarm
  • templates/hooks/metricalarm/sdk_update_pre_build_request.go.tpl — new hook template with tag sync and short-circuit logic
  • pkg/resource/metric_alarm/hooks.go — new file with syncTags implementation
  • pkg/resource/metric_alarm/sdk.go — regenerated with hook injected into sdkUpdate

Test plan

  • Create MetricAlarm CR with env: test tag — alarm created in AWS with correct tag
  • Patch CR to add team: platform tag — AWS alarm updated via TagResource
  • Patch CR to remove team: platform tag — AWS alarm updated via UntagResource
  • Tags-only update does not trigger PutMetricAlarm call
  • ACK.ResourceSynced=True and Ready=True after each reconcile
  • Builds cleanly with go build ./...
  • All unit tests pass

🤖 Generated with Claude Code

mahasiva-amazon and others added 2 commits July 27, 2026 20:53
DescribeAlarms does not return tags. Add a sdk_read_many_post_set_output
hook that calls ListTagsForResource after each DescribeAlarms response
so spec.Tags always reflects the current AWS state, eliminating spurious
reconcile loops when tags are present.

Mark AlarmArn as is_arn so the codegen maps it to
status.ackResourceMetadata.arn — required for the tags hook and for
proper ACK ARN-based cross-resource references.

Fix: test/e2e/metric_alarm.py uses ResourceARN (not ResourceName) for
list_tags_for_resource, which is the correct CloudWatch parameter.

Add test_tag_sync to TestMetricAlarm to exercise the full tag read-back
cycle, and add the metric_alarm_with_tags resource fixture.
PutMetricAlarm silently ignores the Tags field when updating an existing
alarm. This commit implements a syncTags hook that is called from
sdkUpdate whenever Spec.Tags differs between the desired and observed
states. The hook calls TagResource to add/update tags and UntagResource
to remove tags that are no longer in the spec.

A short-circuit is also added so that a tags-only delta does not trigger
an unnecessary PutMetricAlarm call (DifferentExcept guard).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@ack-prow
ack-prow Bot requested review from gustavodiaz7722 and sapphirew July 29, 2026 02:40
@ack-prow

ack-prow Bot commented Jul 29, 2026

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: mahasiva-amazon
Once this PR has been reviewed and has the lgtm label, please assign knottnt for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found 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

@ack-prow ack-prow Bot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Jul 29, 2026
@ack-prow

ack-prow Bot commented Jul 29, 2026

Copy link
Copy Markdown

Hi @mahasiva-amazon. Thanks for your PR.

I'm waiting for a aws-controllers-k8s member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work.

Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cloudwatch-controller: MetricAlarm tag updates fail on adopted resources

2 participants