Skip to content

Commit a217d8c

Browse files
author
Haiyan Meng
committed
Modify generate API
Signed-off-by: Haiyan Meng <[email protected]>
1 parent 8738f2f commit a217d8c

2 files changed

Lines changed: 144 additions & 110 deletions

File tree

cmd/ocitools/generate.go

Lines changed: 112 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package main
22

33
import (
4+
"fmt"
45
"os"
56
"runtime"
7+
"strconv"
8+
"strings"
69

710
"github.com/opencontainers/ocitools/generate"
811
rspec "github.com/opencontainers/runtime-spec/specs-go"
@@ -117,7 +120,11 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
117120
if context.IsSet("label") {
118121
annotations := context.StringSlice("label")
119122
for _, s := range annotations {
120-
if err := g.AddAnnotation(s); err != nil {
123+
pair := strings.Split(s, "=")
124+
if len(pair) != 2 {
125+
return fmt.Errorf("incorrectly specified annotation: %s", s)
126+
}
127+
if err := g.AddAnnotation(pair[0], pair[1]); err != nil {
121128
return err
122129
}
123130
}
@@ -184,7 +191,11 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
184191
if context.IsSet("sysctl") {
185192
sysctls := context.StringSlice("sysctl")
186193
for _, s := range sysctls {
187-
g.AddLinuxSysctl(s)
194+
pair := strings.Split(s, "=")
195+
if len(pair) != 2 {
196+
return fmt.Errorf("incorrectly specified sysctl: %s", s)
197+
}
198+
g.AddLinuxSysctl(pair[0], pair[1])
188199
}
189200
}
190201

@@ -239,7 +250,11 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
239250
if context.IsSet("tmpfs") {
240251
tmpfsSlice := context.StringSlice("tmpfs")
241252
for _, s := range tmpfsSlice {
242-
if err := g.AddTmpfsMount(s); err != nil {
253+
dest, options, err := parseTmpfsMount(s)
254+
if err != nil {
255+
return err
256+
}
257+
if err := g.AddTmpfsMount(dest, options); err != nil {
243258
return err
244259
}
245260
}
@@ -253,7 +268,12 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
253268
if context.IsSet("bind") {
254269
binds := context.StringSlice("bind")
255270
for _, bind := range binds {
256-
if err := g.AddBindMount(bind); err != nil {
271+
source, dest, options, err := parseBindMount(bind)
272+
if err != nil {
273+
return err
274+
}
275+
276+
if err := g.AddBindMount(source, dest, options); err != nil {
257277
return err
258278
}
259279
}
@@ -262,7 +282,8 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
262282
if context.IsSet("prestart") {
263283
preStartHooks := context.StringSlice("prestart")
264284
for _, hook := range preStartHooks {
265-
if err := g.AddPreStartHook(hook); err != nil {
285+
path, args := parseHook(hook)
286+
if err := g.AddPreStartHook(path, args); err != nil {
266287
return err
267288
}
268289
}
@@ -271,7 +292,8 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
271292
if context.IsSet("poststop") {
272293
postStopHooks := context.StringSlice("poststop")
273294
for _, hook := range postStopHooks {
274-
if err := g.AddPostStopHook(hook); err != nil {
295+
path, args := parseHook(hook)
296+
if err := g.AddPostStopHook(path, args); err != nil {
275297
return err
276298
}
277299
}
@@ -280,7 +302,8 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
280302
if context.IsSet("poststart") {
281303
postStartHooks := context.StringSlice("poststart")
282304
for _, hook := range postStartHooks {
283-
if err := g.AddPostStartHook(hook); err != nil {
305+
path, args := parseHook(hook)
306+
if err := g.AddPostStartHook(path, args); err != nil {
284307
return err
285308
}
286309
}
@@ -294,13 +317,23 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
294317
}
295318

296319
for _, uidMap := range uidMaps {
297-
if err := g.AddLinuxUIDMapping(uidMap); err != nil {
320+
hid, cid, size, err := parseIDMapping(uidMap)
321+
if err != nil {
322+
return err
323+
}
324+
325+
if err := g.AddLinuxUIDMapping(hid, cid, size); err != nil {
298326
return err
299327
}
300328
}
301329

302330
for _, gidMap := range gidMaps {
303-
if err := g.AddLinuxGIDMapping(gidMap); err != nil {
331+
hid, cid, size, err := parseIDMapping(gidMap)
332+
if err != nil {
333+
return err
334+
}
335+
336+
if err := g.AddLinuxGIDMapping(hid, cid, size); err != nil {
304337
return err
305338
}
306339
}
@@ -386,3 +419,73 @@ func setupLinuxNamespaces(g *generate.Generator, needsNewUser bool, nsMaps map[s
386419
g.AddOrReplaceLinuxNamespace(nsName, nsPath)
387420
}
388421
}
422+
423+
func parseIDMapping(idms string) (uint32, uint32, uint32, error) {
424+
idm := strings.Split(idms, ":")
425+
if len(idm) != 3 {
426+
return 0, 0, 0, fmt.Errorf("idmappings error: %s", idms)
427+
}
428+
429+
hid, err := strconv.Atoi(idm[0])
430+
if err != nil {
431+
return 0, 0, 0, err
432+
}
433+
434+
cid, err := strconv.Atoi(idm[1])
435+
if err != nil {
436+
return 0, 0, 0, err
437+
}
438+
439+
size, err := strconv.Atoi(idm[2])
440+
if err != nil {
441+
return 0, 0, 0, err
442+
}
443+
444+
return uint32(hid), uint32(cid), uint32(size), nil
445+
}
446+
447+
func parseHook(s string) (string, []string) {
448+
parts := strings.Split(s, ":")
449+
args := []string{}
450+
path := parts[0]
451+
if len(parts) > 1 {
452+
args = parts[1:]
453+
}
454+
return path, args
455+
}
456+
457+
func parseTmpfsMount(s string) (string, []string, error) {
458+
var dest string
459+
var options []string
460+
var err error
461+
462+
parts := strings.Split(s, ":")
463+
if len(parts) == 2 {
464+
dest = parts[0]
465+
options = strings.Split(parts[1], ",")
466+
} else if len(parts) == 1 {
467+
dest = parts[0]
468+
options = []string{"rw", "noexec", "nosuid", "nodev", "size=65536k"}
469+
} else {
470+
err = fmt.Errorf("invalid value for --tmpfs")
471+
}
472+
473+
return dest, options, err
474+
}
475+
476+
func parseBindMount(s string) (string, string, string, error) {
477+
var source, dest string
478+
options := "ro"
479+
480+
bparts := strings.SplitN(s, ":", 3)
481+
switch len(bparts) {
482+
case 2:
483+
source, dest = bparts[0], bparts[1]
484+
case 3:
485+
source, dest, options = bparts[0], bparts[1], bparts[2]
486+
default:
487+
return source, dest, options, fmt.Errorf("--bind should have format src:dest:[options]")
488+
}
489+
490+
return source, dest, options, nil
491+
}

0 commit comments

Comments
 (0)