mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(cloudformation): create an EKS cluster with its encryption and network settings, a replacement included (#8739)
This commit is contained in:
1 parent
77a4a1ff65
commit
523eba559e
13 files changed
+536
-55
No files matched your search
@@ -349,6 +349,17 @@ final class CfnPropertyTypes {
|
||||
return read(path, path.substring(path.lastIndexOf('/') + 1), engine.omitNoValue(raw), shapeName);
|
||||
}
|
||||
|
||||
/**
|
||||
* The property {@code property} of {@code resourceType} in {@code props} read as the shape its
|
||||
* resource schema declares; null when {@code props} lacks it or it is {@code AWS::NoValue}.
|
||||
*/
|
||||
JsonNode readProperty(String resourceType, JsonNode props, String property) {
|
||||
if (props == null || !props.has(property)) {
|
||||
return null;
|
||||
}
|
||||
return read(property, props.get(property), propertyShape(resourceType, property));
|
||||
}
|
||||
|
||||
/** Throws the first model validation failure found by {@link #read}, if any. */
|
||||
void finish() {
|
||||
if (modelFailure != null) {
|
||||
|
||||
+5
-10
@@ -53,11 +53,11 @@ public class DsqlCfnProvisioner implements CfnResourceProvisioner {
|
||||
public void provision(StackResource resource, JsonNode props, ProvisionContext ctx) {
|
||||
String region = ctx.region();
|
||||
CfnPropertyTypes.Reader types = new CfnPropertyTypes.Reader(ctx.engine());
|
||||
JsonNode protection = read(types, props, "DeletionProtectionEnabled");
|
||||
JsonNode kmsKey = read(types, props, "KmsEncryptionKey");
|
||||
JsonNode multiRegion = read(types, props, "MultiRegionProperties");
|
||||
JsonNode tagList = read(types, props, "Tags");
|
||||
JsonNode policyDocument = read(types, props, "PolicyDocument");
|
||||
JsonNode protection = types.readProperty(TYPE, props, "DeletionProtectionEnabled");
|
||||
JsonNode kmsKey = types.readProperty(TYPE, props, "KmsEncryptionKey");
|
||||
JsonNode multiRegion = types.readProperty(TYPE, props, "MultiRegionProperties");
|
||||
JsonNode tagList = types.readProperty(TYPE, props, "Tags");
|
||||
JsonNode policyDocument = types.readProperty(TYPE, props, "PolicyDocument");
|
||||
types.finish();
|
||||
Boolean deletionProtection = protection == null ? null : protection.booleanValue();
|
||||
String kms = kmsKey == null ? null : kmsKey.asText();
|
||||
@@ -104,11 +104,6 @@ public class DsqlCfnProvisioner implements CfnResourceProvisioner {
|
||||
() -> dsqlService.deleteCluster(region, physicalId), "ResourceNotFoundException");
|
||||
}
|
||||
|
||||
private static JsonNode read(CfnPropertyTypes.Reader types, JsonNode props, String property) {
|
||||
return props == null ? null
|
||||
: types.read(property, props.get(property), CfnPropertyTypes.propertyShape(TYPE, property));
|
||||
}
|
||||
|
||||
private DsqlCluster awaitSettled(String region, String identifier) {
|
||||
Instant deadline = Instant.now().plus(READY_TIMEOUT);
|
||||
while (true) {
|
||||
|
||||
+26
@@ -1,5 +1,6 @@
|
||||
package io.floci.aws.services.cloudformation.provisioners;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
@@ -110,6 +111,9 @@ public class EksCfnProvisioner implements CfnResourceProvisioner {
|
||||
// Unstripped: createCluster reads the reserved cluster-args tag, then drops it.
|
||||
Map<String, String> templateTags = ctx.resolveTags(props, "Tags");
|
||||
request.setTags(templateTags);
|
||||
request.setEncryptionConfig(createSetting(props, "EncryptionConfig", ctx, new TypeReference<>() { }));
|
||||
request.setKubernetesNetworkConfig(
|
||||
createSetting(props, "KubernetesNetworkConfig", ctx, new TypeReference<>() { }));
|
||||
Cluster created = eksService.createCluster(request);
|
||||
// The cluster this resource named before is gone or replaced; publish writes only what the
|
||||
// new one has, so its endpoint and certificate must not outlive it.
|
||||
@@ -127,6 +131,28 @@ public class EksCfnProvisioner implements CfnResourceProvisioner {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A create-only property in its API request shape, or null when the template leaves it out. The
|
||||
* template value is read against the type the resource schema declares, so a value of another type
|
||||
* fails the resource with CloudFormation's wording. The request model has no ElasticLoadBalancing
|
||||
* or ServiceIpv6Cidr member, so those members of KubernetesNetworkConfig are not read.
|
||||
*/
|
||||
private static <T> T createSetting(JsonNode props, String name, ProvisionContext ctx, TypeReference<T> type) {
|
||||
CfnPropertyTypes.Reader types = new CfnPropertyTypes.Reader(ctx.engine());
|
||||
JsonNode resolved = types.readProperty(CLUSTER, props, name);
|
||||
types.finish();
|
||||
if (resolved == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return MAPPER.convertValue(CfnApiShapeConverter.convert(resolved, CfnApiShapeConverter.OMIT_NULL_PROPERTIES),
|
||||
type);
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new AwsException("ValidationError", "AWS::EKS::Cluster " + name + " is not valid: "
|
||||
+ e.getMessage(), 400);
|
||||
}
|
||||
}
|
||||
|
||||
private void publish(StackResource r, Cluster cluster) {
|
||||
r.setPhysicalId(cluster.getName());
|
||||
r.getAttributes().put("Arn", cluster.getArn());
|
||||
|
||||
+6
-20
@@ -800,7 +800,7 @@ public class LambdaCfnProvisioner implements CfnResourceProvisioner {
|
||||
}
|
||||
|
||||
private Map<String, String> resolveLambdaEnvironment(CfnPropertyTypes.Reader types, JsonNode props) {
|
||||
JsonNode envNode = readTyped(types, FUNCTION, props, "Environment");
|
||||
JsonNode envNode = types.readProperty(FUNCTION, props, "Environment");
|
||||
JsonNode variables = envNode == null ? null : envNode.get("Variables");
|
||||
if (variables == null) {
|
||||
return Map.of();
|
||||
@@ -812,7 +812,7 @@ public class LambdaCfnProvisioner implements CfnResourceProvisioner {
|
||||
|
||||
private static List<String> resolveStringListOrEmpty(CfnPropertyTypes.Reader types, String resourceType,
|
||||
JsonNode props, String source) {
|
||||
JsonNode values = readTyped(types, resourceType, props, source);
|
||||
JsonNode values = types.readProperty(resourceType, props, source);
|
||||
return values == null ? List.of() : texts(values);
|
||||
}
|
||||
|
||||
@@ -823,7 +823,7 @@ public class LambdaCfnProvisioner implements CfnResourceProvisioner {
|
||||
}
|
||||
|
||||
private List<Object> resolveObjectListOrEmpty(CfnPropertyTypes.Reader types, JsonNode props, String source) {
|
||||
JsonNode resolved = readTyped(types, FUNCTION, props, source);
|
||||
JsonNode resolved = types.readProperty(FUNCTION, props, source);
|
||||
if (resolved == null) {
|
||||
return List.of();
|
||||
}
|
||||
@@ -835,24 +835,10 @@ public class LambdaCfnProvisioner implements CfnResourceProvisioner {
|
||||
/** The object property {@code source}, or {@code defaultValue} when the template leaves it out. */
|
||||
private Map<String, Object> resolveMapOrDefault(CfnPropertyTypes.Reader types, JsonNode props, String source,
|
||||
Map<String, Object> defaultValue) {
|
||||
JsonNode resolved = readTyped(types, FUNCTION, props, source);
|
||||
JsonNode resolved = types.readProperty(FUNCTION, props, source);
|
||||
return resolved == null ? defaultValue : jsonObjectToMap(resolved);
|
||||
}
|
||||
|
||||
/**
|
||||
* The property {@code source} of {@code resourceType} read against the type its resource schema
|
||||
* declares, or null when the template leaves it out, {@code Ref: AWS::NoValue} directly or
|
||||
* through {@code Fn::If} included. A value of another type is refused before the function is
|
||||
* touched; see {@link CfnPropertyTypes}.
|
||||
*/
|
||||
private static JsonNode readTyped(CfnPropertyTypes.Reader types, String resourceType, JsonNode props,
|
||||
String source) {
|
||||
if (props == null || !props.has(source)) {
|
||||
return null;
|
||||
}
|
||||
return types.read(source, props.get(source), CfnPropertyTypes.propertyShape(resourceType, source));
|
||||
}
|
||||
|
||||
private static Map<String, Object> mapWithNullValue(String key) {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put(key, null);
|
||||
@@ -861,7 +847,7 @@ public class LambdaCfnProvisioner implements CfnResourceProvisioner {
|
||||
|
||||
private void putStringListIfPresent(Map<String, Object> request, CfnPropertyTypes.Reader types,
|
||||
JsonNode props, String source) {
|
||||
JsonNode values = readTyped(types, FUNCTION, props, source);
|
||||
JsonNode values = types.readProperty(FUNCTION, props, source);
|
||||
if (values != null) {
|
||||
request.put(source, texts(values));
|
||||
}
|
||||
@@ -869,7 +855,7 @@ public class LambdaCfnProvisioner implements CfnResourceProvisioner {
|
||||
|
||||
private void putResolvedMapIfPresent(Map<String, Object> request, CfnPropertyTypes.Reader types,
|
||||
JsonNode props, String source) {
|
||||
JsonNode resolved = readTyped(types, FUNCTION, props, source);
|
||||
JsonNode resolved = types.readProperty(FUNCTION, props, source);
|
||||
if (resolved != null) {
|
||||
request.put(source, jsonObjectToMap(resolved));
|
||||
}
|
||||
|
||||
@@ -815,6 +815,8 @@ public class EksService implements TagHandler, ResourceProvider, Ec2InstanceLaun
|
||||
}
|
||||
cluster.setTags(request.getTags() != null ? ReservedTags.stripReservedTags(request.getTags()) : new HashMap<>());
|
||||
|
||||
// Refused before the cluster security group exists, so a bad ipFamily leaves nothing behind.
|
||||
resolveIpFamily(request.getKubernetesNetworkConfig());
|
||||
ResourcesVpcConfig vpcConfig = buildVpcConfigResponse(request.getResourcesVpcConfig(), resolvedVpcId);
|
||||
SecurityGroup clusterSg = null;
|
||||
if (ec2Service != null && !vpcConfig.getVpcId().isBlank()) {
|
||||
@@ -1789,10 +1791,19 @@ public class EksService implements TagHandler, ResourceProvider, Ec2InstanceLaun
|
||||
}
|
||||
}
|
||||
config.setServiceIpv4Cidr(resolvedCidr);
|
||||
config.setIpFamily(request != null && request.getIpFamily() != null ? request.getIpFamily() : "ipv4");
|
||||
config.setIpFamily(resolveIpFamily(request));
|
||||
return config;
|
||||
}
|
||||
|
||||
private static String resolveIpFamily(KubernetesNetworkConfig request) {
|
||||
String ipFamily = request != null && request.getIpFamily() != null ? request.getIpFamily() : "ipv4";
|
||||
if (!"ipv4".equals(ipFamily) && !"ipv6".equals(ipFamily)) {
|
||||
throw new AwsException("InvalidParameterException",
|
||||
"Invalid ipFamily '" + ipFamily + "'. Valid values: ipv4 | ipv6", 400);
|
||||
}
|
||||
return ipFamily;
|
||||
}
|
||||
|
||||
static Logging defaultLogging() {
|
||||
return new Logging(List.of(new LogSetup(new ArrayList<>(ALL_LOG_TYPES), false)));
|
||||
}
|
||||
@@ -1855,8 +1866,10 @@ public class EksService implements TagHandler, ResourceProvider, Ec2InstanceLaun
|
||||
"Only one encryption configuration is allowed", 400);
|
||||
}
|
||||
EncryptionConfig config = requestedConfigs.getFirst();
|
||||
if (config == null || config.getResources() == null || config.getResources().isEmpty()
|
||||
|| !List.of("secrets").equals(config.getResources())) {
|
||||
// AWS: Resources is optional, "this field no longer affects which resources are encrypted", and
|
||||
// "secrets" is the only supported value.
|
||||
boolean resourcesOmitted = config != null && (config.getResources() == null || config.getResources().isEmpty());
|
||||
if (config == null || !(resourcesOmitted || List.of("secrets").equals(config.getResources()))) {
|
||||
throw new AwsException("InvalidParameterException",
|
||||
"Invalid k8s resource and provider for encryption", 400);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user