Skip to content

Commit

Permalink
Remove OciMetadata.Command
Browse files Browse the repository at this point in the history
allows null arguments for non-null entryPoint in OciMetadata
defer defaulting arguments to empty list for non-null entryPoint to createConfig
  • Loading branch information
SgtSilvio committed Jul 9, 2024
1 parent b903f15 commit 70bcc93
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ data class OciMetadata(
val user: String?,
val ports: SortedSet<String>,
val environment: SortedMap<String, String>,
val command: Command?,
val entryPoint: List<String>?, // empty (no args) is different from null (not set, inherit)
val arguments: List<String>?, // empty (no args) is different from null (not set, inherit)
val volumes: SortedSet<String>,
val workingDirectory: String?,
val stopSignal: String?,
Expand All @@ -29,11 +30,6 @@ data class OciMetadata(
val layers: List<Layer>,
) : Serializable {

data class Command(
val entryPoint: List<String>?, // empty (no args) is different from null (not set, inherit)
val arguments: List<String>, // default empty
) : Serializable

data class Layer(
val descriptor: Descriptor?,
val creationTime: Instant?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ internal class OciMetadataBuilder : Serializable {
private var user: String? = null
private var ports: Set<String> = emptySet()
private var environment: Map<String, String> = emptyMap()
private var command: OciMetadata.Command? = null
private var entryPoint: List<String>? = null
private var arguments: List<String>? = null
private var volumes: Set<String> = emptySet()
private var workingDirectory: String? = null
private var stopSignal: String? = null
Expand All @@ -32,7 +33,8 @@ internal class OciMetadataBuilder : Serializable {
fun user(v: String?) = apply { user = v }
fun ports(v: Set<String>) = apply { ports = v }
fun environment(v: Map<String, String>) = apply { environment = v }
fun command(v: OciMetadata.Command?) = apply { command = v }
fun entryPoint(v: List<String>?) = apply { entryPoint = v }
fun arguments(v: List<String>?) = apply { arguments = v }
fun volumes(v: Set<String>) = apply { volumes = v }
fun workingDirectory(v: String?) = apply { workingDirectory = v }
fun stopSignal(v: String?) = apply { stopSignal = v }
Expand All @@ -50,7 +52,8 @@ internal class OciMetadataBuilder : Serializable {
user,
ports.toSortedSet(),
environment.toSortedMap(),
command,
entryPoint,
arguments,
volumes.toSortedSet(),
workingDirectory,
stopSignal,
Expand All @@ -63,19 +66,6 @@ internal class OciMetadataBuilder : Serializable {
)
}

internal class OciMetadataCommandBuilder : Serializable {
private var entryPoint: List<String>? = null
private var arguments: List<String>? = null

fun entryPoint(v: List<String>?) = apply { entryPoint = v }
fun arguments(v: List<String>?) = apply { arguments = v }

fun build() = when {
(entryPoint == null) && (arguments == null) -> null
else -> OciMetadata.Command(entryPoint, arguments ?: emptyList())
}
}

internal class OciMetadataLayerBuilder : Serializable {
private var descriptor: OciMetadata.Layer.Descriptor? = null
private var creationTime: SerializableInstant? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ private fun JsonObjectStringBuilder.encodeOciMetadata(metadata: OciMetadata) {
addStringIfNotNull("user", metadata.user)
addArrayIfNotEmpty("ports", metadata.ports)
addObjectIfNotEmpty("environment", metadata.environment)
metadata.command?.let { command ->
addObject("command") { encodeCommand(command) }
}
addArrayIfNotNull("entryPoint", metadata.entryPoint)
addArrayIfNotNull("arguments", metadata.arguments)
addArrayIfNotEmpty("volumes", metadata.volumes)
addStringIfNotNull("workingDirectory", metadata.workingDirectory)
addStringIfNotNull("stopSignal", metadata.stopSignal)
Expand All @@ -28,11 +27,6 @@ private fun JsonObjectStringBuilder.encodeOciMetadata(metadata: OciMetadata) {
addArrayIfNotEmpty("layers", metadata.layers) { addObject { encodeLayer(it) } }
}

private fun JsonObjectStringBuilder.encodeCommand(command: OciMetadata.Command) {
addArrayIfNotNull("entryPoint", command.entryPoint)
addArray("arguments", command.arguments)
}

private fun JsonObjectStringBuilder.encodeLayer(layer: OciMetadata.Layer) {
layer.descriptor?.let { descriptor ->
addObject("descriptor") { encodeLayerDescriptor(descriptor) }
Expand Down Expand Up @@ -62,7 +56,8 @@ private fun JsonObject.decodeOciMetadata() = OciMetadata(
getStringOrNull("user"),
getStringSetOrEmpty("ports"),
getStringMapOrEmpty("environment"),
getOrNull("command") { asObject().decodeCommand() },
getStringListOrNull("entryPoint"),
getStringListOrNull("arguments"),
getStringSetOrEmpty("volumes"),
getStringOrNull("workingDirectory"),
getStringOrNull("stopSignal"),
Expand All @@ -74,11 +69,6 @@ private fun JsonObject.decodeOciMetadata() = OciMetadata(
getOrNull("layers") { asArray().toList { asObject().decodeLayer() } } ?: emptyList(),
)

private fun JsonObject.decodeCommand() = OciMetadata.Command(
getStringListOrNull("entryPoint"),
getStringList("arguments"),
)

private fun JsonObject.decodeLayer() = OciMetadata.Layer(
getOrNull("descriptor") { asObject().decodeLayerDescriptor() },
getInstantOrNull("creationTime"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,8 @@ internal abstract class OciImageDefinitionImpl @Inject constructor(
.zipAbsentAsNull(config.user, OciMetadataBuilder::user)
.zip(config.ports.orElse(emptySet()), OciMetadataBuilder::ports)
.zip(config.environment.orElse(emptyMap()), OciMetadataBuilder::environment)
.zipAbsentAsNull(createMetadataCommand(providerFactory), OciMetadataBuilder::command)
.zipAbsentAsNull(config.entryPoint, OciMetadataBuilder::entryPoint)
.zipAbsentAsNull(config.arguments, OciMetadataBuilder::arguments)
.zip(config.volumes.orElse(emptySet()), OciMetadataBuilder::volumes)
.zipAbsentAsNull(config.workingDirectory, OciMetadataBuilder::workingDirectory)
.zipAbsentAsNull(config.stopSignal, OciMetadataBuilder::stopSignal)
Expand All @@ -298,12 +299,6 @@ internal abstract class OciImageDefinitionImpl @Inject constructor(
.zip(createMetadataLayers(providerFactory), OciMetadataBuilder::layers)
.map { it.build() }

private fun createMetadataCommand(providerFactory: ProviderFactory): Provider<OciMetadata.Command> =
providerFactory.provider { OciMetadataCommandBuilder() }
.zipAbsentAsNull(config.entryPoint, OciMetadataCommandBuilder::entryPoint)
.zipAbsentAsNull(config.arguments, OciMetadataCommandBuilder::arguments)
.map { it.build() }

private fun createMetadataLayers(providerFactory: ProviderFactory): Provider<List<OciMetadata.Layer>> =
providerFactory.provider {
var listProvider = providerFactory.provider { listOf<OciMetadata.Layer>() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@ internal class OciMetadataRegistry(val registryApi: OciRegistryApi) {
// sorted for canonical json: architecture, author, config, created, history, os, os.features, os.version, rootfs, variant
val architecture = configJsonObject.getString("architecture")
val author = configJsonObject.getStringOrNull("author")
var command: OciMetadata.Command? = null
var arguments: List<String>? = null
var entryPoint: List<String>? = null
var environment: SortedMap<String, String> = TreeMap()
var ports: SortedSet<String> = TreeSet()
var configAnnotations: SortedMap<String, String> = TreeMap()
Expand All @@ -206,9 +207,8 @@ internal class OciMetadataRegistry(val registryApi: OciRegistryApi) {
configJsonObject.getOrNull("config") {
asObject().run {
// sorted for canonical json: Cmd, Entrypoint, Env, ExposedPorts, Labels, StopSignal, User, Volumes, WorkingDir
val arguments = getStringListOrNull("Cmd")
val entryPoint = getStringListOrNull("Entrypoint")
command = if ((entryPoint == null) && (arguments == null)) null else OciMetadata.Command(entryPoint, arguments ?: emptyList())
arguments = getStringListOrNull("Cmd")
entryPoint = getStringListOrNull("Entrypoint")
environment = getOrNull("Env") {
asArray().toMap(TreeMap()) {
val environmentString = asString()
Expand Down Expand Up @@ -292,7 +292,8 @@ internal class OciMetadataRegistry(val registryApi: OciRegistryApi) {
user,
ports,
environment,
command,
entryPoint,
arguments,
volumes,
workingDirectory,
stopSignal,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ internal fun createConfig(platform: Platform, variants: List<OciVariant>): OciDa
metadata.user?.let { user = it }
ports.addAll(metadata.ports)
environment += metadata.environment
metadata.command?.let { command ->
command.entryPoint?.let { entryPoint = it }
arguments = command.arguments
metadata.entryPoint?.let {
entryPoint = it
arguments = emptyList()
}
metadata.arguments?.let { arguments = it }
volumes.addAll(metadata.volumes)
metadata.workingDirectory?.let { workingDirectory = it }
metadata.stopSignal?.let { stopSignal = it }
Expand Down

0 comments on commit 70bcc93

Please sign in to comment.