| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package image |
| 16 |
|
| 17 |
import ( |
| 18 |
"bytes" |
| 19 |
"encoding/json" |
| 20 |
"fmt" |
| 21 |
"io" |
| 22 |
"io/ioutil" |
| 23 |
"path/filepath" |
| 24 |
"strconv" |
| 25 |
"strings" |
| 26 |
|
| 27 |
"github.com/opencontainers/image-spec/schema" |
| 28 |
"github.com/opencontainers/image-spec/specs-go/v1" |
| 29 |
"github.com/opencontainers/runtime-spec/specs-go" |
| 30 |
"github.com/pkg/errors" |
| 31 |
) |
| 32 |
|
| 33 |
func findConfig(w walker, d *v1.Descriptor) (*v1.Image, error) { |
| 34 |
var c v1.Image |
| 35 |
cpath := filepath.Join("blobs", string(d.Digest.Algorithm()), d.Digest.Hex()) |
| 36 |
|
| 37 |
switch err := w.find(cpath, func(path string, r io.Reader) error { |
| 38 |
buf, err := ioutil.ReadAll(r) |
| 39 |
if err != nil { |
| 40 |
return errors.Wrapf(err, "%s: error reading config", path) |
| 41 |
} |
| 42 |
|
| 43 |
if err := schema.ValidatorMediaTypeImageConfig.Validate(bytes.NewReader(buf)); err != nil { |
| 44 |
return errors.Wrapf(err, "%s: config validation failed", path) |
| 45 |
} |
| 46 |
|
| 47 |
if err := json.Unmarshal(buf, &c); err != nil { |
| 48 |
return err |
| 49 |
} |
| 50 |
|
| 51 |
return errEOW |
| 52 |
}); err { |
| 53 |
case nil: |
| 54 |
return nil, fmt.Errorf("%s: config not found", cpath) |
| 55 |
case errEOW: |
| 56 |
return &c, nil |
| 57 |
default: |
| 58 |
return nil, err |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
func runtimeSpec(c *v1.Image, rootfs string) (*specs.Spec, error) { |
| 63 |
if c.OS != "linux" { |
| 64 |
return nil, fmt.Errorf("%s: unsupported OS", c.OS) |
| 65 |
} |
| 66 |
|
| 67 |
var s specs.Spec |
| 68 |
s.Version = specs.Version |
| 69 |
|
| 70 |
s.Root = &specs.Root{} |
| 71 |
s.Root.Path = rootfs |
| 72 |
|
| 73 |
s.Process = &specs.Process{} |
| 74 |
s.Process.Terminal = true |
| 75 |
s.Process.Cwd = "/" |
| 76 |
if c.Config.WorkingDir != "" { |
| 77 |
s.Process.Cwd = c.Config.WorkingDir |
| 78 |
} |
| 79 |
s.Process.Env = append(s.Process.Env, c.Config.Env...) |
| 80 |
s.Process.Args = append(s.Process.Args, c.Config.Entrypoint...) |
| 81 |
s.Process.Args = append(s.Process.Args, c.Config.Cmd...) |
| 82 |
|
| 83 |
if len(s.Process.Args) == 0 { |
| 84 |
s.Process.Args = append(s.Process.Args, "sh") |
| 85 |
} |
| 86 |
|
| 87 |
if uid, err := strconv.Atoi(c.Config.User); err == nil { |
| 88 |
s.Process.User.UID = uint32(uid) |
| 89 |
} else if ug := strings.Split(c.Config.User, ":"); len(ug) == 2 { |
| 90 |
uid, err := strconv.Atoi(ug[0]) |
| 91 |
if err != nil { |
| 92 |
return nil, errors.New("config.User: unsupported uid format") |
| 93 |
} |
| 94 |
|
| 95 |
gid, err := strconv.Atoi(ug[1]) |
| 96 |
if err != nil { |
| 97 |
return nil, errors.New("config.User: unsupported gid format") |
| 98 |
} |
| 99 |
|
| 100 |
s.Process.User.UID = uint32(uid) |
| 101 |
s.Process.User.GID = uint32(gid) |
| 102 |
} else if c.Config.User != "" { |
| 103 |
return nil, errors.New("config.User: unsupported format") |
| 104 |
} |
| 105 |
|
| 106 |
s.Linux = &specs.Linux{} |
| 107 |
|
| 108 |
for vol := range c.Config.Volumes { |
| 109 |
s.Mounts = append( |
| 110 |
s.Mounts, |
| 111 |
specs.Mount{ |
| 112 |
Destination: vol, |
| 113 |
Type: "bind", |
| 114 |
Options: []string{"rbind"}, |
| 115 |
}, |
| 116 |
) |
| 117 |
} |
| 118 |
|
| 119 |
return &s, nil |
| 120 |
} |