mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(autoscaling): propagate ASG tags at launch (#1734)
* fix(autoscaling): propagate ASG tags at launch * docs: sync service action tables
This commit is contained in:
1 parent
291752c36e
commit
acedc30017
10 files changed
+117
-26
No files matched your search
+23
-10
@@ -151,7 +151,8 @@ public class AutoScalingQueryHandler {
|
||||
// ── Auto Scaling Group ────────────────────────────────────────────────────
|
||||
|
||||
private Response handleCreateAutoScalingGroup(MultivaluedMap<String, String> p, String region) {
|
||||
service.createAutoScalingGroup(region,
|
||||
ParsedTags parsedTags = parseTags(p);
|
||||
AutoScalingGroup asg = service.createAutoScalingGroup(region,
|
||||
p.getFirst("AutoScalingGroupName"),
|
||||
p.getFirst("LaunchConfigurationName"),
|
||||
p.getFirst("LaunchTemplate.LaunchTemplateId"),
|
||||
@@ -169,7 +170,8 @@ public class AutoScalingQueryHandler {
|
||||
p.getFirst("HealthCheckType"),
|
||||
intParam(p, "HealthCheckGracePeriod", 0),
|
||||
memberList(p, "TerminationPolicies"),
|
||||
parseTags(p));
|
||||
parsedTags.tags(),
|
||||
parsedTags.propagateAtLaunch());
|
||||
return ok(new XmlBuilder()
|
||||
.start("CreateAutoScalingGroupResponse", NS)
|
||||
.raw(AwsQueryResponse.responseMetadata())
|
||||
@@ -304,7 +306,7 @@ public class AutoScalingQueryHandler {
|
||||
.elem("Value", tag.getValue())
|
||||
.elem("ResourceId", asg.getAutoScalingGroupName())
|
||||
.elem("ResourceType", "auto-scaling-group")
|
||||
.elem("PropagateAtLaunch", "false")
|
||||
.elem("PropagateAtLaunch", String.valueOf(asg.getTagPropagateAtLaunch().getOrDefault(tag.getKey(), false)))
|
||||
.end("member");
|
||||
}
|
||||
xml.end("Tags");
|
||||
@@ -413,7 +415,12 @@ public class AutoScalingQueryHandler {
|
||||
|
||||
private Response handleCreateOrUpdateTags(MultivaluedMap<String, String> p, String region) {
|
||||
for (TagRequest tag : parseTagRequests(p)) {
|
||||
service.createOrUpdateTags(region, tag.resourceId(), tag.resourceType(), Map.of(tag.key(), tag.value()));
|
||||
service.createOrUpdateTags(
|
||||
region,
|
||||
tag.resourceId(),
|
||||
tag.resourceType(),
|
||||
Map.of(tag.key(), tag.value()),
|
||||
Map.of(tag.key(), tag.propagateAtLaunch()));
|
||||
}
|
||||
return ok(new XmlBuilder()
|
||||
.start("CreateOrUpdateTagsResponse", NS)
|
||||
@@ -1036,15 +1043,18 @@ public class AutoScalingQueryHandler {
|
||||
return result;
|
||||
}
|
||||
|
||||
private Map<String, String> parseTags(MultivaluedMap<String, String> p) {
|
||||
Map<String, String> result = new LinkedHashMap<>();
|
||||
private ParsedTags parseTags(MultivaluedMap<String, String> p) {
|
||||
Map<String, String> tags = new LinkedHashMap<>();
|
||||
Map<String, Boolean> propagateAtLaunch = new LinkedHashMap<>();
|
||||
for (int i = 1; ; i++) {
|
||||
String key = p.getFirst("Tags.member." + i + ".Key");
|
||||
if (key == null) { break; }
|
||||
String value = p.getFirst("Tags.member." + i + ".Value");
|
||||
result.put(key, value != null ? value : "");
|
||||
tags.put(key, value != null ? value : "");
|
||||
propagateAtLaunch.put(key,
|
||||
Boolean.parseBoolean(p.getFirst("Tags.member." + i + ".PropagateAtLaunch")));
|
||||
}
|
||||
return result;
|
||||
return new ParsedTags(tags, propagateAtLaunch);
|
||||
}
|
||||
|
||||
private List<TagRequest> parseTagRequests(MultivaluedMap<String, String> p) {
|
||||
@@ -1055,12 +1065,15 @@ public class AutoScalingQueryHandler {
|
||||
String resourceId = p.getFirst("Tags.member." + i + ".ResourceId");
|
||||
String resourceType = p.getFirst("Tags.member." + i + ".ResourceType");
|
||||
String value = p.getFirst("Tags.member." + i + ".Value");
|
||||
result.add(new TagRequest(resourceId, resourceType, key, value != null ? value : ""));
|
||||
boolean propagateAtLaunch = Boolean.parseBoolean(p.getFirst("Tags.member." + i + ".PropagateAtLaunch"));
|
||||
result.add(new TagRequest(resourceId, resourceType, key, value != null ? value : "", propagateAtLaunch));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private record TagRequest(String resourceId, String resourceType, String key, String value) {}
|
||||
private record TagRequest(String resourceId, String resourceType, String key, String value, boolean propagateAtLaunch) {}
|
||||
|
||||
private record ParsedTags(Map<String, String> tags, Map<String, Boolean> propagateAtLaunch) {}
|
||||
|
||||
private InstanceRefresh parseInstanceRefresh(MultivaluedMap<String, String> p) {
|
||||
InstanceRefresh refresh = new InstanceRefresh();
|
||||
|
||||
+19
-1
@@ -19,6 +19,7 @@ import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
import io.quarkus.runtime.StartupEvent;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
@@ -293,7 +294,7 @@ public class AutoScalingReconciler {
|
||||
launchSource.securityGroupIds(),
|
||||
subnetId,
|
||||
null,
|
||||
launchSource.instanceTags(),
|
||||
propagatedInstanceTags(asg, launchSource),
|
||||
launchSource.userData(),
|
||||
launchSource.iamInstanceProfile());
|
||||
|
||||
@@ -319,6 +320,23 @@ public class AutoScalingReconciler {
|
||||
}
|
||||
}
|
||||
|
||||
private static List<io.github.hectorvent.floci.services.ec2.model.Tag> propagatedInstanceTags(
|
||||
AutoScalingGroup asg,
|
||||
LaunchSource launchSource) {
|
||||
Map<String, String> tags = new LinkedHashMap<>();
|
||||
for (io.github.hectorvent.floci.services.ec2.model.Tag tag : launchSource.instanceTags()) {
|
||||
tags.put(tag.getKey(), tag.getValue());
|
||||
}
|
||||
asg.getTags().forEach((key, value) -> {
|
||||
if (asg.getTagPropagateAtLaunch().getOrDefault(key, false)) {
|
||||
tags.put(key, value);
|
||||
}
|
||||
});
|
||||
return tags.entrySet().stream()
|
||||
.map(entry -> new io.github.hectorvent.floci.services.ec2.model.Tag(entry.getKey(), entry.getValue()))
|
||||
.toList();
|
||||
}
|
||||
|
||||
private void scaleIn(AutoScalingGroup asg, int count) {
|
||||
List<AsgInstance> candidates = asg.getInstances().stream()
|
||||
.filter(i -> "InService".equals(i.getLifecycleState()))
|
||||
|
||||
+13
-2
@@ -164,7 +164,8 @@ public class AutoScalingService {
|
||||
List<String> targetGroupArns, List<String> lbNames,
|
||||
String healthCheckType, int healthCheckGracePeriod,
|
||||
List<String> terminationPolicies,
|
||||
Map<String, String> tags) {
|
||||
Map<String, String> tags,
|
||||
Map<String, Boolean> tagPropagateAtLaunch) {
|
||||
String key = asgKey(region, name);
|
||||
if (groups.containsKey(key)) {
|
||||
throw new AwsException("AlreadyExists",
|
||||
@@ -206,6 +207,9 @@ public class AutoScalingService {
|
||||
if (tags != null) {
|
||||
asg.getTags().putAll(tags);
|
||||
}
|
||||
if (tagPropagateAtLaunch != null) {
|
||||
asg.getTagPropagateAtLaunch().putAll(tagPropagateAtLaunch);
|
||||
}
|
||||
groups.put(key, asg);
|
||||
return asg;
|
||||
}
|
||||
@@ -326,15 +330,22 @@ public class AutoScalingService {
|
||||
groups.put(asgKey(region, name), asg);
|
||||
}
|
||||
|
||||
public void createOrUpdateTags(String region, String resourceId, String resourceType, Map<String, String> tags) {
|
||||
public void createOrUpdateTags(
|
||||
String region,
|
||||
String resourceId,
|
||||
String resourceType,
|
||||
Map<String, String> tags,
|
||||
Map<String, Boolean> tagPropagateAtLaunch) {
|
||||
AutoScalingGroup asg = requireTaggableGroup(region, resourceId, resourceType);
|
||||
asg.getTags().putAll(tags);
|
||||
asg.getTagPropagateAtLaunch().putAll(tagPropagateAtLaunch);
|
||||
groups.put(asgKey(region, asg.getAutoScalingGroupName()), asg);
|
||||
}
|
||||
|
||||
public void deleteTags(String region, String resourceId, String resourceType, List<String> tagKeys) {
|
||||
AutoScalingGroup asg = requireTaggableGroup(region, resourceId, resourceType);
|
||||
tagKeys.forEach(asg.getTags()::remove);
|
||||
tagKeys.forEach(asg.getTagPropagateAtLaunch()::remove);
|
||||
groups.put(asgKey(region, asg.getAutoScalingGroupName()), asg);
|
||||
}
|
||||
|
||||
|
||||
+4
@@ -35,6 +35,7 @@ public class AutoScalingGroup {
|
||||
private Instant createdTime;
|
||||
private String region;
|
||||
private Map<String, String> tags = new ConcurrentHashMap<>();
|
||||
private Map<String, Boolean> tagPropagateAtLaunch = new ConcurrentHashMap<>();
|
||||
private String status; // null = active, "Delete in progress" = deleting
|
||||
|
||||
public AutoScalingGroup() {}
|
||||
@@ -105,6 +106,9 @@ public class AutoScalingGroup {
|
||||
public Map<String, String> getTags() { return tags; }
|
||||
public void setTags(Map<String, String> v) { this.tags = v; }
|
||||
|
||||
public Map<String, Boolean> getTagPropagateAtLaunch() { return tagPropagateAtLaunch; }
|
||||
public void setTagPropagateAtLaunch(Map<String, Boolean> v) { this.tagPropagateAtLaunch = v; }
|
||||
|
||||
public String getStatus() { return status; }
|
||||
public void setStatus(String v) { this.status = v; }
|
||||
}
|
||||
+15
-1
@@ -847,7 +847,8 @@ public class CloudFormationResourceProvisioner {
|
||||
resolveOptional(props, "HealthCheckType", engine),
|
||||
parseIntProp(props, "HealthCheckGracePeriod", engine, 0),
|
||||
resolveStringList(props, "TerminationPolicies", engine),
|
||||
resolveAsgTags(props, engine));
|
||||
resolveAsgTags(props, engine),
|
||||
resolveAsgTagPropagation(props, engine));
|
||||
// Ref returns the Auto Scaling group name; Fn::GetAtt Arn returns the ASG ARN.
|
||||
r.setPhysicalId(name);
|
||||
r.getAttributes().put("Arn", asg.getAutoScalingGroupArn());
|
||||
@@ -866,6 +867,19 @@ public class CloudFormationResourceProvisioner {
|
||||
return tags;
|
||||
}
|
||||
|
||||
private Map<String, Boolean> resolveAsgTagPropagation(JsonNode props, CloudFormationTemplateEngine engine) {
|
||||
Map<String, Boolean> propagation = new LinkedHashMap<>();
|
||||
if (props != null && props.has("Tags") && props.get("Tags").isArray()) {
|
||||
for (JsonNode tag : props.get("Tags")) {
|
||||
String key = engine.resolve(tag.path("Key"));
|
||||
if (!key.isEmpty()) {
|
||||
propagation.put(key, Boolean.parseBoolean(engine.resolve(tag.path("PropagateAtLaunch"))));
|
||||
}
|
||||
}
|
||||
}
|
||||
return propagation;
|
||||
}
|
||||
|
||||
private List<String> resolveStringList(JsonNode props, String field, CloudFormationTemplateEngine engine) {
|
||||
List<String> values = new ArrayList<>();
|
||||
if (props != null && props.has(field) && props.get(field).isArray()) {
|
||||
|
||||
Reference in new issue
Block a user