Skip to content

Commit 0df1f26

Browse files
committed
use cri annotations to support runtime parameters
Signed-off-by: Starnop <[email protected]>
1 parent 7caca29 commit 0df1f26

File tree

7 files changed

+87
-7
lines changed

7 files changed

+87
-7
lines changed

cri/annotations/annotations.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package annotations
2+
3+
// ContainerType values
4+
const (
5+
// ContainerTypeSandbox represents a pod sandbox container
6+
ContainerTypeSandbox = "sandbox"
7+
8+
// ContainerTypeContainer represents a container running within a pod
9+
ContainerTypeContainer = "container"
10+
11+
// ContainerType is the container type (sandbox or container) annotation
12+
ContainerType = "io.kubernetes.cri-o.ContainerType"
13+
14+
// SandboxName is the sandbox name annotation
15+
SandboxName = "io.kubernetes.cri-o.SandboxName"
16+
17+
// KubernetesRuntime is the runtime
18+
KubernetesRuntime = "io.kubernetes.runtime"
19+
)

cri/v1alpha1/cri.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"time"
1313

1414
apitypes "github.com/alibaba/pouch/apis/types"
15+
anno "github.com/alibaba/pouch/cri/annotations"
1516
"github.com/alibaba/pouch/daemon/config"
1617
"github.com/alibaba/pouch/daemon/mgr"
1718
"github.com/alibaba/pouch/pkg/errtypes"
@@ -236,6 +237,7 @@ func (c *CriManager) RunPodSandbox(ctx context.Context, r *runtime.RunPodSandbox
236237
ID: id,
237238
Config: config,
238239
NetNSPath: netnsPath,
240+
Runtime: config.Annotations[anno.KubernetesRuntime],
239241
}
240242
c.SandboxStore.Put(sandboxMeta)
241243

@@ -462,6 +464,11 @@ func (c *CriManager) CreateContainer(ctx context.Context, r *runtime.CreateConta
462464
if iSpec := config.GetImage(); iSpec != nil {
463465
image = iSpec.Image
464466
}
467+
468+
specAnnotation := make(map[string]string)
469+
specAnnotation[anno.ContainerType] = anno.ContainerTypeContainer
470+
specAnnotation[anno.SandboxName] = podSandboxID
471+
465472
createConfig := &apitypes.ContainerCreateConfig{
466473
ContainerConfig: apitypes.ContainerConfig{
467474
Entrypoint: config.Command,
@@ -471,9 +478,10 @@ func (c *CriManager) CreateContainer(ctx context.Context, r *runtime.CreateConta
471478
WorkingDir: config.WorkingDir,
472479
Labels: labels,
473480
// Interactive containers:
474-
OpenStdin: config.Stdin,
475-
StdinOnce: config.StdinOnce,
476-
Tty: config.Tty,
481+
OpenStdin: config.Stdin,
482+
StdinOnce: config.StdinOnce,
483+
Tty: config.Tty,
484+
SpecAnnotation: specAnnotation,
477485
},
478486
HostConfig: &apitypes.HostConfig{
479487
Binds: generateMountBindings(config.GetMounts()),

cri/v1alpha1/cri_types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ type SandboxMeta struct {
1414

1515
// NetNSPath is the network namespace used by the sandbox.
1616
NetNSPath string
17+
18+
// Runtime is the runtime of sandbox
19+
Runtime string
1720
}
1821

1922
// Key returns sandbox's id.

cri/v1alpha1/cri_utils.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"time"
1313

1414
apitypes "github.com/alibaba/pouch/apis/types"
15+
anno "github.com/alibaba/pouch/cri/annotations"
1516
"github.com/alibaba/pouch/daemon/mgr"
1617
"github.com/alibaba/pouch/pkg/utils"
1718
"github.com/go-openapi/strfmt"
@@ -242,6 +243,12 @@ func makeSandboxPouchConfig(config *runtime.PodSandboxConfig, image string) (*ap
242243
labels[containerTypeLabelKey] = containerTypeLabelSandbox
243244

244245
hc := &apitypes.HostConfig{}
246+
247+
// Apply runtime options.
248+
if annotations := config.GetAnnotations(); annotations != nil {
249+
hc.Runtime = annotations[anno.KubernetesRuntime]
250+
}
251+
245252
createConfig := &apitypes.ContainerCreateConfig{
246253
ContainerConfig: apitypes.ContainerConfig{
247254
Hostname: strfmt.Hostname(config.Hostname),
@@ -607,6 +614,19 @@ func applyContainerSecurityContext(lc *runtime.LinuxContainerConfig, podSandboxI
607614

608615
// Apply Linux-specific options if applicable.
609616
func (c *CriManager) updateCreateConfig(createConfig *apitypes.ContainerCreateConfig, config *runtime.ContainerConfig, sandboxConfig *runtime.PodSandboxConfig, podSandboxID string) error {
617+
// Apply runtime options.
618+
if annotations := config.GetAnnotations(); annotations != nil {
619+
createConfig.HostConfig.Runtime = annotations[anno.KubernetesRuntime]
620+
}
621+
res, err := c.SandboxStore.Get(podSandboxID)
622+
if err != nil {
623+
return fmt.Errorf("failed to get metadata of %q from SandboxStore: %v", podSandboxID, err)
624+
}
625+
sandboxMeta := res.(*SandboxMeta)
626+
if sandboxMeta.Runtime != "" {
627+
createConfig.HostConfig.Runtime = sandboxMeta.Runtime
628+
}
629+
610630
if lc := config.GetLinux(); lc != nil {
611631
// TODO: resource restriction.
612632

cri/v1alpha2/cri.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"time"
1313

1414
apitypes "github.com/alibaba/pouch/apis/types"
15+
anno "github.com/alibaba/pouch/cri/annotations"
1516
"github.com/alibaba/pouch/daemon/config"
1617
"github.com/alibaba/pouch/daemon/mgr"
1718
"github.com/alibaba/pouch/pkg/errtypes"
@@ -236,6 +237,7 @@ func (c *CriManager) RunPodSandbox(ctx context.Context, r *runtime.RunPodSandbox
236237
ID: id,
237238
Config: config,
238239
NetNSPath: netnsPath,
240+
Runtime: config.Annotations[anno.KubernetesRuntime],
239241
}
240242
c.SandboxStore.Put(sandboxMeta)
241243

@@ -470,6 +472,11 @@ func (c *CriManager) CreateContainer(ctx context.Context, r *runtime.CreateConta
470472
if iSpec := config.GetImage(); iSpec != nil {
471473
image = iSpec.Image
472474
}
475+
476+
specAnnotation := make(map[string]string)
477+
specAnnotation[anno.ContainerType] = anno.ContainerTypeContainer
478+
specAnnotation[anno.SandboxName] = podSandboxID
479+
473480
createConfig := &apitypes.ContainerCreateConfig{
474481
ContainerConfig: apitypes.ContainerConfig{
475482
Entrypoint: config.Command,
@@ -479,9 +486,10 @@ func (c *CriManager) CreateContainer(ctx context.Context, r *runtime.CreateConta
479486
WorkingDir: config.WorkingDir,
480487
Labels: labels,
481488
// Interactive containers:
482-
OpenStdin: config.Stdin,
483-
StdinOnce: config.StdinOnce,
484-
Tty: config.Tty,
489+
OpenStdin: config.Stdin,
490+
StdinOnce: config.StdinOnce,
491+
Tty: config.Tty,
492+
SpecAnnotation: specAnnotation,
485493
},
486494
HostConfig: &apitypes.HostConfig{
487495
Binds: generateMountBindings(config.GetMounts()),

cri/v1alpha2/cri_types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ type SandboxMeta struct {
1414

1515
// NetNSPath is the network namespace used by the sandbox.
1616
NetNSPath string
17+
18+
// Runtime is the runtime of sandbox
19+
Runtime string
1720
}
1821

1922
// Key returns sandbox's id.

cri/v1alpha2/cri_utils.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"time"
1313

1414
apitypes "github.com/alibaba/pouch/apis/types"
15+
anno "github.com/alibaba/pouch/cri/annotations"
1516
"github.com/alibaba/pouch/daemon/mgr"
1617
"github.com/alibaba/pouch/pkg/utils"
1718

@@ -240,8 +241,13 @@ func makeSandboxPouchConfig(config *runtime.PodSandboxConfig, image string) (*ap
240241
labels := makeLabels(config.GetLabels(), config.GetAnnotations())
241242
// Apply a label to distinguish sandboxes from regular containers.
242243
labels[containerTypeLabelKey] = containerTypeLabelSandbox
243-
244244
hc := &apitypes.HostConfig{}
245+
246+
// Apply runtime options.
247+
if annotations := config.GetAnnotations(); annotations != nil {
248+
hc.Runtime = annotations[anno.KubernetesRuntime]
249+
}
250+
245251
createConfig := &apitypes.ContainerCreateConfig{
246252
ContainerConfig: apitypes.ContainerConfig{
247253
Hostname: strfmt.Hostname(config.Hostname),
@@ -610,6 +616,19 @@ func applyContainerSecurityContext(lc *runtime.LinuxContainerConfig, podSandboxI
610616

611617
// Apply Linux-specific options if applicable.
612618
func (c *CriManager) updateCreateConfig(createConfig *apitypes.ContainerCreateConfig, config *runtime.ContainerConfig, sandboxConfig *runtime.PodSandboxConfig, podSandboxID string) error {
619+
// Apply runtime options.
620+
if annotations := config.GetAnnotations(); annotations != nil {
621+
createConfig.HostConfig.Runtime = annotations[anno.KubernetesRuntime]
622+
}
623+
res, err := c.SandboxStore.Get(podSandboxID)
624+
if err != nil {
625+
return fmt.Errorf("failed to get metadata of %q from SandboxStore: %v", podSandboxID, err)
626+
}
627+
sandboxMeta := res.(*SandboxMeta)
628+
if sandboxMeta.Runtime != "" {
629+
createConfig.HostConfig.Runtime = sandboxMeta.Runtime
630+
}
631+
613632
if lc := config.GetLinux(); lc != nil {
614633
// TODO: resource restriction.
615634

0 commit comments

Comments
 (0)