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 0e60a7e
Show file tree
Hide file tree
Showing 7 changed files with 38 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
configArtifactTypeOverride 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
configArtifactType := img.configArtifactTypeOverride

// 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 configArtifactType != "" && configType == "application/vnd.oci.empty.v1+json" {
manifest.Config.ArtifactType = configArtifactType
}

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 {
}
}

// SetConfigArtifactType replaces the artifact type with the specified value.
func SetConfigArtifactType(artifactType string) Mutation {
return func(img *image) error {
img.configArtifactTypeOverride = 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
15 changes: 15 additions & 0 deletions pkg/mutate/mutate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,21 @@ 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: "SetConfigArtifactType",
base: img,
ms: []Mutation{
SetConfig(struct{}{}, "application/vnd.oci.empty.v1+json"),
SetConfigArtifactType("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 @@
{}
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 0e60a7e

Please sign in to comment.