Add basic spec and mounts for Darwin

Signed-off-by: Maksym Pavlenko <pavlenko.maksym@gmail.com>
This commit is contained in:
Maksym Pavlenko
2023-01-12 17:00:40 -08:00
parent a43d719ce2
commit 1ade777c24
7 changed files with 201 additions and 32 deletions

View File

@@ -353,7 +353,7 @@ func (c *criService) volumeMounts(containerRootDir string, criMounts []*runtime.
}
// runtimeSpec returns a default runtime spec used in cri-containerd.
func (c *criService) runtimeSpec(id string, baseSpecFile string, opts ...oci.SpecOpts) (*runtimespec.Spec, error) {
func (c *criService) runtimeSpec(id string, platform platforms.Platform, baseSpecFile string, opts ...oci.SpecOpts) (*runtimespec.Spec, error) {
// GenerateSpec needs namespace.
ctx := ctrdutil.NamespacedContext()
container := &containers.Container{ID: id}
@@ -379,7 +379,7 @@ func (c *criService) runtimeSpec(id string, baseSpecFile string, opts ...oci.Spe
return &spec, nil
}
spec, err := oci.GenerateSpec(ctx, nil, container, opts...)
spec, err := oci.GenerateSpecWithPlatform(ctx, nil, platforms.Format(platform), container, opts...)
if err != nil {
return nil, fmt.Errorf("failed to generate spec: %w", err)
}
@@ -421,6 +421,10 @@ func (c *criService) buildContainerSpec(
ociRuntime config.Runtime,
) (_ *runtimespec.Spec, retErr error) {
var (
specOpts []oci.SpecOpts
err error
// Platform helpers
isLinux = platform.OS == "linux"
isWindows = platform.OS == "windows"
isDarwin = platform.OS == "darwin"
@@ -428,7 +432,7 @@ func (c *criService) buildContainerSpec(
switch {
case isLinux:
return c.buildLinuxSpec(
specOpts, err = c.buildLinuxSpec(
id,
sandboxID,
sandboxPid,
@@ -442,7 +446,7 @@ func (c *criService) buildContainerSpec(
ociRuntime,
)
case isWindows:
return c.buildWindowsSpec(
specOpts, err = c.buildWindowsSpec(
id,
sandboxID,
sandboxPid,
@@ -456,7 +460,7 @@ func (c *criService) buildContainerSpec(
ociRuntime,
)
case isDarwin:
return c.buildDarwinSpec(
specOpts, err = c.buildDarwinSpec(
id,
sandboxID,
containerName,
@@ -470,6 +474,12 @@ func (c *criService) buildContainerSpec(
default:
return nil, fmt.Errorf("unsupported spec platform: %s", platform.OS)
}
if err != nil {
return nil, fmt.Errorf("failed to generate spec opts: %w", err)
}
return c.runtimeSpec(id, platform, ociRuntime.BaseRuntimeSpec, specOpts...)
}
func (c *criService) buildLinuxSpec(
@@ -484,7 +494,7 @@ func (c *criService) buildLinuxSpec(
imageConfig *imagespec.ImageConfig,
extraMounts []*runtime.Mount,
ociRuntime config.Runtime,
) (_ *runtimespec.Spec, retErr error) {
) (_ []oci.SpecOpts, retErr error) {
specOpts := []oci.SpecOpts{
oci.WithoutRunMount,
}
@@ -704,7 +714,7 @@ func (c *criService) buildLinuxSpec(
specOpts = append(specOpts, oci.WithLinuxNamespace(runtimespec.LinuxNamespace{Type: runtimespec.CgroupNamespace}))
}
return c.runtimeSpec(id, ociRuntime.BaseRuntimeSpec, specOpts...)
return specOpts, nil
}
func (c *criService) buildWindowsSpec(
@@ -719,7 +729,7 @@ func (c *criService) buildWindowsSpec(
imageConfig *imagespec.ImageConfig,
extraMounts []*runtime.Mount,
ociRuntime config.Runtime,
) (_ *runtimespec.Spec, retErr error) {
) (_ []oci.SpecOpts, retErr error) {
specOpts := []oci.SpecOpts{
customopts.WithProcessArgs(config, imageConfig),
}
@@ -807,7 +817,7 @@ func (c *criService) buildWindowsSpec(
customopts.WithAnnotation(annotations.WindowsHostProcess, strconv.FormatBool(sandboxHpc)),
)
return c.runtimeSpec(id, ociRuntime.BaseRuntimeSpec, specOpts...)
return specOpts, nil
}
func (c *criService) buildDarwinSpec(
@@ -820,7 +830,7 @@ func (c *criService) buildDarwinSpec(
imageConfig *imagespec.ImageConfig,
extraMounts []*runtime.Mount,
ociRuntime config.Runtime,
) (_ *runtimespec.Spec, retErr error) {
) (_ []oci.SpecOpts, retErr error) {
specOpts := []oci.SpecOpts{
customopts.WithProcessArgs(config, imageConfig),
}
@@ -843,6 +853,8 @@ func (c *criService) buildDarwinSpec(
}
specOpts = append(specOpts, oci.WithEnv(env))
specOpts = append(specOpts, customopts.WithDarwinMounts(c.os, config, extraMounts))
for pKey, pValue := range getPassthroughAnnotations(sandboxConfig.Annotations,
ociRuntime.PodAnnotations) {
specOpts = append(specOpts, customopts.WithAnnotation(pKey, pValue))
@@ -863,5 +875,5 @@ func (c *criService) buildDarwinSpec(
customopts.WithAnnotation(annotations.ImageName, imageName),
)
return c.runtimeSpec(id, ociRuntime.BaseRuntimeSpec, specOpts...)
return specOpts, nil
}

View File

@@ -21,11 +21,12 @@ package sbserver
import (
"testing"
"github.com/containerd/containerd/pkg/cri/annotations"
imagespec "github.com/opencontainers/image-spec/specs-go/v1"
runtimespec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/stretchr/testify/assert"
runtime "k8s.io/cri-api/pkg/apis/runtime/v1"
"github.com/containerd/containerd/pkg/cri/annotations"
)
// checkMount is defined by all tests but not used here
@@ -52,6 +53,19 @@ func getCreateContainerTestData() (*runtime.ContainerConfig, *runtime.PodSandbox
},
Labels: map[string]string{"a": "b"},
Annotations: map[string]string{"ca-c": "ca-d"},
Mounts: []*runtime.Mount{
// everything default
{
ContainerPath: "container-path-1",
HostPath: "host-path-1",
},
// readOnly
{
ContainerPath: "container-path-2",
HostPath: "host-path-2",
Readonly: true,
},
},
}
sandboxConfig := &runtime.PodSandboxConfig{
Metadata: &runtime.PodSandboxMetadata{
@@ -69,12 +83,15 @@ func getCreateContainerTestData() (*runtime.ContainerConfig, *runtime.PodSandbox
WorkingDir: "/workspace",
}
specCheck := func(t *testing.T, id string, sandboxID string, sandboxPid uint32, spec *runtimespec.Spec) {
assert.Equal(t, relativeRootfsPath, spec.Root.Path)
assert.Equal(t, []string{"test", "command", "test", "args"}, spec.Process.Args)
assert.Equal(t, "test-cwd", spec.Process.Cwd)
assert.Contains(t, spec.Process.Env, "k1=v1", "k2=v2", "k3=v3=v3bis", "ik4=iv4=iv4bis=boop")
assert.Contains(t, spec.Process.Env, "ik1=iv1", "ik2=iv2", "ik3=iv3=iv3bis", "k4=v4=v4bis=foop")
t.Logf("Check bind mount")
checkMount(t, spec.Mounts, "host-path-1", "container-path-1", "bind", []string{"rw"}, nil)
checkMount(t, spec.Mounts, "host-path-2", "container-path-2", "bind", []string{"ro"}, nil)
t.Logf("Check PodSandbox annotations")
assert.Contains(t, spec.Annotations, annotations.SandboxID)
assert.EqualValues(t, spec.Annotations[annotations.SandboxID], sandboxID)

View File

@@ -418,6 +418,7 @@ func TestBaseRuntimeSpec(t *testing.T) {
out, err := c.runtimeSpec(
"id1",
platforms.DefaultSpec(),
"/etc/containerd/cri-base.json",
oci.WithHostname("new-host"),
oci.WithDomainname("new-domain"),