Skip to content

Commit 669b693

Browse files
loresusopoiana
authored andcommitted
update(pkg/oci/pusher): give the possibility to push with multiple tags
Signed-off-by: Lorenzo Susini <[email protected]>
1 parent a389b41 commit 669b693

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

pkg/oci/constants.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,7 @@ const (
3030

3131
// DefaultRegistry is the default container registry to use.
3232
DefaultRegistry = "ghcr.io"
33+
34+
// DefaultTag is the default tag reference to be used when none is provided.
35+
DefaultTag = "latest"
3336
)

pkg/oci/pusher/options.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type opts struct {
2020
Filepaths []string
2121
Platforms []string
2222
Dependencies []string
23+
Tags []string
2324
}
2425

2526
// Option is a functional option for pusher.
@@ -74,3 +75,11 @@ func WithDependencies(deps ...string) Option {
7475
return nil
7576
}
7677
}
78+
79+
// WithTags sets the tags option.
80+
func WithTags(tags ...string) Option {
81+
return func(o *opts) error {
82+
o.Tags = tags
83+
return nil
84+
}
85+
}

pkg/oci/pusher/pusher.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ func (p *Pusher) Push(ctx context.Context, artifactType oci.ArtifactType,
8989
}
9090
repo.Client = p.Client
9191

92+
// Using ":latest" by default if no tag was provided.
93+
if repo.Reference.Reference == "" {
94+
repo.Reference.Reference = oci.DefaultTag
95+
}
96+
9297
// Set remoteTarget and its tracker.
9398
remoteTarget := oras.Target(repo)
9499

@@ -99,16 +104,16 @@ func (p *Pusher) Push(ctx context.Context, artifactType oci.ArtifactType,
99104
defaultCopyOptions := oras.DefaultCopyGraphOptions
100105
defaultCopyOptions.Concurrency = 1
101106

107+
// Initialize the file store for this artifact.
108+
tmpDir, err := os.MkdirTemp("", "falcoctl")
109+
if err != nil {
110+
return nil, err
111+
}
112+
defer os.RemoveAll(tmpDir)
113+
102114
manifestDescs := make([]*v1.Descriptor, len(o.Filepaths))
103115
var fileStore *file.Store
104116
for i, artifactPath := range o.Filepaths {
105-
// Initialize the file store for this artifact
106-
tmpDir, err := os.MkdirTemp("", "falcoctl")
107-
if err != nil {
108-
return nil, err
109-
}
110-
defer os.RemoveAll(tmpDir)
111-
112117
fileStore = file.New(tmpDir)
113118

114119
platform := ""
@@ -117,7 +122,11 @@ func (p *Pusher) Push(ctx context.Context, artifactType oci.ArtifactType,
117122
}
118123

119124
// Prepare data layer.
120-
if dataDesc, err = p.storeMainLayer(ctx, fileStore, artifactType, artifactPath); err != nil {
125+
absolutePath, err := filepath.Abs(artifactPath)
126+
if err != nil {
127+
return nil, err
128+
}
129+
if dataDesc, err = p.storeMainLayer(ctx, fileStore, artifactType, absolutePath); err != nil {
121130
return nil, err
122131
}
123132

@@ -159,6 +168,11 @@ func (p *Pusher) Push(ctx context.Context, artifactType oci.ArtifactType,
159168

160169
// Tag the root descriptor remotely.
161170
repo.PushReference(ctx, *rootDesc, rootReader, repo.Reference.Reference)
171+
if len(o.Tags) > 0 {
172+
if err = oras.TagN(ctx, remoteTarget, repo.Reference.Reference, o.Tags, oras.DefaultTagNOptions); err != nil {
173+
return nil, err
174+
}
175+
}
162176

163177
return &oci.RegistryResult{
164178
Digest: string(rootDesc.Digest),

0 commit comments

Comments
 (0)