Skip to content

Commit fedbf90

Browse files
committed
generate: Respect --os when generating default templates
Also add --host-specific checks to SetPlatformOS and SetPlatformArch. The SetPlatformArch check is currently just a warning (details in eda1ac7, validate: With --host-specific, compare config platform vs. runtime, 2016-08-16) but I've added an error to its API so callers will be ready when we do actually raise errors (e.g. for '--arch arm' on an amd64 box). Also add IsSet checks to --os and --arch so we don't clobber their template values. Before this commit on my amd64 Linux box: $ echo '{"platform": {"os": "windows", "arch": "386"}}' >template.json $ ocitools generate --template template.json | jq .platform { "os": "linux", "arch": "amd64" } After this commit: $ ocitools generate --template template.json | jq .platform { "os": "windows", "arch": "386" } Signed-off-by: W. Trevor King <wking@tremily.us>
1 parent ea326f0 commit fedbf90

3 files changed

Lines changed: 39 additions & 9 deletions

File tree

cmd/oci-runtime-tool/generate.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
"fmt"
55
"os"
6-
"runtime"
76
"strconv"
87
"strings"
98

@@ -15,7 +14,7 @@ import (
1514

1615
var generateFlags = []cli.Flag{
1716
cli.StringFlag{Name: "apparmor", Usage: "specifies the the apparmor profile for the container"},
18-
cli.StringFlag{Name: "arch", Value: runtime.GOARCH, Usage: "architecture the container is created for"},
17+
cli.StringFlag{Name: "arch", Usage: "architecture the container is created for"},
1918
cli.StringSliceFlag{Name: "args", Usage: "command to run in the container"},
2019
cli.StringSliceFlag{Name: "bind", Usage: "bind mount directories src:dest[:options...]"},
2120
cli.StringSliceFlag{Name: "cap-add", Usage: "add Linux capabilities"},
@@ -52,7 +51,7 @@ var generateFlags = []cli.Flag{
5251
cli.StringFlag{Name: "network", Usage: "network namespace"},
5352
cli.BoolFlag{Name: "no-new-privileges", Usage: "set no new privileges bit for the container process"},
5453
cli.IntFlag{Name: "oom-score-adj", Usage: "oom_score_adj for the container"},
55-
cli.StringFlag{Name: "os", Value: runtime.GOOS, Usage: "operating system the container is created for"},
54+
cli.StringFlag{Name: "os", Usage: "operating system the container is created for"},
5655
cli.StringFlag{Name: "output", Usage: "output file (defaults to stdout)"},
5756
cli.StringFlag{Name: "pid", Usage: "pid namespace"},
5857
cli.StringSliceFlag{Name: "poststart", Usage: "set command to run in poststart hooks"},
@@ -92,7 +91,12 @@ var generateCommand = cli.Command{
9291
Before: before,
9392
Action: func(context *cli.Context) error {
9493
// Start from the default template.
95-
specgen, err := generate.New(nil)
94+
var osPointer *string
95+
if context.IsSet("os") {
96+
goos := context.String("os")
97+
osPointer = &goos
98+
}
99+
specgen, err := generate.New(osPointer)
96100
if err != nil {
97101
return err
98102
}
@@ -143,8 +147,19 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
143147
g.SetHostname(context.String("hostname"))
144148
}
145149

146-
g.SetPlatformOS(context.String("os"))
147-
g.SetPlatformArch(context.String("arch"))
150+
if context.IsSet("os") {
151+
err := g.SetPlatformOS(context.String("os"))
152+
if err != nil {
153+
return err
154+
}
155+
}
156+
157+
if context.IsSet("arch") {
158+
err := g.SetPlatformArch(context.String("arch"))
159+
if err != nil {
160+
return err
161+
}
162+
}
148163

149164
if context.IsSet("label") {
150165
annotations := context.StringSlice("label")

generate/generate.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
rspec "github.com/opencontainers/runtime-spec/specs-go"
1313
"github.com/opencontainers/runtime-tools/generate/seccomp"
14+
"github.com/Sirupsen/logrus"
1415
"github.com/syndtr/gocapability/capability"
1516
)
1617

@@ -284,15 +285,23 @@ func (g *Generator) RemoveAnnotation(key string) {
284285
}
285286

286287
// SetPlatformOS sets g.spec.Process.OS.
287-
func (g *Generator) SetPlatformOS(os string) {
288+
func (g *Generator) SetPlatformOS(os string) error {
289+
if g.HostSpecific && os != runtime.GOOS {
290+
return fmt.Errorf("cannot set platform.os to %s on a %s host", os, runtime.GOOS)
291+
}
288292
g.initSpec()
289293
g.spec.Platform.OS = os
294+
return nil
290295
}
291296

292297
// SetPlatformArch sets g.spec.Platform.Arch.
293-
func (g *Generator) SetPlatformArch(arch string) {
298+
func (g *Generator) SetPlatformArch(arch string) error {
299+
if g.HostSpecific && arch != runtime.GOARCH {
300+
logrus.Warnf("setting platform.arch to %s on a %s host", arch, runtime.GOARCH)
301+
}
294302
g.initSpec()
295303
g.spec.Platform.Arch = arch
304+
return nil
296305
}
297306

298307
// SetProcessUID sets g.spec.Process.User.UID.

man/oci-runtime-tool-generate.1.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,13 @@ read the configuration from `config.json`.
173173
Specifies oom_score_adj for the container.
174174

175175
**--os**=OS
176-
Operating system used within the container
176+
Operating system used within the container. When no template is
177+
set, the default is to use the `GOOS` ocitools was built with and to
178+
default other settings based on this setting. For example, if
179+
`GOOS` is `linux`, `ocitools generate` will generate a default Linux
180+
config, `ocitools generate --os=windows` will generate a default
181+
Windows config, and `ocitools --host-specific generate --os=windows`
182+
will exit with an error.
177183

178184
**--output**=PATH
179185
Instead of writing the configuration JSON to stdout, write it to a

0 commit comments

Comments
 (0)