Skip to content

Commit

Permalink
Create --append flag to add file to existing artifact
Browse files Browse the repository at this point in the history
Fixes: https://issues.redhat.com/browse/RUN-2444

Signed-off-by: Jan Rodák <hony.com@seznam.cz>
  • Loading branch information
Honny1 committed Feb 7, 2025
1 parent 16a72c8 commit f29ac6a
Show file tree
Hide file tree
Showing 8 changed files with 310 additions and 80 deletions.
8 changes: 7 additions & 1 deletion cmd/podman/artifact/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var (
type artifactAddOptions struct {
ArtifactType string
Annotations []string
Append bool
}

var (
Expand All @@ -41,12 +42,15 @@ func init() {
flags := addCmd.Flags()

annotationFlagName := "annotation"
flags.StringArrayVar(&addOpts.Annotations, annotationFlagName, nil, "set an `annotation` for the specified artifact")
flags.StringArrayVar(&addOpts.Annotations, annotationFlagName, nil, "set an `annotation` for the specified files of artifact")
_ = addCmd.RegisterFlagCompletionFunc(annotationFlagName, completion.AutocompleteNone)

addTypeFlagName := "type"
flags.StringVar(&addOpts.ArtifactType, addTypeFlagName, "", "Use type to describe an artifact")
_ = addCmd.RegisterFlagCompletionFunc(addTypeFlagName, completion.AutocompleteNone)

appendFlagName := "append"
flags.BoolVarP(&addOpts.Append, appendFlagName, "a", false, "Append files to an existing artifact")
}

func add(cmd *cobra.Command, args []string) error {
Expand All @@ -58,6 +62,8 @@ func add(cmd *cobra.Command, args []string) error {
}
opts.Annotations = annots
opts.ArtifactType = addOpts.ArtifactType
opts.Append = addOpts.Append

report, err := registry.ImageEngine().ArtifactAdd(registry.Context(), args[0], args[1:], opts)
if err != nil {
return err
Expand Down
6 changes: 6 additions & 0 deletions docs/source/markdown/podman-artifact-add.1.md.in
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ added.

@@option annotation.manifest

Note: Set annotations for each file being added.

#### **--append**, **-a**

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

#### **--help**

Print usage statement.
Expand Down
1 change: 1 addition & 0 deletions pkg/domain/entities/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
type ArtifactAddOptions struct {
Annotations map[string]string
ArtifactType string
Append bool
}

type ArtifactInspectOptions struct {
Expand Down
1 change: 1 addition & 0 deletions pkg/domain/infra/abi/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ func (ir *ImageEngine) ArtifactAdd(ctx context.Context, name string, paths []str
addOptions := types.AddOptions{
Annotations: opts.Annotations,
ArtifactType: opts.ArtifactType,
Append: opts.Append,
}

artifactDigest, err := artStore.Add(ctx, name, paths, &addOptions)
Expand Down
116 changes: 87 additions & 29 deletions pkg/libartifact/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ var (
ErrEmptyArtifactName = errors.New("artifact name cannot be empty")
)

const ManifestSchemaVersion = 2

type ArtifactStore struct {
SystemContext *types.SystemContext
storePath string
Expand Down Expand Up @@ -162,23 +164,73 @@ 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
// string input is for possible custom artifact types.
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 {
return nil, ErrEmptyArtifactName
}

artifactManifestLayers := make([]specV1.Descriptor, 0)
if options.Append && len(options.ArtifactType) > 0 {
return nil, errors.New("append option is not compatible with ArtifactType option")
}

// 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)
}

// Check if artifact already exists
artifacts, err := as.getArtifacts(ctx, nil)
if err != nil {
return nil, err
}

// Check if artifact exists; in GetByName not getting an
// error means it exists
if _, _, err := artifacts.GetByNameOrDigest(dest); err == nil {
return nil, fmt.Errorf("artifact %s already exists", dest)
var artifactManifest specV1.Manifest
var oldDigest *digest.Digest
fileNames := map[string]struct{}{}

if !options.Append {
// Check if artifact exists; in GetByName not getting an
// error means it exists
_, _, err := artifacts.GetByNameOrDigest(dest)
if err == nil {
return nil, fmt.Errorf("artifact %s already exists", dest)
}
artifactManifest = specV1.Manifest{
Versioned: specs.Versioned{SchemaVersion: ManifestSchemaVersion},
MediaType: specV1.MediaTypeImageManifest,
ArtifactType: options.ArtifactType,
// TODO This should probably be configurable once the CLI is capable
Config: specV1.DescriptorEmptyJSON,
Layers: make([]specV1.Descriptor, 0),
}
} else {
artifact, _, err := artifacts.GetByNameOrDigest(dest)
if err != nil {
return nil, err
}
if artifact == nil {
return nil, fmt.Errorf("artifact %s doesn't exists", dest)
}
artifactManifest = artifact.Manifest.Manifest
oldDigest, err = artifact.GetDigest()
if err != nil {
return nil, err
}

for _, layer := range artifactManifest.Layers {
if value, ok := layer.Annotations[specV1.AnnotationTitle]; ok && value != "" {
fileNames[layer.Annotations[specV1.AnnotationTitle]] = struct{}{}
}
}
}

for _, path := range paths {
fileName := filepath.Base(path)
if _, ok := fileNames[fileName]; ok {
return nil, fmt.Errorf("file: %q already exists in artifact", fileName)
}
fileNames[fileName] = struct{}{}
}

ir, err := layout.NewReference(as.storePath, dest)
Expand All @@ -192,14 +244,9 @@ func (as ArtifactStore) Add(ctx context.Context, dest string, paths []string, op
}
defer imageDest.Close()

// ImageDestination, in general, requires the caller to write a full image; here we may write only the added layers.
// This works for the oci/layout transport we hard-code.
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
newBlobDigest, newBlobSize, err := layout.PutBlobFromLocalFile(ctx, imageDest, path)
if err != nil {
Expand All @@ -210,35 +257,25 @@ func (as ArtifactStore) Add(ctx context.Context, dest string, paths []string, op
return nil, err
}

annots[specV1.AnnotationTitle] = filepath.Base(path)

annotations := maps.Clone(options.Annotations)
annotations[specV1.AnnotationTitle] = filepath.Base(path)
newLayer := specV1.Descriptor{
MediaType: detectedType,
Digest: newBlobDigest,
Size: newBlobSize,
Annotations: annots,
Annotations: annotations,
}

artifactManifestLayers = append(artifactManifestLayers, newLayer)
}

artifactManifest := specV1.Manifest{
Versioned: specs.Versioned{SchemaVersion: 2},
MediaType: specV1.MediaTypeImageManifest,
// TODO This should probably be configurable once the CLI is capable
Config: specV1.DescriptorEmptyJSON,
Layers: artifactManifestLayers,
artifactManifest.Layers = append(artifactManifest.Layers, newLayer)
}

artifactManifest.ArtifactType = options.ArtifactType

rawData, err := json.Marshal(artifactManifest)
if err != nil {
return nil, err
}
if err := imageDest.PutManifest(ctx, rawData, nil); err != nil {
return nil, err
}

unparsed := newUnparsedArtifactImage(ir, artifactManifest)
if err := imageDest.Commit(ctx, unparsed); err != nil {
return nil, err
Expand All @@ -251,6 +288,27 @@ func (as ArtifactStore) Add(ctx context.Context, dest string, paths []string, op
if err := createEmptyStanza(filepath.Join(as.storePath, specV1.ImageBlobsDir, artifactManifestDigest.Algorithm().String(), artifactManifest.Config.Digest.Encoded())); err != nil {
logrus.Errorf("failed to check or write empty stanza file: %v", err)
}

// Clean up after append. Remove previous artifact from store.
if oldDigest != nil {
lrs, err := layout.List(as.storePath)
if err != nil {
return nil, err
}

for _, l := range lrs {
if oldDigest.String() == l.ManifestDescriptor.Digest.String() {
if _, ok := l.ManifestDescriptor.Annotations[specV1.AnnotationRefName]; ok {
continue
}

if err := l.Reference.DeleteImage(ctx, as.SystemContext); err != nil {
return nil, err
}
break
}
}
}
return &artifactManifestDigest, nil
}

Expand All @@ -269,7 +327,7 @@ func (as ArtifactStore) readIndex() (*specV1.Index, error) { //nolint:unused
func (as ArtifactStore) createEmptyManifest() error {
index := specV1.Index{
MediaType: specV1.MediaTypeImageIndex,
Versioned: specs.Versioned{SchemaVersion: 2},
Versioned: specs.Versioned{SchemaVersion: ManifestSchemaVersion},
}
rawData, err := json.Marshal(&index)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions pkg/libartifact/types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ type GetArtifactOptions struct{}
type AddOptions struct {
Annotations map[string]string `json:"annotations,omitempty"`
ArtifactType string `json:",omitempty"`
// append option is not compatible with ArtifactType option
Append bool `json:",omitempty"`
}
Loading

0 comments on commit f29ac6a

Please sign in to comment.