mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(ecr): clean up registry storage (#2982)
Remove every manifest for a forced repository deletion and reclaim the shared registry volume only when storage pruning is configured.
This commit is contained in:
1 parent
2b531dad3a
commit
732e009207
5 files changed
+257
-13
No files matched your search
@@ -7,6 +7,7 @@ import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.resource.ExplorerResource;
|
||||
import io.github.hectorvent.floci.core.resource.ResourceProvider;
|
||||
import io.github.hectorvent.floci.core.resource.SupportedResourceType;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.ecr.model.AuthorizationData;
|
||||
@@ -189,21 +190,16 @@ public class EcrService implements ResourceProvider {
|
||||
String key = key(region, account, repositoryName);
|
||||
Repository repo = repoStore.get(key).orElseThrow(() -> notFound(repositoryName, account));
|
||||
|
||||
// Check whether the registry has any tagged images for this repo. If
|
||||
// ensureStarted() can't talk to docker (no daemon), assume the repo is
|
||||
// empty — this allows control-plane unit tests to delete without docker.
|
||||
List<String> tags = listTagsBestEffort(account, region, repositoryName);
|
||||
if (!tags.isEmpty() && !force) {
|
||||
if (!force && !tags.isEmpty()) {
|
||||
throw new AwsException("RepositoryNotEmptyException",
|
||||
"The repository with name '" + repositoryName
|
||||
+ "' in registry with id '" + account + "' cannot be deleted because it still contains images",
|
||||
400);
|
||||
}
|
||||
|
||||
if (force && !tags.isEmpty()) {
|
||||
// Phase 5 will issue real DELETE /v2/<name>/manifests/<digest> calls.
|
||||
LOG.infov("Force-deleting ECR repository {0} containing {1} tag(s) (manifest deletion deferred)",
|
||||
repositoryName, tags.size());
|
||||
if (force) {
|
||||
deleteRepositoryStorage(account, region, repositoryName);
|
||||
}
|
||||
|
||||
repoStore.delete(key);
|
||||
@@ -212,6 +208,9 @@ public class EcrService implements ResourceProvider {
|
||||
for (ImageMetadata meta : imageMetaStore.scan(k -> k.startsWith(metaPrefix))) {
|
||||
imageMetaStore.delete(metaPrefix + meta.getDigest());
|
||||
}
|
||||
if (!hasRepositories()) {
|
||||
registryManager.pruneStorage();
|
||||
}
|
||||
LOG.infov("Deleted ECR repository {0}/{1}/{2}", region, account, repositoryName);
|
||||
return repo;
|
||||
}
|
||||
@@ -544,6 +543,28 @@ public class EcrService implements ResourceProvider {
|
||||
}
|
||||
}
|
||||
|
||||
private void deleteRepositoryStorage(String account, String region, String repositoryName) {
|
||||
try {
|
||||
registryManager.deleteRepositoryStorage(account, region, repositoryName);
|
||||
} catch (Exception e) {
|
||||
throw registryFailure(repositoryName, e);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean hasRepositories() {
|
||||
if (repoStore instanceof AccountAwareStorageBackend<?> accountAware) {
|
||||
return !accountAware.scanAllAccounts().isEmpty();
|
||||
}
|
||||
return !repoStore.scan(k -> true).isEmpty();
|
||||
}
|
||||
|
||||
private AwsException registryFailure(String repositoryName, Exception cause) {
|
||||
LOG.warnv("Could not delete ECR repository {0} from the backing registry: {1}",
|
||||
repositoryName, cause.getMessage());
|
||||
return new AwsException("ServerException",
|
||||
"Could not delete images from repository '" + repositoryName + "'", 500);
|
||||
}
|
||||
|
||||
private static String key(String region, String account, String repoName) {
|
||||
return region + "::" + account + "::" + repoName;
|
||||
}
|
||||
|
||||
+91
-4
@@ -45,7 +45,10 @@ public class EcrRegistryManager {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(EcrRegistryManager.class);
|
||||
private static final int CONTAINER_INTERNAL_PORT = 5000;
|
||||
private static final int REPOSITORY_DELETION_TIMEOUT_SECONDS = 10;
|
||||
private static final int REPOSITORY_DELETION_KILL_GRACE_SECONDS = 1;
|
||||
private static final String NAMED_VOLUME = "floci-ecr-registry-data";
|
||||
private static final String REPOSITORIES_PATH = "/var/lib/registry/docker/registry/v2/repositories/";
|
||||
|
||||
/** Matches an AWS-shaped ECR image URI: {@code <account>.dkr.ecr.<region>.amazonaws.com/<repo>[:tag]}. */
|
||||
private static final java.util.regex.Pattern AWS_ECR_URI =
|
||||
@@ -363,16 +366,100 @@ public class EcrRegistryManager {
|
||||
return new GcResult(output.toString(), durationMs);
|
||||
}
|
||||
|
||||
/** Removes repository manifest storage without removing descendant repository directories. */
|
||||
public synchronized void deleteRepositoryStorage(String accountId, String region, String repositoryName) {
|
||||
ensureStarted();
|
||||
if (!started || containerId == null) {
|
||||
throw new IllegalStateException("ECR registry is not started");
|
||||
}
|
||||
String repository = internalRepoName(accountId, region, repositoryName);
|
||||
String path = repositoryStoragePath(repository);
|
||||
StringBuilder output = new StringBuilder();
|
||||
DockerClient dockerClient = lifecycleManager.getDockerClient();
|
||||
ExecCreateCmdResponse exec = dockerClient
|
||||
.execCreateCmd(containerId)
|
||||
.withCmd("timeout", "-k", String.valueOf(REPOSITORY_DELETION_KILL_GRACE_SECONDS),
|
||||
String.valueOf(REPOSITORY_DELETION_TIMEOUT_SECONDS), "rm", "-rf",
|
||||
path + "/_layers", path + "/_manifests", path + "/_uploads")
|
||||
.withAttachStdout(true)
|
||||
.withAttachStderr(true)
|
||||
.exec();
|
||||
try {
|
||||
boolean completed = dockerClient.execStartCmd(exec.getId())
|
||||
.exec(new ResultCallback.Adapter<Frame>() {
|
||||
@Override
|
||||
public void onNext(Frame frame) {
|
||||
output.append(new String(frame.getPayload(), StandardCharsets.UTF_8));
|
||||
}
|
||||
})
|
||||
.awaitCompletion(REPOSITORY_DELETION_TIMEOUT_SECONDS
|
||||
+ REPOSITORY_DELETION_KILL_GRACE_SECONDS + 1, TimeUnit.SECONDS);
|
||||
if (!completed) {
|
||||
throw new RuntimeException("repository deletion timed out");
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new RuntimeException("repository deletion interrupted", e);
|
||||
}
|
||||
Long exitCode = dockerClient.inspectExecCmd(exec.getId()).exec().getExitCodeLong();
|
||||
if (exitCode == null || exitCode != 0) {
|
||||
throw new RuntimeException("repository deletion failed: " + output);
|
||||
}
|
||||
}
|
||||
|
||||
/** Stops the container if {@code keepRunningOnShutdown=false}. Called from EmulatorLifecycle hooks. */
|
||||
public void shutdown() {
|
||||
public synchronized void shutdown() {
|
||||
if (config.services().ecr().keepRunningOnShutdown()) {
|
||||
if (started && containerId != null) {
|
||||
LOG.infov("Leaving ECR backing registry container {0} running for next start-up", containerId);
|
||||
}
|
||||
return;
|
||||
}
|
||||
stopRegistry();
|
||||
removeStorageIfConfigured();
|
||||
}
|
||||
|
||||
/** Removes the shared registry storage after the final ECR repository is deleted. */
|
||||
public synchronized void pruneStorage() {
|
||||
if (!shouldPruneStorage()) {
|
||||
return;
|
||||
}
|
||||
stopRegistry();
|
||||
removeStorageIfConfigured();
|
||||
}
|
||||
|
||||
private void stopRegistry() {
|
||||
if (!started || containerId == null) {
|
||||
return;
|
||||
}
|
||||
if (config.services().ecr().keepRunningOnShutdown()) {
|
||||
LOG.infov("Leaving ECR backing registry container {0} running for next start-up", containerId);
|
||||
lifecycleManager.stopAndRemove(containerId, logStream);
|
||||
portAllocator.release(hostPort);
|
||||
containerId = null;
|
||||
logStream = null;
|
||||
started = false;
|
||||
reconciled = false;
|
||||
hostPort = config.services().ecr().registryBasePort();
|
||||
}
|
||||
|
||||
private void removeStorageIfConfigured() {
|
||||
if (!shouldPruneStorage() || !ContainerStorageHelper.isNamedVolumeMode(config)) {
|
||||
return;
|
||||
}
|
||||
lifecycleManager.stopAndRemove(containerId, logStream);
|
||||
ContainerStorageHelper.removeNamedVolume(
|
||||
config, lifecycleManager, ContainerStorageHelper.dockerName(config, NAMED_VOLUME));
|
||||
}
|
||||
|
||||
private boolean shouldPruneStorage() {
|
||||
return "memory".equals(config.storage().mode()) || config.storage().pruneVolumesOnDelete();
|
||||
}
|
||||
|
||||
private static String repositoryStoragePath(String repository) {
|
||||
for (String segment : repository.split("/")) {
|
||||
if (segment.isEmpty() || ".".equals(segment) || "..".equals(segment)) {
|
||||
throw new IllegalArgumentException("Invalid registry repository path");
|
||||
}
|
||||
}
|
||||
return REPOSITORIES_PATH + repository;
|
||||
}
|
||||
|
||||
private void adoptExisting(Container existing) {
|
||||
|
||||
Reference in new issue
Block a user