Skip to content

Add new "bashbrew remote arches" command #56

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
!go.sum
!manifest/
!pkg/
!registry/
!scripts/
31 changes: 22 additions & 9 deletions architecture/oci-platform.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package architecture

import "path"
import (
"path"

"github.com/containerd/containerd/platforms"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
)

// https://github.com/opencontainers/image-spec/blob/v1.0.1/image-index.md#image-index-property-descriptions
// see "platform" (under "manifests")
type OCIPlatform struct {
OS string `json:"os"`
Architecture string `json:"architecture"`
Variant string `json:"variant,omitempty"`

//OSVersion string `json:"os.version,omitempty"`
//OSFeatures []string `json:"os.features,omitempty"`
}
type OCIPlatform ocispec.Platform

var SupportedArches = map[string]OCIPlatform{
"amd64": {OS: "linux", Architecture: "amd64"},
Expand All @@ -36,3 +34,18 @@ func (p OCIPlatform) String() string {
p.Variant,
)
}

func Normalize(p ocispec.Platform) ocispec.Platform {
p = platforms.Normalize(p)
if p.Architecture == "arm64" && p.Variant == "" {
// 😭 https://github.com/containerd/containerd/blob/1c90a442489720eec95342e1789ee8a5e1b9536f/platforms/database.go#L98 (inconsistent normalization of "linux/arm -> linux/arm/v7" vs "linux/arm64/v8 -> linux/arm64")
p.Variant = "v8"
// TODO get pedantic about amd64 variants too? (in our defense, those variants didn't exist when we defined our "amd64", unlike "arm64v8" 👀)
}
return p
}

func (p OCIPlatform) Is(q OCIPlatform) bool {
// (assumes "p" and "q" are both already bashbrew normalized, like one of the SupportedArches above)
return p.OS == q.OS && p.Architecture == q.Architecture && p.Variant == q.Variant
}
44 changes: 44 additions & 0 deletions architecture/oci-platform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"testing"

"github.com/docker-library/bashbrew/architecture"

ocispec "github.com/opencontainers/image-spec/specs-go/v1"
)

func TestString(t *testing.T) {
Expand All @@ -21,3 +23,45 @@ func TestString(t *testing.T) {
})
}
}

func TestIs(t *testing.T) {
tests := map[bool][][2]architecture.OCIPlatform{
true: {
{architecture.SupportedArches["amd64"], architecture.SupportedArches["amd64"]},
{architecture.SupportedArches["arm32v5"], architecture.SupportedArches["arm32v5"]},
{architecture.SupportedArches["arm32v6"], architecture.SupportedArches["arm32v6"]},
{architecture.SupportedArches["arm32v7"], architecture.SupportedArches["arm32v7"]},
{architecture.SupportedArches["arm64v8"], architecture.OCIPlatform{OS: "linux", Architecture: "arm64", Variant: "v8"}},
{architecture.SupportedArches["windows-amd64"], architecture.OCIPlatform{OS: "windows", Architecture: "amd64", OSVersion: "1.2.3.4"}},
},
false: {
{architecture.SupportedArches["amd64"], architecture.OCIPlatform{OS: "linux", Architecture: "amd64", Variant: "v4"}},
{architecture.SupportedArches["amd64"], architecture.SupportedArches["arm64v8"]},
{architecture.SupportedArches["amd64"], architecture.SupportedArches["i386"]},
{architecture.SupportedArches["amd64"], architecture.SupportedArches["windows-amd64"]},
{architecture.SupportedArches["arm32v7"], architecture.SupportedArches["arm32v6"]},
{architecture.SupportedArches["arm32v7"], architecture.SupportedArches["arm64v8"]},
{architecture.SupportedArches["arm64v8"], architecture.OCIPlatform{OS: "linux", Architecture: "arm64", Variant: "v9"}},
},
}
for expected, test := range tests {
for _, platforms := range test {
t.Run(platforms[0].String()+" vs "+platforms[1].String(), func(t *testing.T) {
if got := platforms[0].Is(platforms[1]); got != expected {
t.Errorf("expected %v; got %v", expected, got)
}
})
}
}
}

func TestNormalize(t *testing.T) {
for arch, expected := range architecture.SupportedArches {
t.Run(arch, func(t *testing.T) {
normal := architecture.OCIPlatform(architecture.Normalize(ocispec.Platform(expected)))
if !expected.Is(normal) {
t.Errorf("expected %#v; got %#v", expected, normal)
}
})
}
}
11 changes: 7 additions & 4 deletions cmd/bashbrew/cmd-push.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func cmdPush(c *cli.Context) error {
}

// we can't use "r.Tags()" here because it will include SharedTags, which we never want to push directly (see "cmd-put-shared.go")
TagsLoop:
for i, tag := range entry.Tags {
if uniq && i > 0 {
break
Expand All @@ -47,10 +48,12 @@ func cmdPush(c *cli.Context) error {

if !force {
localImageId, _ := dockerInspect("{{.Id}}", tag)
registryImageId := fetchRegistryImageId(tag)
if registryImageId != "" && localImageId == registryImageId {
fmt.Fprintf(os.Stderr, "skipping %s (remote image matches local)\n", tag)
continue
registryImageIds := fetchRegistryImageIds(tag)
for _, registryImageId := range registryImageIds {
if localImageId == registryImageId {
fmt.Fprintf(os.Stderr, "skipping %s (remote image matches local)\n", tag)
continue TagsLoop
}
}
}
fmt.Printf("Pushing %s\n", tag)
Expand Down
70 changes: 70 additions & 0 deletions cmd/bashbrew/cmd-remote-arches.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package main

import (
"context"
"encoding/json"
"fmt"
"sort"

"github.com/docker-library/bashbrew/registry"

ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/urfave/cli"
)

func cmdRemoteArches(c *cli.Context) error {
args := c.Args()
if len(args) < 1 {
return fmt.Errorf("expected at least one argument")
}
doJson := c.Bool("json")
ctx := context.Background()
for _, arg := range args {
img, err := registry.Resolve(ctx, arg)
if err != nil {
return err
}

arches, err := img.Architectures(ctx)
if err != nil {
return err
}

if doJson {
ret := struct {
Ref string `json:"ref"`
Desc ocispec.Descriptor `json:"desc"`
Arches map[string][]ocispec.Descriptor `json:"arches"`
}{
Ref: img.ImageRef,
Desc: img.Desc,
Arches: map[string][]ocispec.Descriptor{},
}
for arch, imgs := range arches {
for _, obj := range imgs {
ret.Arches[arch] = append(ret.Arches[arch], obj.Desc)
}
}
out, err := json.Marshal(ret)
if err != nil {
return err
}
fmt.Println(string(out))
} else {
fmt.Printf("%s -> %s\n", img.ImageRef, img.Desc.Digest)

// Go.....
keys := []string{}
for arch := range arches {
keys = append(keys, arch)
}
sort.Strings(keys)
for _, arch := range keys {
for _, obj := range arches[arch] {
fmt.Printf(" %s -> %s\n", arch, obj.Desc.Digest)
}
}
}
}
return nil
}
21 changes: 21 additions & 0 deletions cmd/bashbrew/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,11 @@ func main() {
Name: "target-namespace",
Usage: `target namespace to act into ("docker tag namespace/repo:tag target-namespace/repo:tag", "docker push target-namespace/repo:tag")`,
},

"json": cli.BoolFlag{
Name: "json",
Usage: "output machine-readable JSON instead of human-readable text",
},
}

app.Commands = []cli.Command{
Expand Down Expand Up @@ -395,6 +400,22 @@ func main() {

Category: "plumbing",
},
{
Name: "remote",
Usage: "query registries for bashbrew-related data",
Before: subcommandBeforeFactory("remote"),
Category: "plumbing",
Subcommands: []cli.Command{
{
Name: "arches",
Usage: "returns a list of bashbrew architectures and content descriptors for the specified image(s)",
Flags: []cli.Flag{
commonFlags["json"],
},
Action: cmdRemoteArches,
},
},
},
}

err := app.Run(os.Args)
Expand Down
Loading