Merge pull request #25170 from baude/artifactoptions

Add type and annotations to artifact add
This commit is contained in:
openshift-merge-bot[bot]
2025-01-31 17:19:18 +00:00
committed by GitHub
13 changed files with 133 additions and 30 deletions

View File

@ -3,15 +3,17 @@ package artifact
import ( import (
"fmt" "fmt"
"github.com/containers/common/pkg/completion"
"github.com/containers/podman/v5/cmd/podman/common" "github.com/containers/podman/v5/cmd/podman/common"
"github.com/containers/podman/v5/cmd/podman/registry" "github.com/containers/podman/v5/cmd/podman/registry"
"github.com/containers/podman/v5/pkg/domain/entities" "github.com/containers/podman/v5/pkg/domain/entities"
"github.com/containers/podman/v5/pkg/domain/utils"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var ( var (
addCmd = &cobra.Command{ addCmd = &cobra.Command{
Use: "add ARTIFACT PATH [...PATH]", Use: "add [options] ARTIFACT PATH [...PATH]",
Short: "Add an OCI artifact to the local store", Short: "Add an OCI artifact to the local store",
Long: "Add an OCI artifact to the local store from the local filesystem", Long: "Add an OCI artifact to the local store from the local filesystem",
RunE: add, RunE: add,
@ -22,15 +24,41 @@ var (
} }
) )
type artifactAddOptions struct {
ArtifactType string
Annotations []string
}
var (
addOpts artifactAddOptions
)
func init() { func init() {
registry.Commands = append(registry.Commands, registry.CliCommand{ registry.Commands = append(registry.Commands, registry.CliCommand{
Command: addCmd, Command: addCmd,
Parent: artifactCmd, Parent: artifactCmd,
}) })
flags := addCmd.Flags()
annotationFlagName := "annotation"
flags.StringArrayVar(&addOpts.Annotations, annotationFlagName, nil, "set an `annotation` for the specified artifact")
_ = addCmd.RegisterFlagCompletionFunc(annotationFlagName, completion.AutocompleteNone)
addTypeFlagName := "type"
flags.StringVar(&addOpts.ArtifactType, addTypeFlagName, "", "Use type to describe an artifact")
_ = addCmd.RegisterFlagCompletionFunc(addTypeFlagName, completion.AutocompleteNone)
} }
func add(cmd *cobra.Command, args []string) error { func add(cmd *cobra.Command, args []string) error {
report, err := registry.ImageEngine().ArtifactAdd(registry.Context(), args[0], args[1:], entities.ArtifactAddoptions{}) opts := new(entities.ArtifactAddOptions)
annots, err := utils.ParseAnnotations(addOpts.Annotations)
if err != nil {
return err
}
opts.Annotations = annots
opts.ArtifactType = addOpts.ArtifactType
report, err := registry.ImageEngine().ArtifactAdd(registry.Context(), args[0], args[1:], opts)
if err != nil { if err != nil {
return err return err
} }

View File

@ -1,3 +1,4 @@
podman-artifact-add.1.md
podman-artifact-pull.1.md podman-artifact-pull.1.md
podman-artifact-push.1.md podman-artifact-push.1.md
podman-attach.1.md podman-attach.1.md

View File

@ -1,5 +1,5 @@
####> This option file is used in: ####> This option file is used in:
####> podman manifest add, manifest annotate ####> podman artifact add, manifest add, manifest annotate
####> If file is edited, make sure the changes ####> If file is edited, make sure the changes
####> are applicable to all of those. ####> are applicable to all of those.
#### **--annotation**=*annotation=value* #### **--annotation**=*annotation=value*

View File

@ -19,10 +19,15 @@ added.
## OPTIONS ## OPTIONS
@@option annotation.manifest
#### **--help** #### **--help**
Print usage statement. Print usage statement.
#### **--type**
Set a type for the artifact being added.
## EXAMPLES ## EXAMPLES
@ -39,6 +44,10 @@ $ podman artifact add quay.io/myartifact/myml:latest /tmp/foobar1.ml /tmp/foobar
1487acae11b5a30948c50762882036b41ac91a7b9514be8012d98015c95ddb78 1487acae11b5a30948c50762882036b41ac91a7b9514be8012d98015c95ddb78
``` ```
Set an annotation for an artifact
```
$ podman artifact add --annotation date=2025-01-30 quay.io/myartifact/myml:latest /tmp/foobar1.ml
```
## SEE ALSO ## SEE ALSO

View File

@ -29,6 +29,7 @@ import (
"github.com/containers/podman/v5/pkg/channel" "github.com/containers/podman/v5/pkg/channel"
"github.com/containers/podman/v5/pkg/domain/entities" "github.com/containers/podman/v5/pkg/domain/entities"
"github.com/containers/podman/v5/pkg/domain/infra/abi" "github.com/containers/podman/v5/pkg/domain/infra/abi"
domainUtils "github.com/containers/podman/v5/pkg/domain/utils"
"github.com/containers/podman/v5/pkg/errorhandling" "github.com/containers/podman/v5/pkg/errorhandling"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/gorilla/schema" "github.com/gorilla/schema"
@ -520,24 +521,17 @@ func ManifestModify(w http.ResponseWriter, r *http.Request) {
return return
} }
annotationsFromAnnotationSlice := func(annotation []string) map[string]string {
annotations := make(map[string]string)
for _, annotationSpec := range annotation {
key, val, hasVal := strings.Cut(annotationSpec, "=")
if !hasVal {
utils.Error(w, http.StatusBadRequest, fmt.Errorf("no value given for annotation %q", key))
return nil
}
annotations[key] = val
}
return annotations
}
if len(body.ManifestAddOptions.Annotation) != 0 { if len(body.ManifestAddOptions.Annotation) != 0 {
if len(body.ManifestAddOptions.Annotations) != 0 { if len(body.ManifestAddOptions.Annotations) != 0 {
utils.Error(w, http.StatusBadRequest, fmt.Errorf("can not set both Annotation and Annotations")) utils.Error(w, http.StatusBadRequest, fmt.Errorf("can not set both Annotation and Annotations"))
return return
} }
body.ManifestAddOptions.Annotations = annotationsFromAnnotationSlice(body.ManifestAddOptions.Annotation) annots, err := domainUtils.ParseAnnotations(body.ManifestAddOptions.Annotation)
if err != nil {
utils.Error(w, http.StatusBadRequest, err)
return
}
body.ManifestAddOptions.Annotations = annots
body.ManifestAddOptions.Annotation = nil body.ManifestAddOptions.Annotation = nil
} }
if len(body.ManifestAddOptions.IndexAnnotation) != 0 { if len(body.ManifestAddOptions.IndexAnnotation) != 0 {
@ -545,7 +539,12 @@ func ManifestModify(w http.ResponseWriter, r *http.Request) {
utils.Error(w, http.StatusBadRequest, fmt.Errorf("can not set both IndexAnnotation and IndexAnnotations")) utils.Error(w, http.StatusBadRequest, fmt.Errorf("can not set both IndexAnnotation and IndexAnnotations"))
return return
} }
body.ManifestAddOptions.IndexAnnotations = annotationsFromAnnotationSlice(body.ManifestAddOptions.IndexAnnotation) annots, err := domainUtils.ParseAnnotations(body.ManifestAddOptions.IndexAnnotation)
if err != nil {
utils.Error(w, http.StatusBadRequest, err)
return
}
body.ManifestAddOptions.IndexAnnotations = annots
body.ManifestAddOptions.IndexAnnotation = nil body.ManifestAddOptions.IndexAnnotation = nil
} }

View File

@ -9,7 +9,8 @@ import (
"github.com/opencontainers/go-digest" "github.com/opencontainers/go-digest"
) )
type ArtifactAddoptions struct { type ArtifactAddOptions struct {
Annotations map[string]string
ArtifactType string ArtifactType string
} }

View File

@ -9,7 +9,7 @@ import (
) )
type ImageEngine interface { //nolint:interfacebloat type ImageEngine interface { //nolint:interfacebloat
ArtifactAdd(ctx context.Context, name string, paths []string, opts ArtifactAddoptions) (*ArtifactAddReport, error) ArtifactAdd(ctx context.Context, name string, paths []string, opts *ArtifactAddOptions) (*ArtifactAddReport, error)
ArtifactInspect(ctx context.Context, name string, opts ArtifactInspectOptions) (*ArtifactInspectReport, error) ArtifactInspect(ctx context.Context, name string, opts ArtifactInspectOptions) (*ArtifactInspectReport, error)
ArtifactList(ctx context.Context, opts ArtifactListOptions) ([]*ArtifactListReport, error) ArtifactList(ctx context.Context, opts ArtifactListOptions) ([]*ArtifactListReport, error)
ArtifactPull(ctx context.Context, name string, opts ArtifactPullOptions) (*ArtifactPullReport, error) ArtifactPull(ctx context.Context, name string, opts ArtifactPullOptions) (*ArtifactPullReport, error)

View File

@ -11,6 +11,7 @@ import (
"github.com/containers/common/libimage" "github.com/containers/common/libimage"
"github.com/containers/podman/v5/pkg/domain/entities" "github.com/containers/podman/v5/pkg/domain/entities"
"github.com/containers/podman/v5/pkg/libartifact/store" "github.com/containers/podman/v5/pkg/libartifact/store"
"github.com/containers/podman/v5/pkg/libartifact/types"
) )
func getDefaultArtifactStore(ir *ImageEngine) string { func getDefaultArtifactStore(ir *ImageEngine) string {
@ -152,12 +153,18 @@ func (ir *ImageEngine) ArtifactPush(ctx context.Context, name string, opts entit
err = artStore.Push(ctx, name, name, copyOpts) err = artStore.Push(ctx, name, name, copyOpts)
return &entities.ArtifactPushReport{}, err return &entities.ArtifactPushReport{}, err
} }
func (ir *ImageEngine) ArtifactAdd(ctx context.Context, name string, paths []string, opts entities.ArtifactAddoptions) (*entities.ArtifactAddReport, error) { func (ir *ImageEngine) ArtifactAdd(ctx context.Context, name string, paths []string, opts *entities.ArtifactAddOptions) (*entities.ArtifactAddReport, error) {
artStore, err := store.NewArtifactStore(getDefaultArtifactStore(ir), ir.Libpod.SystemContext()) artStore, err := store.NewArtifactStore(getDefaultArtifactStore(ir), ir.Libpod.SystemContext())
if err != nil { if err != nil {
return nil, err return nil, err
} }
artifactDigest, err := artStore.Add(ctx, name, paths, opts.ArtifactType)
addOptions := types.AddOptions{
Annotations: opts.Annotations,
ArtifactType: opts.ArtifactType,
}
artifactDigest, err := artStore.Add(ctx, name, paths, &addOptions)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -9,7 +9,7 @@ import (
// TODO For now, no remote support has been added. We need the API to firm up first. // TODO For now, no remote support has been added. We need the API to firm up first.
func ArtifactAdd(ctx context.Context, path, name string, opts entities.ArtifactAddoptions) error { func ArtifactAdd(ctx context.Context, path, name string, opts entities.ArtifactAddOptions) error {
return fmt.Errorf("not implemented") return fmt.Errorf("not implemented")
} }
@ -33,6 +33,6 @@ func (ir *ImageEngine) ArtifactPush(ctx context.Context, name string, opts entit
return nil, fmt.Errorf("not implemented") return nil, fmt.Errorf("not implemented")
} }
func (ir *ImageEngine) ArtifactAdd(ctx context.Context, name string, paths []string, opts entities.ArtifactAddoptions) (*entities.ArtifactAddReport, error) { func (ir *ImageEngine) ArtifactAdd(ctx context.Context, name string, paths []string, opts *entities.ArtifactAddOptions) (*entities.ArtifactAddReport, error) {
return nil, fmt.Errorf("not implemented") return nil, fmt.Errorf("not implemented")
} }

View File

@ -1,6 +1,7 @@
package utils package utils
import ( import (
"fmt"
"net/url" "net/url"
"strings" "strings"
@ -39,3 +40,17 @@ func ToURLValues(f []string) (filters url.Values) {
} }
return return
} }
// ParseAnnotations takes a string slice of options, expected to be "key=val" and returns
// a string map where the map index is the key and points to the value
func ParseAnnotations(options []string) (map[string]string, error) {
annotations := make(map[string]string)
for _, annotationSpec := range options {
key, val, hasVal := strings.Cut(annotationSpec, "=")
if !hasVal {
return nil, fmt.Errorf("no value given for annotation %q", key)
}
annotations[key] = val
}
return annotations, nil
}

View File

@ -8,6 +8,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"maps"
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
@ -160,7 +161,8 @@ func (as ArtifactStore) Push(ctx context.Context, src, dest string, opts libimag
// Add takes one or more local files and adds them to the local artifact store. The empty // Add takes one or more local files and adds them to the local artifact store. The empty
// string input is for possible custom artifact types. // string input is for possible custom artifact types.
func (as ArtifactStore) Add(ctx context.Context, dest string, paths []string, _ string) (*digest.Digest, error) { func (as ArtifactStore) Add(ctx context.Context, dest string, paths []string, options *libartTypes.AddOptions) (*digest.Digest, error) {
annots := maps.Clone(options.Annotations)
if len(dest) == 0 { if len(dest) == 0 {
return nil, ErrEmptyArtifactName return nil, ErrEmptyArtifactName
} }
@ -191,6 +193,13 @@ func (as ArtifactStore) Add(ctx context.Context, dest string, paths []string, _
defer imageDest.Close() defer imageDest.Close()
for _, path := range paths { for _, path := range paths {
// currently we don't allow override of the filename ; if a user requirement emerges,
// we could seemingly accommodate but broadens possibilities of something bad happening
// for things like `artifact extract`
if _, hasTitle := options.Annotations[specV1.AnnotationTitle]; hasTitle {
return nil, fmt.Errorf("cannot override filename with %s annotation", specV1.AnnotationTitle)
}
// get the new artifact into the local store // get the new artifact into the local store
newBlobDigest, newBlobSize, err := layout.PutBlobFromLocalFile(ctx, imageDest, path) newBlobDigest, newBlobSize, err := layout.PutBlobFromLocalFile(ctx, imageDest, path)
if err != nil { if err != nil {
@ -200,14 +209,16 @@ func (as ArtifactStore) Add(ctx context.Context, dest string, paths []string, _
if err != nil { if err != nil {
return nil, err return nil, err
} }
newArtifactAnnotations := map[string]string{}
newArtifactAnnotations[specV1.AnnotationTitle] = filepath.Base(path) annots[specV1.AnnotationTitle] = filepath.Base(path)
newLayer := specV1.Descriptor{ newLayer := specV1.Descriptor{
MediaType: detectedType, MediaType: detectedType,
Digest: newBlobDigest, Digest: newBlobDigest,
Size: newBlobSize, Size: newBlobSize,
Annotations: newArtifactAnnotations, Annotations: annots,
} }
artifactManifestLayers = append(artifactManifestLayers, newLayer) artifactManifestLayers = append(artifactManifestLayers, newLayer)
} }
@ -215,11 +226,12 @@ func (as ArtifactStore) Add(ctx context.Context, dest string, paths []string, _
Versioned: specs.Versioned{SchemaVersion: 2}, Versioned: specs.Versioned{SchemaVersion: 2},
MediaType: specV1.MediaTypeImageManifest, MediaType: specV1.MediaTypeImageManifest,
// TODO This should probably be configurable once the CLI is capable // TODO This should probably be configurable once the CLI is capable
ArtifactType: "", Config: specV1.DescriptorEmptyJSON,
Config: specV1.DescriptorEmptyJSON, Layers: artifactManifestLayers,
Layers: artifactManifestLayers,
} }
artifactManifest.ArtifactType = options.ArtifactType
rawData, err := json.Marshal(artifactManifest) rawData, err := json.Marshal(artifactManifest)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -3,3 +3,9 @@ package types
// GetArtifactOptions is a struct containing options that for obtaining artifacts. // GetArtifactOptions is a struct containing options that for obtaining artifacts.
// It is meant for future growth or changes required without wacking the API // It is meant for future growth or changes required without wacking the API
type GetArtifactOptions struct{} type GetArtifactOptions struct{}
// AddOptions are additional descriptors of an artifact file
type AddOptions struct {
Annotations map[string]string `json:"annotations,omitempty"`
ArtifactType string `json:",omitempty"`
}

View File

@ -67,6 +67,31 @@ var _ = Describe("Podman artifact", func() {
Expect(addAgain.ErrorToString()).To(Equal(fmt.Sprintf("Error: artifact %s already exists", artifact1Name))) Expect(addAgain.ErrorToString()).To(Equal(fmt.Sprintf("Error: artifact %s already exists", artifact1Name)))
}) })
It("podman artifact add with options", func() {
artifact1Name := "localhost/test/artifact1"
artifact1File, err := createArtifactFile(1024)
Expect(err).ToNot(HaveOccurred())
artifactType := "octet/foobar"
annotation1 := "color=blue"
annotation2 := "flavor=lemon"
podmanTest.PodmanExitCleanly([]string{"artifact", "add", "--type", artifactType, "--annotation", annotation1, "--annotation", annotation2, artifact1Name, artifact1File}...)
inspectSingleSession := podmanTest.PodmanExitCleanly([]string{"artifact", "inspect", artifact1Name}...)
a := libartifact.Artifact{}
err = json.Unmarshal([]byte(inspectSingleSession.OutputToString()), &a)
Expect(err).ToNot(HaveOccurred())
Expect(a.Name).To(Equal(artifact1Name))
Expect(a.Manifests[0].ArtifactType).To(Equal(artifactType))
Expect(a.Manifests[0].Layers[0].Annotations["color"]).To(Equal("blue"))
Expect(a.Manifests[0].Layers[0].Annotations["flavor"]).To(Equal("lemon"))
failSession := podmanTest.Podman([]string{"artifact", "add", "--annotation", "org.opencontainers.image.title=foobar", "foobar", artifact1File})
failSession.WaitWithDefaultTimeout()
Expect(failSession).Should(Exit(125))
Expect(failSession.ErrorToString()).Should(Equal("Error: cannot override filename with org.opencontainers.image.title annotation"))
})
It("podman artifact add multiple", func() { It("podman artifact add multiple", func() {
artifact1File1, err := createArtifactFile(1024) artifact1File1, err := createArtifactFile(1024)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())