Skip to content

Commit

Permalink
Closes cri-o#3914 [FEAT] Add new parameter disable_hostport_mapping i…
Browse files Browse the repository at this point in the history
…n CRI-O

Signed-off-by: T K Chandra Hasan <t.k.chandra.hasan@ibm.com>
  • Loading branch information
hasan4791 committed Feb 5, 2023
1 parent 6e861a8 commit 85671dd
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 1 deletion.
1 change: 1 addition & 0 deletions completions/bash/crio
Expand Up @@ -44,6 +44,7 @@ h
--default-transport
--default-ulimits
--device-ownership-from-security-context
--disable-hostport-mapping
--drop-infra-ctr
--enable-criu-support
--enable-metrics
Expand Down
1 change: 1 addition & 0 deletions completions/fish/crio.fish
Expand Up @@ -49,6 +49,7 @@ complete -c crio -n '__fish_crio_no_subcommand' -f -l default-sysctls -r -d 'Sys
complete -c crio -n '__fish_crio_no_subcommand' -f -l default-transport -r -d 'A prefix to prepend to image names that cannot be pulled as-is.'
complete -c crio -n '__fish_crio_no_subcommand' -f -l default-ulimits -r -d 'Ulimits to apply to containers by default (name=soft:hard).'
complete -c crio -n '__fish_crio_no_subcommand' -f -l device-ownership-from-security-context -d 'Set devices\' uid/gid ownership from runAsUser/runAsGroup.'
complete -c crio -n '__fish_crio_no_subcommand' -f -l disable-hostport-mapping -d 'If true, CRI-O would disable the hostport mapping.'
complete -c crio -n '__fish_crio_no_subcommand' -f -l drop-infra-ctr -d 'Determines whether pods are created without an infra container, when the pod is not using a pod level PID namespace.'
complete -c crio -n '__fish_crio_no_subcommand' -f -l enable-criu-support -d 'Enable CRIU integration, requires that the criu binary is available in $PATH.'
complete -c crio -n '__fish_crio_no_subcommand' -f -l enable-metrics -d 'Enable metrics endpoint for the server on localhost:9090.'
Expand Down
1 change: 1 addition & 0 deletions completions/zsh/_crio
Expand Up @@ -51,6 +51,7 @@ it later with **--config**. Global options will modify the output.'
'--default-transport'
'--default-ulimits'
'--device-ownership-from-security-context'
'--disable-hostport-mapping'
'--drop-infra-ctr'
'--enable-criu-support'
'--enable-metrics'
Expand Down
3 changes: 3 additions & 0 deletions docs/crio.8.md
Expand Up @@ -42,6 +42,7 @@ crio
[--default-transport]=[value]
[--default-ulimits]=[value]
[--device-ownership-from-security-context]
[--disable-hostport-mapping]
[--drop-infra-ctr]
[--enable-criu-support]
[--enable-metrics]
Expand Down Expand Up @@ -213,6 +214,8 @@ crio [GLOBAL OPTIONS] command [COMMAND OPTIONS] [ARGUMENTS...]

**--device-ownership-from-security-context**: Set devices' uid/gid ownership from runAsUser/runAsGroup.

**--disable-hostport-mapping**: If true, CRI-O would disable the hostport mapping.

**--drop-infra-ctr**: Determines whether pods are created without an infra container, when the pod is not using a pod level PID namespace.

**--enable-criu-support**: Enable CRIU integration, requires that the criu binary is available in $PATH.
Expand Down
3 changes: 3 additions & 0 deletions docs/crio.conf.5.md
Expand Up @@ -293,6 +293,9 @@ the container runtime configuration.
**enable_pod_events**=false
Enable CRI-O to generate the container pod-level events in order to optimize the performance of the Pod Lifecycle Event Generator (PLEG) module in Kubelet.

**disable_hostport_mapping**=false
Enable/Disable the container hostport mapping in CRI-O. Default value is set to 'false'.

### CRIO.RUNTIME.RUNTIMES TABLE
The "crio.runtime.runtimes" table defines a list of OCI compatible runtimes. The runtime to use is picked based on the runtime handler provided by the CRI. If no runtime handler is provided, the runtime will be picked based on the level of trust of the workload. This option supports live configuration reload. This option supports live configuration reload.

Expand Down
9 changes: 9 additions & 0 deletions internal/criocli/criocli.go
Expand Up @@ -389,6 +389,9 @@ func mergeConfig(config *libconfig.Config, ctx *cli.Context) error {
if ctx.IsSet("enable-pod-events") {
config.EnablePodEvents = ctx.Bool("enable-pod-events")
}
if ctx.IsSet("disable-hostport-mapping") {
config.DisableHostPortMapping = ctx.Bool("disable-hostport-mapping")
}
return nil
}

Expand Down Expand Up @@ -1083,6 +1086,12 @@ func getCrioFlags(defConf *libconfig.Config) []cli.Flag {
Usage: "If true, CRI-O starts sending the container events to the kubelet",
EnvVars: []string{"ENABLE_POD_EVENTS"},
},
&cli.BoolFlag{
Name: "disable-hostport-mapping",
Usage: "If true, CRI-O would disable the hostport mapping.",
EnvVars: []string{"DISABLE_HOSTPORT_MAPPING"},
Value: defConf.DisableHostPortMapping,
},
}
}

Expand Down
22 changes: 22 additions & 0 deletions internal/hostport/noop_hostport_manager.go
@@ -0,0 +1,22 @@
package hostport

import "github.com/sirupsen/logrus"

// This interface implements, when hostport mapping is disabled in CRI-O
type noopHostportManager struct{}

// NewNoopHostportManager creates a new HostPortManager
func NewNoopHostportManager() HostPortManager {
logrus.Info("HostPort Mapping is Disabled in CRI-O")
return &noopHostportManager{}
}

func (mh *noopHostportManager) Add(id string, podPortMapping *PodPortMapping, natInterfaceName string) error {
logrus.Debug("HostPort Mapping is Disabled in CRI-O")
return nil
}

func (mh *noopHostportManager) Remove(id string, podPortMapping *PodPortMapping) error {
logrus.Debug("HostPort Mapping is Disabled in CRI-O")
return nil
}
5 changes: 5 additions & 0 deletions pkg/config/config.go
Expand Up @@ -436,6 +436,10 @@ type RuntimeConfig struct {

// namespaceManager is the internal NamespaceManager configuration
namespaceManager *nsmgr.NamespaceManager

// Option to disable hostport mapping in CRI-O
// Default value is 'false'
DisableHostPortMapping bool `toml:"disable_hostport_mapping"`
}

// ImageConfig represents the "crio.image" TOML config table.
Expand Down Expand Up @@ -820,6 +824,7 @@ func DefaultConfig() (*Config, error) {
namespaceManager: nsmgr.New(defaultNamespacesDir, ""),
rdtConfig: rdt.New(),
ulimitsConfig: ulimits.New(),
DisableHostPortMapping: false,
},
ImageConfig: ImageConfig{
DefaultTransport: "docker://",
Expand Down
12 changes: 12 additions & 0 deletions pkg/config/template.go
Expand Up @@ -445,6 +445,11 @@ func initCrioTemplateConfig(c *Config) ([]*templateConfigValue, error) {
group: crioRuntimeConfig,
isDefaultValue: WorkloadsEqual(dc.Workloads, c.Workloads),
},
{
templateString: templateStringCrioRuntimeDisableHostPortMapping,
group: crioRuntimeConfig,
isDefaultValue: simpleEqual(dc.DisableHostPortMapping, c.DisableHostPortMapping),
},
{
templateString: templateStringCrioImageDefaultTransport,
group: crioImageConfig,
Expand Down Expand Up @@ -1240,6 +1245,13 @@ const templateStringCrioRuntimeWorkloads = `# The workloads table defines ways t
{{ end }}
`

const templateStringCrioRuntimeDisableHostPortMapping = `# disable_hostport_mapping determines whether to enable/disable
# the container hostport mapping in CRI-O.
# Default value is set to 'false'
{{ $.Comment }}disable_hostport_mapping = {{ .DisableHostPortMapping }}
`

const templateStringCrioImage = `# The crio.image table contains settings pertaining to the management of OCI images.
#
# CRI-O reads its configured registries defaults from the system wide
Expand Down
8 changes: 7 additions & 1 deletion server/server.go
Expand Up @@ -424,7 +424,13 @@ func New(
return nil, err
}

hostportManager := hostport.NewMetaHostportManager()
// Check for hostport mapping
var hostportManager hostport.HostPortManager
if config.RuntimeConfig.DisableHostPortMapping {
hostportManager = hostport.NewNoopHostportManager()
} else {
hostportManager = hostport.NewMetaHostportManager()
}

idMappings, err := getIDMappings(config)
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions server/server_test.go
Expand Up @@ -219,6 +219,14 @@ var _ = t.Describe("Server", func() {
mockNewServer()
serverConfig.StreamIdleTimeout = "200ms"

server, err := server.New(context.Background(), libMock)
Expect(err).To(BeNil())
Expect(server).ToNot(BeNil())
})
It("should succeed with hostport mapping disabled", func() {
mockNewServer()
serverConfig.RuntimeConfig.DisableHostPortMapping = true

server, err := server.New(context.Background(), libMock)
Expect(err).To(BeNil())
Expect(server).ToNot(BeNil())
Expand Down
11 changes: 11 additions & 0 deletions test/config.bats
Expand Up @@ -119,3 +119,14 @@ EOF
# then
"$CRIO_BINARY_PATH" -c "$TESTDIR"/workload.conf -d "" config
}

@test "config dir should fail with invalid disable_hostport_mapping option" {
# given
printf '[crio.runtime]\ndisable_hostport_mapping = false\n' > "$CRIO_CONFIG"
printf '[crio.runtime]\ndisable_hostport_mapping = "no"\n' > "$CRIO_CONFIG_DIR"/00-default

# when
run "$CRIO_BINARY_PATH" -c "$CRIO_CONFIG" -d "$CRIO_CONFIG_DIR"
# then
[ "$status" -ne 0 ]
}

0 comments on commit 85671dd

Please sign in to comment.