mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(autoscaling): preserve launch configuration user data (#5600)
* fix(autoscaling): preserve launch configuration user data * fix(autoscaling): reject unusable launch configuration data
This commit is contained in:
1 parent
86b19047f3
commit
ddc0e0257b
10 files changed
+604
-19
No files matched your search
+24
-2
@@ -25,7 +25,9 @@ import jakarta.enterprise.event.Observes;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -365,7 +367,9 @@ public class AutoScalingReconciler {
|
||||
propagatedInstanceTags(asg, launchSource),
|
||||
launchSource.userData(),
|
||||
launchSource.iamInstanceProfile(),
|
||||
launchSource.associatePublicIpAddress());
|
||||
launchSource.associatePublicIpAddress(), null, 0, null, null, null,
|
||||
launchSource.encodedUserData() != null ? launchSource.encodedUserData()
|
||||
: encodeLegacyUserData(launchSource.userData()));
|
||||
|
||||
List<String> launchedInstanceIds = new ArrayList<>();
|
||||
for (Instance ec2Inst : reservation.getInstances()) {
|
||||
@@ -559,6 +563,16 @@ public class AutoScalingReconciler {
|
||||
private LaunchSource resolveLaunchSource(AutoScalingGroup asg) {
|
||||
LaunchConfiguration lc = resolveLaunchConfiguration(asg);
|
||||
if (lc != null) {
|
||||
String userData = lc.getUserData();
|
||||
if (Boolean.TRUE.equals(lc.getUserDataBase64Encoded())) {
|
||||
try {
|
||||
userData = Ec2UserDataDecoder.decode(userData);
|
||||
} catch (AwsException e) {
|
||||
LOG.warnv("ASG {0}: cannot decode user data for launch configuration {1}: {2}",
|
||||
asg.getAutoScalingGroupName(), lc.getLaunchConfigurationName(), e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return new LaunchSource(
|
||||
lc.getLaunchConfigurationName(),
|
||||
lc.getImageId(),
|
||||
@@ -566,7 +580,8 @@ public class AutoScalingReconciler {
|
||||
lc.getKeyName(),
|
||||
lc.getSecurityGroups(),
|
||||
List.of(),
|
||||
lc.getUserData(),
|
||||
userData,
|
||||
Boolean.TRUE.equals(lc.getUserDataBase64Encoded()) ? lc.getUserData() : null,
|
||||
lc.getIamInstanceProfile(),
|
||||
null,
|
||||
null,
|
||||
@@ -597,6 +612,7 @@ public class AutoScalingReconciler {
|
||||
version.getData().getInstanceTags(),
|
||||
Ec2UserDataDecoder.decodeIfMissing(
|
||||
version.getData().getUserData(), version.getData().getEncodedUserData()),
|
||||
null,
|
||||
ec2Service.iamInstanceProfileArn(version.getData()),
|
||||
asg.getLaunchTemplateId(),
|
||||
asg.getLaunchTemplateName(),
|
||||
@@ -628,6 +644,7 @@ public class AutoScalingReconciler {
|
||||
version.getData().getInstanceTags(),
|
||||
Ec2UserDataDecoder.decodeIfMissing(
|
||||
version.getData().getUserData(), version.getData().getEncodedUserData()),
|
||||
null,
|
||||
ec2Service.iamInstanceProfileArn(version.getData()),
|
||||
specification.getLaunchTemplateId() == null
|
||||
? mixedLaunchTemplate.getLaunchTemplateId()
|
||||
@@ -641,6 +658,10 @@ public class AutoScalingReconciler {
|
||||
return null;
|
||||
}
|
||||
|
||||
private static String encodeLegacyUserData(String userData) {
|
||||
return userData == null ? null : Base64.getEncoder().encodeToString(userData.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
private LaunchConfiguration resolveLaunchConfiguration(AutoScalingGroup asg) {
|
||||
String lcName = asg.getLaunchConfigurationName();
|
||||
if (lcName == null || lcName.isBlank()) {
|
||||
@@ -736,6 +757,7 @@ public class AutoScalingReconciler {
|
||||
List<String> securityGroupIds,
|
||||
List<io.github.hectorvent.floci.services.ec2.model.Tag> instanceTags,
|
||||
String userData,
|
||||
String encodedUserData,
|
||||
String iamInstanceProfile,
|
||||
String launchTemplateId,
|
||||
String launchTemplateName,
|
||||
|
||||
+10
-1
@@ -9,14 +9,17 @@ import io.github.hectorvent.floci.core.storage.StorageBackedMap;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.autoscaling.model.*;
|
||||
import io.github.hectorvent.floci.services.ec2.Ec2Service;
|
||||
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 jakarta.annotation.PostConstruct;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Instant;
|
||||
import java.util.*;
|
||||
import java.util.Base64;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -195,13 +198,18 @@ public class AutoScalingService {
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
if (isBlank(userData)) {
|
||||
userData = source.getUserData();
|
||||
userData = source.getEncodedUserData();
|
||||
if (userData == null && source.getUserData() != null) {
|
||||
userData = Base64.getEncoder().encodeToString(
|
||||
source.getUserData().getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
}
|
||||
if (isBlank(iamInstanceProfile)) {
|
||||
iamInstanceProfile = source.getIamInstanceProfileArn();
|
||||
}
|
||||
}
|
||||
}
|
||||
Ec2UserDataDecoder.decode(userData);
|
||||
LaunchConfiguration lc = new LaunchConfiguration();
|
||||
lc.setLaunchConfigurationName(name);
|
||||
lc.setLaunchConfigurationArn(
|
||||
@@ -212,6 +220,7 @@ public class AutoScalingService {
|
||||
lc.setKeyName(keyName);
|
||||
lc.setSecurityGroups(securityGroups != null ? new ArrayList<>(securityGroups) : new ArrayList<>());
|
||||
lc.setUserData(userData);
|
||||
lc.setUserDataBase64Encoded(Boolean.TRUE);
|
||||
lc.setIamInstanceProfile(iamInstanceProfile);
|
||||
lc.setAssociatePublicIpAddress(associatePublicIpAddress);
|
||||
// AWS documents InstanceMonitoring as enabled by default and always returns the
|
||||
|
||||
+6
@@ -18,6 +18,9 @@ public class LaunchConfiguration {
|
||||
private String keyName;
|
||||
private List<String> securityGroups = new ArrayList<>();
|
||||
private String userData;
|
||||
// Historical configurations mixed API base64 and decoded instance text. An absent marker
|
||||
// retains their existing launch behavior; only newly created records have known encoding.
|
||||
private Boolean userDataBase64Encoded;
|
||||
private String iamInstanceProfile;
|
||||
// Nullable on purpose: AWS treats an absent flag as "fall back to the
|
||||
// subnet's MapPublicIpOnLaunch" and an explicit false as an override.
|
||||
@@ -53,6 +56,9 @@ public class LaunchConfiguration {
|
||||
public String getUserData() { return userData; }
|
||||
public void setUserData(String v) { this.userData = v; }
|
||||
|
||||
public Boolean getUserDataBase64Encoded() { return userDataBase64Encoded; }
|
||||
public void setUserDataBase64Encoded(Boolean v) { this.userDataBase64Encoded = v; }
|
||||
|
||||
public String getIamInstanceProfile() { return iamInstanceProfile; }
|
||||
public void setIamInstanceProfile(String v) { this.iamInstanceProfile = v; }
|
||||
|
||||
|
||||
Reference in new issue
Block a user