Skip to content

Commit

Permalink
use cri annotations to support runtime parameters
Browse files Browse the repository at this point in the history
Signed-off-by: Starnop <starnop@163.com>
  • Loading branch information
starnop committed Jun 25, 2018
1 parent a8b169c commit c84d56a
Show file tree
Hide file tree
Showing 12 changed files with 148 additions and 55 deletions.
3 changes: 3 additions & 0 deletions apis/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1964,6 +1964,9 @@ definitions:
QuotaID:
type: "string"
description: "set disk quota by specified quota id, if id < 0, it means pouchd alloc a unique quota id"
ContainerID:
type: "string"
description: "The ID of the container"

ContainerCreateResp:
description: "response returned by daemon when container create successfully"
Expand Down
5 changes: 5 additions & 0 deletions apis/types/container_config.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 17 additions & 6 deletions cri/v1alpha1/cri.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

apitypes "github.com/alibaba/pouch/apis/types"
anno "github.com/alibaba/pouch/daemon/annotations"
"github.com/alibaba/pouch/daemon/config"
"github.com/alibaba/pouch/daemon/mgr"
"github.com/alibaba/pouch/pkg/errtypes"
Expand Down Expand Up @@ -161,19 +162,23 @@ func (c *CriManager) RunPodSandbox(ctx context.Context, r *runtime.RunPodSandbox
return nil, err
}

id, err := c.ContainerMgr.GenerateID()
if err != nil {
return nil, err
}

// Step 2: Create the sandbox container.
createConfig, err := makeSandboxPouchConfig(config, image)
createConfig, err := makeSandboxPouchConfig(config, id, image)
if err != nil {
return nil, fmt.Errorf("failed to make sandbox pouch config for pod %q: %v", config.Metadata.Name, err)
}

sandboxName := makeSandboxName(config)

createResp, err := c.ContainerMgr.Create(ctx, sandboxName, createConfig)
_, err = c.ContainerMgr.Create(ctx, sandboxName, createConfig)
if err != nil {
return nil, fmt.Errorf("failed to create a sandbox for pod %q: %v", config.Metadata.Name, err)
}
id := createResp.ID
defer func() {
// If running sandbox failed, clean up the container.
if retErr != nil {
Expand Down Expand Up @@ -459,6 +464,11 @@ func (c *CriManager) CreateContainer(ctx context.Context, r *runtime.CreateConta
if iSpec := config.GetImage(); iSpec != nil {
image = iSpec.Image
}

specAnnotation := make(map[string]string)
specAnnotation[anno.ContainerType] = anno.ContainerTypeSandbox
specAnnotation[anno.SandboxID] = podSandboxID

createConfig := &apitypes.ContainerCreateConfig{
ContainerConfig: apitypes.ContainerConfig{
Entrypoint: config.Command,
Expand All @@ -468,9 +478,10 @@ func (c *CriManager) CreateContainer(ctx context.Context, r *runtime.CreateConta
WorkingDir: config.WorkingDir,
Labels: labels,
// Interactive containers:
OpenStdin: config.Stdin,
StdinOnce: config.StdinOnce,
Tty: config.Tty,
OpenStdin: config.Stdin,
StdinOnce: config.StdinOnce,
Tty: config.Tty,
SpecAnnotation: specAnnotation,
},
HostConfig: &apitypes.HostConfig{
Binds: generateMountBindings(config.GetMounts()),
Expand Down
26 changes: 22 additions & 4 deletions cri/v1alpha1/cri_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

apitypes "github.com/alibaba/pouch/apis/types"
anno "github.com/alibaba/pouch/daemon/annotations"
"github.com/alibaba/pouch/daemon/mgr"
"github.com/alibaba/pouch/pkg/utils"
"github.com/go-openapi/strfmt"
Expand Down Expand Up @@ -235,18 +236,30 @@ func applySandboxLinuxOptions(hc *apitypes.HostConfig, lc *runtime.LinuxPodSandb
}

// makeSandboxPouchConfig returns apitypes.ContainerCreateConfig based on runtimeapi.PodSandboxConfig.
func makeSandboxPouchConfig(config *runtime.PodSandboxConfig, image string) (*apitypes.ContainerCreateConfig, error) {
func makeSandboxPouchConfig(config *runtime.PodSandboxConfig, sandboxID string, image string) (*apitypes.ContainerCreateConfig, error) {
// Merge annotations and labels because pouch supports only labels.
labels := makeLabels(config.GetLabels(), config.GetAnnotations())
// Apply a label to distinguish sandboxes from regular containers.
labels[containerTypeLabelKey] = containerTypeLabelSandbox

hc := &apitypes.HostConfig{}

// Apply runtime options.
if annotations := config.GetAnnotations(); annotations != nil {
hc.Runtime = annotations[anno.KubernetesRuntime]
}

specAnnotation := make(map[string]string)
specAnnotation[anno.ContainerType] = anno.ContainerTypeSandbox
specAnnotation[anno.SandboxID] = sandboxID

createConfig := &apitypes.ContainerCreateConfig{
ContainerConfig: apitypes.ContainerConfig{
Hostname: strfmt.Hostname(config.Hostname),
Image: image,
Labels: labels,
Hostname: strfmt.Hostname(config.Hostname),
Image: image,
Labels: labels,
ContainerID: sandboxID,
SpecAnnotation: specAnnotation,
},
HostConfig: hc,
NetworkingConfig: &apitypes.NetworkingConfig{},
Expand Down Expand Up @@ -607,6 +620,11 @@ func applyContainerSecurityContext(lc *runtime.LinuxContainerConfig, podSandboxI

// Apply Linux-specific options if applicable.
func (c *CriManager) updateCreateConfig(createConfig *apitypes.ContainerCreateConfig, config *runtime.ContainerConfig, sandboxConfig *runtime.PodSandboxConfig, podSandboxID string) error {
// Apply runtime options.
if annotations := config.GetAnnotations(); annotations != nil {
createConfig.HostConfig.Runtime = annotations[anno.KubernetesRuntime]
}

if lc := config.GetLinux(); lc != nil {
// TODO: resource restriction.

Expand Down
29 changes: 15 additions & 14 deletions cri/v1alpha1/cri_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,20 +272,21 @@ func Test_parseSandboxName(t *testing.T) {

func Test_makeSandboxPouchConfig(t *testing.T) {
type args struct {
config *runtime.PodSandboxConfig
image string
config *runtime.PodSandboxConfig
sandboxID string
image string
}
tests := []struct {
name string
args args
want *apitypes.ContainerCreateConfig
wantErr bool
}{
// TODO: Add test cases.
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := makeSandboxPouchConfig(tt.args.config, tt.args.image)
got, err := makeSandboxPouchConfig(tt.args.config, tt.args.sandboxID, tt.args.image)
if (err != nil) != tt.wantErr {
t.Errorf("makeSandboxPouchConfig() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down Expand Up @@ -341,7 +342,7 @@ func Test_toCriSandbox(t *testing.T) {
want *runtime.PodSandbox
wantErr bool
}{
// TODO: Add test cases.
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -557,7 +558,7 @@ func Test_makeContainerName(t *testing.T) {
args args
want string
}{
// TODO: Add test cases.
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -578,7 +579,7 @@ func Test_modifyContainerNamespaceOptions(t *testing.T) {
name string
args args
}{
// TODO: Add test cases.
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -599,7 +600,7 @@ func Test_applyContainerSecurityContext(t *testing.T) {
args args
wantErr bool
}{
// TODO: Add test cases.
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -627,7 +628,7 @@ func TestCriManager_updateCreateConfig(t *testing.T) {
args args
wantErr bool
}{
// TODO: Add test cases.
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -652,7 +653,7 @@ func Test_toCriContainer(t *testing.T) {
want *runtime.Container
wantErr bool
}{
// TODO: Add test cases.
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -679,7 +680,7 @@ func Test_imageToCriImage(t *testing.T) {
want *runtime.Image
wantErr bool
}{
// TODO: Add test cases.
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -710,7 +711,7 @@ func TestCriManager_ensureSandboxImageExists(t *testing.T) {
args args
wantErr bool
}{
// TODO: Add test cases.
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -735,7 +736,7 @@ func Test_getUserFromImageUser(t *testing.T) {
want *int64
want1 string
}{
// TODO: Add test cases.
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -759,7 +760,7 @@ func Test_parseUserFromImageUser(t *testing.T) {
args args
want string
}{
// TODO: Add test cases.
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
23 changes: 17 additions & 6 deletions cri/v1alpha2/cri.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

apitypes "github.com/alibaba/pouch/apis/types"
anno "github.com/alibaba/pouch/daemon/annotations"
"github.com/alibaba/pouch/daemon/config"
"github.com/alibaba/pouch/daemon/mgr"
"github.com/alibaba/pouch/pkg/errtypes"
Expand Down Expand Up @@ -161,19 +162,23 @@ func (c *CriManager) RunPodSandbox(ctx context.Context, r *runtime.RunPodSandbox
return nil, err
}

id, err := c.ContainerMgr.GenerateID()
if err != nil {
return nil, err
}

// Step 2: Create the sandbox container.
createConfig, err := makeSandboxPouchConfig(config, image)
createConfig, err := makeSandboxPouchConfig(config, id, image)
if err != nil {
return nil, fmt.Errorf("failed to make sandbox pouch config for pod %q: %v", config.Metadata.Name, err)
}

sandboxName := makeSandboxName(config)

createResp, err := c.ContainerMgr.Create(ctx, sandboxName, createConfig)
_, err = c.ContainerMgr.Create(ctx, sandboxName, createConfig)
if err != nil {
return nil, fmt.Errorf("failed to create a sandbox for pod %q: %v", config.Metadata.Name, err)
}
id := createResp.ID
defer func() {
// If running sandbox failed, clean up the container.
if retErr != nil {
Expand Down Expand Up @@ -467,6 +472,11 @@ func (c *CriManager) CreateContainer(ctx context.Context, r *runtime.CreateConta
if iSpec := config.GetImage(); iSpec != nil {
image = iSpec.Image
}

specAnnotation := make(map[string]string)
specAnnotation[anno.ContainerType] = anno.ContainerTypeSandbox
specAnnotation[anno.SandboxID] = podSandboxID

createConfig := &apitypes.ContainerCreateConfig{
ContainerConfig: apitypes.ContainerConfig{
Entrypoint: config.Command,
Expand All @@ -476,9 +486,10 @@ func (c *CriManager) CreateContainer(ctx context.Context, r *runtime.CreateConta
WorkingDir: config.WorkingDir,
Labels: labels,
// Interactive containers:
OpenStdin: config.Stdin,
StdinOnce: config.StdinOnce,
Tty: config.Tty,
OpenStdin: config.Stdin,
StdinOnce: config.StdinOnce,
Tty: config.Tty,
SpecAnnotation: specAnnotation,
},
HostConfig: &apitypes.HostConfig{
Binds: generateMountBindings(config.GetMounts()),
Expand Down
27 changes: 22 additions & 5 deletions cri/v1alpha2/cri_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

apitypes "github.com/alibaba/pouch/apis/types"
anno "github.com/alibaba/pouch/daemon/annotations"
"github.com/alibaba/pouch/daemon/mgr"
"github.com/alibaba/pouch/pkg/utils"

Expand Down Expand Up @@ -235,18 +236,29 @@ func applySandboxLinuxOptions(hc *apitypes.HostConfig, lc *runtime.LinuxPodSandb
}

// makeSandboxPouchConfig returns apitypes.ContainerCreateConfig based on runtime.PodSandboxConfig.
func makeSandboxPouchConfig(config *runtime.PodSandboxConfig, image string) (*apitypes.ContainerCreateConfig, error) {
func makeSandboxPouchConfig(config *runtime.PodSandboxConfig, sandboxID string, image string) (*apitypes.ContainerCreateConfig, error) {
// Merge annotations and labels because pouch supports only labels.
labels := makeLabels(config.GetLabels(), config.GetAnnotations())
// Apply a label to distinguish sandboxes from regular containers.
labels[containerTypeLabelKey] = containerTypeLabelSandbox

hc := &apitypes.HostConfig{}

// Apply runtime options.
if annotations := config.GetAnnotations(); annotations != nil {
hc.Runtime = annotations[anno.KubernetesRuntime]
}

specAnnotation := make(map[string]string)
specAnnotation[anno.ContainerType] = anno.ContainerTypeSandbox
specAnnotation[anno.SandboxID] = sandboxID

createConfig := &apitypes.ContainerCreateConfig{
ContainerConfig: apitypes.ContainerConfig{
Hostname: strfmt.Hostname(config.Hostname),
Image: image,
Labels: labels,
Hostname: strfmt.Hostname(config.Hostname),
Image: image,
Labels: labels,
ContainerID: sandboxID,
SpecAnnotation: specAnnotation,
},
HostConfig: hc,
NetworkingConfig: &apitypes.NetworkingConfig{},
Expand Down Expand Up @@ -610,6 +622,11 @@ func applyContainerSecurityContext(lc *runtime.LinuxContainerConfig, podSandboxI

// Apply Linux-specific options if applicable.
func (c *CriManager) updateCreateConfig(createConfig *apitypes.ContainerCreateConfig, config *runtime.ContainerConfig, sandboxConfig *runtime.PodSandboxConfig, podSandboxID string) error {
// Apply runtime options.
if annotations := config.GetAnnotations(); annotations != nil {
createConfig.HostConfig.Runtime = annotations[anno.KubernetesRuntime]
}

if lc := config.GetLinux(); lc != nil {
// TODO: resource restriction.

Expand Down
Loading

0 comments on commit c84d56a

Please sign in to comment.