|
| 1 | +// Copyright 2016 The Linux Foundation |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package image |
| 16 | + |
| 17 | +import ( |
| 18 | + "bytes" |
| 19 | + "encoding/json" |
| 20 | + "fmt" |
| 21 | + "io" |
| 22 | + "io/ioutil" |
| 23 | + "os" |
| 24 | + "path/filepath" |
| 25 | + "strconv" |
| 26 | + "strings" |
| 27 | + |
| 28 | + "github.com/opencontainers/image-spec/schema" |
| 29 | + "github.com/opencontainers/runtime-spec/specs-go" |
| 30 | + "github.com/pkg/errors" |
| 31 | +) |
| 32 | + |
| 33 | +type cfg struct { |
| 34 | + User string |
| 35 | + Memory int64 |
| 36 | + MemorySwap int64 |
| 37 | + CPUShares int64 `json:"CpuShares"` |
| 38 | + ExposedPorts map[string]struct{} |
| 39 | + Env []string |
| 40 | + Entrypoint []string |
| 41 | + Cmd []string |
| 42 | + Volumes map[string]struct{} |
| 43 | + WorkingDir string |
| 44 | +} |
| 45 | + |
| 46 | +type config struct { |
| 47 | + Architecture string `json:"architecture"` |
| 48 | + OS string `json:"os"` |
| 49 | + Config cfg `json:"config"` |
| 50 | +} |
| 51 | + |
| 52 | +func findConfig(w walker, d *descriptor) (*config, error) { |
| 53 | + var c config |
| 54 | + cpath := filepath.Join("blobs", d.Digest) |
| 55 | + |
| 56 | + f := func(path string, info os.FileInfo, r io.Reader) error { |
| 57 | + if info.IsDir() { |
| 58 | + return nil |
| 59 | + } |
| 60 | + |
| 61 | + if filepath.Clean(path) != cpath { |
| 62 | + return nil |
| 63 | + } |
| 64 | + |
| 65 | + buf, err := ioutil.ReadAll(r) |
| 66 | + if err != nil { |
| 67 | + return errors.Wrapf(err, "%s: error reading config", path) |
| 68 | + } |
| 69 | + |
| 70 | + if err := schema.MediaTypeImageSerializationConfig.Validate(bytes.NewReader(buf)); err != nil { |
| 71 | + return errors.Wrapf(err, "%s: config validation failed", path) |
| 72 | + } |
| 73 | + |
| 74 | + if err := json.Unmarshal(buf, &c); err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + |
| 78 | + return errEOW |
| 79 | + } |
| 80 | + |
| 81 | + switch err := w.walk(f); err { |
| 82 | + case nil: |
| 83 | + return nil, fmt.Errorf("%s: config not found", cpath) |
| 84 | + case errEOW: |
| 85 | + // found, continue below |
| 86 | + default: |
| 87 | + return nil, err |
| 88 | + } |
| 89 | + |
| 90 | + return &c, nil |
| 91 | +} |
| 92 | + |
| 93 | +func (c *config) runtimeSpec(rootfs string) (*specs.Spec, error) { |
| 94 | + var s specs.Spec |
| 95 | + s.Version = "0.5.0" |
| 96 | + s.Root.Path = rootfs |
| 97 | + s.Process.Cwd = c.Config.WorkingDir |
| 98 | + s.Process.Env = append([]string(nil), c.Config.Env...) |
| 99 | + s.Process.Args = append([]string(nil), c.Config.Entrypoint...) |
| 100 | + s.Process.Args = append(s.Process.Args, c.Config.Cmd...) |
| 101 | + |
| 102 | + if uid, err := strconv.Atoi(c.Config.User); err == nil { |
| 103 | + s.Process.User.UID = uint32(uid) |
| 104 | + } else if ug := strings.Split(c.Config.User, ":"); len(ug) == 2 { |
| 105 | + uid, err := strconv.Atoi(ug[0]) |
| 106 | + if err != nil { |
| 107 | + return nil, errors.New("config.User: unsupported uid format") |
| 108 | + } |
| 109 | + |
| 110 | + gid, err := strconv.Atoi(ug[1]) |
| 111 | + if err != nil { |
| 112 | + return nil, errors.New("config.User: unsupported gid format") |
| 113 | + } |
| 114 | + |
| 115 | + s.Process.User.UID = uint32(uid) |
| 116 | + s.Process.User.GID = uint32(gid) |
| 117 | + } else { |
| 118 | + return nil, errors.New("config.User: unsupported format") |
| 119 | + } |
| 120 | + |
| 121 | + s.Platform.OS = c.OS |
| 122 | + s.Platform.Arch = c.Architecture |
| 123 | + |
| 124 | + if c.OS == "linux" { |
| 125 | + mem := uint64(c.Config.Memory) |
| 126 | + swap := uint64(c.Config.MemorySwap) |
| 127 | + shares := uint64(c.Config.CPUShares) |
| 128 | + |
| 129 | + s.Linux.Resources = &specs.Resources{ |
| 130 | + CPU: &specs.CPU{ |
| 131 | + Shares: &shares, |
| 132 | + }, |
| 133 | + |
| 134 | + Memory: &specs.Memory{ |
| 135 | + Limit: &mem, |
| 136 | + Reservation: &mem, |
| 137 | + Swap: &swap, |
| 138 | + }, |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + for vol := range c.Config.Volumes { |
| 143 | + s.Mounts = append( |
| 144 | + s.Mounts, |
| 145 | + specs.Mount{ |
| 146 | + Destination: vol, |
| 147 | + Type: "bind", |
| 148 | + Options: []string{"rbind"}, |
| 149 | + }, |
| 150 | + ) |
| 151 | + } |
| 152 | + |
| 153 | + return &s, nil |
| 154 | +} |
0 commit comments