Skip to content

Commit edf3349

Browse files
committed
Add ability to set layer media type for artifacts
in #25884, it was pointed out that the standard detection used to determine the artifact's file type can be wrong. in those cases, it would be handy for the user to be able to override the media type of the layer. as such, added a new option called `--file-type`, which is optional, and allows users to do just that. `podman artifact add --file-type text/yaml quay.io/artifact/config:latest ./config.yaml ` Fixes: #25884 Signed-off-by: Brent Baude <[email protected]>
1 parent 51c4df1 commit edf3349

File tree

7 files changed

+41
-7
lines changed

7 files changed

+41
-7
lines changed

cmd/podman/artifact/add.go

+9-2
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,9 @@ 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)")
5460
}
5561

5662
func add(cmd *cobra.Command, args []string) error {
@@ -63,6 +69,7 @@ func add(cmd *cobra.Command, args []string) error {
6369
opts.Annotations = annots
6470
opts.ArtifactType = addOpts.ArtifactType
6571
opts.Append = addOpts.Append
72+
opts.FileType = addOpts.FileType
6673

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

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

+14
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

+1
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

+1
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

+10-4
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

+3
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

+3-1
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)