mirror of
https://github.com/containerd/containerd.git
synced 2026-08-09 01:21:15 +00:00
Fix extensions API and update tests
Signed-off-by: Maksym Pavlenko <pavlenko.maksym@gmail.com>
This commit is contained in:
committed by
Mike Brown
parent
58022a748a
commit
0f55bdd49c
@@ -44,18 +44,17 @@ func (p *BootstrapParams) AddExtension(msg proto.Message) error {
|
||||
}
|
||||
|
||||
// FindExtension finds an extension matching the type of dst and unmarshals it.
|
||||
// Returns true if found, false if not found.
|
||||
func (p *BootstrapParams) FindExtension(dst proto.Message) error {
|
||||
func (p *BootstrapParams) FindExtension(dst proto.Message) (bool, error) {
|
||||
name := dst.ProtoReflect().Descriptor().FullName()
|
||||
|
||||
for _, ext := range p.Extensions {
|
||||
if ext.GetValue().MessageIs(dst) {
|
||||
if err := ext.GetValue().UnmarshalTo(dst); err != nil {
|
||||
return fmt.Errorf("failed to unmarshal extension %q: %w", name, err)
|
||||
return false, fmt.Errorf("failed to unmarshal extension %q: %w", name, err)
|
||||
}
|
||||
return nil
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
return false, nil
|
||||
}
|
||||
|
||||
@@ -21,43 +21,47 @@ import (
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
options "github.com/containerd/containerd/api/types/runc/options"
|
||||
"google.golang.org/protobuf/types/known/anypb"
|
||||
)
|
||||
|
||||
func TestExtensions(t *testing.T) {
|
||||
params := &BootstrapParams{}
|
||||
|
||||
err := params.AddExtension(&RuncV2Extensions{SpecAnnotations: map[string]string{"test": "value"}})
|
||||
err := params.AddExtension(&options.Options{ShimCgroup: "test-cgroup"})
|
||||
require.NoError(t, err)
|
||||
|
||||
got := &RuncV2Extensions{}
|
||||
err = params.FindExtension(got)
|
||||
got := &options.Options{}
|
||||
found, err := params.FindExtension(got)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "value", got.SpecAnnotations["test"])
|
||||
assert.True(t, found)
|
||||
assert.Equal(t, "test-cgroup", got.ShimCgroup)
|
||||
}
|
||||
|
||||
func TestExtensionNotFound(t *testing.T) {
|
||||
params := &BootstrapParams{}
|
||||
|
||||
got := &RuncV2Extensions{}
|
||||
err := params.FindExtension(got)
|
||||
got := &options.Options{}
|
||||
found, err := params.FindExtension(got)
|
||||
require.NoError(t, err)
|
||||
assert.False(t, found)
|
||||
}
|
||||
|
||||
func TestAddExtensionWithAny(t *testing.T) {
|
||||
params := &BootstrapParams{}
|
||||
|
||||
ext := &RuncV2Extensions{SpecAnnotations: map[string]string{"test": "annotation"}}
|
||||
ext := &options.Options{ShimCgroup: "test-cgroup"}
|
||||
anyVal, err := anypb.New(ext)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = params.AddExtension(anyVal)
|
||||
require.NoError(t, err)
|
||||
|
||||
got := &RuncV2Extensions{}
|
||||
err = params.FindExtension(got)
|
||||
got := &options.Options{}
|
||||
found, err := params.FindExtension(got)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "annotation", got.SpecAnnotations["test"])
|
||||
assert.True(t, found)
|
||||
assert.Equal(t, "test-cgroup", got.ShimCgroup)
|
||||
|
||||
assert.Contains(t, params.Extensions[0].Value.TypeUrl, "RuncV2Extensions")
|
||||
assert.Contains(t, params.Extensions[0].Value.TypeUrl, "Options")
|
||||
}
|
||||
|
||||
@@ -263,26 +263,26 @@ func (manager) Start(ctx context.Context, opts *shim.BootstrapParams) (_ *shim.B
|
||||
go cmd.Wait()
|
||||
|
||||
var runcOpts options.Options
|
||||
if err := opts.FindExtension(&runcOpts); err != nil {
|
||||
if found, err := opts.FindExtension(&runcOpts); err != nil {
|
||||
return nil, fmt.Errorf("failed to fetch runc options: %w", err)
|
||||
}
|
||||
|
||||
if shimCgroup := runcOpts.GetShimCgroup(); shimCgroup != "" {
|
||||
if cgroups.Mode() == cgroups.Unified {
|
||||
cg, err := cgroupsv2.Load(shimCgroup)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to load cgroup %s: %w", shimCgroup, err)
|
||||
}
|
||||
if err := cg.AddProc(uint64(cmd.Process.Pid)); err != nil {
|
||||
return nil, fmt.Errorf("failed to join cgroup %s: %w", shimCgroup, err)
|
||||
}
|
||||
} else {
|
||||
cg, err := cgroup1.Load(cgroup1.StaticPath(shimCgroup))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to load cgroup %s: %w", shimCgroup, err)
|
||||
}
|
||||
if err := cg.AddProc(uint64(cmd.Process.Pid)); err != nil {
|
||||
return nil, fmt.Errorf("failed to join cgroup %s: %w", shimCgroup, err)
|
||||
} else if found {
|
||||
if shimCgroup := runcOpts.GetShimCgroup(); shimCgroup != "" {
|
||||
if cgroups.Mode() == cgroups.Unified {
|
||||
cg, err := cgroupsv2.Load(shimCgroup)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to load cgroup %s: %w", shimCgroup, err)
|
||||
}
|
||||
if err := cg.AddProc(uint64(cmd.Process.Pid)); err != nil {
|
||||
return nil, fmt.Errorf("failed to join cgroup %s: %w", shimCgroup, err)
|
||||
}
|
||||
} else {
|
||||
cg, err := cgroup1.Load(cgroup1.StaticPath(shimCgroup))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to load cgroup %s: %w", shimCgroup, err)
|
||||
}
|
||||
if err := cg.AddProc(uint64(cmd.Process.Pid)); err != nil {
|
||||
return nil, fmt.Errorf("failed to join cgroup %s: %w", shimCgroup, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
9
vendor/github.com/containerd/containerd/api/runtime/boot/v1/helpers.go
generated
vendored
9
vendor/github.com/containerd/containerd/api/runtime/boot/v1/helpers.go
generated
vendored
@@ -44,18 +44,17 @@ func (p *BootstrapParams) AddExtension(msg proto.Message) error {
|
||||
}
|
||||
|
||||
// FindExtension finds an extension matching the type of dst and unmarshals it.
|
||||
// Returns true if found, false if not found.
|
||||
func (p *BootstrapParams) FindExtension(dst proto.Message) error {
|
||||
func (p *BootstrapParams) FindExtension(dst proto.Message) (bool, error) {
|
||||
name := dst.ProtoReflect().Descriptor().FullName()
|
||||
|
||||
for _, ext := range p.Extensions {
|
||||
if ext.GetValue().MessageIs(dst) {
|
||||
if err := ext.GetValue().UnmarshalTo(dst); err != nil {
|
||||
return fmt.Errorf("failed to unmarshal extension %q: %w", name, err)
|
||||
return false, fmt.Errorf("failed to unmarshal extension %q: %w", name, err)
|
||||
}
|
||||
return nil
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
return false, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user