mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(autoscaling): tag member instances with aws:autoscaling:groupName (#6471)
* fix(autoscaling): tag member instances with aws:autoscaling:groupName Auto Scaling adds the aws:autoscaling:groupName tag, valued with the group's name, to every instance it launches or attaches, and removes it when the instance is detached. Floci never set it, so filtering EC2 instances on tag:aws:autoscaling:groupName found none of a group's members. - Launches add the tag after the launch template's instance tags and the group's PropagateAtLaunch tags, so the group's actual name wins over any stored tag with that key. - AttachInstances applies the group's PropagateAtLaunch tags and the group-name tag to the instances EC2 holds. It refuses the whole batch with ValidationError when an instance already belongs to a group, so one group cannot overwrite another group's tag, and an instance named twice in one request joins once. An ID EC2 has no instance for still joins, as before, but gets no tags, which would otherwise outlive it in EC2's DescribeTags. - DetachInstances removes only the group-name tag, and only where it still names that group. It goes through a new service-owned EC2 tag delete, because the public DeleteTags refuses aws: keys. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * test(compat): cover the Auto Scaling group-name tag through the SDK Drives launch, attach, a refused second attach and detach with the AWS SDK for Java and checks the instance tags EC2 reports after each step. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> --------- Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>
This commit is contained in:
1 parent
75d7923290
commit
c517e4eb76
9 files changed
+610
-44
No files matched your search
+1
-18
@@ -596,7 +596,7 @@ public class AutoScalingReconciler {
|
||||
return ec2Service.runInstances(
|
||||
asg.getRegion(), launchSource.imageId(), launchSource.instanceType(), 1, 1,
|
||||
launchSource.keyName(), launchSource.securityGroupIds(), placement.subnetId(), clientToken,
|
||||
propagatedInstanceTags(asg, launchSource), launchSource.userData(),
|
||||
AutoScalingService.propagatedInstanceTags(asg, launchSource.instanceTags()), launchSource.userData(),
|
||||
launchSource.launchConfigurationName() == null ? launchSource.iamInstanceProfile()
|
||||
: asgService.resolveLaunchConfigurationProfile(launchSource.iamInstanceProfile()),
|
||||
launchSource.associatePublicIpAddress(), null, 0, launchZone, launchSource.metadataOptions(), null,
|
||||
@@ -718,23 +718,6 @@ public class AutoScalingReconciler {
|
||||
private record LaunchPlacement(String subnetId, String availabilityZone, int availableIpAddresses,
|
||||
boolean requiresIpv4Address) {}
|
||||
|
||||
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()))
|
||||
|
||||
+77
-17
@@ -16,6 +16,7 @@ import io.github.hectorvent.floci.services.ec2.Ec2UserDataDecoder;
|
||||
import io.github.hectorvent.floci.services.ec2.model.Instance;
|
||||
import io.github.hectorvent.floci.services.ec2.model.LaunchTemplate;
|
||||
import io.github.hectorvent.floci.services.ec2.model.LaunchTemplateData;
|
||||
import io.github.hectorvent.floci.services.ec2.model.Tag;
|
||||
import io.github.hectorvent.floci.services.iam.PassedRoleLookup;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
@@ -35,6 +36,9 @@ import java.util.stream.Collectors;
|
||||
@ApplicationScoped
|
||||
public class AutoScalingService implements PassedRoleLookup {
|
||||
|
||||
/** The tag Auto Scaling puts on every instance it launches or attaches, valued with the group's name. */
|
||||
static final String GROUP_NAME_TAG = "aws:autoscaling:groupName";
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(AutoScalingService.class);
|
||||
private static final String FULLY_FORMED_LAUNCH_TEMPLATE = "You must use a valid fully-formed launch template. ";
|
||||
static final String MISSING_LAUNCH_TEMPLATE_IMAGE_ID_MESSAGE =
|
||||
@@ -846,25 +850,77 @@ public class AutoScalingService implements PassedRoleLookup {
|
||||
return instanceId != null && asg.getInstances().stream().anyMatch(i -> instanceId.equals(i.getInstanceId()));
|
||||
}
|
||||
|
||||
/**
|
||||
* The tags an instance carries as a member of {@code asg}: the launch template's instance tags,
|
||||
* then the group's PropagateAtLaunch tags, which win a key both set, then the group-name tag,
|
||||
* which names {@code asg} whatever the other two say.
|
||||
*/
|
||||
static List<Tag> propagatedInstanceTags(AutoScalingGroup asg, List<Tag> templateTags) {
|
||||
Map<String, String> tags = new LinkedHashMap<>();
|
||||
templateTags.forEach(tag -> tags.put(tag.getKey(), tag.getValue()));
|
||||
asg.getTags().forEach((key, value) -> {
|
||||
if (asg.getTagPropagateAtLaunch().getOrDefault(key, false)) {
|
||||
tags.put(key, value);
|
||||
}
|
||||
});
|
||||
tags.put(GROUP_NAME_TAG, asg.getAutoScalingGroupName());
|
||||
return tags.entrySet().stream().map(entry -> new Tag(entry.getKey(), entry.getValue())).toList();
|
||||
}
|
||||
|
||||
public void attachInstances(String region, String name, List<String> instanceIds) {
|
||||
AutoScalingGroup asg = requireGroup(region, name);
|
||||
for (String id : instanceIds) {
|
||||
AsgInstance inst = new AsgInstance();
|
||||
inst.setInstanceId(id);
|
||||
inst.setLifecycleState("InService");
|
||||
inst.setHealthStatus("Healthy");
|
||||
inst.setAvailabilityZone(
|
||||
asg.getAvailabilityZones().isEmpty() ? region + "a" : asg.getAvailabilityZones().get(0));
|
||||
inst.setLaunchConfigurationName(asg.getLaunchConfigurationName());
|
||||
inst.setLaunchTemplateId(asg.getLaunchTemplateId());
|
||||
inst.setLaunchTemplateName(asg.getLaunchTemplateName());
|
||||
inst.setLaunchTemplateVersion(asg.getLaunchTemplateVersion());
|
||||
asg.getInstances().add(inst);
|
||||
// An instance joins once, however often the request names it.
|
||||
List<String> requested = new ArrayList<>(new LinkedHashSet<>(instanceIds));
|
||||
// The region lock keeps two groups from each attaching the same instance at once.
|
||||
synchronized (lockFor(region)) {
|
||||
synchronized (lockFor(asgKey(region, name))) {
|
||||
AutoScalingGroup asg = requireGroup(region, name);
|
||||
// An instance belongs to one group at a time, so the whole batch fails before any tag changes.
|
||||
List<String> conflicts = new ArrayList<>();
|
||||
for (AutoScalingGroup group : groups.values()) {
|
||||
if (group == null || !region.equals(group.getRegion())) {
|
||||
continue;
|
||||
}
|
||||
// Under the group's own lock, which guards its member list against the reconciler.
|
||||
synchronized (lockFor(asgKey(region, group.getAutoScalingGroupName()))) {
|
||||
for (AsgInstance member : group.getInstances()) {
|
||||
if (requested.contains(member.getInstanceId())) {
|
||||
conflicts.add("The Instance: " + member.getInstanceId()
|
||||
+ " is already part of AutoScalingGroup:" + group.getAutoScalingGroupName());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!conflicts.isEmpty()) {
|
||||
throw new AwsException("ValidationError", conflicts.toString(), 400);
|
||||
}
|
||||
// Attaching does not yet refuse an ID EC2 has no instance for, as AWS does, so such
|
||||
// an ID gets no tags: they would outlive it in EC2's DescribeTags.
|
||||
List<String> existing = requested.stream()
|
||||
.filter(id -> ec2Service.findInstanceForAccount(regionResolver.getAccountId(), region, id)
|
||||
.isPresent())
|
||||
.toList();
|
||||
if (!existing.isEmpty()) {
|
||||
ec2Service.createSystemTags(region, existing, propagatedInstanceTags(asg, List.of()));
|
||||
}
|
||||
for (String id : requested) {
|
||||
AsgInstance inst = new AsgInstance();
|
||||
inst.setInstanceId(id);
|
||||
inst.setLifecycleState("InService");
|
||||
inst.setHealthStatus("Healthy");
|
||||
inst.setAvailabilityZone(
|
||||
asg.getAvailabilityZones().isEmpty() ? region + "a" : asg.getAvailabilityZones().get(0));
|
||||
inst.setLaunchConfigurationName(asg.getLaunchConfigurationName());
|
||||
inst.setLaunchTemplateId(asg.getLaunchTemplateId());
|
||||
inst.setLaunchTemplateName(asg.getLaunchTemplateName());
|
||||
inst.setLaunchTemplateVersion(asg.getLaunchTemplateVersion());
|
||||
asg.getInstances().add(inst);
|
||||
}
|
||||
if (asg.getInstances().size() > asg.getDesiredCapacity()) {
|
||||
asg.setDesiredCapacity(asg.getInstances().size());
|
||||
}
|
||||
groups.put(asgKey(region, name), asg);
|
||||
}
|
||||
}
|
||||
if (asg.getInstances().size() > asg.getDesiredCapacity()) {
|
||||
asg.setDesiredCapacity(asg.getInstances().size());
|
||||
}
|
||||
groups.put(asgKey(region, name), asg);
|
||||
}
|
||||
|
||||
public void detachInstances(String region, String name, List<String> instanceIds,
|
||||
@@ -874,6 +930,10 @@ public class AutoScalingService implements PassedRoleLookup {
|
||||
AutoScalingGroup asg = requireGroup(region, name);
|
||||
List<String> detached = asg.getInstances().stream().map(AsgInstance::getInstanceId)
|
||||
.filter(instanceIds::contains).toList();
|
||||
if (!detached.isEmpty()) {
|
||||
// Detaching removes only the group-name tag, and only where it still names this group.
|
||||
ec2Service.deleteSystemTag(region, detached, GROUP_NAME_TAG, name);
|
||||
}
|
||||
releaseRefreshOwnership(asg, detached);
|
||||
asg.getInstances().removeIf(i -> instanceIds.contains(i.getInstanceId()));
|
||||
if (decrementDesiredCapacity) {
|
||||
|
||||
@@ -9984,6 +9984,15 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
public void deleteTags(String region, List<String> resourceIds, List<Tag> tagList) {
|
||||
validateTagTargets(region, resourceIds);
|
||||
validateUserTagKeys(tagList);
|
||||
deleteResourceTags(region, resourceIds, tagList);
|
||||
}
|
||||
|
||||
/** Service-owned tag cleanup, separate from the public EC2 DeleteTags operation. */
|
||||
public void deleteSystemTag(String region, List<String> resourceIds, String tagKey, String tagValue) {
|
||||
deleteResourceTags(region, resourceIds, List.of(new Tag(tagKey, tagValue)));
|
||||
}
|
||||
|
||||
private void deleteResourceTags(String region, List<String> resourceIds, List<Tag> tagList) {
|
||||
ensureDefaultResources(region);
|
||||
for (String resourceId : resourceIds) {
|
||||
withTopologyLockIfNeeded(region, resourceId, () -> {
|
||||
|
||||
Reference in new issue
Block a user