From e7fe03b69b1614fb8ef4eb15a5cd5a2e3cd8dfe7 Mon Sep 17 00:00:00 2001 From: Hector Ventura Date: Mon, 21 Sep 2026 06:04:45 -0500 Subject: [PATCH] refactor(cloudformation): share EC2 tag reconciliation (#4057) Ec2InstanceCfnProvisioner and Ec2SecurityGroupCfnProvisioner carried an identical tag-reconcile loop (describeTags, staleTagKeys, deleteTags, createTags); the instance one also hand-rolled tag resolution instead of using ProvisionContext.resolveTags. Extract the loop into Ec2Tags.reconcile(ec2Service, region, resourceId, desired) and drive both from ctx.resolveTags. Behavior-preserving: the loops were identical, and ctx.resolveTags matches the instance's hand-rolled parse (skipping blank keys, defaulting a null value to empty). Refs #3884 --- .../Ec2InstanceCfnProvisioner.java | 37 +-------------- .../Ec2SecurityGroupCfnProvisioner.java | 27 +---------- .../cloudformation/provisioners/Ec2Tags.java | 46 +++++++++++++++++++ 3 files changed, 48 insertions(+), 62 deletions(-) create mode 100644 src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2Tags.java diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2InstanceCfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2InstanceCfnProvisioner.java index 8045bddb74..448a89e6b6 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2InstanceCfnProvisioner.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2InstanceCfnProvisioner.java @@ -16,7 +16,6 @@ import org.jboss.logging.Logger; import java.util.ArrayList; import java.util.HashMap; -import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Set; @@ -89,7 +88,7 @@ public class Ec2InstanceCfnProvisioner implements CfnResourceProvisioner { if (prior != null && !createOnlyChanged(prior, imageId, subnetId, keyName)) { r.setPhysicalId(prior.getInstanceId()); publishInstanceAttributes(r, prior); - reconcileTags(prior.getInstanceId(), region, props, engine); + Ec2Tags.reconcile(ec2Service, region, prior.getInstanceId(), ctx.resolveTags(props, "Tags")); ReplacementCleanup.record(r, ctx, attributesBefore); return; } @@ -219,40 +218,6 @@ public class Ec2InstanceCfnProvisioner implements CfnResourceProvisioner { } /** Reconciles the instance tags to the template when the instance is kept in place. */ - private void reconcileTags(String instanceId, String region, JsonNode props, - CloudFormationTemplateEngine engine) { - Map desired = new LinkedHashMap<>(); - JsonNode tagsNode = props != null ? engine.resolveNode(props.get("Tags")) : null; - if (tagsNode != null && tagsNode.isArray()) { - for (JsonNode tag : tagsNode) { - String key = engine.resolve(tag.path("Key")); - if (key != null && !key.isEmpty()) { - String value = engine.resolve(tag.path("Value")); - desired.put(key, value == null ? "" : value); - } - } - } - Map current = new LinkedHashMap<>(); - for (Map entry : ec2Service.describeTags(region, Map.of("resource-id", List.of(instanceId)))) { - current.put(entry.get("key"), entry.get("value")); - } - List stale = ProvisionContext.staleTagKeys(current, desired); - if (!stale.isEmpty()) { - List remove = new ArrayList<>(); - for (String key : stale) { - remove.add(new Tag(key, current.get(key))); - } - ec2Service.deleteTags(region, List.of(instanceId), remove); - } - if (!desired.isEmpty()) { - List add = new ArrayList<>(); - for (Map.Entry entry : desired.entrySet()) { - add.add(new Tag(entry.getKey(), entry.getValue())); - } - ec2Service.createTags(region, List.of(instanceId), add); - } - } - @Override public boolean hasReplacementUpdate(StackResource resource) { return ReplacementCleanup.hasReplacement(resource); diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2SecurityGroupCfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2SecurityGroupCfnProvisioner.java index fe477b4626..7a6f8adba7 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2SecurityGroupCfnProvisioner.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2SecurityGroupCfnProvisioner.java @@ -10,7 +10,6 @@ import io.github.hectorvent.floci.services.ec2.model.IpRange; import io.github.hectorvent.floci.services.ec2.model.Ipv6Range; import io.github.hectorvent.floci.services.ec2.model.PrefixListId; import io.github.hectorvent.floci.services.ec2.model.SecurityGroup; -import io.github.hectorvent.floci.services.ec2.model.Tag; import io.github.hectorvent.floci.services.ec2.model.UserIdGroupPair; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; @@ -18,7 +17,6 @@ import org.jboss.logging.Logger; import java.util.ArrayList; import java.util.Arrays; -import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; @@ -104,7 +102,7 @@ public class Ec2SecurityGroupCfnProvisioner implements CfnResourceProvisioner { EGRESS_RULES_ATTR, perms -> ec2Service.authorizeSecurityGroupEgress(region, sg.getGroupId(), perms), perms -> ec2Service.revokeSecurityGroupEgress(region, sg.getGroupId(), perms)); - reconcileTags(props, sg.getGroupId(), region, ctx); + Ec2Tags.reconcile(ec2Service, region, sg.getGroupId(), ctx.resolveTags(props, "Tags")); } @Override @@ -176,29 +174,6 @@ public class Ec2SecurityGroupCfnProvisioner implements CfnResourceProvisioner { * declared tags and removes any the template dropped. AWS treats SecurityGroup Tags as an * in-place (no interruption) update. */ - private void reconcileTags(JsonNode props, String groupId, String region, ProvisionContext ctx) { - Map desired = ctx.resolveTags(props, "Tags"); - Map current = new LinkedHashMap<>(); - for (Map entry : ec2Service.describeTags(region, Map.of("resource-id", List.of(groupId)))) { - current.put(entry.get("key"), entry.get("value")); - } - List stale = ProvisionContext.staleTagKeys(current, desired); - if (!stale.isEmpty()) { - List remove = new ArrayList<>(); - for (String key : stale) { - remove.add(new Tag(key, current.get(key))); - } - ec2Service.deleteTags(region, List.of(groupId), remove); - } - if (!desired.isEmpty()) { - List add = new ArrayList<>(); - for (Map.Entry entry : desired.entrySet()) { - add.add(new Tag(entry.getKey(), entry.getValue())); - } - ec2Service.createTags(region, List.of(groupId), add); - } - } - /** * Resolves a peer group's name to its id, the same lookup {@code Ec2Service} performs when it * stores an authorized rule. Group names are unique per VPC rather than per region, so the diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2Tags.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2Tags.java new file mode 100644 index 0000000000..e58f930b3b --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2Tags.java @@ -0,0 +1,46 @@ +package io.github.hectorvent.floci.services.cloudformation.provisioners; + +import io.github.hectorvent.floci.services.ec2.Ec2Service; +import io.github.hectorvent.floci.services.ec2.model.Tag; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +/** + * Tag reconciliation shared by the EC2 provisioners ({@code Ec2InstanceCfnProvisioner} and + * {@code Ec2SecurityGroupCfnProvisioner}). Both drive it from the desired tags resolved via + * {@link ProvisionContext#resolveTags}; the reconcile itself is identical. + */ +final class Ec2Tags { + + private Ec2Tags() { + } + + /** + * Brings a resource's tags to {@code desired}: removes the keys AWS currently has that the + * template no longer declares, then creates or overwrites the declared ones. + */ + static void reconcile(Ec2Service ec2Service, String region, String resourceId, Map desired) { + Map current = new LinkedHashMap<>(); + for (Map entry : ec2Service.describeTags(region, Map.of("resource-id", List.of(resourceId)))) { + current.put(entry.get("key"), entry.get("value")); + } + List stale = ProvisionContext.staleTagKeys(current, desired); + if (!stale.isEmpty()) { + List remove = new ArrayList<>(); + for (String key : stale) { + remove.add(new Tag(key, current.get(key))); + } + ec2Service.deleteTags(region, List.of(resourceId), remove); + } + if (!desired.isEmpty()) { + List add = new ArrayList<>(); + for (Map.Entry entry : desired.entrySet()) { + add.add(new Tag(entry.getKey(), entry.getValue())); + } + ec2Service.createTags(region, List.of(resourceId), add); + } + } +}