From 8b3597685026916f761aff33dae60162f744a8d7 Mon Sep 17 00:00:00 2001 From: Abel Feng Date: Mon, 5 Jun 2023 21:01:31 +0800 Subject: [PATCH] sandbox: add Sandboxer field to sandbox metadata Signed-off-by: Abel Feng --- metadata/buckets.go | 2 ++ metadata/sandbox.go | 10 ++++++++++ metadata/sandbox_test.go | 13 +++++++++---- sandbox/helpers.go | 2 ++ sandbox/store.go | 2 ++ 5 files changed, 25 insertions(+), 4 deletions(-) diff --git a/metadata/buckets.go b/metadata/buckets.go index e5254f0a6000..9ccfaee83ef5 100644 --- a/metadata/buckets.go +++ b/metadata/buckets.go @@ -114,6 +114,7 @@ │ ├──createdat : - Created at │ ├──updatedat : - Updated at │ ├──spec : - Proto marshaled spec + │ ├──sandboxer : - Sandboxer name │ ├──runtime │ │ ├──name : - Runtime name │ │ └──options : - Proto marshaled options @@ -173,6 +174,7 @@ var ( bucketKeyRef = []byte("ref") bucketKeyExpireAt = []byte("expireat") bucketKeySandboxID = []byte("sandboxid") + bucketKeySandboxer = []byte("sandboxer") deprecatedBucketKeyObjectIngest = []byte("ingest") // stores ingest links, deprecated in v1.2 ) diff --git a/metadata/sandbox.go b/metadata/sandbox.go index 81bee2b75d14..542a1b3c0cb3 100644 --- a/metadata/sandbox.go +++ b/metadata/sandbox.go @@ -292,6 +292,10 @@ func (s *sandboxStore) write(parent *bbolt.Bucket, instance *api.Sandbox, overwr return err } + if err := bucket.Put(bucketKeySandboxer, []byte(instance.Sandboxer)); err != nil { + return err + } + runtimeBucket, err := bucket.CreateBucketIfNotExists(bucketKeyRuntime) if err != nil { return err @@ -350,6 +354,12 @@ func (s *sandboxStore) read(parent *bbolt.Bucket, id []byte) (api.Sandbox, error if err != nil { return api.Sandbox{}, err } + sandboxer := bucket.Get(bucketKeySandboxer) + if sandboxer == nil { + inst.Sandboxer = "" + } else { + inst.Sandboxer = string(sandboxer) + } return inst, nil } diff --git a/metadata/sandbox_test.go b/metadata/sandbox_test.go index 711fb0c13abf..84870890819f 100644 --- a/metadata/sandbox_test.go +++ b/metadata/sandbox_test.go @@ -43,6 +43,7 @@ func TestSandboxCreate(t *testing.T) { Name: "test", Options: &types.Any{TypeUrl: "url/3", Value: []byte{4, 5, 6}}, }, + Sandboxer: "test-sandboxer", } _, err := store.Create(ctx, in) @@ -91,7 +92,8 @@ func TestSandboxUpdate(t *testing.T) { Extensions: map[string]typeurl.Any{ "ext2": &types.Any{TypeUrl: "url2", Value: []byte{4, 5, 6}}, // will append `ext1` }, - Runtime: api.RuntimeOpts{Name: "test"}, // no change + Runtime: api.RuntimeOpts{Name: "test"}, // no change + Sandboxer: "test-sandboxer", // no change }); err != nil { t.Fatal(err) } @@ -120,7 +122,8 @@ func TestSandboxUpdate(t *testing.T) { "ext1": &types.Any{TypeUrl: "url1", Value: []byte{1, 2}}, "ext2": &types.Any{TypeUrl: "url2", Value: []byte{4, 5, 6}}, }, - Runtime: api.RuntimeOpts{Name: "test"}, + Runtime: api.RuntimeOpts{Name: "test"}, + Sandboxer: "test-sandboxer", } assertEqualInstances(t, out, expected) @@ -158,7 +161,8 @@ func TestSandboxList(t *testing.T) { TypeUrl: "test", Value: []byte{9}, }}, - Runtime: api.RuntimeOpts{Name: "test"}, + Runtime: api.RuntimeOpts{Name: "test"}, + Sandboxer: "test-sandboxer", }, } @@ -203,7 +207,8 @@ func TestSandboxListWithFilter(t *testing.T) { TypeUrl: "test", Value: []byte{9}, }}, - Runtime: api.RuntimeOpts{Name: "test"}, + Runtime: api.RuntimeOpts{Name: "test"}, + Sandboxer: "test-sandboxer", }, } diff --git a/sandbox/helpers.go b/sandbox/helpers.go index bfe0b23d33df..09667adb6365 100644 --- a/sandbox/helpers.go +++ b/sandbox/helpers.go @@ -35,6 +35,7 @@ func ToProto(sandbox *Sandbox) *types.Sandbox { Name: sandbox.Runtime.Name, Options: protobuf.FromAny(sandbox.Runtime.Options), }, + Sandboxer: sandbox.Sandboxer, Labels: sandbox.Labels, CreatedAt: protobuf.ToTimestamp(sandbox.CreatedAt), UpdatedAt: protobuf.ToTimestamp(sandbox.UpdatedAt), @@ -61,6 +62,7 @@ func FromProto(sandboxpb *types.Sandbox) Sandbox { Labels: sandboxpb.Labels, Runtime: runtime, Spec: sandboxpb.Spec, + Sandboxer: sandboxpb.Sandboxer, CreatedAt: protobuf.FromTimestamp(sandboxpb.CreatedAt), UpdatedAt: protobuf.FromTimestamp(sandboxpb.UpdatedAt), Extensions: extensions, diff --git a/sandbox/store.go b/sandbox/store.go index cda646dde279..6cffafb9b3c7 100644 --- a/sandbox/store.go +++ b/sandbox/store.go @@ -35,6 +35,8 @@ type Sandbox struct { Runtime RuntimeOpts // Spec carries the runtime specification used to implement the sandbox Spec typeurl.Any + // Sandboxer is the sandbox controller who manages the sandbox + Sandboxer string // CreatedAt is the time at which the sandbox was created CreatedAt time.Time // UpdatedAt is the time at which the sandbox was updated