Skip to content

Commit

Permalink
Move OciMetadata.Layer -> OciLayerMetadata
Browse files Browse the repository at this point in the history
Move OciMetadata.Layer.Descriptor -> OciLayerDescriptor
  • Loading branch information
SgtSilvio committed Jul 14, 2024
1 parent b0c08d1 commit 18fc8b2
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ internal class OciVariant(
)

internal class OciLayer( // TODO internal?
val descriptor: OciMetadata.Layer.Descriptor,
val descriptor: OciLayerDescriptor,
val file: File,
)

Expand Down Expand Up @@ -168,7 +168,7 @@ abstract class OciImagesInputTask : DefaultTask() {
return digestToLayerFile
}

private fun checkDuplicateLayer(layerDescriptor: OciMetadata.Layer.Descriptor, file1: File, file2: File) {
private fun checkDuplicateLayer(layerDescriptor: OciLayerDescriptor, file1: File, file2: File) {
if (!FileUtils.contentEquals(file1, file2)) {
throw IllegalStateException("hash collision for digest ${layerDescriptor.digest}: expected file contents of $file1 and $file2 to be the same")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,15 +277,15 @@ internal abstract class OciImageDefinitionImpl @Inject constructor(
OciMetadataBuilder::manifestDescriptorAnnotations,
)
.zip(imageDefinition.indexAnnotations.orElse(emptyMap()), OciMetadataBuilder::indexAnnotations)
.zip(createMetadataLayers(providerFactory), OciMetadataBuilder::layers)
.zip(createLayerMetadataList(providerFactory), OciMetadataBuilder::layers)
.map { it.build() }

private fun createMetadataLayers(providerFactory: ProviderFactory): Provider<List<OciMetadata.Layer>> =
private fun createLayerMetadataList(providerFactory: ProviderFactory): Provider<List<OciLayerMetadata>> =
providerFactory.provider { layers.list }.flatMap { layers ->
var listProvider = providerFactory.provider { listOf<OciMetadata.Layer>() }
var listProvider = providerFactory.provider { listOf<OciLayerMetadata>() }
for (layer in layers) {
layer as Layer
listProvider = listProvider.zip(layer.createMetadataLayer(providerFactory)) { list, e -> list + e }
listProvider = listProvider.zip(layer.createLayerMetadata(providerFactory)) { list, e -> list + e }
}
listProvider
}
Expand Down Expand Up @@ -407,26 +407,23 @@ internal abstract class OciImageDefinitionImpl @Inject constructor(

fun getTask() = externalTask ?: task

fun createMetadataLayer(providerFactory: ProviderFactory): Provider<OciMetadata.Layer> =
providerFactory.provider { OciMetadataLayerBuilder() }
.zipAbsentAsNull(metadata.creationTime, OciMetadataLayerBuilder::creationTime)
.zipAbsentAsNull(metadata.author, OciMetadataLayerBuilder::author)
.zipAbsentAsNull(metadata.createdBy, OciMetadataLayerBuilder::createdBy)
.zipAbsentAsNull(metadata.comment, OciMetadataLayerBuilder::comment)
.zipAbsentAsNull(
createMetadataLayerDescriptor(providerFactory),
OciMetadataLayerBuilder::descriptor,
)
fun createLayerMetadata(providerFactory: ProviderFactory): Provider<OciLayerMetadata> =
providerFactory.provider { OciLayerMetadataBuilder() }
.zipAbsentAsNull(metadata.creationTime, OciLayerMetadataBuilder::creationTime)
.zipAbsentAsNull(metadata.author, OciLayerMetadataBuilder::author)
.zipAbsentAsNull(metadata.createdBy, OciLayerMetadataBuilder::createdBy)
.zipAbsentAsNull(metadata.comment, OciLayerMetadataBuilder::comment)
.zipAbsentAsNull(createLayerDescriptor(providerFactory), OciLayerMetadataBuilder::descriptor)
.map { it.build() }

private fun createMetadataLayerDescriptor(providerFactory: ProviderFactory): Provider<OciMetadata.Layer.Descriptor> {
private fun createLayerDescriptor(providerFactory: ProviderFactory): Provider<OciLayerDescriptor> {
val task = providerFactory.provider { getTask() }.flatMap { it }
return providerFactory.provider { OciMetadataLayerDescriptorBuilder() }
.zip(metadata.annotations.orElse(emptyMap()), OciMetadataLayerDescriptorBuilder::annotations)
.zipAbsentAsNull(task.flatMap { it.mediaType }, OciMetadataLayerDescriptorBuilder::mediaType)
.zipAbsentAsNull(task.flatMap { it.digest }, OciMetadataLayerDescriptorBuilder::digest)
.zipAbsentAsNull(task.flatMap { it.size }, OciMetadataLayerDescriptorBuilder::size)
.zipAbsentAsNull(task.flatMap { it.diffId }, OciMetadataLayerDescriptorBuilder::diffId)
return providerFactory.provider { OciLayerDescriptorBuilder() }
.zip(metadata.annotations.orElse(emptyMap()), OciLayerDescriptorBuilder::annotations)
.zipAbsentAsNull(task.flatMap { it.mediaType }, OciLayerDescriptorBuilder::mediaType)
.zipAbsentAsNull(task.flatMap { it.digest }, OciLayerDescriptorBuilder::digest)
.zipAbsentAsNull(task.flatMap { it.size }, OciLayerDescriptorBuilder::size)
.zipAbsentAsNull(task.flatMap { it.diffId }, OciLayerDescriptorBuilder::diffId)
.map { it.build() }
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,13 @@ internal class OciImageMetadataRegistry(val registryApi: OciRegistryApi) {
}
var i = 0
val layers = history?.map { historyEntry ->
OciMetadata.Layer(
OciLayerMetadata(
if (historyEntry.emptyLayer) null else run {
// TODO index check
val descriptor = layerDescriptors[i]
val diffId = diffIds[i]
i++
OciMetadata.Layer.Descriptor(
OciLayerDescriptor(
normalizeLayerMediaType(descriptor.mediaType),
descriptor.digest,
descriptor.size,
Expand All @@ -263,8 +263,8 @@ internal class OciImageMetadataRegistry(val registryApi: OciRegistryApi) {
)
// TODO check i == layerDescriptors.size
} ?: layerDescriptors.zip(diffIds) { descriptor, diffId ->
OciMetadata.Layer(
OciMetadata.Layer.Descriptor(
OciLayerMetadata(
OciLayerDescriptor(
normalizeLayerMediaType(descriptor.mediaType),
descriptor.digest,
descriptor.size,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,21 @@ data class OciMetadata(
val manifestAnnotations: SortedMap<String, String>,
val manifestDescriptorAnnotations: SortedMap<String, String>,
val indexAnnotations: SortedMap<String, String>,
val layers: List<Layer>,
) : Serializable {
val layers: List<OciLayerMetadata>,
) : Serializable

data class Layer(
val descriptor: Descriptor?,
val creationTime: Instant?,
val author: String?,
val createdBy: String?,
val comment: String?,
) : Serializable {
data class OciLayerMetadata(
val descriptor: OciLayerDescriptor?,
val creationTime: Instant?,
val author: String?,
val createdBy: String?,
val comment: String?,
) : Serializable

data class Descriptor(
override val mediaType: String,
override val digest: OciDigest,
override val size: Long,
val diffId: OciDigest,
override val annotations: SortedMap<String, String>,
) : OciDescriptor, Serializable
}
}
data class OciLayerDescriptor(
override val mediaType: String,
override val digest: OciDigest,
override val size: Long,
val diffId: OciDigest,
override val annotations: SortedMap<String, String>,
) : OciDescriptor, Serializable
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ internal class OciMetadataBuilder : Serializable {
private var manifestAnnotations: Map<String, String> = emptyMap()
private var manifestDescriptorAnnotations: Map<String, String> = emptyMap()
private var indexAnnotations: Map<String, String> = emptyMap()
private var layers: List<OciMetadata.Layer> = emptyList()
private var layers: List<OciLayerMetadata> = emptyList()

fun imageReference(v: OciImageReference) = apply { imageReference = v }
fun creationTime(v: Instant?) = apply { creationTime = v?.toSerializableInstant() }
Expand All @@ -41,7 +41,7 @@ internal class OciMetadataBuilder : Serializable {
fun manifestAnnotations(v: Map<String, String>) = apply { manifestAnnotations = v }
fun manifestDescriptorAnnotations(v: Map<String, String>) = apply { manifestDescriptorAnnotations = v }
fun indexAnnotations(v: Map<String, String>) = apply { indexAnnotations = v }
fun layers(v: List<OciMetadata.Layer>) = apply { layers = v }
fun layers(v: List<OciLayerMetadata>) = apply { layers = v }

fun build() = OciMetadata(
imageReference!!,
Expand All @@ -64,23 +64,23 @@ internal class OciMetadataBuilder : Serializable {
)
}

internal class OciMetadataLayerBuilder : Serializable {
private var descriptor: OciMetadata.Layer.Descriptor? = null
internal class OciLayerMetadataBuilder : Serializable {
private var descriptor: OciLayerDescriptor? = null
private var creationTime: SerializableInstant? = null
private var author: String? = null
private var createdBy: String? = null
private var comment: String? = null

fun descriptor(v: OciMetadata.Layer.Descriptor?) = apply { descriptor = v }
fun descriptor(v: OciLayerDescriptor?) = apply { descriptor = v }
fun creationTime(v: Instant?) = apply { creationTime = v?.toSerializableInstant() }
fun author(v: String?) = apply { author = v }
fun createdBy(v: String?) = apply { createdBy = v }
fun comment(v: String?) = apply { comment = v }

fun build() = OciMetadata.Layer(descriptor, creationTime?.toInstant(), author, createdBy, comment)
fun build() = OciLayerMetadata(descriptor, creationTime?.toInstant(), author, createdBy, comment)
}

internal class OciMetadataLayerDescriptorBuilder : Serializable {
internal class OciLayerDescriptorBuilder : Serializable {
private var mediaType: String? = null
private var digest: OciDigest? = null
private var size: Long? = null
Expand All @@ -95,7 +95,7 @@ internal class OciMetadataLayerDescriptorBuilder : Serializable {

fun build() = when {
(mediaType == null) && (digest == null) && (size == null) && (diffId == null) && annotations.isEmpty() -> null
else -> OciMetadata.Layer.Descriptor(
else -> OciLayerDescriptor(
mediaType!!,
digest!!,
size!!,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ private fun JsonObjectStringBuilder.encodeOciMetadata(metadata: OciMetadata) {
addObjectIfNotEmpty("manifestAnnotations", metadata.manifestAnnotations)
addObjectIfNotEmpty("manifestDescriptorAnnotations", metadata.manifestDescriptorAnnotations)
addObjectIfNotEmpty("indexAnnotations", metadata.indexAnnotations)
addArrayIfNotEmpty("layers", metadata.layers) { addObject { encodeLayer(it) } }
addArrayIfNotEmpty("layers", metadata.layers) { addObject { encodeOciLayerMetadata(it) } }
}

private fun JsonObjectStringBuilder.encodeLayer(layer: OciMetadata.Layer) {
private fun JsonObjectStringBuilder.encodeOciLayerMetadata(layer: OciLayerMetadata) {
layer.descriptor?.let { descriptor ->
addObject("descriptor") { encodeLayerDescriptor(descriptor) }
addObject("descriptor") { encodeOciLayerDescriptor(descriptor) }
}
addStringIfNotNull("creationTime", layer.creationTime?.toString())
addStringIfNotNull("author", layer.author)
addStringIfNotNull("createdBy", layer.createdBy)
addStringIfNotNull("comment", layer.comment)
}

private fun JsonObjectStringBuilder.encodeLayerDescriptor(descriptor: OciMetadata.Layer.Descriptor) {
private fun JsonObjectStringBuilder.encodeOciLayerDescriptor(descriptor: OciLayerDescriptor) {
if (descriptor.mediaType != GZIP_COMPRESSED_LAYER_MEDIA_TYPE) {
addString("mediaType", descriptor.mediaType)
}
Expand Down Expand Up @@ -63,18 +63,18 @@ private fun JsonObject.decodeOciMetadata() = OciMetadata(
getStringMapOrEmpty("manifestAnnotations"),
getStringMapOrEmpty("manifestDescriptorAnnotations"),
getStringMapOrEmpty("indexAnnotations"),
getOrNull("layers") { asArray().toList { asObject().decodeLayer() } } ?: emptyList(),
getOrNull("layers") { asArray().toList { asObject().decodeOciLayerMetadata() } } ?: emptyList(),
)

private fun JsonObject.decodeLayer() = OciMetadata.Layer(
getOrNull("descriptor") { asObject().decodeLayerDescriptor() },
private fun JsonObject.decodeOciLayerMetadata() = OciLayerMetadata(
getOrNull("descriptor") { asObject().decodeOciLayerDescriptor() },
getInstantOrNull("creationTime"),
getStringOrNull("author"),
getStringOrNull("createdBy"),
getStringOrNull("comment"),
)

private fun JsonObject.decodeLayerDescriptor() = OciMetadata.Layer.Descriptor(
private fun JsonObject.decodeOciLayerDescriptor() = OciLayerDescriptor(
getStringOrNull("metadata") ?: GZIP_COMPRESSED_LAYER_MEDIA_TYPE,
getOciDigest("digest"),
getLong("size"),
Expand Down

0 comments on commit 18fc8b2

Please sign in to comment.