From b25fafb75359a61912ab76a13fa37d6ac0130c22 Mon Sep 17 00:00:00 2001 From: Dayuan Date: Sun, 28 Apr 2024 13:33:44 +0800 Subject: [PATCH] fix: replace current platform image with the new one when it already have existed in the image index (#1077) --- pkg/cmd/mod/mod_push.go | 4 ++-- pkg/oci/client/push.go | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/pkg/cmd/mod/mod_push.go b/pkg/cmd/mod/mod_push.go index a751b721..c15951ff 100644 --- a/pkg/cmd/mod/mod_push.go +++ b/pkg/cmd/mod/mod_push.go @@ -247,7 +247,7 @@ func (o *PushModOptions) Run() error { // Copy to temp module dir and push artifact to OCI repository err = ioutil.CopyDir(targetDir, o.ModulePath, func(path string) bool { - skipDirs := []string{filepath.Join(o.ModulePath, ".git"), filepath.Join(o.ModulePath, "src")} + skipDirs := []string{filepath.Join(o.ModulePath, ".git"), filepath.Join(o.ModulePath, ".idea"), filepath.Join(o.ModulePath, "src")} // skip files in skipDirs for _, dir := range skipDirs { @@ -354,7 +354,7 @@ func (o *PushModOptions) buildModule() (string, error) { func buildBinary(goBin, operatingSystem, arch, srcDirectory, outfile string, ioStreams genericiooptions.IOStreams) (string, error) { if outfile == "" { // If no outfile is supplied, write the Go binary to a temporary file. - f, err := os.CreateTemp("", "generator.*") + f, err := os.CreateTemp("", "module.*") if err != nil { return "", fmt.Errorf("unable to create go program temp file: %w", err) } diff --git a/pkg/oci/client/push.go b/pkg/oci/client/push.go index e91d0a73..dd5f338a 100644 --- a/pkg/oci/client/push.go +++ b/pkg/oci/client/push.go @@ -11,6 +11,7 @@ import ( "github.com/google/go-containerregistry/pkg/crane" "github.com/google/go-containerregistry/pkg/v1" "github.com/google/go-containerregistry/pkg/v1/empty" + "github.com/google/go-containerregistry/pkg/v1/match" "github.com/google/go-containerregistry/pkg/v1/mutate" "github.com/google/go-containerregistry/pkg/v1/partial" "github.com/google/go-containerregistry/pkg/v1/remote" @@ -147,7 +148,10 @@ func (c *Client) Push( Add: image, Descriptor: *newDesc, } - idx := mutate.AppendManifests(base, addendum) + + // replace current platform image with the new one + // remove is removed before adds + idx := mutate.AppendManifests(mutate.RemoveManifests(base, match.Platforms(*newDesc.Platform)), addendum) idxDigest, err := idx.Digest() if err != nil { return "", "", fmt.Errorf("parsing index digest failed: %w", err)