Skip to content

Commit

Permalink
add: override artifactType
Browse files Browse the repository at this point in the history
Signed-off-by: Adrian Wobito <[email protected]>
  • Loading branch information
wobito committed Jul 31, 2024
1 parent d41f36e commit f526749
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 5 deletions.
16 changes: 11 additions & 5 deletions pkg/mutate/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ import (
)

type image struct {
base v1.Image
overrides []v1.Layer
history *v1.History
configFileOverride any
configTypeOverride types.MediaType
base v1.Image
overrides []v1.Layer
history *v1.History
configFileOverride any
configTypeOverride types.MediaType
artifactTypeOverride string

computed bool
diffIDs []v1.Hash
Expand Down Expand Up @@ -86,6 +87,7 @@ func (img *image) populate() error {

configFile := img.configFileOverride
configType := img.configTypeOverride
artifactType := img.artifactTypeOverride

// If configFile is not overridden, populate from the base image.
if configFile == nil {
Expand Down Expand Up @@ -151,6 +153,10 @@ func (img *image) populate() error {
manifest.Config.Data = config
}

if artifactType != "" && configType == "application/vnd.oci.empty.v1+json" {
manifest.Config.ArtifactType = artifactType
}

img.computed = true
img.diffIDs = diffIDs
img.byDiffID = byDiffID
Expand Down
8 changes: 8 additions & 0 deletions pkg/mutate/mutate.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ func SetConfig(configFile any, configType types.MediaType) Mutation {
}
}

// SetArtifactType replaces the artifact type with the specified value.
func SetArtifactType(artifactType string) Mutation {
return func(img *image) error {
img.artifactTypeOverride = artifactType
return nil
}
}

// Apply performs the specified mutation(s) to a base image, returning the resulting image.
func Apply(base v1.Image, ms ...Mutation) (v1.Image, error) {
if len(ms) == 0 {
Expand Down
22 changes: 22 additions & 0 deletions pkg/mutate/mutate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,28 @@ func TestApply(t *testing.T) {
SetConfig(&v1.ConfigFile{Author: "Author"}, types.DockerConfigJSON),
},
},
{
name: "SetConfigEmpty",
base: img,
ms: []Mutation{
SetConfig(struct{}{}, "application/vnd.oci.empty.v1+json"),
},
},
{
name: "SetArtifactType",
base: img,
ms: []Mutation{
SetArtifactType("application/vnd.sylabs.container"),
},
},
{
name: "SetArtifactTypeWithEmptyConfig",
base: img,
ms: []Mutation{
SetConfig(struct{}{}, "application/vnd.oci.empty.v1+json"),
SetArtifactType("application/vnd.sylabs.container"),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"architecture":"arm64","container":"b2af51419cbf516f3c99b877a64906b21afedc175bd3cd082eb5798e2f277bb4","created":"2022-03-19T16:12:58.923371954Z","docker_version":"20.10.12","history":[{"created":"2022-03-19T16:12:58.834095198Z","created_by":"/bin/sh -c #(nop) COPY file:a79dd5bda1e77203401956a93401d3aef45221fc750295a4291896f3386f4f54 in / "},{"created":"2022-03-19T16:12:58.923371954Z","created_by":"/bin/sh -c #(nop) CMD [\"/hello\"]","empty_layer":true}],"os":"linux","rootfs":{"type":"layers","diff_ids":["sha256:efb53921da3394806160641b72a2cbd34ca1a9a8345ac670a85a04ad3d0e3507"]},"config":{"Cmd":["/hello"],"Env":["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"],"Image":"sha256:cc0fff24c4ece63ade5d9f549e42c926cf569112c4f5c439a4a57f3f33f5588b"},"variant":"v8"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"schemaVersion":2,"mediaType":"application/vnd.docker.distribution.manifest.v2+json","config":{"mediaType":"application/vnd.docker.container.image.v1+json","size":788,"digest":"sha256:93ad81f8071afb1a00ef481a6034c0bf59a18bc2e1fba8bceceecb0acf2bddc3"},"layers":[{"mediaType":"application/vnd.docker.image.rootfs.diff.tar.gzip","size":3208,"digest":"sha256:7050e35b49f5e348c4809f5eff915842962cb813f32062d3bbdd35c750dd7d01"}]}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"schemaVersion":2,"mediaType":"application/vnd.docker.distribution.manifest.v2+json","config":{"mediaType":"application/vnd.oci.empty.v1+json","size":2,"digest":"sha256:44136fa355b3678a1146ad16f7e8649e94fb4fc21fe77e8310c060f61caaff8a","artifactType":"application/vnd.sylabs.container"},"layers":[{"mediaType":"application/vnd.docker.image.rootfs.diff.tar.gzip","size":3208,"digest":"sha256:7050e35b49f5e348c4809f5eff915842962cb813f32062d3bbdd35c750dd7d01"}]}
1 change: 1 addition & 0 deletions pkg/mutate/testdata/TestApply/SetConfigEmpty/config.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"schemaVersion":2,"mediaType":"application/vnd.docker.distribution.manifest.v2+json","config":{"mediaType":"application/vnd.oci.empty.v1+json","size":2,"digest":"sha256:44136fa355b3678a1146ad16f7e8649e94fb4fc21fe77e8310c060f61caaff8a"},"layers":[{"mediaType":"application/vnd.docker.image.rootfs.diff.tar.gzip","size":3208,"digest":"sha256:7050e35b49f5e348c4809f5eff915842962cb813f32062d3bbdd35c750dd7d01"}]}

0 comments on commit f526749

Please sign in to comment.