metadata: expand container runtime into bucket
Signed-off-by: Stephen J Day <stephen.day@docker.com>
This commit is contained in:
parent
6fbe4bd568
commit
ea44901921
@ -1,10 +1,7 @@
|
||||
package containers
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/gob"
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
@ -28,39 +25,6 @@ type RuntimeInfo struct {
|
||||
Options map[string]string
|
||||
}
|
||||
|
||||
type marshaledRuntimeInfo struct {
|
||||
Name string
|
||||
Options map[string]string
|
||||
}
|
||||
|
||||
func (r *RuntimeInfo) MarshalBinary() ([]byte, error) {
|
||||
buf := bytes.NewBuffer(nil)
|
||||
if err := gob.NewEncoder(buf).Encode(marshaledRuntimeInfo{
|
||||
Name: r.Name,
|
||||
Options: r.Options,
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
func (r *RuntimeInfo) UnmarshalBinary(data []byte) error {
|
||||
buf := data
|
||||
if len(buf) == 0 {
|
||||
return errors.New("RuntimeInfo: no data")
|
||||
}
|
||||
var (
|
||||
mr marshaledRuntimeInfo
|
||||
reader = bytes.NewReader(buf)
|
||||
)
|
||||
if err := gob.NewDecoder(reader).Decode(&mr); err != nil {
|
||||
return err
|
||||
}
|
||||
r.Name = mr.Name
|
||||
r.Options = mr.Options
|
||||
return nil
|
||||
}
|
||||
|
||||
type Store interface {
|
||||
Get(ctx context.Context, id string) (Container, error)
|
||||
List(ctx context.Context, filter string) ([]Container, error)
|
||||
|
@ -39,6 +39,8 @@ var (
|
||||
bucketKeyLabels = []byte("labels")
|
||||
bucketKeyImage = []byte("image")
|
||||
bucketKeyRuntime = []byte("runtime")
|
||||
bucketKeyName = []byte("name")
|
||||
bucketKeyOptions = []byte("options")
|
||||
bucketKeySpec = []byte("spec")
|
||||
bucketKeyRootFS = []byte("rootfs")
|
||||
bucketKeyCreatedAt = []byte("createdat")
|
||||
|
@ -146,9 +146,26 @@ func readContainer(container *containers.Container, bkt *bolt.Bucket) error {
|
||||
case string(bucketKeyImage):
|
||||
container.Image = string(v)
|
||||
case string(bucketKeyRuntime):
|
||||
if err := container.Runtime.UnmarshalBinary(v); err != nil {
|
||||
return err
|
||||
rbkt := bkt.Bucket(bucketKeyRuntime)
|
||||
if rbkt == nil {
|
||||
return nil // skip runtime. should be an error?
|
||||
}
|
||||
|
||||
n := rbkt.Get(bucketKeyName)
|
||||
if n != nil {
|
||||
container.Runtime.Name = string(n)
|
||||
}
|
||||
|
||||
obkt := rbkt.Bucket(bucketKeyOptions)
|
||||
if obkt == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
container.Runtime.Options = map[string]string{}
|
||||
return obkt.ForEach(func(k, v []byte) error {
|
||||
container.Runtime.Options[string(k)] = string(v)
|
||||
return nil
|
||||
})
|
||||
case string(bucketKeySpec):
|
||||
container.Spec = make([]byte, len(v))
|
||||
copy(container.Spec, v)
|
||||
@ -189,13 +206,9 @@ func writeContainer(container *containers.Container, bkt *bolt.Bucket) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
runtime, err := container.Runtime.MarshalBinary()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, v := range [][2][]byte{
|
||||
{bucketKeyImage, []byte(container.Image)},
|
||||
{bucketKeyRuntime, runtime},
|
||||
{bucketKeySpec, container.Spec},
|
||||
{bucketKeyRootFS, []byte(container.RootFS)},
|
||||
{bucketKeyCreatedAt, createdAt},
|
||||
@ -205,6 +218,33 @@ func writeContainer(container *containers.Container, bkt *bolt.Bucket) error {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if rbkt := bkt.Bucket(bucketKeyRuntime); rbkt != nil {
|
||||
if err := bkt.DeleteBucket(bucketKeyRuntime); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
rbkt, err := bkt.CreateBucket(bucketKeyRuntime)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := rbkt.Put(bucketKeyName, []byte(container.Runtime.Name)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
obkt, err := rbkt.CreateBucket(bucketKeyOptions)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for k, v := range container.Runtime.Options {
|
||||
if err := obkt.Put([]byte(k), []byte(v)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// Remove existing labels to keep from merging
|
||||
if lbkt := bkt.Bucket(bucketKeyLabels); lbkt != nil {
|
||||
if err := bkt.DeleteBucket(bucketKeyLabels); err != nil {
|
||||
|
Loading…
Reference in New Issue
Block a user