Skip to content

Instantly share code, notes, and snippets.

@imjasonh
Created Apr 13, 2021
Embed
What would you like to do?
package main
import (
"flag"
"fmt"
"log"
"strings"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/empty"
"github.com/google/go-containerregistry/pkg/v1/mutate"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/google/go-containerregistry/pkg/v1/tarball"
"github.com/google/go-containerregistry/pkg/v1/types"
)
var refstr = flag.String("ref", "", "Reference to push manifest list to")
func main() {
flag.Parse()
ref, err := name.ParseReference(*refstr)
if err != nil {
log.Fatalf("name.ParseReference: %v", err)
}
// GCR doesn't allow an image with no layers, so let's create an image
// with one empty layer.
l, err := tarball.LayerFromReader(strings.NewReader(""))
if err != nil {
log.Fatalf("tarball.LayerFromReader: %v", err)
}
img, err := mutate.AppendLayers(empty.Image, l)
if err != nil {
log.Fatalf("mutate.AppendLayers: %v", err)
}
var idx v1.ImageIndex = empty.Index
for _, p := range []v1.Platform{{
OS: "linux",
Architecture: "amd64",
}, {
OS: "windows",
Architecture: "amd64",
}} {
idx = mutate.AppendManifests(idx, mutate.IndexAddendum{
Add: img,
Descriptor: v1.Descriptor{
MediaType: types.OCIManifestSchema1,
Platform: p.DeepCopy(),
},
})
}
if err := remote.WriteIndex(ref, idx, remote.WithAuthFromKeychain(authn.DefaultKeychain)); err != nil {
log.Fatalf("remote.WriteIndex: %v", err)
}
d, err := idx.Digest()
if err != nil {
log.Fatalf("index.Digest: %v", err)
}
fmt.Println(ref.Context().Digest(d.String()))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment