Add sandbox store helpers
Signed-off-by: Maksym Pavlenko <pavlenko.maksym@gmail.com>
This commit is contained in:
parent
7b55b23a82
commit
e47c433d57
@ -369,9 +369,5 @@ func (s *sandboxStore) validate(new *api.Sandbox) error {
|
|||||||
return fmt.Errorf("updated date must not be zero: %w", errdefs.ErrInvalidArgument)
|
return fmt.Errorf("updated date must not be zero: %w", errdefs.ErrInvalidArgument)
|
||||||
}
|
}
|
||||||
|
|
||||||
if new.Runtime.Name == "" {
|
|
||||||
return fmt.Errorf("sandbox.Runtime.Name must be set: %w", errdefs.ErrInvalidArgument)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -18,8 +18,10 @@ package sandbox
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/containerd/containerd/errdefs"
|
||||||
"github.com/containerd/typeurl"
|
"github.com/containerd/typeurl"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -64,3 +66,51 @@ type Store interface {
|
|||||||
// Delete a sandbox from metadata store using the id
|
// Delete a sandbox from metadata store using the id
|
||||||
Delete(ctx context.Context, id string) error
|
Delete(ctx context.Context, id string) error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AddExtension is a helper function to add sandbox metadata extension.
|
||||||
|
func (s *Sandbox) AddExtension(name string, obj interface{}) error {
|
||||||
|
if s.Extensions == nil {
|
||||||
|
s.Extensions = map[string]typeurl.Any{}
|
||||||
|
}
|
||||||
|
|
||||||
|
out, err := typeurl.MarshalAny(obj)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to marshal sandbox extension %q: %w", name, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
s.Extensions[name] = out
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddLabel adds a label to sandbox's labels.
|
||||||
|
func (s *Sandbox) AddLabel(name string, value string) {
|
||||||
|
if s.Labels == nil {
|
||||||
|
s.Labels = map[string]string{}
|
||||||
|
}
|
||||||
|
|
||||||
|
s.Labels[name] = value
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetExtension retrieves a sandbox extension by name.
|
||||||
|
func (s *Sandbox) GetExtension(name string, obj interface{}) error {
|
||||||
|
out, ok := s.Extensions[name]
|
||||||
|
if !ok {
|
||||||
|
return errdefs.ErrNotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := typeurl.UnmarshalTo(out, obj); err != nil {
|
||||||
|
return fmt.Errorf("failed to unmarshal sandbox extension %q: %w", name, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetLabel retrieves a sandbox label by name.
|
||||||
|
func (s *Sandbox) GetLabel(name string) (string, error) {
|
||||||
|
out, ok := s.Labels[name]
|
||||||
|
if !ok {
|
||||||
|
return "", fmt.Errorf("unable to find label %q in sandbox metadata: %w", name, errdefs.ErrNotFound)
|
||||||
|
}
|
||||||
|
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
40
sandbox/store_test.go
Normal file
40
sandbox/store_test.go
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
/*
|
||||||
|
Copyright The containerd Authors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package sandbox
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/containerd/typeurl"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestAddExtension(t *testing.T) {
|
||||||
|
sb := Sandbox{ID: "1"}
|
||||||
|
|
||||||
|
type test struct{ Name string }
|
||||||
|
|
||||||
|
typeurl.Register(&test{})
|
||||||
|
|
||||||
|
var in = test{Name: "test"}
|
||||||
|
assert.NoError(t, sb.AddExtension("test", &in))
|
||||||
|
|
||||||
|
var out test
|
||||||
|
err := sb.GetExtension("test", &out)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, "test", out.Name)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user