Skip to content

Commit 0cef98d

Browse files
committed
mkctr: fix a typo, unify some error handling, flesh out docs, add --goarch
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
1 parent c75ea14 commit 0cef98d

1 file changed

Lines changed: 19 additions & 20 deletions

File tree

mkctr.go

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"os/exec"
2020
"path/filepath"
2121
"runtime"
22+
"slices"
2223
"strings"
2324
"time"
2425

@@ -81,6 +82,7 @@ type buildParams struct {
8182
publish bool
8283
ldflags string
8384
gotags string
85+
goarch []string
8486
target string
8587
verbose bool
8688
annotations map[string]string // OCI image annotations
@@ -96,7 +98,8 @@ func main() {
9698
ldflagsArg = flag.String("ldflags", "", "the --ldflags value to pass to go")
9799
gotags = flag.String("gotags", "", "the --tags value to pass to go")
98100
push = flag.Bool("push", false, "publish the image")
99-
target = flag.String("target", "", "build for a specific env (options: flyio, local)")
101+
target = flag.String("target", "", `build for a specific env (options: "", "flyio", "local")`)
102+
goarch = flag.String("goarch", "arm,arm64,amd64,386", "comma-separated list of architectures to build (if supported by --base image)")
100103
verbose = flag.Bool("v", false, "verbose build output")
101104
annotations = flag.String("annotations", "", `OCI image annotations https://github.com/opencontainers/image-spec/blob/main/annotations.md.
102105
Annotations must be comma separated key=value pairs, i.e key1=val1,key2=val2. For a single image manifest annotations will get added to the image manifest.
@@ -105,13 +108,13 @@ func main() {
105108
)
106109
flag.Parse()
107110
if *tagArg == "" {
108-
log.Fatal("tags must be set")
111+
log.Fatal("--tags must be set")
109112
}
110113
if *repos == "" {
111-
log.Fatal("registries must be set")
114+
log.Fatal("--repos must be set")
112115
}
113116
if *baseImage == "" {
114-
log.Fatal("baseImage must be set")
117+
log.Fatal("--base must be set")
115118
}
116119
switch *target {
117120
case "", "flyio", "local":
@@ -144,6 +147,7 @@ func main() {
144147
gotags: *gotags,
145148
target: *target,
146149
verbose: *verbose,
150+
goarch: strings.Split(*goarch, ","),
147151
annotations: parseAnnotations(*annotations),
148152
}
149153

@@ -180,20 +184,18 @@ func canRunLocal(p v1.Platform) bool {
180184
return false
181185
}
182186

183-
func verifyPlatform(p v1.Platform, target string) error {
187+
func (bp *buildParams) verifyPlatform(p v1.Platform) error {
184188
if p.OS != "linux" {
185189
return fmt.Errorf("unsupported OS: %v", p.OS)
186190
}
187-
if target == "local" && !canRunLocal(p) {
188-
return fmt.Errorf("not required for target %q", target)
191+
if bp.target == "local" && !canRunLocal(p) {
192+
return fmt.Errorf("not required for target %q", bp.target)
189193
}
190-
if target == "flyio" && p.Architecture != "amd64" {
191-
return fmt.Errorf("not required for target %q", target)
194+
if bp.target == "flyio" && p.Architecture != "amd64" {
195+
return fmt.Errorf("not required for target %q", bp.target)
192196
}
193-
switch p.Architecture {
194-
case "arm", "arm64", "amd64", "386":
195-
default:
196-
return fmt.Errorf("unsupported arch: %v", p.Architecture)
197+
if !slices.Contains(bp.goarch, p.Architecture) {
198+
return fmt.Errorf("architecture %q not in requested goarch values %q", p.Architecture, bp.goarch)
197199
}
198200
return nil
199201
}
@@ -235,7 +237,7 @@ func fetchAndBuild(bp *buildParams) error {
235237
p.Variant = config.Variant
236238
}
237239

238-
if err := verifyPlatform(p, bp.target); err != nil {
240+
if err := bp.verifyPlatform(p); err != nil {
239241
return err
240242
}
241243
logf := withPrefix(logf, fmt.Sprintf("%v/%v: ", p.OS, p.Architecture))
@@ -284,7 +286,7 @@ func fetchAndBuild(bp *buildParams) error {
284286
if id.Platform == nil {
285287
return fmt.Errorf("unknown platform for image: %v", bp.baseImage)
286288
}
287-
if err := verifyPlatform(*id.Platform, bp.target); err != nil {
289+
if err := bp.verifyPlatform(*id.Platform); err != nil {
288290
logf("skipping: %v", err)
289291
continue
290292
}
@@ -391,11 +393,8 @@ func goarm(platform v1.Platform) (string, error) {
391393
return "", fmt.Errorf("not arm: %v", platform.Architecture)
392394
}
393395
v := platform.Variant
394-
if len(v) != 2 {
395-
return "", fmt.Errorf("unexpected varient: %v", v)
396-
}
397-
if v[0] != 'v' || !('0' <= v[1] && v[1] <= '9') {
398-
return "", fmt.Errorf("unexpected varient: %v", v)
396+
if len(v) != 2 || v[0] != 'v' || !('0' <= v[1] && v[1] <= '9') {
397+
return "", fmt.Errorf("unexpected ARM variant %q", v)
399398
}
400399
return string(v[1]), nil
401400
}

0 commit comments

Comments
 (0)