Skip to content

Commit

Permalink
Use URI instead of String for registryUrl in OciRegistryApi
Browse files Browse the repository at this point in the history
  • Loading branch information
SgtSilvio committed Jul 16, 2024
1 parent eebbb40 commit c82b300
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 80 deletions.
4 changes: 2 additions & 2 deletions src/main/kotlin/io/github/sgtsilvio/gradle/oci/OciPushTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ internal abstract class OciPushService : BuildService<BuildServiceParameters.Non
val progressPrefix = "Pushing $imageName > blob $digest"
progressLogger.start("pushing blob", progressPrefix)
registryApi.pushBlobIfNotPresent(
context.registryUrl.toString(),
context.registryUrl,
imageName,
digest,
size,
Expand Down Expand Up @@ -276,7 +276,7 @@ internal abstract class OciPushService : BuildService<BuildServiceParameters.Non
val progressLogger = context.progressLoggerFactory.newOperation(OciPushService::class.java)
progressLogger.start("pushing manifest", "Pushing $imageName > manifest $reference ($mediaType)")
registryApi.pushManifest(
context.registryUrl.toString(),
context.registryUrl,
imageName,
reference,
mediaType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import io.github.sgtsilvio.gradle.oci.metadata.*
import io.github.sgtsilvio.gradle.oci.platform.Platform
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono
import java.net.URI
import java.time.Instant
import java.util.*

Expand All @@ -14,39 +15,46 @@ import java.util.*
internal class OciImageMetadataRegistry(val registryApi: OciRegistryApi) {

fun pullMultiPlatformImageMetadata(
registry: String,
registryUrl: URI,
imageReference: OciImageReference,
credentials: Credentials?,
): Mono<OciMultiPlatformImageMetadata> =
registryApi.pullManifest(registry, imageReference.name, imageReference.tag.replaceFirst('!', ':'), credentials)
.transformToMultiPlatformImageMetadata(registry, imageReference, credentials)
): Mono<OciMultiPlatformImageMetadata> = registryApi.pullManifest(
registryUrl,
imageReference.name,
imageReference.tag.replaceFirst('!', ':'),
credentials,
).transformToMultiPlatformImageMetadata(registryUrl, imageReference, credentials)

fun pullMultiPlatformImageMetadata(
registry: String,
registryUrl: URI,
imageReference: OciImageReference,
digest: OciDigest,
size: Int,
credentials: Credentials?,
): Mono<OciMultiPlatformImageMetadata> =
registryApi.pullManifest(registry, imageReference.name, digest, size, credentials)
.transformToMultiPlatformImageMetadata(registry, imageReference, credentials)
): Mono<OciMultiPlatformImageMetadata> = registryApi.pullManifest(
registryUrl,
imageReference.name,
digest,
size,
credentials,
).transformToMultiPlatformImageMetadata(registryUrl, imageReference, credentials)

private fun Mono<OciData>.transformToMultiPlatformImageMetadata(
registry: String,
registryUrl: URI,
imageReference: OciImageReference,
credentials: Credentials?,
): Mono<OciMultiPlatformImageMetadata> = flatMap { manifest ->
transformToMultiPlatformImageMetadata(registry, imageReference, manifest, credentials)
transformToMultiPlatformImageMetadata(registryUrl, imageReference, manifest, credentials)
}

private fun transformToMultiPlatformImageMetadata(
registry: String,
registryUrl: URI,
imageReference: OciImageReference,
manifest: OciData,
credentials: Credentials?,
): Mono<OciMultiPlatformImageMetadata> = when (manifest.mediaType) {
INDEX_MEDIA_TYPE -> transformIndexToMultiPlatformImageMetadata(
registry,
registryUrl,
imageReference,
manifest,
credentials,
Expand All @@ -56,7 +64,7 @@ internal class OciImageMetadataRegistry(val registryApi: OciRegistryApi) {
)

MANIFEST_MEDIA_TYPE -> transformManifestToMultiPlatformImageMetadata(
registry,
registryUrl,
imageReference,
manifest,
credentials,
Expand All @@ -65,7 +73,7 @@ internal class OciImageMetadataRegistry(val registryApi: OciRegistryApi) {
)

DOCKER_MANIFEST_LIST_MEDIA_TYPE -> transformIndexToMultiPlatformImageMetadata(
registry,
registryUrl,
imageReference,
manifest,
credentials,
Expand All @@ -75,7 +83,7 @@ internal class OciImageMetadataRegistry(val registryApi: OciRegistryApi) {
)

DOCKER_MANIFEST_MEDIA_TYPE -> transformManifestToMultiPlatformImageMetadata(
registry,
registryUrl,
imageReference,
manifest,
credentials,
Expand All @@ -87,7 +95,7 @@ internal class OciImageMetadataRegistry(val registryApi: OciRegistryApi) {
}

private fun transformIndexToMultiPlatformImageMetadata(
registry: String,
registryUrl: URI,
imageReference: OciImageReference,
index: OciData,
credentials: Credentials?,
Expand All @@ -104,7 +112,7 @@ internal class OciImageMetadataRegistry(val registryApi: OciRegistryApi) {
Mono.empty()
} else {
registryApi.pullManifest(
registry,
registryUrl,
imageReference.name,
manifestDescriptor.digest,
manifestDescriptor.size.toInt(),
Expand All @@ -114,7 +122,7 @@ internal class OciImageMetadataRegistry(val registryApi: OciRegistryApi) {
throw IllegalStateException("media type in manifest descriptor ($manifestMediaType) and manifest (${manifest.mediaType}) do not match")
}
transformManifestToImageMetadata(
registry,
registryUrl,
imageReference,
manifest,
manifestDescriptor.annotations,
Expand Down Expand Up @@ -145,14 +153,14 @@ internal class OciImageMetadataRegistry(val registryApi: OciRegistryApi) {
}

private fun transformManifestToMultiPlatformImageMetadata(
registry: String,
registryUrl: URI,
imageReference: OciImageReference,
manifest: OciData,
credentials: Credentials?,
configMediaType: String,
layerMediaTypePrefix: String,
): Mono<OciMultiPlatformImageMetadata> = transformManifestToImageMetadata(
registry,
registryUrl,
imageReference,
manifest,
TreeMap(),
Expand All @@ -163,7 +171,7 @@ internal class OciImageMetadataRegistry(val registryApi: OciRegistryApi) {
).map { OciMultiPlatformImageMetadata(mapOf(it), manifest.digest, manifest.bytes.size) }

private fun transformManifestToImageMetadata(
registry: String,
registryUrl: URI,
imageReference: OciImageReference,
manifest: OciData,
manifestDescriptorAnnotations: SortedMap<String, String>,
Expand All @@ -183,7 +191,7 @@ internal class OciImageMetadataRegistry(val registryApi: OciRegistryApi) {
if ((configDescriptor.mediaType != configMediaType) || layerDescriptors.any { !it.mediaType.startsWith(layerMediaTypePrefix) }) {
return Mono.empty()
}
return registryApi.pullBlobAsString(registry, imageReference.name, configDescriptor.digest, configDescriptor.size, credentials).map { config ->
return registryApi.pullBlobAsString(registryUrl, imageReference.name, configDescriptor.digest, configDescriptor.size, credentials).map { config ->
val configJsonObject = jsonObject(config)
// sorted for canonical json: architecture, author, config, created, history, os, os.features, os.version, rootfs, variant
val architecture = configJsonObject.getString("architecture")
Expand Down
Loading

0 comments on commit c82b300

Please sign in to comment.