Skip to content

Commit 301f43a

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

File tree

14 files changed

+140
-33
lines changed

14 files changed

+140
-33
lines changed

apis/swagger.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1964,6 +1964,9 @@ definitions:
19641964
QuotaID:
19651965
type: "string"
19661966
description: "set disk quota by specified quota id, if id < 0, it means pouchd alloc a unique quota id"
1967+
ContainerID:
1968+
type: "string"
1969+
description: "The ID of the container"
19671970

19681971
ContainerCreateResp:
19691972
description: "response returned by daemon when container create successfully"

apis/types/container_config.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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: 18 additions & 6 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"
@@ -161,19 +162,23 @@ func (c *CriManager) RunPodSandbox(ctx context.Context, r *runtime.RunPodSandbox
161162
return nil, err
162163
}
163164

165+
id, err := c.ContainerMgr.GenerateID()
166+
if err != nil {
167+
return nil, err
168+
}
169+
164170
// Step 2: Create the sandbox container.
165-
createConfig, err := makeSandboxPouchConfig(config, image)
171+
createConfig, err := makeSandboxPouchConfig(config, id, image)
166172
if err != nil {
167173
return nil, fmt.Errorf("failed to make sandbox pouch config for pod %q: %v", config.Metadata.Name, err)
168174
}
169175

170176
sandboxName := makeSandboxName(config)
171177

172-
createResp, err := c.ContainerMgr.Create(ctx, sandboxName, createConfig)
178+
_, err = c.ContainerMgr.Create(ctx, sandboxName, createConfig)
173179
if err != nil {
174180
return nil, fmt.Errorf("failed to create a sandbox for pod %q: %v", config.Metadata.Name, err)
175181
}
176-
id := createResp.ID
177182
defer func() {
178183
// If running sandbox failed, clean up the container.
179184
if retErr != nil {
@@ -236,6 +241,7 @@ func (c *CriManager) RunPodSandbox(ctx context.Context, r *runtime.RunPodSandbox
236241
ID: id,
237242
Config: config,
238243
NetNSPath: netnsPath,
244+
Runtime: config.Annotations[anno.KubernetesRuntime],
239245
}
240246
c.SandboxStore.Put(sandboxMeta)
241247

@@ -462,6 +468,11 @@ func (c *CriManager) CreateContainer(ctx context.Context, r *runtime.CreateConta
462468
if iSpec := config.GetImage(); iSpec != nil {
463469
image = iSpec.Image
464470
}
471+
472+
specAnnotation := make(map[string]string)
473+
specAnnotation[anno.ContainerType] = anno.ContainerTypeContainer
474+
specAnnotation[anno.SandboxName] = podSandboxID
475+
465476
createConfig := &apitypes.ContainerCreateConfig{
466477
ContainerConfig: apitypes.ContainerConfig{
467478
Entrypoint: config.Command,
@@ -471,9 +482,10 @@ func (c *CriManager) CreateContainer(ctx context.Context, r *runtime.CreateConta
471482
WorkingDir: config.WorkingDir,
472483
Labels: labels,
473484
// Interactive containers:
474-
OpenStdin: config.Stdin,
475-
StdinOnce: config.StdinOnce,
476-
Tty: config.Tty,
485+
OpenStdin: config.Stdin,
486+
StdinOnce: config.StdinOnce,
487+
Tty: config.Tty,
488+
SpecAnnotation: specAnnotation,
477489
},
478490
HostConfig: &apitypes.HostConfig{
479491
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: 25 additions & 4 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"
@@ -235,18 +236,25 @@ func applySandboxLinuxOptions(hc *apitypes.HostConfig, lc *runtime.LinuxPodSandb
235236
}
236237

237238
// makeSandboxPouchConfig returns apitypes.ContainerCreateConfig based on runtimeapi.PodSandboxConfig.
238-
func makeSandboxPouchConfig(config *runtime.PodSandboxConfig, image string) (*apitypes.ContainerCreateConfig, error) {
239+
func makeSandboxPouchConfig(config *runtime.PodSandboxConfig, sandboxID, image string) (*apitypes.ContainerCreateConfig, error) {
239240
// Merge annotations and labels because pouch supports only labels.
240241
labels := makeLabels(config.GetLabels(), config.GetAnnotations())
241242
// Apply a label to distinguish sandboxes from regular containers.
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{
247-
Hostname: strfmt.Hostname(config.Hostname),
248-
Image: image,
249-
Labels: labels,
254+
Hostname: strfmt.Hostname(config.Hostname),
255+
Image: image,
256+
Labels: labels,
257+
ContainerID: sandboxID,
250258
},
251259
HostConfig: hc,
252260
NetworkingConfig: &apitypes.NetworkingConfig{},
@@ -607,6 +615,19 @@ func applyContainerSecurityContext(lc *runtime.LinuxContainerConfig, podSandboxI
607615

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

cri/v1alpha1/cri_utils_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,9 @@ func Test_parseSandboxName(t *testing.T) {
272272

273273
func Test_makeSandboxPouchConfig(t *testing.T) {
274274
type args struct {
275-
config *runtime.PodSandboxConfig
276-
image string
275+
config *runtime.PodSandboxConfig
276+
sandboxID string
277+
image string
277278
}
278279
tests := []struct {
279280
name string
@@ -285,7 +286,7 @@ func Test_makeSandboxPouchConfig(t *testing.T) {
285286
}
286287
for _, tt := range tests {
287288
t.Run(tt.name, func(t *testing.T) {
288-
got, err := makeSandboxPouchConfig(tt.args.config, tt.args.image)
289+
got, err := makeSandboxPouchConfig(tt.args.config, tt.args.sandboxID, tt.args.image)
289290
if (err != nil) != tt.wantErr {
290291
t.Errorf("makeSandboxPouchConfig() error = %v, wantErr %v", err, tt.wantErr)
291292
return

cri/v1alpha2/cri.go

Lines changed: 18 additions & 6 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"
@@ -161,19 +162,23 @@ func (c *CriManager) RunPodSandbox(ctx context.Context, r *runtime.RunPodSandbox
161162
return nil, err
162163
}
163164

165+
id, err := c.ContainerMgr.GenerateID()
166+
if err != nil {
167+
return nil, err
168+
}
169+
164170
// Step 2: Create the sandbox container.
165-
createConfig, err := makeSandboxPouchConfig(config, image)
171+
createConfig, err := makeSandboxPouchConfig(config, id, image)
166172
if err != nil {
167173
return nil, fmt.Errorf("failed to make sandbox pouch config for pod %q: %v", config.Metadata.Name, err)
168174
}
169175

170176
sandboxName := makeSandboxName(config)
171177

172-
createResp, err := c.ContainerMgr.Create(ctx, sandboxName, createConfig)
178+
_, err = c.ContainerMgr.Create(ctx, sandboxName, createConfig)
173179
if err != nil {
174180
return nil, fmt.Errorf("failed to create a sandbox for pod %q: %v", config.Metadata.Name, err)
175181
}
176-
id := createResp.ID
177182
defer func() {
178183
// If running sandbox failed, clean up the container.
179184
if retErr != nil {
@@ -236,6 +241,7 @@ func (c *CriManager) RunPodSandbox(ctx context.Context, r *runtime.RunPodSandbox
236241
ID: id,
237242
Config: config,
238243
NetNSPath: netnsPath,
244+
Runtime: config.Annotations[anno.KubernetesRuntime],
239245
}
240246
c.SandboxStore.Put(sandboxMeta)
241247

@@ -470,6 +476,11 @@ func (c *CriManager) CreateContainer(ctx context.Context, r *runtime.CreateConta
470476
if iSpec := config.GetImage(); iSpec != nil {
471477
image = iSpec.Image
472478
}
479+
480+
specAnnotation := make(map[string]string)
481+
specAnnotation[anno.ContainerType] = anno.ContainerTypeContainer
482+
specAnnotation[anno.SandboxName] = podSandboxID
483+
473484
createConfig := &apitypes.ContainerCreateConfig{
474485
ContainerConfig: apitypes.ContainerConfig{
475486
Entrypoint: config.Command,
@@ -479,9 +490,10 @@ func (c *CriManager) CreateContainer(ctx context.Context, r *runtime.CreateConta
479490
WorkingDir: config.WorkingDir,
480491
Labels: labels,
481492
// Interactive containers:
482-
OpenStdin: config.Stdin,
483-
StdinOnce: config.StdinOnce,
484-
Tty: config.Tty,
493+
OpenStdin: config.Stdin,
494+
StdinOnce: config.StdinOnce,
495+
Tty: config.Tty,
496+
SpecAnnotation: specAnnotation,
485497
},
486498
HostConfig: &apitypes.HostConfig{
487499
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: 25 additions & 5 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

@@ -235,18 +236,24 @@ func applySandboxLinuxOptions(hc *apitypes.HostConfig, lc *runtime.LinuxPodSandb
235236
}
236237

237238
// makeSandboxPouchConfig returns apitypes.ContainerCreateConfig based on runtime.PodSandboxConfig.
238-
func makeSandboxPouchConfig(config *runtime.PodSandboxConfig, image string) (*apitypes.ContainerCreateConfig, error) {
239+
func makeSandboxPouchConfig(config *runtime.PodSandboxConfig, sandboxID, image string) (*apitypes.ContainerCreateConfig, error) {
239240
// Merge annotations and labels because pouch supports only labels.
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{
247-
Hostname: strfmt.Hostname(config.Hostname),
248-
Image: image,
249-
Labels: labels,
253+
Hostname: strfmt.Hostname(config.Hostname),
254+
Image: image,
255+
Labels: labels,
256+
ContainerID: sandboxID,
250257
},
251258
HostConfig: hc,
252259
NetworkingConfig: &apitypes.NetworkingConfig{},
@@ -610,6 +617,19 @@ func applyContainerSecurityContext(lc *runtime.LinuxContainerConfig, podSandboxI
610617

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

0 commit comments

Comments
 (0)