Skip to content

Commit a0f3373

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

File tree

12 files changed

+126
-33
lines changed

12 files changed

+126
-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/v1alpha1/cri.go

Lines changed: 17 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/daemon/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 {
@@ -459,6 +464,11 @@ func (c *CriManager) CreateContainer(ctx context.Context, r *runtime.CreateConta
459464
if iSpec := config.GetImage(); iSpec != nil {
460465
image = iSpec.Image
461466
}
467+
468+
specAnnotation := make(map[string]string)
469+
specAnnotation[anno.ContainerType] = anno.ContainerTypeSandbox
470+
specAnnotation[anno.SandboxID] = podSandboxID
471+
462472
createConfig := &apitypes.ContainerCreateConfig{
463473
ContainerConfig: apitypes.ContainerConfig{
464474
Entrypoint: config.Command,
@@ -468,9 +478,10 @@ func (c *CriManager) CreateContainer(ctx context.Context, r *runtime.CreateConta
468478
WorkingDir: config.WorkingDir,
469479
Labels: labels,
470480
// Interactive containers:
471-
OpenStdin: config.Stdin,
472-
StdinOnce: config.StdinOnce,
473-
Tty: config.Tty,
481+
OpenStdin: config.Stdin,
482+
StdinOnce: config.StdinOnce,
483+
Tty: config.Tty,
484+
SpecAnnotation: specAnnotation,
474485
},
475486
HostConfig: &apitypes.HostConfig{
476487
Binds: generateMountBindings(config.GetMounts()),

cri/v1alpha1/cri_utils.go

Lines changed: 22 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/daemon/annotations"
1516
"github.com/alibaba/pouch/daemon/mgr"
1617
"github.com/alibaba/pouch/pkg/utils"
1718
"github.com/go-openapi/strfmt"
@@ -235,18 +236,30 @@ 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 string, 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+
252+
specAnnotation := make(map[string]string)
253+
specAnnotation[anno.ContainerType] = anno.ContainerTypeSandbox
254+
specAnnotation[anno.SandboxID] = sandboxID
255+
245256
createConfig := &apitypes.ContainerCreateConfig{
246257
ContainerConfig: apitypes.ContainerConfig{
247-
Hostname: strfmt.Hostname(config.Hostname),
248-
Image: image,
249-
Labels: labels,
258+
Hostname: strfmt.Hostname(config.Hostname),
259+
Image: image,
260+
Labels: labels,
261+
ContainerID: sandboxID,
262+
SpecAnnotation: specAnnotation,
250263
},
251264
HostConfig: hc,
252265
NetworkingConfig: &apitypes.NetworkingConfig{},
@@ -607,6 +620,11 @@ func applyContainerSecurityContext(lc *runtime.LinuxContainerConfig, podSandboxI
607620

608621
// Apply Linux-specific options if applicable.
609622
func (c *CriManager) updateCreateConfig(createConfig *apitypes.ContainerCreateConfig, config *runtime.ContainerConfig, sandboxConfig *runtime.PodSandboxConfig, podSandboxID string) error {
623+
// Apply runtime options.
624+
if annotations := config.GetAnnotations(); annotations != nil {
625+
createConfig.HostConfig.Runtime = annotations[anno.KubernetesRuntime]
626+
}
627+
610628
if lc := config.GetLinux(); lc != nil {
611629
// TODO: resource restriction.
612630

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: 17 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/daemon/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 {
@@ -467,6 +472,11 @@ func (c *CriManager) CreateContainer(ctx context.Context, r *runtime.CreateConta
467472
if iSpec := config.GetImage(); iSpec != nil {
468473
image = iSpec.Image
469474
}
475+
476+
specAnnotation := make(map[string]string)
477+
specAnnotation[anno.ContainerType] = anno.ContainerTypeSandbox
478+
specAnnotation[anno.SandboxID] = podSandboxID
479+
470480
createConfig := &apitypes.ContainerCreateConfig{
471481
ContainerConfig: apitypes.ContainerConfig{
472482
Entrypoint: config.Command,
@@ -476,9 +486,10 @@ func (c *CriManager) CreateContainer(ctx context.Context, r *runtime.CreateConta
476486
WorkingDir: config.WorkingDir,
477487
Labels: labels,
478488
// Interactive containers:
479-
OpenStdin: config.Stdin,
480-
StdinOnce: config.StdinOnce,
481-
Tty: config.Tty,
489+
OpenStdin: config.Stdin,
490+
StdinOnce: config.StdinOnce,
491+
Tty: config.Tty,
492+
SpecAnnotation: specAnnotation,
482493
},
483494
HostConfig: &apitypes.HostConfig{
484495
Binds: generateMountBindings(config.GetMounts()),

cri/v1alpha2/cri_utils.go

Lines changed: 22 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/daemon/annotations"
1516
"github.com/alibaba/pouch/daemon/mgr"
1617
"github.com/alibaba/pouch/pkg/utils"
1718

@@ -235,18 +236,29 @@ 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 string, 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+
251+
specAnnotation := make(map[string]string)
252+
specAnnotation[anno.ContainerType] = anno.ContainerTypeSandbox
253+
specAnnotation[anno.SandboxID] = sandboxID
254+
245255
createConfig := &apitypes.ContainerCreateConfig{
246256
ContainerConfig: apitypes.ContainerConfig{
247-
Hostname: strfmt.Hostname(config.Hostname),
248-
Image: image,
249-
Labels: labels,
257+
Hostname: strfmt.Hostname(config.Hostname),
258+
Image: image,
259+
Labels: labels,
260+
ContainerID: sandboxID,
261+
SpecAnnotation: specAnnotation,
250262
},
251263
HostConfig: hc,
252264
NetworkingConfig: &apitypes.NetworkingConfig{},
@@ -610,6 +622,11 @@ func applyContainerSecurityContext(lc *runtime.LinuxContainerConfig, podSandboxI
610622

611623
// Apply Linux-specific options if applicable.
612624
func (c *CriManager) updateCreateConfig(createConfig *apitypes.ContainerCreateConfig, config *runtime.ContainerConfig, sandboxConfig *runtime.PodSandboxConfig, podSandboxID string) error {
625+
// Apply runtime options.
626+
if annotations := config.GetAnnotations(); annotations != nil {
627+
createConfig.HostConfig.Runtime = annotations[anno.KubernetesRuntime]
628+
}
629+
613630
if lc := config.GetLinux(); lc != nil {
614631
// TODO: resource restriction.
615632

cri/v1alpha2/cri_utils_test.go

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

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

daemon/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.container-type"
13+
14+
// SandboxID is the sandbox ID annotation
15+
SandboxID = "io.kubernetes.cri.sandbox-id"
16+
17+
// KubernetesRuntime is the runtime
18+
KubernetesRuntime = "io.kubernetes.runtime"
19+
)

daemon/mgr/container.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ type ContainerMgr interface {
129129

130130
// Logs is used to return log created by the container.
131131
Logs(ctx context.Context, name string, logsOpt *types.ContainerLogsOptions) (<-chan *logger.LogMessage, bool, error)
132+
133+
// GenerateID generates an ID for newly created container. We must ensure that
134+
// this ID has not used yet.
135+
GenerateID() (string, error)
132136
}
133137

134138
// ContainerManager is the default implement of interface ContainerMgr.
@@ -248,9 +252,12 @@ func (mgr *ContainerManager) Create(ctx context.Context, name string, config *ty
248252
return nil, fmt.Errorf("NetworkingConfig cannot be nil")
249253
}
250254

251-
id, err := mgr.generateID()
252-
if err != nil {
253-
return nil, err
255+
id := ""
256+
if id = config.ContainerID; id == "" {
257+
id, err = mgr.GenerateID()
258+
if err != nil {
259+
return nil, err
260+
}
254261
}
255262

256263
if name == "" {

0 commit comments

Comments
 (0)