mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -05:00
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
This commit is contained in:
1 parent
eb247a50aa
commit
e7fe03b69b
3 files changed
+48
-62
No files matched your search
+1
-36
@@ -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<String, String> 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<String, String> current = new LinkedHashMap<>();
|
||||
for (Map<String, String> entry : ec2Service.describeTags(region, Map.of("resource-id", List.of(instanceId)))) {
|
||||
current.put(entry.get("key"), entry.get("value"));
|
||||
}
|
||||
List<String> stale = ProvisionContext.staleTagKeys(current, desired);
|
||||
if (!stale.isEmpty()) {
|
||||
List<Tag> 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<Tag> add = new ArrayList<>();
|
||||
for (Map.Entry<String, String> 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);
|
||||
|
||||
+1
-26
@@ -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<String, String> desired = ctx.resolveTags(props, "Tags");
|
||||
Map<String, String> current = new LinkedHashMap<>();
|
||||
for (Map<String, String> entry : ec2Service.describeTags(region, Map.of("resource-id", List.of(groupId)))) {
|
||||
current.put(entry.get("key"), entry.get("value"));
|
||||
}
|
||||
List<String> stale = ProvisionContext.staleTagKeys(current, desired);
|
||||
if (!stale.isEmpty()) {
|
||||
List<Tag> 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<Tag> add = new ArrayList<>();
|
||||
for (Map.Entry<String, String> 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
|
||||
|
||||
+46
@@ -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<String, String> desired) {
|
||||
Map<String, String> current = new LinkedHashMap<>();
|
||||
for (Map<String, String> entry : ec2Service.describeTags(region, Map.of("resource-id", List.of(resourceId)))) {
|
||||
current.put(entry.get("key"), entry.get("value"));
|
||||
}
|
||||
List<String> stale = ProvisionContext.staleTagKeys(current, desired);
|
||||
if (!stale.isEmpty()) {
|
||||
List<Tag> 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<Tag> add = new ArrayList<>();
|
||||
for (Map.Entry<String, String> entry : desired.entrySet()) {
|
||||
add.add(new Tag(entry.getKey(), entry.getValue()));
|
||||
}
|
||||
ec2Service.createTags(region, List.of(resourceId), add);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user