mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(ecr): record image push time for registry uploads (#8530)
An image pushed through the Registry V2 endpoint now records its push time when the registry accepts the manifest, so DescribeImages reports the push time instead of the time of the first read. A tag added to an existing digest keeps the first push time, a digest delete clears it, and a metadata write that fails keeps the registry's response and the first push time for the next read.
This commit is contained in:
1 parent
f733c91a74
commit
e8f5a696a8
5 files changed
+283
-22
No files matched your search
@@ -289,9 +289,29 @@ class EcrImagePushApiDockerIntegrationTest {
|
||||
.body("image.imageManifestMediaType", equalTo("application/vnd.oci.image.manifest.v1+json"));
|
||||
}
|
||||
|
||||
/** Last, since every class on this profile shares one registry. */
|
||||
@Test
|
||||
@Order(10)
|
||||
void registryRetagPreservesImagePushTime() throws Exception {
|
||||
String host = "000000000000.dkr.ecr.us-east-1.localhost";
|
||||
String path = "/v2/" + repo + "/manifests/";
|
||||
Fetched pushed = registryRequest(host, path + "registry", HttpMethod.PUT, null, manifest);
|
||||
assertEquals(201, pushed.statusCode());
|
||||
assertEquals(manifestDigest, pushed.digest());
|
||||
|
||||
long pushTime = call("DescribeImages", "{\"repositoryName\":\"" + repo
|
||||
+ "\",\"imageIds\":[{\"imageTag\":\"registry\"}]}")
|
||||
.statusCode(200).extract().jsonPath().getLong("imageDetails[0].imagePushedAt");
|
||||
|
||||
assertEquals(201, registryRequest(host, path + "registry-retag", HttpMethod.PUT, null, manifest).statusCode());
|
||||
long retaggedPushTime = call("DescribeImages", "{\"repositoryName\":\"" + repo
|
||||
+ "\",\"imageIds\":[{\"imageTag\":\"registry-retag\"}]}")
|
||||
.statusCode(200).extract().jsonPath().getLong("imageDetails[0].imagePushedAt");
|
||||
assertEquals(pushTime, retaggedPushTime);
|
||||
}
|
||||
|
||||
/** Last, since every class on this profile shares one registry. */
|
||||
@Test
|
||||
@Order(11)
|
||||
void cleanUp() {
|
||||
call("DeleteRepository", "{\"repositoryName\":\"" + repo + "\",\"force\":true}").statusCode(200);
|
||||
call("DeleteRepository", "{\"repositoryName\":\"" + immutableRepo + "\",\"force\":true}").statusCode(200);
|
||||
@@ -322,19 +342,27 @@ class EcrImagePushApiDockerIntegrationTest {
|
||||
|
||||
/** A GET through Floci's registry data plane, by the host name a Docker client would use. */
|
||||
private Fetched registryGet(String host, String path, String accept) throws Exception {
|
||||
return registryRequest(host, path, HttpMethod.GET, accept, null);
|
||||
}
|
||||
|
||||
private Fetched registryRequest(String host, String path, HttpMethod method, String accept,
|
||||
String manifestBody) throws Exception {
|
||||
int port = RestAssured.port;
|
||||
HttpClient client = vertx.createHttpClient();
|
||||
RequestOptions options = new RequestOptions()
|
||||
.setServer(SocketAddress.inetSocketAddress(port, "127.0.0.1"))
|
||||
.setHost(host)
|
||||
.setPort(port)
|
||||
.setMethod(HttpMethod.GET)
|
||||
.setMethod(method)
|
||||
.setURI(path);
|
||||
return client.request(options)
|
||||
.compose(request -> {
|
||||
if (accept != null) {
|
||||
request.putHeader("Accept", accept);
|
||||
}
|
||||
if (manifestBody != null) {
|
||||
return request.putHeader("Content-Type", DOCKER_MANIFEST).send(manifestBody);
|
||||
}
|
||||
return request.send();
|
||||
})
|
||||
.compose(response -> response.body().map(body -> new Fetched(
|
||||
|
||||
@@ -869,6 +869,54 @@ class EcrServiceTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void describeKeepsAcceptedPushTimeWhileMetadataWritesFail() throws Exception {
|
||||
String repo = "batch-delete/push-recovery";
|
||||
String internal = ACCOUNT + "/" + REGION + "/" + repo;
|
||||
AtomicBoolean unavailable = new AtomicBoolean(true);
|
||||
InMemoryStorage<String, ImageMetadata> metaStore = new InMemoryStorage<>() {
|
||||
@Override
|
||||
public void put(String key, ImageMetadata metadata) {
|
||||
if (unavailable.get()) {
|
||||
throw new IllegalStateException("metadata store unavailable");
|
||||
}
|
||||
super.put(key, metadata);
|
||||
}
|
||||
};
|
||||
EcrService withMeta = new EcrService(new InMemoryStorage<>(), metaStore, pullThroughCacheRuleStore,
|
||||
registryManager, Mockito.mock(EmulatorConfig.class), new RegionResolver(REGION, ACCOUNT));
|
||||
try (MultiTagRegistryServer registry = new MultiTagRegistryServer(
|
||||
internal, DELETE_DIGEST, DELETE_MANIFEST, "stable")) {
|
||||
when(registryManager.httpClient()).thenReturn(new RegistryHttpClient("http://localhost:" + registry.port()));
|
||||
withMeta.createRepository(repo, null, null, null, null, null, null, REGION);
|
||||
Instant pushedAt = Instant.parse("2026-01-01T00:00:00Z");
|
||||
assertThrows(IllegalStateException.class,
|
||||
() -> withMeta.recordImagePush(ACCOUNT, REGION, repo, DELETE_DIGEST, pushedAt));
|
||||
|
||||
List<ImageIdentifier> images = List.of(new ImageIdentifier("stable", null));
|
||||
EcrService.DescribeImagesResult duringOutage = withMeta.describeImages(repo, images, null, REGION);
|
||||
assertEquals(1, duringOutage.imageDetails().size());
|
||||
assertEquals(pushedAt, duringOutage.imageDetails().getFirst().getImagePushedAt());
|
||||
assertTrue(metaStore.scan(key -> true).isEmpty());
|
||||
|
||||
unavailable.set(false);
|
||||
assertEquals(pushedAt, withMeta.describeImages(repo, images, null, REGION)
|
||||
.imageDetails().getFirst().getImagePushedAt());
|
||||
assertEquals(pushedAt, metaStore.scan(key -> true).getFirst().getPushedAt());
|
||||
|
||||
withMeta.removeImagePush(ACCOUNT, REGION, repo, DELETE_DIGEST);
|
||||
unavailable.set(true);
|
||||
assertThrows(IllegalStateException.class,
|
||||
() -> withMeta.recordImagePush(ACCOUNT, REGION, repo, DELETE_DIGEST, pushedAt));
|
||||
withMeta.removeImagePush(ACCOUNT, REGION, repo, DELETE_DIGEST);
|
||||
|
||||
unavailable.set(false);
|
||||
Instant repushedAt = pushedAt.plusSeconds(1);
|
||||
assertEquals(repushedAt,
|
||||
withMeta.recordImagePush(ACCOUNT, REGION, repo, DELETE_DIGEST, repushedAt).getPushedAt());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void batchDeleteImageByTagKeepsTheImagePushedAtOfTheSurvivingTag() throws Exception {
|
||||
String repositoryName = "batch-delete/pushed-at";
|
||||
|
||||
@@ -44,13 +44,16 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/** Unit tests for routing AWS-shaped ECR repository URIs through Floci's data plane. */
|
||||
class EcrRegistryDataPlaneTest {
|
||||
|
||||
private Vertx vertx;
|
||||
private EcrService ecrService;
|
||||
private Semaphore repositoryWriteLock;
|
||||
private CountDownLatch repositoryWriteLockRefused;
|
||||
|
||||
@@ -243,8 +246,61 @@ class EcrRegistryDataPlaneTest {
|
||||
"/v2/000000000000/us-east-1/platform/api/blobs/uploads/upload-id", request));
|
||||
}
|
||||
|
||||
@Test
|
||||
void recordsOnlyAcceptedManifestWritesBeforeResponding() throws Exception {
|
||||
String digest = "sha256:" + "a".repeat(64);
|
||||
AtomicBoolean accept = new AtomicBoolean();
|
||||
HttpServer registry = vertx.createHttpServer()
|
||||
.requestHandler(request -> request.bodyHandler(body -> {
|
||||
int successStatus = request.method() == HttpMethod.DELETE ? 202 : 201;
|
||||
request.response().putHeader("Docker-Content-Digest", digest)
|
||||
.setStatusCode(accept.get() ? successStatus : 400).end();
|
||||
}))
|
||||
.listen(0, "127.0.0.1")
|
||||
.toCompletionStage().toCompletableFuture().get(2, TimeUnit.SECONDS);
|
||||
|
||||
HttpServer dataPlane = null;
|
||||
try {
|
||||
dataPlane = dataPlane(registry, false);
|
||||
assertEquals(400, putManifest(dataPlane.actualPort()).statusCode());
|
||||
Mockito.verify(ecrService, Mockito.never()).recordImagePush(anyString(), anyString(),
|
||||
anyString(), anyString(), any());
|
||||
String manifestPath = "/v2/platform/api/manifests/" + digest;
|
||||
assertEquals(400, request(dataPlane.actualPort(), HttpMethod.DELETE, manifestPath).status());
|
||||
Mockito.verify(ecrService, Mockito.never()).removeImagePush(anyString(), anyString(), anyString(), anyString());
|
||||
|
||||
Mockito.doAnswer(call -> {
|
||||
assertEquals(0, repositoryWriteLock.availablePermits());
|
||||
return null;
|
||||
}).when(ecrService).recordImagePush(eq("000000000000"), eq("us-east-1"),
|
||||
eq("platform/api"), eq(digest), any());
|
||||
accept.set(true);
|
||||
assertEquals(201, putManifest(dataPlane.actualPort()).statusCode());
|
||||
Mockito.verify(ecrService).recordImagePush(eq("000000000000"), eq("us-east-1"),
|
||||
eq("platform/api"), eq(digest), any());
|
||||
|
||||
Mockito.doAnswer(call -> {
|
||||
assertEquals(0, repositoryWriteLock.availablePermits());
|
||||
return null;
|
||||
}).when(ecrService).removeImagePush("000000000000", "us-east-1", "platform/api", digest);
|
||||
assertEquals(202, request(dataPlane.actualPort(), HttpMethod.DELETE, manifestPath).status());
|
||||
Mockito.verify(ecrService).removeImagePush("000000000000", "us-east-1", "platform/api", digest);
|
||||
|
||||
Mockito.doThrow(new IllegalStateException("metadata store unavailable"))
|
||||
.when(ecrService).recordImagePush(eq("000000000000"), eq("us-east-1"),
|
||||
eq("platform/api"), eq(digest), any());
|
||||
assertEquals(201, putManifest(dataPlane.actualPort()).statusCode());
|
||||
} finally {
|
||||
if (dataPlane != null) {
|
||||
dataPlane.close().toCompletionStage().toCompletableFuture().get(2, TimeUnit.SECONDS);
|
||||
}
|
||||
registry.close().toCompletionStage().toCompletableFuture().get(2, TimeUnit.SECONDS);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void immutableTagRejectsReplacementBeforeItReachesTheRegistry() throws Exception {
|
||||
String digest = "sha256:" + "b".repeat(64);
|
||||
AtomicBoolean tagExists = new AtomicBoolean();
|
||||
AtomicInteger manifestWrites = new AtomicInteger();
|
||||
AtomicReference<String> headPath = new AtomicReference<>();
|
||||
@@ -263,7 +319,7 @@ class EcrRegistryDataPlaneTest {
|
||||
manifestBody.set(body.toString());
|
||||
manifestWrites.incrementAndGet();
|
||||
tagExists.set(true);
|
||||
request.response().setStatusCode(201).end();
|
||||
request.response().setStatusCode(201).putHeader("Docker-Content-Digest", digest).end();
|
||||
});
|
||||
} else {
|
||||
request.response().setStatusCode(405).end();
|
||||
@@ -275,6 +331,9 @@ class EcrRegistryDataPlaneTest {
|
||||
HttpServer dataPlane = null;
|
||||
try {
|
||||
dataPlane = dataPlane(registry, true);
|
||||
Mockito.doThrow(new IllegalStateException("transient metadata store failure")).doReturn(null)
|
||||
.when(ecrService).recordImagePush(eq("000000000000"), eq("us-east-1"),
|
||||
eq("platform/api"), eq(digest), any());
|
||||
|
||||
assertEquals(201, putManifest(dataPlane.actualPort()).statusCode());
|
||||
|
||||
@@ -290,6 +349,8 @@ class EcrRegistryDataPlaneTest {
|
||||
error.getString("message"));
|
||||
|
||||
assertEquals(1, manifestWrites.get());
|
||||
Mockito.verify(ecrService, Mockito.times(2)).recordImagePush(eq("000000000000"), eq("us-east-1"),
|
||||
eq("platform/api"), eq(digest), any());
|
||||
assertEquals("/v2/legacy/platform-api/manifests/v1", headPath.get());
|
||||
assertEquals("/v2/legacy/platform-api/manifests/v1", manifestPath.get());
|
||||
assertEquals("{}", manifestBody.get());
|
||||
@@ -914,7 +975,7 @@ class EcrRegistryDataPlaneTest {
|
||||
|
||||
private HttpServer dataPlane(EcrRegistryManager registryManager, boolean immutableTags,
|
||||
CountDownLatch tagReleased) throws Exception {
|
||||
EcrService ecrService = Mockito.mock(EcrService.class);
|
||||
ecrService = Mockito.mock(EcrService.class);
|
||||
EmulatorConfig config = Mockito.mock(EmulatorConfig.class, Mockito.RETURNS_DEEP_STUBS);
|
||||
when(config.services().ecr().enabled()).thenReturn(true);
|
||||
when(config.services().ecr().uriStyle()).thenReturn("hostname");
|
||||
|
||||
Reference in new issue
Block a user