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 VPC, logging and access settings (#8747)
As the next step of #6606, an AWS::EKS::Cluster is now created with its ResourcesVpcConfig, Logging and AccessConfig, a replacement included, and ClusterSecurityGroupId resolves through Fn::GetAtt. A template naming a subnet that does not exist fails the stack, as on AWS, and an EnabledTypes entry with no Type fails the resource. An in-place change to the three is logged and not applied until Floci has UpdateClusterConfig. CreateCluster now also refuses subnets from two VPCs, a PublicAccessCidrs entry that is not a CIDR, and an IPv6 block on an IPv4 cluster, and runs every check before it creates the cluster security group, so a refused request leaves no group behind for a stack's VPC rollback.
This commit is contained in:
1 parent
252a6012b3
commit
f082117a85
8 files changed
+198
-40
No files matched your search
+44
-6
@@ -10,6 +10,8 @@ import io.floci.aws.services.cloudformation.model.StackResource;
|
||||
import io.floci.aws.services.eks.EksService;
|
||||
import io.floci.aws.services.eks.model.Cluster;
|
||||
import io.floci.aws.services.eks.model.CreateClusterRequest;
|
||||
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 jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
@@ -40,7 +42,8 @@ public class EksCfnProvisioner implements CfnResourceProvisioner {
|
||||
|
||||
/** Every attribute {@code publish} writes for a cluster. */
|
||||
private static final List<String> CLUSTER_ATTRIBUTES =
|
||||
List.of("Arn", "Endpoint", "CertificateAuthorityData", "OpenIdConnectIssuerUrl");
|
||||
List.of("Arn", "Endpoint", "CertificateAuthorityData", "OpenIdConnectIssuerUrl",
|
||||
"ClusterSecurityGroupId");
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
/** Every attribute {@code publishNodegroup} writes for a nodegroup. */
|
||||
private static final List<String> NODEGROUP_ATTRIBUTES = List.of("Id", "ClusterName", "NodegroupName", "Arn");
|
||||
@@ -114,6 +117,9 @@ public class EksCfnProvisioner implements CfnResourceProvisioner {
|
||||
request.setEncryptionConfig(createSetting(props, "EncryptionConfig", ctx, new TypeReference<>() { }));
|
||||
request.setKubernetesNetworkConfig(
|
||||
createSetting(props, "KubernetesNetworkConfig", ctx, new TypeReference<>() { }));
|
||||
request.setResourcesVpcConfig(createSetting(props, "ResourcesVpcConfig", ctx, new TypeReference<>() { }));
|
||||
request.setAccessConfig(createSetting(props, "AccessConfig", ctx, new TypeReference<>() { }));
|
||||
request.setLogging(logging(props, ctx));
|
||||
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.
|
||||
@@ -132,15 +138,14 @@ public class EksCfnProvisioner implements CfnResourceProvisioner {
|
||||
}
|
||||
|
||||
/**
|
||||
* A create-only property in its API request shape, or null when the template leaves it out. The
|
||||
* A 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.
|
||||
* or ServiceIpv6Cidr member of KubernetesNetworkConfig and no ControlPlaneEgressMode member of
|
||||
* ResourcesVpcConfig, so those 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();
|
||||
JsonNode resolved = readSetting(props, name, ctx);
|
||||
if (resolved == null) {
|
||||
return null;
|
||||
}
|
||||
@@ -153,12 +158,45 @@ public class EksCfnProvisioner implements CfnResourceProvisioner {
|
||||
}
|
||||
}
|
||||
|
||||
private static JsonNode readSetting(JsonNode props, String name, ProvisionContext ctx) {
|
||||
CfnPropertyTypes.Reader types = new CfnPropertyTypes.Reader(ctx.engine());
|
||||
JsonNode resolved = types.readProperty(CLUSTER, props, name);
|
||||
types.finish();
|
||||
return resolved;
|
||||
}
|
||||
|
||||
/**
|
||||
* Logging in the API shape. The template lists the enabled types as
|
||||
* {@code ClusterLogging.EnabledTypes[].Type}; the service disables every type not listed.
|
||||
*/
|
||||
private static Logging logging(JsonNode props, ProvisionContext ctx) {
|
||||
JsonNode resolved = readSetting(props, "Logging", ctx);
|
||||
if (resolved == null) {
|
||||
return null;
|
||||
}
|
||||
List<String> enabled = new ArrayList<>();
|
||||
JsonNode types = resolved.path("ClusterLogging").path("EnabledTypes");
|
||||
types.forEach(entry -> {
|
||||
String type = entry.path("Type").asText("");
|
||||
if (type.isBlank()) {
|
||||
throw new AwsException("ValidationError",
|
||||
"AWS::EKS::Cluster Logging.ClusterLogging.EnabledTypes entry has no Type", 400);
|
||||
}
|
||||
enabled.add(type);
|
||||
});
|
||||
return new Logging(List.of(new LogSetup(enabled, true)));
|
||||
}
|
||||
|
||||
private void publish(StackResource r, Cluster cluster) {
|
||||
r.setPhysicalId(cluster.getName());
|
||||
r.getAttributes().put("Arn", cluster.getArn());
|
||||
if (cluster.getEndpoint() != null) {
|
||||
r.getAttributes().put("Endpoint", cluster.getEndpoint());
|
||||
}
|
||||
if (cluster.getResourcesVpcConfig() != null
|
||||
&& cluster.getResourcesVpcConfig().getClusterSecurityGroupId() != null) {
|
||||
r.getAttributes().put("ClusterSecurityGroupId", cluster.getResourcesVpcConfig().getClusterSecurityGroupId());
|
||||
}
|
||||
if (cluster.getCertificateAuthority() != null && cluster.getCertificateAuthority().getData() != null) {
|
||||
r.getAttributes().put("CertificateAuthorityData", cluster.getCertificateAuthority().getData());
|
||||
}
|
||||
|
||||
@@ -815,9 +815,14 @@ 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);
|
||||
// The checks below run before the cluster security group is created, so a request they refuse
|
||||
// leaves no group behind for a stack's VPC rollback to trip over.
|
||||
String ipFamily = resolveIpFamily(request.getKubernetesNetworkConfig());
|
||||
ResourcesVpcConfig vpcConfig = buildVpcConfigResponse(request.getResourcesVpcConfig(), resolvedVpcId, ipFamily);
|
||||
String vpcCidr = resolveClusterVpcCidr(region, vpcConfig.getVpcId());
|
||||
KubernetesNetworkConfig networkConfig = buildNetworkConfig(request.getKubernetesNetworkConfig(), vpcCidr);
|
||||
Logging logging = buildLogging(request.getLogging());
|
||||
List<EncryptionConfig> encryptionConfig = buildEncryptionConfig(request.getEncryptionConfig());
|
||||
SecurityGroup clusterSg = null;
|
||||
if (ec2Service != null && !vpcConfig.getVpcId().isBlank()) {
|
||||
try {
|
||||
@@ -832,11 +837,10 @@ public class EksService implements TagHandler, ResourceProvider, Ec2InstanceLaun
|
||||
}
|
||||
}
|
||||
cluster.setResourcesVpcConfig(vpcConfig);
|
||||
String vpcCidr = resolveClusterVpcCidr(region, vpcConfig.getVpcId());
|
||||
cluster.setKubernetesNetworkConfig(buildNetworkConfig(request.getKubernetesNetworkConfig(), vpcCidr));
|
||||
cluster.setKubernetesNetworkConfig(networkConfig);
|
||||
cluster.setPodCidr(EksClusterManager.DEFAULT_POD_CIDR);
|
||||
cluster.setLogging(buildLogging(request.getLogging()));
|
||||
cluster.setEncryptionConfig(buildEncryptionConfig(request.getEncryptionConfig()));
|
||||
cluster.setLogging(logging);
|
||||
cluster.setEncryptionConfig(encryptionConfig);
|
||||
cluster.setStatus(ClusterStatus.CREATING);
|
||||
cluster.setPlatformVersion("eks.1");
|
||||
cluster.setCertificateAuthority(new CertificateAuthority(""));
|
||||
@@ -1684,17 +1688,25 @@ public class EksService implements TagHandler, ResourceProvider, Ec2InstanceLaun
|
||||
}
|
||||
String vpcId = null;
|
||||
for (String subnetId : vpcConfig.getSubnetIds()) {
|
||||
String subnetVpcId;
|
||||
try {
|
||||
vpcId = ec2Service.requireSubnet(region, subnetId).getVpcId();
|
||||
subnetVpcId = ec2Service.requireSubnet(region, subnetId).getVpcId();
|
||||
} catch (AwsException e) {
|
||||
throw new AwsException("InvalidParameterException",
|
||||
"Subnet ID '" + subnetId + "' does not exist", 400);
|
||||
}
|
||||
if (vpcId != null && !vpcId.equals(subnetVpcId)) {
|
||||
throw new AwsException("InvalidParameterException",
|
||||
"Subnet ID '" + subnetId + "' is in VPC '" + subnetVpcId
|
||||
+ "', not in VPC '" + vpcId + "' of the other subnets", 400);
|
||||
}
|
||||
vpcId = subnetVpcId;
|
||||
}
|
||||
return vpcId;
|
||||
}
|
||||
|
||||
private ResourcesVpcConfig buildVpcConfigResponse(ResourcesVpcConfig request, String resolvedVpcId) {
|
||||
private ResourcesVpcConfig buildVpcConfigResponse(ResourcesVpcConfig request, String resolvedVpcId,
|
||||
String ipFamily) {
|
||||
ResourcesVpcConfig response = new ResourcesVpcConfig();
|
||||
if (request != null) {
|
||||
response.setSubnetIds(request.getSubnetIds() != null ? request.getSubnetIds() : List.of());
|
||||
@@ -1709,6 +1721,20 @@ public class EksService implements TagHandler, ResourceProvider, Ec2InstanceLaun
|
||||
request.getEndpointPublicAccess() != null ? request.getEndpointPublicAccess() : Boolean.TRUE);
|
||||
response.setEndpointPrivateAccess(
|
||||
request.getEndpointPrivateAccess() != null ? request.getEndpointPrivateAccess() : Boolean.FALSE);
|
||||
if (request.getPublicAccessCidrs() != null) {
|
||||
for (String cidr : request.getPublicAccessCidrs()) {
|
||||
if (!SecurityGroupPolicy.validCidr(cidr)) {
|
||||
throw new AwsException("InvalidParameterException",
|
||||
"The specified parameter resourcesVpcConfig.publicAccessCidrs is not valid: "
|
||||
+ cidr, 400);
|
||||
}
|
||||
if (cidr.contains(":") && !"ipv6".equals(ipFamily)) {
|
||||
throw new AwsException("InvalidParameterException",
|
||||
"The specified parameter resourcesVpcConfig.publicAccessCidrs is not valid: "
|
||||
+ cidr + ". IPv6 blocks cannot be added to IPv4 clusters.", 400);
|
||||
}
|
||||
}
|
||||
}
|
||||
response.setPublicAccessCidrs(
|
||||
request.getPublicAccessCidrs() != null ? request.getPublicAccessCidrs() : List.of("0.0.0.0/0"));
|
||||
} else {
|
||||
|
||||
Reference in new issue
Block a user