Skip to content

Commit a3e1320

Browse files
Merge pull request #25909 from baude/issue25884
Add ability to set layer media type for artifacts
2 parents 9180f55 + fdfed99 commit a3e1320

File tree

7 files changed

+42
-7
lines changed

7 files changed

+42
-7
lines changed

cmd/podman/artifact/add.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,18 @@ var (
1919
RunE: add,
2020
Args: cobra.MinimumNArgs(2),
2121
ValidArgsFunction: common.AutocompleteArtifactAdd,
22-
Example: `podman artifact add quay.io/myimage/myartifact:latest /tmp/foobar.txt`,
23-
Annotations: map[string]string{registry.EngineMode: registry.ABIMode},
22+
Example: `podman artifact add quay.io/myimage/myartifact:latest /tmp/foobar.txt
23+
podman artifact add --file-type text/yaml quay.io/myimage/myartifact:latest /tmp/foobar.yaml
24+
podman artifact add --append quay.io/myimage/myartifact:latest /tmp/foobar.tar.gz`,
25+
Annotations: map[string]string{registry.EngineMode: registry.ABIMode},
2426
}
2527
)
2628

2729
type artifactAddOptions struct {
2830
ArtifactType string
2931
Annotations []string
3032
Append bool
33+
FileType string
3134
}
3235

3336
var (
@@ -51,6 +54,10 @@ func init() {
5154

5255
appendFlagName := "append"
5356
flags.BoolVarP(&addOpts.Append, appendFlagName, "a", false, "Append files to an existing artifact")
57+
58+
fileTypeFlagName := "file-type"
59+
flags.StringVarP(&addOpts.FileType, fileTypeFlagName, "", "", "Set file type to use for the artifact (layer)")
60+
_ = addCmd.RegisterFlagCompletionFunc(fileTypeFlagName, completion.AutocompleteNone)
5461
}
5562

5663
func add(cmd *cobra.Command, args []string) error {
@@ -63,6 +70,7 @@ func add(cmd *cobra.Command, args []string) error {
6370
opts.Annotations = annots
6471
opts.ArtifactType = addOpts.ArtifactType
6572
opts.Append = addOpts.Append
73+
opts.FileType = addOpts.FileType
6674

6775
report, err := registry.ImageEngine().ArtifactAdd(registry.Context(), args[0], args[1:], opts)
6876
if err != nil {

docs/source/markdown/podman-artifact-add.1.md.in

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ Note: Set annotations for each file being added.
2727

2828
Append files to an existing artifact. This option cannot be used with the **--type** option.
2929

30+
#### **--file-type**
31+
32+
Set the media type of the artifact file instead of allowing detection to determine the type
33+
3034
#### **--help**
3135

3236
Print usage statement.
@@ -55,6 +59,16 @@ Set an annotation for an artifact
5559
$ podman artifact add --annotation date=2025-01-30 quay.io/myartifact/myml:latest /tmp/foobar1.ml
5660
```
5761

62+
Append a file to an existing artifact
63+
```
64+
$ podman artifact add --append quay.io/myartifact/tarballs:latest /tmp/foobar.tar.gz
65+
```
66+
67+
Override the media type of the artifact being added
68+
```
69+
$ podman artifact add --file-type text/yaml quay.io/myartifact/descriptors:latest /tmp/info.yaml
70+
```
71+
5872

5973
## SEE ALSO
6074
**[podman(1)](podman.1.md)**, **[podman-artifact(1)](podman-artifact.1.md)**

pkg/domain/entities/artifact.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type ArtifactAddOptions struct {
1313
Annotations map[string]string
1414
ArtifactType string
1515
Append bool
16+
FileType string
1617
}
1718

1819
type ArtifactExtractOptions struct {

pkg/domain/infra/abi/artifact.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ func (ir *ImageEngine) ArtifactAdd(ctx context.Context, name string, paths []str
192192
Annotations: opts.Annotations,
193193
ArtifactType: opts.ArtifactType,
194194
Append: opts.Append,
195+
FileType: opts.FileType,
195196
}
196197

197198
artifactDigest, err := artStore.Add(ctx, name, paths, &addOptions)

pkg/libartifact/store/store.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -251,20 +251,26 @@ func (as ArtifactStore) Add(ctx context.Context, dest string, paths []string, op
251251
// ImageDestination, in general, requires the caller to write a full image; here we may write only the added layers.
252252
// This works for the oci/layout transport we hard-code.
253253
for _, path := range paths {
254+
mediaType := options.FileType
254255
// get the new artifact into the local store
255256
newBlobDigest, newBlobSize, err := layout.PutBlobFromLocalFile(ctx, imageDest, path)
256257
if err != nil {
257258
return nil, err
258259
}
259-
detectedType, err := determineManifestType(path)
260-
if err != nil {
261-
return nil, err
260+
261+
// If we did not receive an override for the layer's mediatype, use
262+
// detection to determine it.
263+
if len(mediaType) < 1 {
264+
mediaType, err = determineManifestType(path)
265+
if err != nil {
266+
return nil, err
267+
}
262268
}
263269

264270
annotations := maps.Clone(options.Annotations)
265271
annotations[specV1.AnnotationTitle] = filepath.Base(path)
266272
newLayer := specV1.Descriptor{
267-
MediaType: detectedType,
273+
MediaType: mediaType,
268274
Digest: newBlobDigest,
269275
Size: newBlobSize,
270276
Annotations: annotations,

pkg/libartifact/types/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ type AddOptions struct {
1010
ArtifactType string `json:",omitempty"`
1111
// append option is not compatible with ArtifactType option
1212
Append bool `json:",omitempty"`
13+
// FileType describes the media type for the layer. It is an override
14+
// for the standard detection
15+
FileType string `json:",omitempty"`
1316
}
1417

1518
// FilterBlobOptions options used to filter for a single blob in an artifact

test/e2e/artifact_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ var _ = Describe("Podman artifact", func() {
8888
})
8989

9090
It("podman artifact add with options", func() {
91+
yamlType := "text/yaml"
9192
artifact1Name := "localhost/test/artifact1"
9293
artifact1File, err := createArtifactFile(1024)
9394
Expect(err).ToNot(HaveOccurred())
@@ -96,13 +97,14 @@ var _ = Describe("Podman artifact", func() {
9697
annotation1 := "color=blue"
9798
annotation2 := "flavor=lemon"
9899

99-
podmanTest.PodmanExitCleanly("artifact", "add", "--type", artifactType, "--annotation", annotation1, "--annotation", annotation2, artifact1Name, artifact1File)
100+
podmanTest.PodmanExitCleanly("artifact", "add", "--file-type", yamlType, "--type", artifactType, "--annotation", annotation1, "--annotation", annotation2, artifact1Name, artifact1File)
100101

101102
a := podmanTest.InspectArtifact(artifact1Name)
102103
Expect(a.Name).To(Equal(artifact1Name))
103104
Expect(a.Manifest.ArtifactType).To(Equal(artifactType))
104105
Expect(a.Manifest.Layers[0].Annotations["color"]).To(Equal("blue"))
105106
Expect(a.Manifest.Layers[0].Annotations["flavor"]).To(Equal("lemon"))
107+
Expect(a.Manifest.Layers[0].MediaType).To(Equal(yamlType))
106108

107109
failSession := podmanTest.Podman([]string{"artifact", "add", "--annotation", "org.opencontainers.image.title=foobar", "foobar", artifact1File})
108110
failSession.WaitWithDefaultTimeout()

0 commit comments

Comments
 (0)