Skip to content

Instantly share code, notes, and snippets.

@imjasonh
Last active Jun 8, 2021
Embed
What would you like to do?
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"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/types"
)
func main() {
src1, src2, dst := os.Args[1], os.Args[2], os.Args[3]
log.Println("combining", src1, src2, "into", dst)
dstr, err := name.ParseReference(dst)
if err != nil {
log.Fatal(err)
}
pull := func(s string) (v1.ImageIndex, error) {
r, err := name.ParseReference(s)
if err != nil {
return nil, err
}
return remote.Index(r)
}
src1i, err := pull(src1)
if err != nil {
log.Fatalf("pulling %q: %v", src1, err)
}
src2i, err := pull(src2)
if err != nil {
log.Fatalf("pulling %q: %v", src2, err)
}
plats := map[*v1.Platform]bool{}
var adds []mutate.IndexAddendum
add := func(idx v1.ImageIndex) error {
mf, err := idx.IndexManifest()
if err != nil {
return err
}
for _, desc := range mf.Manifests {
if plats[desc.Platform] {
return fmt.Errorf("conflicting platform %+v", *desc.Platform)
}
plats[desc.Platform] = true
log.Printf("found platform %s/%s, %s", desc.Platform.OS, desc.Platform.Architecture, desc.MediaType)
img, err := idx.Image(desc.Digest)
if err != nil {
return err
}
adds = append(adds, mutate.IndexAddendum{
Add: img,
Descriptor: desc,
})
}
return nil
}
log.Println("---", src1, "---")
if err := add(src1i); err != nil {
log.Fatal(err)
}
log.Println("---", src2, "---")
if err := add(src2i); err != nil {
log.Fatal(err)
}
dsti := mutate.AppendManifests(mutate.IndexMediaType(empty.Index, types.DockerManifestList), adds...)
mf, _ := dsti.IndexManifest()
b, _ := json.MarshalIndent(mf, "", " ")
log.Println(string(b))
log.Println("pushing...")
if err := remote.WriteIndex(dstr, dsti, remote.WithAuthFromKeychain(authn.DefaultKeychain)); err != nil {
log.Fatalf("pushing %q: %v", dst, err)
}
log.Println("pushed")
}
2021/06/08 09:53:37 combining gcr.io/distroless/static:nonroot mcr.microsoft.com/windows/nanoserver:1809 into gcr.io/imjasonh/combined
2021/06/08 09:53:38 --- gcr.io/distroless/static:nonroot ---
2021/06/08 09:53:38 found platform linux/amd64
2021/06/08 09:53:38 found platform linux/arm
2021/06/08 09:53:38 found platform linux/arm64
2021/06/08 09:53:38 found platform linux/ppc64le
2021/06/08 09:53:39 found platform linux/s390x
2021/06/08 09:53:39 --- mcr.microsoft.com/windows/nanoserver:1809 ---
2021/06/08 09:53:39 found platform windows/amd64
2021/06/08 09:53:39 found platform windows/arm
2021/06/08 09:53:39 validating...
2021/06/08 09:55:53 pushing...
2021/06/08 09:56:00 pushing "gcr.io/imjasonh/combined": PUT https://gcr.io/v2/imjasonh/combined/manifests/latest: MANIFEST_INVALID: Failed to parse manifest for request "/v2/imjasonh/combined/manifests/latest": Image index child manifest not supported: application/vnd.docker.distribution.manifest.v2+json.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment