Skip to content

Commit

Permalink
added debug logging
Browse files Browse the repository at this point in the history
  • Loading branch information
cmdjulian committed Jun 7, 2023
1 parent 1a472c0 commit 004a01e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 23 deletions.
3 changes: 3 additions & 0 deletions kirc-suspending/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ dependencies {
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jdk8")
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310")

// logging
implementation("io.github.microutils:kotlin-logging-jvm:3.0.5")

// insecure connections
implementation("io.github.hakky54:sslcontext-kickstart:8.0.0")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,44 +29,62 @@ import de.cmdjulian.kirc.spec.manifest.DockerManifestV2
import de.cmdjulian.kirc.spec.manifest.Manifest
import de.cmdjulian.kirc.spec.manifest.ManifestSingle
import de.cmdjulian.kirc.spec.manifest.OciManifestV1
import mu.KotlinLogging

internal class SuspendingContainerImageRegistryClientImpl(private val api: ContainerRegistryApi) :
SuspendingContainerImageRegistryClient {

private val logger = KotlinLogging.logger { }

override suspend fun testConnection() {
api.ping().onError { throw it.toRegistryClientError() }
api.ping().onError {
logger.trace(it) { "error on ping" }
throw it.toRegistryClientError()
}
}

override suspend fun repositories(limit: Int?, last: Int?): List<Repository> =
api.repositories(limit, last)
.map(Catalog::repositories)
.onError { logger.trace(it) { "error on repositories retrieve" } }
.getOrElse { throw it.toRegistryClientError() }

override suspend fun tags(repository: Repository, limit: Int?, last: Int?): List<Tag> =
api.tags(repository, limit, last)
.map(TagList::tags)
.onError { logger.trace(it) { "error on tags retrieve" } }
.getOrElse { throw it.toRegistryClientError() }

override suspend fun exists(repository: Repository, reference: Reference): Boolean =
api.digest(repository, reference)
.map { true }
.onError { logger.trace(it) { "error on exists" } }
.getOrElse { if (it.response.statusCode == 404) false else throw it.toRegistryClientError() }

override suspend fun manifest(repository: Repository, reference: Reference): Manifest =
api.manifests(repository, reference).getOrElse { throw it.toRegistryClientError() }
api.manifests(repository, reference)
.onError { logger.trace(it) { "error on manifest retrieve" } }
.getOrElse { throw it.toRegistryClientError() }

override suspend fun manifestDelete(repository: Repository, reference: Reference): Digest =
api.deleteManifest(repository, reference).getOrElse { throw it.toRegistryClientError() }
api.deleteManifest(repository, reference)
.onError { logger.trace(it) { "error on delete manifest" } }
.getOrElse { throw it.toRegistryClientError() }

override suspend fun manifestDigest(repository: Repository, reference: Reference): Digest =
api.digest(repository, reference).getOrElse { throw it.toRegistryClientError() }
api.digest(repository, reference)
.onError { logger.trace(it) { "error on manifest digest retrieve" } }
.getOrElse { throw it.toRegistryClientError() }

override suspend fun blob(repository: Repository, digest: Digest): ByteArray =
api.blob(repository, digest).getOrElse { throw it.toRegistryClientError() }
api.blob(repository, digest)
.onError { logger.trace(it) { "error on blob retrieve" } }
.getOrElse { throw it.toRegistryClientError() }

override suspend fun config(repository: Repository, reference: Reference): ImageConfig =
api.manifest(repository, reference)
.map { config(repository, it) }
.onError { logger.trace(it) { "error on image config retrieve" } }
.getOrElse { throw it.toRegistryClientError() }

override suspend fun config(repository: Repository, manifest: ManifestSingle): ImageConfig =
Expand All @@ -77,6 +95,7 @@ internal class SuspendingContainerImageRegistryClientImpl(private val api: Conta
is OciManifestV1 -> JsonMapper.readValue<OciImageConfigV1>(config)
}
}
.onError { logger.trace(it) { "error on image config retrieve" } }
.getOrElse { throw it.toRegistryClientError() }

override suspend fun toImageClient(repository: Repository, reference: Reference): SuspendingContainerImageClient =
Expand All @@ -92,23 +111,20 @@ internal class SuspendingContainerImageRegistryClientImpl(private val api: Conta
)
}

override fun toImageClient(
repository: Repository,
reference: Reference,
manifest: ManifestSingle,
) = when (reference) {
is Tag -> SuspendingContainerImageClientImpl(
this,
ContainerImageName(repository = repository, tag = reference),
manifest,
)

is Digest -> SuspendingContainerImageClientImpl(
this,
ContainerImageName(repository = repository, digest = reference),
manifest,
)
}
override fun toImageClient(repository: Repository, reference: Reference, manifest: ManifestSingle) =
when (reference) {
is Tag -> SuspendingContainerImageClientImpl(
this,
ContainerImageName(repository = repository, tag = reference),
manifest,
)

is Digest -> SuspendingContainerImageClientImpl(
this,
ContainerImageName(repository = repository, digest = reference),
manifest,
)
}
}

private fun FuelError.toRegistryClientError(): RegistryClientException = when (response.statusCode) {
Expand All @@ -118,7 +134,6 @@ private fun FuelError.toRegistryClientError(): RegistryClientException = when (r
404 -> NotFoundException(tryOrNull { JsonMapper.readValue(response.data) }, this)
405 -> MethodNotAllowed(tryOrNull { JsonMapper.readValue(response.data) }, this)
in 406..499 -> UnexpectedErrorException(tryOrNull { JsonMapper.readValue(response.data) }, this)

else -> UnknownErrorException(this)
}

Expand Down

0 comments on commit 004a01e

Please sign in to comment.