Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add rich container mode #767

Merged
30 changes: 29 additions & 1 deletion apis/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1078,7 +1078,21 @@ definitions:
type: "array"
items:
type: "string"

Rich:
type: "boolean"
description: "Whether to start container in rich container mode. (default false)"
x-nullable: false
RichMode:
type: "string"
description: "Choose one rich container mode.(default dumb-init)"
enum:
- "dumb-init"
- "sbin-init"
- "systemd"
InitScript:
type: "string"
description: "Initial script executed in container. The script will be executed before entrypoint or command"

ContainerCreateResp:
description: "response returned by daemon when container create successfully"
type: "object"
Expand Down Expand Up @@ -1291,6 +1305,20 @@ definitions:
description: "Whether to enable lxcfs."
type: "boolean"
x-nullable: false
Rich:
type: "boolean"
description: "Whether to start container in rich container mode. (default false)"
x-nullable: false
RichMode:
type: "string"
description: "Choose one rich container mode.(default dumb-init)"
enum:
- "dumb-init"
- "sbin-init"
- "systemd"
InitScript:
type: "string"
description: "Initial script executed in container. The script will be executed before entrypoint or command"
- $ref: "#/definitions/Resources"

Resources:
Expand Down
63 changes: 63 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.

71 changes: 71 additions & 0 deletions apis/types/host_config.go

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

4 changes: 4 additions & 0 deletions cli/common_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,9 @@ func addCommonFlags(flagSet *pflag.FlagSet) *container {

flagSet.StringVarP(&c.workdir, "workdir", "w", "", "Set the working directory in a container")

flagSet.BoolVar(&c.rich, "rich", false, "Start container in rich container mode. (default false)")
flagSet.StringVar(&c.richMode, "rich-mode", "", "Choose one rich container mode. dumb-init(default), systemd, sbin-init")
flagSet.StringVar(&c.initScript, "initscript", "", "Initial script executed in container")

return c
}
8 changes: 8 additions & 0 deletions cli/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ type container struct {
blkioDeviceReadIOps ThrottleIOpsDevice
blkioDeviceWriteIOps ThrottleIOpsDevice
IntelRdtL3Cbm string

//add for rich container mode
rich bool
richMode string
initScript string
}

func (c *container) config() (*types.ContainerCreateConfig, error) {
Expand Down Expand Up @@ -135,6 +140,9 @@ func (c *container) config() (*types.ContainerCreateConfig, error) {
User: c.user,
Hostname: strfmt.Hostname(c.hostname),
Labels: labels,
Rich: c.rich,
RichMode: c.richMode,
InitScript: c.initScript,
},

HostConfig: &types.HostConfig{
Expand Down
53 changes: 53 additions & 0 deletions daemon/mgr/container_rich_mode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package mgr

import (
"fmt"

"github.com/alibaba/pouch/apis/types"
)

const (
richModeEnv = "rich_mode=true"
richModeLaunchEnv = "rich_mode_launch_manner"

//for ali internal
interRichModeEnv = "ali_run_mode=common_vm"
)

func richContainerModeEnv(c *ContainerMeta) []string {
var (
ret = []string{}
setRichMode = false
richMode = ""
)

envs := c.Config.Env

//if set inter_rich_mode_env, you can also run in rich container mode
for _, e := range envs {
if e == interRichModeEnv {
setRichMode = true
richMode = types.ContainerConfigRichModeSystemd
break
}
}

if c.Config.Rich {
setRichMode = true
}

if c.Config.RichMode != "" {
richMode = c.Config.RichMode
}

//if not set rich mode manner, default set dumb-init
if richMode == "" {
richMode = types.ContainerConfigRichModeDumbInit
}

if setRichMode {
ret = append(ret, richModeEnv, fmt.Sprintf("%s=%s", richModeLaunchEnv, richMode))
}

return ret
}