|
| 1 | +// registry_push copies a saved container image archive to a registry. It exists |
| 2 | +// for the Kubernetes test harness so an HTTP test registry does not have to be |
| 3 | +// added to the runner machine's Docker daemon configuration. |
| 4 | +package main |
| 5 | + |
| 6 | +import ( |
| 7 | + "flag" |
| 8 | + "fmt" |
| 9 | + "os" |
| 10 | + |
| 11 | + "github.com/google/go-containerregistry/pkg/authn" |
| 12 | + "github.com/google/go-containerregistry/pkg/name" |
| 13 | + "github.com/google/go-containerregistry/pkg/v1/remote" |
| 14 | + "github.com/google/go-containerregistry/pkg/v1/tarball" |
| 15 | +) |
| 16 | + |
| 17 | +func main() { |
| 18 | + insecure := flag.Bool("insecure", false, "push to the registry over plain HTTP") |
| 19 | + tarPath := flag.String("tar", "", "path to an image archive produced by docker save") |
| 20 | + flag.Parse() |
| 21 | + if flag.NArg() != 1 || *tarPath == "" { |
| 22 | + fmt.Fprintln(os.Stderr, "usage: registry_push [--insecure] --tar PATH IMAGE") |
| 23 | + os.Exit(2) |
| 24 | + } |
| 25 | + |
| 26 | + imageName := flag.Arg(0) |
| 27 | + source, err := name.NewTag(imageName) |
| 28 | + if err != nil { |
| 29 | + fatalf("parse source image reference: %v", err) |
| 30 | + } |
| 31 | + image, err := tarball.ImageFromPath(*tarPath, &source) |
| 32 | + if err != nil { |
| 33 | + fatalf("read image archive: %v", err) |
| 34 | + } |
| 35 | + |
| 36 | + var nameOptions []name.Option |
| 37 | + if *insecure { |
| 38 | + nameOptions = append(nameOptions, name.Insecure) |
| 39 | + } |
| 40 | + destination, err := name.ParseReference(imageName, nameOptions...) |
| 41 | + if err != nil { |
| 42 | + fatalf("parse destination image reference: %v", err) |
| 43 | + } |
| 44 | + if err := remote.Write(destination, image, remote.WithAuth(authn.Anonymous)); err != nil { |
| 45 | + fatalf("push image: %v", err) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func fatalf(format string, args ...any) { |
| 50 | + fmt.Fprintf(os.Stderr, format+"\n", args...) |
| 51 | + os.Exit(1) |
| 52 | +} |
0 commit comments