Skip to content

Commit ea44901

Browse files
committed
metadata: expand container runtime into bucket
Signed-off-by: Stephen J Day <stephen.day@docker.com>
1 parent 6fbe4bd commit ea44901

File tree

3 files changed

+49
-43
lines changed

3 files changed

+49
-43
lines changed

containers/containers.go

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package containers
22

33
import (
4-
"bytes"
54
"context"
6-
"encoding/gob"
7-
"errors"
85
"time"
96
)
107

@@ -28,39 +25,6 @@ type RuntimeInfo struct {
2825
Options map[string]string
2926
}
3027

31-
type marshaledRuntimeInfo struct {
32-
Name string
33-
Options map[string]string
34-
}
35-
36-
func (r *RuntimeInfo) MarshalBinary() ([]byte, error) {
37-
buf := bytes.NewBuffer(nil)
38-
if err := gob.NewEncoder(buf).Encode(marshaledRuntimeInfo{
39-
Name: r.Name,
40-
Options: r.Options,
41-
}); err != nil {
42-
return nil, err
43-
}
44-
return buf.Bytes(), nil
45-
}
46-
47-
func (r *RuntimeInfo) UnmarshalBinary(data []byte) error {
48-
buf := data
49-
if len(buf) == 0 {
50-
return errors.New("RuntimeInfo: no data")
51-
}
52-
var (
53-
mr marshaledRuntimeInfo
54-
reader = bytes.NewReader(buf)
55-
)
56-
if err := gob.NewDecoder(reader).Decode(&mr); err != nil {
57-
return err
58-
}
59-
r.Name = mr.Name
60-
r.Options = mr.Options
61-
return nil
62-
}
63-
6428
type Store interface {
6529
Get(ctx context.Context, id string) (Container, error)
6630
List(ctx context.Context, filter string) ([]Container, error)

metadata/buckets.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ var (
3939
bucketKeyLabels = []byte("labels")
4040
bucketKeyImage = []byte("image")
4141
bucketKeyRuntime = []byte("runtime")
42+
bucketKeyName = []byte("name")
43+
bucketKeyOptions = []byte("options")
4244
bucketKeySpec = []byte("spec")
4345
bucketKeyRootFS = []byte("rootfs")
4446
bucketKeyCreatedAt = []byte("createdat")

metadata/containers.go

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,26 @@ func readContainer(container *containers.Container, bkt *bolt.Bucket) error {
146146
case string(bucketKeyImage):
147147
container.Image = string(v)
148148
case string(bucketKeyRuntime):
149-
if err := container.Runtime.UnmarshalBinary(v); err != nil {
150-
return err
149+
rbkt := bkt.Bucket(bucketKeyRuntime)
150+
if rbkt == nil {
151+
return nil // skip runtime. should be an error?
152+
}
153+
154+
n := rbkt.Get(bucketKeyName)
155+
if n != nil {
156+
container.Runtime.Name = string(n)
157+
}
158+
159+
obkt := rbkt.Bucket(bucketKeyOptions)
160+
if obkt == nil {
161+
return nil
151162
}
163+
164+
container.Runtime.Options = map[string]string{}
165+
return obkt.ForEach(func(k, v []byte) error {
166+
container.Runtime.Options[string(k)] = string(v)
167+
return nil
168+
})
152169
case string(bucketKeySpec):
153170
container.Spec = make([]byte, len(v))
154171
copy(container.Spec, v)
@@ -189,13 +206,9 @@ func writeContainer(container *containers.Container, bkt *bolt.Bucket) error {
189206
if err != nil {
190207
return err
191208
}
192-
runtime, err := container.Runtime.MarshalBinary()
193-
if err != nil {
194-
return err
195-
}
209+
196210
for _, v := range [][2][]byte{
197211
{bucketKeyImage, []byte(container.Image)},
198-
{bucketKeyRuntime, runtime},
199212
{bucketKeySpec, container.Spec},
200213
{bucketKeyRootFS, []byte(container.RootFS)},
201214
{bucketKeyCreatedAt, createdAt},
@@ -205,6 +218,33 @@ func writeContainer(container *containers.Container, bkt *bolt.Bucket) error {
205218
return err
206219
}
207220
}
221+
222+
if rbkt := bkt.Bucket(bucketKeyRuntime); rbkt != nil {
223+
if err := bkt.DeleteBucket(bucketKeyRuntime); err != nil {
224+
return err
225+
}
226+
}
227+
228+
rbkt, err := bkt.CreateBucket(bucketKeyRuntime)
229+
if err != nil {
230+
return err
231+
}
232+
233+
if err := rbkt.Put(bucketKeyName, []byte(container.Runtime.Name)); err != nil {
234+
return err
235+
}
236+
237+
obkt, err := rbkt.CreateBucket(bucketKeyOptions)
238+
if err != nil {
239+
return err
240+
}
241+
242+
for k, v := range container.Runtime.Options {
243+
if err := obkt.Put([]byte(k), []byte(v)); err != nil {
244+
return err
245+
}
246+
}
247+
208248
// Remove existing labels to keep from merging
209249
if lbkt := bkt.Bucket(bucketKeyLabels); lbkt != nil {
210250
if err := bkt.DeleteBucket(bucketKeyLabels); err != nil {

0 commit comments

Comments
 (0)