Skip to content

Commit

Permalink
Cleanup OciMetadataRegistry
Browse files Browse the repository at this point in the history
  • Loading branch information
SgtSilvio committed Jul 10, 2024
1 parent 0ee4e9c commit 35cdd0e
Showing 1 changed file with 20 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import java.util.*
*/
internal class OciMetadataRegistry(val registryApi: OciRegistryApi) {

data class Metadata(val metadata: OciMetadata, val platform: Platform, val digest: OciDigest, val size: Int) // TODO rename
data class Metadata(val metadata: OciMetadata, val platform: Platform, val digest: OciDigest, val size: Int)

fun pullMetadataList(
registry: String,
Expand All @@ -40,7 +40,7 @@ internal class OciMetadataRegistry(val registryApi: OciRegistryApi) {
transformToMetadataList(registry, imageReference, manifest, credentials)
}

private fun transformToMetadataList( // TODO inline?
private fun transformToMetadataList(
registry: String,
imageReference: OciImageReference,
manifest: OciRegistryApi.Manifest,
Expand All @@ -49,57 +49,54 @@ internal class OciMetadataRegistry(val registryApi: OciRegistryApi) {
INDEX_MEDIA_TYPE -> transformIndexToMetadataList(
registry,
imageReference,
manifest.data,
manifest,
credentials,
INDEX_MEDIA_TYPE,
MANIFEST_MEDIA_TYPE,
CONFIG_MEDIA_TYPE,
LAYER_MEDIA_TYPE_PREFIX,
)

MANIFEST_MEDIA_TYPE -> transformManifestToMetadataList(
registry,
imageReference,
manifest.data,
manifest.digest,
manifest,
credentials,
MANIFEST_MEDIA_TYPE,
CONFIG_MEDIA_TYPE,
LAYER_MEDIA_TYPE_PREFIX,
)

DOCKER_MANIFEST_LIST_MEDIA_TYPE -> transformIndexToMetadataList(
registry,
imageReference,
manifest.data,
manifest,
credentials,
DOCKER_MANIFEST_LIST_MEDIA_TYPE,
DOCKER_MANIFEST_MEDIA_TYPE,
DOCKER_CONFIG_MEDIA_TYPE,
DOCKER_LAYER_MEDIA_TYPE,
)

DOCKER_MANIFEST_MEDIA_TYPE -> transformManifestToMetadataList(
registry,
imageReference,
manifest.data,
manifest.digest,
manifest,
credentials,
DOCKER_MANIFEST_MEDIA_TYPE,
DOCKER_CONFIG_MEDIA_TYPE,
DOCKER_LAYER_MEDIA_TYPE,
)

else -> throw IllegalStateException("unsupported manifest media type '${manifest.mediaType}'")
}

private fun transformIndexToMetadataList(
registry: String,
imageReference: OciImageReference,
index: ByteArray,
index: OciRegistryApi.Manifest,
credentials: Credentials?,
indexMediaType: String,
manifestMediaType: String,
configMediaType: String,
layerMediaTypePrefix: String,
): Mono<List<Metadata>> {
val indexJsonObject = jsonObject(String(index))
val indexJsonObject = jsonObject(String(index.data))
val indexAnnotations = indexJsonObject.getStringMapOrEmpty("annotations")
val metadataMonoList = indexJsonObject.get("manifests") {
asArray().toList {
Expand All @@ -120,12 +117,10 @@ internal class OciMetadataRegistry(val registryApi: OciRegistryApi) {
transformManifestToMetadata(
registry,
imageReference,
manifest.data,
manifest.digest,
manifest,
manifestDescriptor.annotations,
indexAnnotations,
credentials,
manifestMediaType,
configMediaType,
layerMediaTypePrefix,
)
Expand All @@ -138,7 +133,7 @@ internal class OciMetadataRegistry(val registryApi: OciRegistryApi) {
}
}
}
indexJsonObject.requireStringOrNull("mediaType", indexMediaType)
indexJsonObject.requireStringOrNull("mediaType", index.mediaType)
indexJsonObject.requireLong("schemaVersion", 2)
// the same order as in the manifest is guaranteed by mergeSequential
return Flux.mergeSequential(metadataMonoList).collectList()
Expand All @@ -147,44 +142,38 @@ internal class OciMetadataRegistry(val registryApi: OciRegistryApi) {
private fun transformManifestToMetadataList(
registry: String,
imageReference: OciImageReference,
manifest: ByteArray,
digest: OciDigest,
manifest: OciRegistryApi.Manifest,
credentials: Credentials?,
manifestMediaType: String,
configMediaType: String,
layerMediaTypePrefix: String,
): Mono<List<Metadata>> = transformManifestToMetadata(
registry,
imageReference,
manifest,
digest,
TreeMap(),
TreeMap(),
credentials,
manifestMediaType,
configMediaType,
layerMediaTypePrefix,
).map { listOf(it) }

private fun transformManifestToMetadata(
registry: String,
imageReference: OciImageReference,
manifest: ByteArray,
digest: OciDigest,
manifest: OciRegistryApi.Manifest,
manifestDescriptorAnnotations: SortedMap<String, String>,
indexAnnotations: SortedMap<String, String>,
credentials: Credentials?,
manifestMediaType: String,
configMediaType: String,
layerMediaTypePrefix: String,
): Mono<Metadata> {
val manifestJsonObject = jsonObject(String(manifest))
val manifestJsonObject = jsonObject(String(manifest.data))
val manifestAnnotations = manifestJsonObject.getStringMapOrEmpty("annotations")
val configDescriptor = manifestJsonObject.get("config") { asObject().decodeOciDescriptor() }
val layerDescriptors =
manifestJsonObject.getOrNull("layers") { asArray().toList { asObject().decodeOciDescriptor() } }
?: emptyList()
manifestJsonObject.requireStringOrNull("mediaType", manifestMediaType)
manifestJsonObject.requireStringOrNull("mediaType", manifest.mediaType)
manifestJsonObject.requireLong("schemaVersion", 2)
if ((configDescriptor.mediaType != configMediaType) || layerDescriptors.any { !it.mediaType.startsWith(layerMediaTypePrefix) }) {
return Mono.empty()
Expand Down Expand Up @@ -304,8 +293,8 @@ internal class OciMetadataRegistry(val registryApi: OciRegistryApi) {
layers,
),
Platform(os, architecture, variant, osVersion, osFeatures),
digest,
manifest.size,
manifest.digest,
manifest.data.size,
)
}
}
Expand Down

0 comments on commit 35cdd0e

Please sign in to comment.