mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(cloudformation): update an EKS cluster's logging, authentication mode and endpoint access in place (#8871)
* fix(cloudformation): update an EKS cluster's logging, authentication mode and endpoint access in place * test(cloudformation): pin an EKS cluster's config updated in place and rolled back * fix(cloudformation): fail an EKS cluster update that drops its subnet or security group list * test(cloudformation): pin a refused EKS subnet or security group change * fix(cloudformation): restore only the EKS cluster settings an update changed * test(cloudformation): pin an EKS rollback that keeps settings the update did not change
This commit is contained in:
1 parent
896a5cde34
commit
9b19f17fe7
6 files changed
+282
-10
No files matched your search
@@ -219,7 +219,7 @@ cross-resource references.
|
||||
| ECR | `Repository` |
|
||||
| ECS | `Cluster`, `TaskDefinition`, `Service`, `CapacityProvider`, `ClusterCapacityProviderAssociations` |
|
||||
| EFS | `FileSystem` (metadata only, there is no NFS data plane), `MountTarget` (`SecurityGroups` update in place, every other property replaces), `AccessPoint` (`AccessPointTags` update in place, every other property replaces) |
|
||||
| EKS | `Cluster` (created with `Name`, `Version`, `RoleArn`, `Tags`, `EncryptionConfig`, `ResourcesVpcConfig`, `Logging`, `AccessConfig` and `KubernetesNetworkConfig.IpFamily` and `ServiceIpv4Cidr`, a replacement included; a change to `EncryptionConfig`, `IpFamily` or `ServiceIpv4Cidr` replaces the cluster with the new value applied; `ElasticLoadBalancing`, `ServiceIpv6Cidr` and `ResourcesVpcConfig.ControlPlaneEgressMode` are accepted and ignored; updated in place for `Tags`, keeping tags added outside the stack; a change to `Name`, `RoleArn`, `EncryptionConfig`, `BootstrapSelfManagedAddons`, `OutpostConfig`, `AccessConfig.BootstrapClusterCreatorAdminPermissions`, `KubernetesNetworkConfig.IpFamily` or `KubernetesNetworkConfig.ServiceIpv4Cidr` replaces the cluster, as the registry marks them create-only, and deletes the old one once the update commits; a change to `Version`, `ResourcesVpcConfig`, `Logging` or `AccessConfig` is accepted with a warning and not applied, as the emulator has no `UpdateClusterVersion` or `UpdateClusterConfig`), `Nodegroup` (created with `ClusterName`, `NodegroupName`, `NodeRole`, `Subnets` and `Tags`; updated in place for `Tags`, keeping tags added outside the stack; a change to `ClusterName`, `NodegroupName`, `NodeRole`, `Subnets`, `AmiType`, `CapacityType`, `DiskSize`, `InstanceTypes` or `RemoteAccess` replaces the nodegroup, as the registry marks them create-only, and deletes the old one once the update commits; `ScalingConfig`, `Labels`, `Taints`, `UpdateConfig`, `Version`, `ReleaseVersion`, `ForceUpdateEnabled`, `LaunchTemplate`, `NodeRepairConfig` and `WarmPoolConfig` are accepted and not applied, whether changed or dropped, with a warning, as the emulator cannot update a nodegroup in place) |
|
||||
| EKS | `Cluster` (created with `Name`, `Version`, `RoleArn`, `Tags`, `EncryptionConfig`, `ResourcesVpcConfig`, `Logging`, `AccessConfig` and `KubernetesNetworkConfig.IpFamily` and `ServiceIpv4Cidr`, a replacement included; a change to `EncryptionConfig`, `IpFamily` or `ServiceIpv4Cidr` replaces the cluster with the new value applied; `ElasticLoadBalancing`, `ServiceIpv6Cidr` and `ResourcesVpcConfig.ControlPlaneEgressMode` are accepted and ignored; updated in place for `Tags`, keeping tags added outside the stack, for `Logging`, for `AccessConfig.AuthenticationMode` (a mode cannot be moved back, so a stack update that does so fails, and a stack rollback restores it) and for `ResourcesVpcConfig` endpoint access and `PublicAccessCidrs`; a change to `Name`, `RoleArn`, `EncryptionConfig`, `BootstrapSelfManagedAddons`, `OutpostConfig`, `AccessConfig.BootstrapClusterCreatorAdminPermissions`, `KubernetesNetworkConfig.IpFamily` or `KubernetesNetworkConfig.ServiceIpv4Cidr` replaces the cluster, as the registry marks them create-only, and deletes the old one once the update commits; a change to `Version` is accepted with a warning and not applied, as the emulator has no `UpdateClusterVersion`; a change to the subnets or security groups of `ResourcesVpcConfig` fails the resource, as the emulator's `UpdateClusterConfig` does not change them), `Nodegroup` (created with `ClusterName`, `NodegroupName`, `NodeRole`, `Subnets` and `Tags`; updated in place for `Tags`, keeping tags added outside the stack; a change to `ClusterName`, `NodegroupName`, `NodeRole`, `Subnets`, `AmiType`, `CapacityType`, `DiskSize`, `InstanceTypes` or `RemoteAccess` replaces the nodegroup, as the registry marks them create-only, and deletes the old one once the update commits; `ScalingConfig`, `Labels`, `Taints`, `UpdateConfig`, `Version`, `ReleaseVersion`, `ForceUpdateEnabled`, `LaunchTemplate`, `NodeRepairConfig` and `WarmPoolConfig` are accepted and not applied, whether changed or dropped, with a warning, as the emulator cannot update a nodegroup in place) |
|
||||
| RDS | `DBInstance` (starts a real container), `DBCluster` (starts a real container), `DBSubnetGroup`, `DBParameterGroup`, `DBClusterParameterGroup`, `DBProxy`, `DBProxyTargetGroup` |
|
||||
| Aurora DSQL | `Cluster` |
|
||||
| ElastiCache | `CacheCluster` (starts a real container; modifying an existing cluster in place is not supported), `ReplicationGroup` (starts a real container; modifying an existing group in place is not supported), `SubnetGroup`, `User` (Tags are ignored), `UserGroup` (Tags are ignored), `ServerlessCache` (GetAtt uses the dotted Endpoint.* and ReaderEndpoint.* form) |
|
||||
|
||||
+154
-8
@@ -8,12 +8,15 @@ import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.ReservedTags;
|
||||
import io.floci.aws.services.cloudformation.model.StackResource;
|
||||
import io.floci.aws.services.eks.EksService;
|
||||
import io.floci.aws.services.eks.model.AccessConfig;
|
||||
import io.floci.aws.services.eks.model.Cluster;
|
||||
import io.floci.aws.services.eks.model.CreateClusterRequest;
|
||||
import io.floci.aws.services.eks.model.KubernetesNetworkConfig;
|
||||
import io.floci.aws.services.eks.model.LogSetup;
|
||||
import io.floci.aws.services.eks.model.Logging;
|
||||
import io.floci.aws.services.eks.model.Nodegroup;
|
||||
import io.floci.aws.services.eks.model.ResourcesVpcConfig;
|
||||
import io.floci.aws.services.eks.model.UpdateClusterConfigRequest;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
@@ -21,10 +24,12 @@ import org.jboss.logging.Logger;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
@@ -49,6 +54,8 @@ public class EksCfnProvisioner implements CfnResourceProvisioner {
|
||||
/** Every attribute {@code publishNodegroup} writes for a nodegroup. */
|
||||
private static final List<String> NODEGROUP_ATTRIBUTES = List.of("Id", "ClusterName", "NodegroupName", "Arn");
|
||||
private static final String SNAPSHOT_ATTR = CfnRollback.EKS_CLUSTER_UPDATE_SNAPSHOT_ATTR;
|
||||
/** The cluster properties updateInPlace applies through UpdateClusterConfig, besides Tags. */
|
||||
private static final Set<String> CLUSTER_APPLIED = Set.of("ResourcesVpcConfig", "Logging", "AccessConfig");
|
||||
private static final String NODEGROUP_SNAPSHOT_ATTR = CfnRollback.EKS_NODEGROUP_UPDATE_SNAPSHOT_ATTR;
|
||||
// The tag keys the last deployment resolved, reserved ones included, as Ec2Tags.recordKeys records
|
||||
// them, so an update removes only those and keeps a tag added outside the stack.
|
||||
@@ -233,25 +240,41 @@ public class EksCfnProvisioner implements CfnResourceProvisioner {
|
||||
|
||||
/**
|
||||
* The update AWS makes for the properties it does not replace on: the cluster keeps its name.
|
||||
* EksService has no UpdateClusterVersion or UpdateClusterConfig, so the one mutable property
|
||||
* floci honors is Tags, and a change to any other property is accepted with a warning.
|
||||
* Tags, Logging, AccessConfig.AuthenticationMode and the endpoint access members of ResourcesVpcConfig
|
||||
* are applied, and only when the template's value differs from the live cluster's. EksService has no
|
||||
* UpdateClusterVersion, so a changed Version is accepted with a warning and not applied. AWS lets
|
||||
* UpdateClusterConfig choose different subnets and security groups, but floci does not apply that, so a
|
||||
* changed or dropped subnet or security group list fails the resource. The snapshot holds, and the
|
||||
* rollback restores, only the config families the update changes, taken from the same read the request is
|
||||
* computed from.
|
||||
*/
|
||||
private void updateInPlace(StackResource r, JsonNode props, ProvisionContext ctx, Cluster live,
|
||||
PropertyChanges changes) {
|
||||
warnIgnoredChanges("cluster", live.getName(), changes);
|
||||
warnIgnoredChanges("cluster", live.getName(), changes, CLUSTER_APPLIED);
|
||||
// One read of the cluster: the request, the snapshot and the apply all start from it.
|
||||
Cluster current = currentOr(live);
|
||||
UpdateClusterConfigRequest configRequest = configRequest(props, ctx, changes, current);
|
||||
updateTagsInPlace(r, props, ctx, live.getArn(), live.getTags(), live.getName(), TAG_KEYS_ATTR, SNAPSHOT_ATTR,
|
||||
() -> publish(r, live), true);
|
||||
() -> publish(r, live), true, snapshot -> {
|
||||
snapshot.put("name", live.getName());
|
||||
snapshotConfig(snapshot, current, configRequest);
|
||||
}, () -> {
|
||||
if (configRequest != null) {
|
||||
eksService.updateClusterConfig(live.getName(), configRequest);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Nothing create-only changed on the in-place path, so every other property the engine reports
|
||||
* as added, changed or dropped is one EksService has no call to apply: it is accepted with a
|
||||
* warning. Tags are applied by {@link #updateTagsInPlace}.
|
||||
* warning. Tags are applied by {@link #updateTagsInPlace}, and {@code applied} names the others the caller applies.
|
||||
*/
|
||||
private static void warnIgnoredChanges(String kind, String name, PropertyChanges changes) {
|
||||
private static void warnIgnoredChanges(String kind, String name, PropertyChanges changes, Set<String> applied) {
|
||||
List<String> ignored = new ArrayList<>();
|
||||
for (PropertyChanges.PropertyChange change : changes.byName().values()) {
|
||||
if (change.kind() != PropertyChanges.Kind.UNCHANGED && !"Tags".equals(change.name())) {
|
||||
if (change.kind() != PropertyChanges.Kind.UNCHANGED && !"Tags".equals(change.name())
|
||||
&& !applied.contains(change.name())) {
|
||||
ignored.add(change.name());
|
||||
}
|
||||
}
|
||||
@@ -269,6 +292,18 @@ public class EksCfnProvisioner implements CfnResourceProvisioner {
|
||||
private void updateTagsInPlace(StackResource r, JsonNode props, ProvisionContext ctx, String arn,
|
||||
Map<String, String> currentTags, String name, String keysAttr,
|
||||
String snapshotAttr, Runnable publish, boolean warnReserved) {
|
||||
updateTagsInPlace(r, props, ctx, arn, currentTags, name, keysAttr, snapshotAttr, publish, warnReserved,
|
||||
snapshot -> { }, () -> { });
|
||||
}
|
||||
|
||||
/**
|
||||
* As above, with {@code snapshotExtra} adding what the rollback restores and {@code applyExtra} running
|
||||
* with the tag convergence when the update is applied.
|
||||
*/
|
||||
private void updateTagsInPlace(StackResource r, JsonNode props, ProvisionContext ctx, String arn,
|
||||
Map<String, String> currentTags, String name, String keysAttr,
|
||||
String snapshotAttr, Runnable publish, boolean warnReserved,
|
||||
Consumer<ObjectNode> snapshotExtra, Runnable applyExtra) {
|
||||
Map<String, String> templateTags = ctx.resolveTags(props, "Tags");
|
||||
Map<String, String> desired = ReservedTags.stripReservedTags(templateTags);
|
||||
Map<String, String> liveTags = currentTags == null ? Map.of() : currentTags;
|
||||
@@ -281,10 +316,12 @@ public class EksCfnProvisioner implements CfnResourceProvisioner {
|
||||
}
|
||||
ObjectNode snapshot = MAPPER.createObjectNode();
|
||||
snapshot.put("arn", arn);
|
||||
snapshotExtra.accept(snapshot);
|
||||
Ec2Tags.snapshotOwned(snapshot, owned, desired, liveTags, r.getAttributes().get(keysAttr));
|
||||
publish.run();
|
||||
r.getAttributes().put(snapshotAttr, snapshot.toString());
|
||||
CfnRollback.guarded(r, ctx, false, () -> { }, () -> restore(r, snapshot, keysAttr), () -> {
|
||||
applyExtra.run();
|
||||
converge(arn, desired, Ec2Tags.touched(owned, desired));
|
||||
Ec2Tags.recordKeys(r, keysAttr, templateTags);
|
||||
});
|
||||
@@ -305,6 +342,108 @@ public class EksCfnProvisioner implements CfnResourceProvisioner {
|
||||
name, key));
|
||||
}
|
||||
|
||||
/**
|
||||
* The UpdateClusterConfig request for the families whose changed template value differs from the live
|
||||
* cluster, or null when none does. A member the template dropped is read as its AWS default: public access
|
||||
* on, private access off, every address allowed and no log type exported; floci applies that default itself.
|
||||
* A dropped AuthenticationMode asks for nothing, as a mode cannot be moved back. The subnet and security
|
||||
* group lists go along as the template has them, a dropped list counting as an empty one, so the service
|
||||
* refuses one that differs from the live cluster's.
|
||||
*/
|
||||
private UpdateClusterConfigRequest configRequest(JsonNode props, ProvisionContext ctx, PropertyChanges changes,
|
||||
Cluster live) {
|
||||
ResourcesVpcConfig vpc = null;
|
||||
Logging logging = null;
|
||||
AccessConfig access = null;
|
||||
ResourcesVpcConfig current = live.getResourcesVpcConfig();
|
||||
if (changed(changes, "ResourcesVpcConfig") && current != null) {
|
||||
ResourcesVpcConfig desired = createSetting(props, "ResourcesVpcConfig", ctx, new TypeReference<>() { });
|
||||
boolean publicAccess = desired == null || desired.getEndpointPublicAccess() == null
|
||||
|| desired.getEndpointPublicAccess();
|
||||
boolean privateAccess = desired != null && Boolean.TRUE.equals(desired.getEndpointPrivateAccess());
|
||||
List<String> cidrs = desired == null || desired.getPublicAccessCidrs() == null
|
||||
? List.of("0.0.0.0/0") : desired.getPublicAccessCidrs();
|
||||
List<String> subnets = desired == null ? null
|
||||
: desired.getSubnetIds() == null ? List.of() : desired.getSubnetIds();
|
||||
List<String> securityGroups = desired == null ? null
|
||||
: desired.getSecurityGroupIds() == null ? List.of() : desired.getSecurityGroupIds();
|
||||
boolean listsDiffer = desired != null && (differs(subnets, current.getSubnetIds())
|
||||
|| differs(securityGroups, current.getSecurityGroupIds()));
|
||||
if (listsDiffer || publicAccess != Boolean.TRUE.equals(current.getEndpointPublicAccess())
|
||||
|| privateAccess != Boolean.TRUE.equals(current.getEndpointPrivateAccess())
|
||||
|| differs(cidrs, current.getPublicAccessCidrs())) {
|
||||
vpc = new ResourcesVpcConfig();
|
||||
vpc.setEndpointPublicAccess(publicAccess);
|
||||
vpc.setEndpointPrivateAccess(privateAccess);
|
||||
vpc.setPublicAccessCidrs(cidrs);
|
||||
vpc.setSubnetIds(subnets);
|
||||
vpc.setSecurityGroupIds(securityGroups);
|
||||
}
|
||||
}
|
||||
if (changed(changes, "Logging")) {
|
||||
Logging enabled = logging(props, ctx);
|
||||
List<String> on = enabled == null ? List.of() : enabled.getClusterLogging().get(0).getTypes();
|
||||
if (!new HashSet<>(on).equals(EksService.enabledLogTypes(live.getLogging()))) {
|
||||
List<String> off = new ArrayList<>(EksService.ALL_LOG_TYPES);
|
||||
off.removeAll(on);
|
||||
List<LogSetup> entries = new ArrayList<>();
|
||||
if (!on.isEmpty()) {
|
||||
entries.add(new LogSetup(on, true));
|
||||
}
|
||||
if (!off.isEmpty()) {
|
||||
entries.add(new LogSetup(off, false));
|
||||
}
|
||||
logging = new Logging(entries);
|
||||
}
|
||||
}
|
||||
if (changed(changes, "AccessConfig")) {
|
||||
AccessConfig desired = createSetting(props, "AccessConfig", ctx, new TypeReference<>() { });
|
||||
AccessConfig liveAccess = live.getAccessConfig();
|
||||
if (desired != null && desired.authenticationMode() != null && (liveAccess == null
|
||||
|| !desired.authenticationMode().equals(liveAccess.authenticationMode()))) {
|
||||
access = new AccessConfig(desired.authenticationMode(), null);
|
||||
}
|
||||
}
|
||||
return vpc == null && logging == null && access == null ? null
|
||||
: new UpdateClusterConfigRequest(vpc, logging, access, null);
|
||||
}
|
||||
|
||||
private static boolean changed(PropertyChanges changes, String name) {
|
||||
return !changes.unchanged().contains(name) && changes.kindOf(name) != null;
|
||||
}
|
||||
|
||||
private static boolean differs(List<String> desired, List<String> live) {
|
||||
return desired != null && !new HashSet<>(desired).equals(new HashSet<>(live == null ? List.of() : live));
|
||||
}
|
||||
|
||||
/** The cluster as it is now, or {@code live} when it cannot be read again. */
|
||||
private Cluster currentOr(Cluster live) {
|
||||
Cluster current = liveCluster(live.getName());
|
||||
return current == null ? live : current;
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the live value of each family {@code request} changes, and only those, so the rollback restores
|
||||
* only what this update wrote and leaves a family another writer changed since untouched. A null request
|
||||
* (a tag-only update) saves no config.
|
||||
*/
|
||||
private static void snapshotConfig(ObjectNode snapshot, Cluster live, UpdateClusterConfigRequest request) {
|
||||
if (request == null) {
|
||||
return;
|
||||
}
|
||||
ResourcesVpcConfig vpc = live.getResourcesVpcConfig();
|
||||
if (request.resourcesVpcConfig() != null && vpc != null) {
|
||||
snapshot.set("endpoint", MAPPER.valueToTree(vpc));
|
||||
}
|
||||
if (request.logging() != null && live.getLogging() != null) {
|
||||
snapshot.set("logging", MAPPER.valueToTree(live.getLogging()));
|
||||
}
|
||||
if (request.accessConfig() != null && live.getAccessConfig() != null
|
||||
&& live.getAccessConfig().authenticationMode() != null) {
|
||||
snapshot.put("authenticationMode", live.getAccessConfig().authenticationMode());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Drives the tags of a cluster or nodegroup to {@code tags}: the one path an update and its rollback take.
|
||||
*/
|
||||
@@ -323,6 +462,13 @@ public class EksCfnProvisioner implements CfnResourceProvisioner {
|
||||
List<String> touched = new ArrayList<>();
|
||||
snapshot.path("touched").forEach(key -> touched.add(key.asText()));
|
||||
converge(CfnRollback.snapshotText(snapshot, "arn"), CfnRollback.snapshotTags(snapshot), touched);
|
||||
String name = CfnRollback.snapshotText(snapshot, "name");
|
||||
if (name != null && (snapshot.has("endpoint") || snapshot.has("logging") || snapshot.has("authenticationMode"))) {
|
||||
eksService.restoreClusterConfig(name,
|
||||
snapshot.has("endpoint") ? MAPPER.convertValue(snapshot.get("endpoint"), ResourcesVpcConfig.class) : null,
|
||||
snapshot.has("logging") ? MAPPER.convertValue(snapshot.get("logging"), Logging.class) : null,
|
||||
snapshot.has("authenticationMode") ? snapshot.get("authenticationMode").asText() : null);
|
||||
}
|
||||
Ec2Tags.restoreRecord(r, keysAttr, snapshot);
|
||||
}
|
||||
|
||||
@@ -494,7 +640,7 @@ public class EksCfnProvisioner implements CfnResourceProvisioner {
|
||||
*/
|
||||
private void updateNodegroupInPlace(StackResource r, JsonNode props, ProvisionContext ctx, Nodegroup live,
|
||||
PropertyChanges changes) {
|
||||
warnIgnoredChanges("nodegroup", live.getNodegroupName(), changes);
|
||||
warnIgnoredChanges("nodegroup", live.getNodegroupName(), changes, Set.of());
|
||||
updateTagsInPlace(r, props, ctx, live.getNodegroupArn(), live.getTags(), live.getNodegroupName(),
|
||||
NODEGROUP_TAG_KEYS_ATTR, NODEGROUP_SNAPSHOT_ATTR, () -> publishNodegroup(r, live), false);
|
||||
}
|
||||
|
||||
@@ -1186,6 +1186,29 @@ public class EksService implements TagHandler, ResourceProvider, Ec2InstanceLaun
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Puts back values an update changed, with no AWS rules applied: a stack rollback may move the
|
||||
* authentication mode backwards, which UpdateClusterConfig refuses.
|
||||
*/
|
||||
public synchronized void restoreClusterConfig(String name, ResourcesVpcConfig endpoint, Logging logging,
|
||||
String authenticationMode) {
|
||||
Cluster cluster = requireClusterInRequestRegion(name);
|
||||
saveClusterIfNotDeleting(regionResolver.getAccountId(), clusterKey(cluster), current -> {
|
||||
if (endpoint != null && current.getResourcesVpcConfig() != null) {
|
||||
current.getResourcesVpcConfig().setEndpointPublicAccess(endpoint.getEndpointPublicAccess());
|
||||
current.getResourcesVpcConfig().setEndpointPrivateAccess(endpoint.getEndpointPrivateAccess());
|
||||
current.getResourcesVpcConfig().setPublicAccessCidrs(endpoint.getPublicAccessCidrs());
|
||||
}
|
||||
if (logging != null) {
|
||||
current.setLogging(logging);
|
||||
}
|
||||
if (authenticationMode != null && current.getAccessConfig() != null) {
|
||||
current.setAccessConfig(new AccessConfig(authenticationMode,
|
||||
current.getAccessConfig().bootstrapClusterCreatorAdminPermissions()));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/** The log types a {@link Logging} setting enables; empty for a null setting. */
|
||||
public static Set<String> enabledLogTypes(Logging logging) {
|
||||
Set<String> enabled = new HashSet<>();
|
||||
|
||||
+77
@@ -12,6 +12,7 @@ import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static io.floci.aws.services.cloudformation.CfnStackTestSupport.createUpdateChangeSet;
|
||||
@@ -322,6 +323,68 @@ class CloudFormationEksIntegrationTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void aClustersConfigIsRestoredWhenAnUpdateRollsBackAndChangedInPlaceWhenItSucceeds() {
|
||||
String stack = "cfn-eks-cfgupd-" + Long.toString(System.nanoTime(), 36);
|
||||
try (CloudFormationClient client = AwsTestClients.create(
|
||||
CloudFormationClient.builder(), "test", Region.US_EAST_1)) {
|
||||
CfnStackWaits.createStack(client, stack, configStack(false, false), "CREATE_COMPLETE");
|
||||
String id = physicalId(client, stack, "Subject");
|
||||
|
||||
CfnStackWaits.updateStack(client, stack, configStack(true, true), "UPDATE_ROLLBACK_COMPLETE");
|
||||
JsonPath cluster = given().get("/clusters/" + physicalId(client, stack, "Subject")).jsonPath();
|
||||
assertEquals(true, cluster.getBoolean("cluster.resourcesVpcConfig.endpointPublicAccess"));
|
||||
assertEquals("API_AND_CONFIG_MAP", cluster.getString("cluster.accessConfig.authenticationMode"));
|
||||
assertEquals(List.of(), cluster.getList("cluster.logging.clusterLogging.findAll { it.enabled == true }"));
|
||||
|
||||
CfnStackWaits.updateStack(client, stack, configStack(true, false), "UPDATE_COMPLETE");
|
||||
assertEquals(id, physicalId(client, stack, "Subject"));
|
||||
cluster = given().get("/clusters/" + id).jsonPath();
|
||||
assertEquals(false, cluster.getBoolean("cluster.resourcesVpcConfig.endpointPublicAccess"));
|
||||
assertEquals(true, cluster.getBoolean("cluster.resourcesVpcConfig.endpointPrivateAccess"));
|
||||
assertEquals(List.of("api"),
|
||||
cluster.getList("cluster.logging.clusterLogging.find { it.enabled == true }.types"));
|
||||
assertEquals("API", cluster.getString("cluster.accessConfig.authenticationMode"));
|
||||
} finally {
|
||||
CfnStackWaits.deleteStack(stack);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void aRefusedSubnetOrSecurityGroupChangeRollsTheStackBackAndAReorderingIsNoChange() {
|
||||
String stack = "cfn-eks-net-" + Long.toString(System.nanoTime(), 36);
|
||||
// {subnets, security groups, expected status}: a replaced subnet and a dropped list are changes, an order is not
|
||||
String[][] steps = {{"SubA, SubC", "Sg", "UPDATE_ROLLBACK_COMPLETE"},
|
||||
{"SubA, SubB", "", "UPDATE_ROLLBACK_COMPLETE"}, {"SubB, SubA", "Sg", "UPDATE_COMPLETE"}};
|
||||
try (CloudFormationClient client = AwsTestClients.create(
|
||||
CloudFormationClient.builder(), "test", Region.US_EAST_1)) {
|
||||
CfnStackWaits.createStack(client, stack, netStack("SubA, SubB", "Sg"), "CREATE_COMPLETE");
|
||||
JsonPath before = given().get("/clusters/" + physicalId(client, stack, "Subject")).jsonPath();
|
||||
for (String[] step : steps) {
|
||||
CfnStackWaits.updateStack(client, stack, netStack(step[0], step[1]), step[2]);
|
||||
JsonPath after = given().get("/clusters/" + physicalId(client, stack, "Subject")).jsonPath();
|
||||
for (String list : List.of("subnetIds", "securityGroupIds")) {
|
||||
assertEquals(before.getList("cluster.resourcesVpcConfig." + list).stream().sorted().toList(),
|
||||
after.getList("cluster.resourcesVpcConfig." + list).stream().sorted().toList(), step[0] + step[1]);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
CfnStackWaits.deleteStack(stack);
|
||||
}
|
||||
}
|
||||
|
||||
private static String netStack(String subnets, String groups) {
|
||||
Function<String, String> refs = names -> names.isEmpty() ? "" : "{\"Ref\": \""
|
||||
+ names.replace(", ", "\"}, {\"Ref\": \"") + "\"}";
|
||||
return "{\"Resources\": {\"Vpc\": {\"Type\": \"AWS::EC2::VPC\", \"Properties\": {\"CidrBlock\": \"10.7.0.0/16\"}},"
|
||||
+ "\"Sg\": {\"Type\": \"AWS::EC2::SecurityGroup\", \"Properties\": {\"GroupDescription\": \"eks\","
|
||||
+ " \"VpcId\": {\"Ref\": \"Vpc\"}}}," + subnet("SubA", "10.7.1.0/24", "us-east-1a")
|
||||
+ subnet("SubB", "10.7.2.0/24", "us-east-1b") + subnet("SubC", "10.7.3.0/24", "us-east-1c")
|
||||
+ "\"Subject\": {\"Type\": \"AWS::EKS::Cluster\", \"Properties\": {" + ROLE
|
||||
+ "\"ResourcesVpcConfig\": {\"SubnetIds\": [" + refs.apply(subnets) + "]"
|
||||
+ (groups.isEmpty() ? "" : ", \"SecurityGroupIds\": [" + refs.apply(groups) + "]") + "}}}}}";
|
||||
}
|
||||
|
||||
@Test
|
||||
void aClusterNamingAMissingSubnetFailsTheStack() {
|
||||
String stack = "cfn-eks-nosub-" + Long.toString(System.nanoTime(), 36);
|
||||
@@ -354,6 +417,20 @@ class CloudFormationEksIntegrationTest {
|
||||
+ " \"EndpointPrivateAccess\": true}";
|
||||
}
|
||||
|
||||
/** The cluster of the config test: its original settings, or the updated ones; a failing resource follows it when asked. */
|
||||
private static String configStack(boolean updated, boolean fails) {
|
||||
String settings = updated
|
||||
? "\"EndpointPublicAccess\": false, \"EndpointPrivateAccess\": true}, \"Logging\": {\"ClusterLogging\": "
|
||||
+ "{\"EnabledTypes\": [{\"Type\": \"api\"}]}}, \"AccessConfig\": {\"AuthenticationMode\": \"API\"}"
|
||||
: "\"EndpointPublicAccess\": true}, \"AccessConfig\": {\"AuthenticationMode\": \"API_AND_CONFIG_MAP\"}";
|
||||
return "{\"Resources\": {"
|
||||
+ "\"Vpc\": {\"Type\": \"AWS::EC2::VPC\", \"Properties\": {\"CidrBlock\": \"10.7.0.0/16\"}},"
|
||||
+ subnet("SubA", "10.7.1.0/24", "us-east-1a") + subnet("SubB", "10.7.2.0/24", "us-east-1b")
|
||||
+ "\"Subject\": {\"Type\": \"AWS::EKS::Cluster\", \"Properties\": {" + ROLE
|
||||
+ "\"ResourcesVpcConfig\": {\"SubnetIds\": [{\"Ref\": \"SubA\"}, {\"Ref\": \"SubB\"}], " + settings
|
||||
+ "}}" + (fails ? CfnStackWaits.failingResourceAfter("Subject") : "") + "}}";
|
||||
}
|
||||
|
||||
private static String cluster(String role, String team) {
|
||||
return subjectStack("", "", "AWS::EKS::Cluster", "\"RoleArn\": \"arn:aws:iam::000000000000:role/" + role
|
||||
+ "\", \"Tags\": [{\"Key\": \"team\", \"Value\": \"" + team + "\"}]");
|
||||
|
||||
+26
@@ -10,6 +10,7 @@ import io.floci.aws.services.eks.model.CertificateAuthority;
|
||||
import io.floci.aws.services.eks.model.Cluster;
|
||||
import io.floci.aws.services.eks.model.ClusterIdentity;
|
||||
import io.floci.aws.services.eks.model.CreateClusterRequest;
|
||||
import io.floci.aws.services.eks.model.Logging;
|
||||
import io.floci.aws.services.eks.model.Nodegroup;
|
||||
import io.floci.aws.services.eks.model.OidcIdentity;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -18,6 +19,7 @@ import org.mockito.ArgumentCaptor;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
@@ -69,6 +71,30 @@ class EksCfnProvisionerTest {
|
||||
r.getAttributes().get("OpenIdConnectIssuerUrl"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aTagsOnlyUpdateRollsBackNoClusterConfig() throws Exception {
|
||||
// A tags-only update saves no cluster config, so its rollback writes none back over another caller's.
|
||||
Cluster created = new Cluster();
|
||||
created.setName("prod");
|
||||
created.setArn("arn:aws:eks:us-east-1:000000000000:cluster/prod");
|
||||
when(eks.createCluster(any(CreateClusterRequest.class))).thenReturn(created);
|
||||
StackResource r = resource("AWS::EKS::Cluster", "Control");
|
||||
provisioner.provision(r, props("{\"Name\": \"prod\", \"RoleArn\": \"arn:aws:iam::000000000000:role/eks\"}"),
|
||||
ctx());
|
||||
created.setTags(new HashMap<>(Map.of("team", "a")));
|
||||
created.setLogging(new Logging());
|
||||
when(eks.describeCluster("prod")).thenReturn(created);
|
||||
JsonNode before = props("{\"Name\": \"prod\", \"RoleArn\": \"arn:aws:iam::000000000000:role/eks\", "
|
||||
+ "\"Tags\": [{\"Key\": \"team\", \"Value\": \"a\"}]}");
|
||||
JsonNode after = props("{\"Name\": \"prod\", \"RoleArn\": \"arn:aws:iam::000000000000:role/eks\", "
|
||||
+ "\"Tags\": [{\"Key\": \"team\", \"Value\": \"b\"}]}");
|
||||
ProvisionContext update = new ProvisionContext(ctx().engine(), "us-east-1", "000000000000", "my-stack",
|
||||
"prod", event -> { }, before, PropertyChanges.between("AWS::EKS::Cluster", true, before, after));
|
||||
provisioner.provision(r, after, update);
|
||||
assertTrue(provisioner.rollbackUpdate(r));
|
||||
verify(eks, never()).restoreClusterConfig(any(), any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void absentClusterNameIsGeneratedFromStackAndLogicalId() throws Exception {
|
||||
Cluster created = new Cluster();
|
||||
|
||||
@@ -226,7 +226,7 @@ notes:
|
||||
AWS::CDK::Metadata: accepted; no-op
|
||||
AWS::Transfer::Server: 'management plane only; Ref returns the server ARN; Arn, ServerId and State attributes supported; AS2 managed egress IPs are not modeled; Domain replacement and IdentityProviderType changes are not supported'
|
||||
AWS::Transfer::User: 'management plane only; Ref returns the user ARN; Arn attribute supported; Policy and PosixProfile are not supported; a ServerId or UserName change replaces the user'
|
||||
AWS::EKS::Cluster: 'created with `Name`, `Version`, `RoleArn`, `Tags`, `EncryptionConfig`, `ResourcesVpcConfig`, `Logging`, `AccessConfig` and `KubernetesNetworkConfig.IpFamily` and `ServiceIpv4Cidr`, a replacement included; a change to `EncryptionConfig`, `IpFamily` or `ServiceIpv4Cidr` replaces the cluster with the new value applied; `ElasticLoadBalancing`, `ServiceIpv6Cidr` and `ResourcesVpcConfig.ControlPlaneEgressMode` are accepted and ignored; updated in place for `Tags`, keeping tags added outside the stack; a change to `Name`, `RoleArn`, `EncryptionConfig`, `BootstrapSelfManagedAddons`, `OutpostConfig`, `AccessConfig.BootstrapClusterCreatorAdminPermissions`, `KubernetesNetworkConfig.IpFamily` or `KubernetesNetworkConfig.ServiceIpv4Cidr` replaces the cluster, as the registry marks them create-only, and deletes the old one once the update commits; a change to `Version`, `ResourcesVpcConfig`, `Logging` or `AccessConfig` is accepted with a warning and not applied, as the emulator has no `UpdateClusterVersion` or `UpdateClusterConfig`'
|
||||
AWS::EKS::Cluster: 'created with `Name`, `Version`, `RoleArn`, `Tags`, `EncryptionConfig`, `ResourcesVpcConfig`, `Logging`, `AccessConfig` and `KubernetesNetworkConfig.IpFamily` and `ServiceIpv4Cidr`, a replacement included; a change to `EncryptionConfig`, `IpFamily` or `ServiceIpv4Cidr` replaces the cluster with the new value applied; `ElasticLoadBalancing`, `ServiceIpv6Cidr` and `ResourcesVpcConfig.ControlPlaneEgressMode` are accepted and ignored; updated in place for `Tags`, keeping tags added outside the stack, for `Logging`, for `AccessConfig.AuthenticationMode` (a mode cannot be moved back, so a stack update that does so fails, and a stack rollback restores it) and for `ResourcesVpcConfig` endpoint access and `PublicAccessCidrs`; a change to `Name`, `RoleArn`, `EncryptionConfig`, `BootstrapSelfManagedAddons`, `OutpostConfig`, `AccessConfig.BootstrapClusterCreatorAdminPermissions`, `KubernetesNetworkConfig.IpFamily` or `KubernetesNetworkConfig.ServiceIpv4Cidr` replaces the cluster, as the registry marks them create-only, and deletes the old one once the update commits; a change to `Version` is accepted with a warning and not applied, as the emulator has no `UpdateClusterVersion`; a change to the subnets or security groups of `ResourcesVpcConfig` fails the resource, as the emulator''s `UpdateClusterConfig` does not change them'
|
||||
AWS::EKS::Nodegroup: 'created with `ClusterName`, `NodegroupName`, `NodeRole`, `Subnets` and `Tags`; updated in place for `Tags`, keeping tags added outside the stack; a change to `ClusterName`, `NodegroupName`, `NodeRole`, `Subnets`, `AmiType`, `CapacityType`, `DiskSize`, `InstanceTypes` or `RemoteAccess` replaces the nodegroup, as the registry marks them create-only, and deletes the old one once the update commits; `ScalingConfig`, `Labels`, `Taints`, `UpdateConfig`, `Version`, `ReleaseVersion`, `ForceUpdateEnabled`, `LaunchTemplate`, `NodeRepairConfig` and `WarmPoolConfig` are accepted and not applied, whether changed or dropped, with a warning, as the emulator cannot update a nodegroup in place'
|
||||
AWS::IAM::User: 'template `LoginProfile` and `PermissionsBoundary` are ignored; an API-created login profile is removed on delete'
|
||||
AWS::ApiGateway::Authorizer: 'created with `Name`, `Type`, `AuthorizerUri`, `IdentitySource`, `AuthorizerResultTtlInSeconds` and `ProviderARNs`; updated in place for those, keeping the id; a template that drops `AuthorizerResultTtlInSeconds`, `IdentitySource` or `ProviderARNs` restores the TTL of 300 and clears the identity source and the pools, a dropped `AuthorizerUri` keeps the current one, and dropping `IdentitySource` from a `TOKEN` or `COGNITO_USER_POOLS` authorizer fails the resource, as AWS requires it there; a change to `RestApiId` replaces the authorizer, as the registry marks it create-only, and deletes the old one once the update commits; `AuthorizerCredentials`, `AuthType` and `IdentityValidationExpression` are not stored'
|
||||
|
||||
Reference in new issue
Block a user