mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(ecr): never start the registry container while ECR is disabled (#6281)
This commit is contained in:
1 parent
7635345474
commit
a29df66d33
13 files changed
+420
-8
No files matched your search
+12
@@ -51,6 +51,13 @@ public class CloudFormationTemplateEngine {
|
||||
String resolve(String stackName, String outputName, String region, String roleArn);
|
||||
}
|
||||
|
||||
/**
|
||||
* Internal attribute on a stub whose type is served by a provisioner whose service is turned
|
||||
* off. The template engine reads it to refuse {@code Fn::GetAtt} on an attribute the stub
|
||||
* does not have, instead of publishing the unresolved placeholder.
|
||||
*/
|
||||
public static final String SERVICE_DISABLED_ATTR = "__FlociServiceDisabled";
|
||||
|
||||
private final String accountId;
|
||||
private final String region;
|
||||
private final String stackName;
|
||||
@@ -798,6 +805,11 @@ public class CloudFormationTemplateEngine {
|
||||
if (attrs != null && attrs.containsKey(attrName) && !attrName.startsWith("__Floci")) {
|
||||
return attrs.get(attrName);
|
||||
}
|
||||
if (attrs != null && attrs.containsKey(SERVICE_DISABLED_ATTR)) {
|
||||
throw new AwsException("ValidationError", "Template error: " + logicalId
|
||||
+ " was stubbed because its service is disabled, so attribute " + attrName
|
||||
+ " is unavailable.", 400);
|
||||
}
|
||||
LOG.warnv("Unresolved GetAtt: {0}.{1}", logicalId, attrName);
|
||||
unresolvedIntrinsics.add("Fn::GetAtt " + logicalId + "." + attrName);
|
||||
return logicalId + "." + attrName;
|
||||
|
||||
+46
-7
@@ -110,27 +110,45 @@ public class CfnResourceDispatcher {
|
||||
if (arn != null && arn.startsWith(STUB_ARN_PREFIX)) {
|
||||
resource.getAttributes().remove("Arn");
|
||||
}
|
||||
// The service is back on, so the real resource has every attribute it declares.
|
||||
resource.getAttributes().remove(CloudFormationTemplateEngine.SERVICE_DISABLED_ATTR);
|
||||
} else if (registry.isServiceDisabled(resourceType) && existingPhysicalId != null
|
||||
&& !isStub(existingPhysicalId, resource.getAttributes())) {
|
||||
// A real resource a stack created while the service was on. Stubbing it would
|
||||
// replace its physical id and the stack would lose the entity, so refuse instead.
|
||||
throw new AwsException("ValidationError", "The service for " + resourceType
|
||||
+ " is disabled, so the existing resource " + existingPhysicalId
|
||||
+ " cannot be updated.", 400);
|
||||
} else if (!stubUnsupportedResourceTypesAllowed()) {
|
||||
// Before the physical id below is assigned, so the Cloud Control path sees a
|
||||
// resource with none and reports this message rather than a success. On the stack
|
||||
// path the catch below turns it into CREATE_FAILED with the same sentence, which
|
||||
// rolls the stack back.
|
||||
throw new AwsException("ValidationError", unsupportedResourceTypeMessage(resourceType), 400);
|
||||
throw new AwsException("ValidationError", unavailableResourceTypeMessage(resourceType), 400);
|
||||
} else {
|
||||
// Warn, not debug, and a status reason on the resource: the stub reports
|
||||
// CREATE_COMPLETE while creating nothing, so without both the stack is
|
||||
// indistinguishable from one where every resource was really provisioned. The
|
||||
// reason reaches DescribeStackEvents through the event CloudFormationService
|
||||
// already builds from it.
|
||||
LOG.warnv("Stubbing unsupported resource type {0} ({1}): nothing is created "
|
||||
+ "for it. Set floci.services.cloudformation."
|
||||
+ "allow-stub-unsupported-resource-types=false to fail the "
|
||||
+ "stack instead.",
|
||||
resourceType, logicalId);
|
||||
resource.setStatusReason(unsupportedResourceTypeMessage(resourceType)
|
||||
if (registry.isServiceDisabled(resourceType)) {
|
||||
LOG.warnv("Stubbing resource type {0} ({1}): its service is disabled, so nothing "
|
||||
+ "is created for it. {2}",
|
||||
resourceType, logicalId, enableServiceHint(resourceType));
|
||||
} else {
|
||||
LOG.warnv("Stubbing unsupported resource type {0} ({1}): nothing is created "
|
||||
+ "for it. Set floci.services.cloudformation."
|
||||
+ "allow-stub-unsupported-resource-types=false to fail the "
|
||||
+ "stack instead.",
|
||||
resourceType, logicalId);
|
||||
}
|
||||
resource.setStatusReason(unavailableResourceTypeMessage(resourceType)
|
||||
+ " It was stubbed and nothing was created for it.");
|
||||
resource.setPhysicalId(logicalId + "-" + UUID.randomUUID().toString().substring(0, 8));
|
||||
resource.getAttributes().put("Arn", STUB_ARN_PREFIX + logicalId);
|
||||
if (registry.isServiceDisabled(resourceType)) {
|
||||
resource.getAttributes().put(CloudFormationTemplateEngine.SERVICE_DISABLED_ATTR, "true");
|
||||
}
|
||||
}
|
||||
resource.setStatus("CREATE_COMPLETE");
|
||||
} catch (Exception e) {
|
||||
@@ -173,6 +191,21 @@ public class CfnResourceDispatcher {
|
||||
return config == null || config.services().cloudformation().allowStubUnsupportedResourceTypes();
|
||||
}
|
||||
|
||||
/** What Floci says about a resource type it will not provision: unsupported, or its service is off. */
|
||||
private String unavailableResourceTypeMessage(String resourceType) {
|
||||
if (!registry.isServiceDisabled(resourceType)) {
|
||||
return unsupportedResourceTypeMessage(resourceType);
|
||||
}
|
||||
return "Resource type " + resourceType + " is not provisioned because its Floci service is disabled. "
|
||||
+ enableServiceHint(resourceType);
|
||||
}
|
||||
|
||||
private String enableServiceHint(String resourceType) {
|
||||
String flag = registry.serviceEnabledFlag(resourceType);
|
||||
return flag == null ? "Enable the service to provision it."
|
||||
: "Set " + flag + "=true to provision it.";
|
||||
}
|
||||
|
||||
/** The one sentence Floci says about a resource type it has no provisioner for. */
|
||||
private static String unsupportedResourceTypeMessage(String resourceType) {
|
||||
return "Resource type " + resourceType + " is not supported by Floci.";
|
||||
@@ -219,6 +252,12 @@ public class CfnResourceDispatcher {
|
||||
owner.delete(resource, region);
|
||||
return;
|
||||
}
|
||||
if (registry.isServiceDisabled(resourceType)
|
||||
&& !isStub(resource.getPhysicalId(), resource.getAttributes())) {
|
||||
// Completing this delete would drop the stack's record of an entity still there.
|
||||
throw new AwsException("ValidationError", "The service for " + resourceType
|
||||
+ " is disabled, so " + resource.getPhysicalId() + " cannot be deleted.", 400);
|
||||
}
|
||||
delete(resourceType, resource.getPhysicalId(), region);
|
||||
}
|
||||
|
||||
|
||||
+13
@@ -23,6 +23,19 @@ public interface CfnResourceProvisioner {
|
||||
|
||||
void provision(StackResource resource, JsonNode properties, ProvisionContext ctx);
|
||||
|
||||
/**
|
||||
* True when the service this provisioner wraps is turned off. {@link CloudFormationResourceRegistry}
|
||||
* then serves none of its types, so the dispatcher stubs them instead of provisioning them.
|
||||
*/
|
||||
default boolean serviceDisabled() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/** The config property that turns this provisioner's service on, named in messages about it being off. */
|
||||
default String serviceEnabledFlag() {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Most implementations delegate to {@code service.deleteX(physicalId, region)}. */
|
||||
default void delete(String resourceType, String physicalId, String region) {
|
||||
// no-op by default: some resource types have no backing delete
|
||||
|
||||
+24
-1
@@ -44,6 +44,29 @@ public class CloudFormationResourceRegistry {
|
||||
}
|
||||
|
||||
public Optional<CfnResourceProvisioner> forType(String resourceType) {
|
||||
CfnResourceProvisioner provisioner = lookup(resourceType);
|
||||
if (provisioner != null && provisioner.serviceDisabled()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
return Optional.ofNullable(provisioner);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a provisioner exists for {@code resourceType} but its service is turned off, so
|
||||
* {@link #forType} serves nothing for it: the type is stubbed, not unsupported.
|
||||
*/
|
||||
public boolean isServiceDisabled(String resourceType) {
|
||||
CfnResourceProvisioner provisioner = lookup(resourceType);
|
||||
return provisioner != null && provisioner.serviceDisabled();
|
||||
}
|
||||
|
||||
/** The config property that enables the disabled service behind {@code resourceType}, or null. */
|
||||
public String serviceEnabledFlag(String resourceType) {
|
||||
CfnResourceProvisioner provisioner = lookup(resourceType);
|
||||
return provisioner == null ? null : provisioner.serviceEnabledFlag();
|
||||
}
|
||||
|
||||
private CfnResourceProvisioner lookup(String resourceType) {
|
||||
CfnResourceProvisioner provisioner = byType.get(resourceType);
|
||||
if (provisioner == null && resourceType != null && resourceType.startsWith("Custom::")) {
|
||||
// Custom::* is a dynamic, unenumerable prefix, so it cannot be a registered key. An
|
||||
@@ -51,7 +74,7 @@ public class CloudFormationResourceRegistry {
|
||||
// above); only otherwise-unmatched Custom:: types fall through to the generic handler.
|
||||
provisioner = byType.get(CUSTOM_RESOURCE_TYPE);
|
||||
}
|
||||
return Optional.ofNullable(provisioner);
|
||||
return provisioner;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+10
@@ -39,6 +39,16 @@ public class EcrCfnProvisioner implements CfnResourceProvisioner {
|
||||
return Set.of("AWS::ECR::Repository");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean serviceDisabled() {
|
||||
return ecrService.isDisabled();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String serviceEnabledFlag() {
|
||||
return "floci.services.ecr.enabled";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void provision(StackResource r, JsonNode props, ProvisionContext ctx) {
|
||||
// CDK bootstrap requires lower-case repository names; CFN-generated suffixes can include
|
||||
|
||||
@@ -132,6 +132,10 @@ public class EcrService implements ResourceProvider {
|
||||
this.registryManager.setReconcileHook(this::reconcileFromCatalog);
|
||||
}
|
||||
|
||||
public boolean isDisabled() {
|
||||
return !config.services().ecr().enabled();
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// Pull through cache rules
|
||||
// ============================================================
|
||||
|
||||
@@ -124,6 +124,9 @@ public class EcrRegistryManager {
|
||||
if (image == null) {
|
||||
return null;
|
||||
}
|
||||
if (!config.services().ecr().enabled()) {
|
||||
return image;
|
||||
}
|
||||
Matcher m = AWS_ECR_URI.matcher(image);
|
||||
if (!m.matches()) {
|
||||
return image;
|
||||
@@ -272,6 +275,9 @@ public class EcrRegistryManager {
|
||||
* @return true when the registry container is running
|
||||
*/
|
||||
public boolean tryEnsureStarted() {
|
||||
if (!config.services().ecr().enabled()) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
ensureStarted();
|
||||
} catch (RuntimeException e) {
|
||||
@@ -294,6 +300,9 @@ public class EcrRegistryManager {
|
||||
* thread-safe. Throws if Docker is unreachable.
|
||||
*/
|
||||
public synchronized void ensureStarted() {
|
||||
if (!config.services().ecr().enabled()) {
|
||||
throw new IllegalStateException("ECR is disabled, the backing registry is not started");
|
||||
}
|
||||
if (started) {
|
||||
ContainerPresence presence = lifecycleManager.presenceOf(containerId);
|
||||
if (presence == ContainerPresence.RUNNING || presence == ContainerPresence.UNKNOWN) {
|
||||
|
||||
Reference in new issue
Block a user