Skip to content

Commit

Permalink
Support the containers.conf container_name_as_hostname option
Browse files Browse the repository at this point in the history
When containers.conf has the "container_name_as_hostname" option set,
use that value, with values that don't fit `[A-Za-z0-9][A-Za-z0-9._-]+`
stripped out.

Signed-off-by: Nalin Dahyabhai <[email protected]>
  • Loading branch information
nalind committed Jan 24, 2025
1 parent 21fe6dc commit a433090
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 2 deletions.
25 changes: 25 additions & 0 deletions run_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2092,3 +2092,28 @@ func relabel(path, mountLabel string, shared bool) error {
}
return nil
}

// mapContainerNameToHostname returns the passed-in string with characters that
// don't match the pattern "[A-Za-z0-9][A-Za-z0-9._-]+" stripped out.
func mapContainerNameToHostname(containerName string) string {
const initialAllowedChars = "abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "0123456789"
const allowedChars = initialAllowedChars + "._-"
var mapped string
if containerName != "" {
mapped += strings.Map(func(r rune) rune {
if !strings.Contains(initialAllowedChars, string(r)) {
return -1
}
return r
}, containerName[:1])
}
if len(containerName) > 1 {
mapped += strings.Map(func(r rune) rune {
if !strings.Contains(allowedChars, string(r)) {
return -1
}
return r
}, containerName[1:])
}
return mapped
}
12 changes: 11 additions & 1 deletion run_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,17 @@ func (b *Builder) configureNamespaces(g *generate.Generator, options *RunOptions
} else if b.Hostname() != "" {
g.SetHostname(b.Hostname())
} else {
g.SetHostname(stringid.TruncateID(b.ContainerID))
hostname := stringid.TruncateID(b.ContainerID)
defConfig, err := config.Default()
if err != nil {
return false, "", fmt.Errorf("failed to get container config: %w", err)
}
if defConfig.Containers.ContainerNameAsHostName {
if mapped := mapContainerNameToHostname(b.Container); mapped != "" {
hostname = mapped
}
}
g.SetHostname(hostname)
}
} else {
g.SetHostname("")
Expand Down
12 changes: 11 additions & 1 deletion run_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -991,7 +991,17 @@ func (b *Builder) configureNamespaces(g *generate.Generator, options *RunOptions
} else if b.Hostname() != "" {
g.SetHostname(b.Hostname())
} else {
g.SetHostname(stringid.TruncateID(b.ContainerID))
hostname := stringid.TruncateID(b.ContainerID)
defConfig, err := config.Default()
if err != nil {
return false, "", fmt.Errorf("failed to get container config: %w", err)
}
if defConfig.Containers.ContainerNameAsHostName {
if mapped := mapContainerNameToHostname(b.Container); mapped != "" {
hostname = mapped
}
}
g.SetHostname(hostname)
}
} else {
g.SetHostname("")
Expand Down
4 changes: 4 additions & 0 deletions tests/containers.conf
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,7 @@ default_capabilities = [
default_sysctls = [
"net.ipv4.ping_group_range=0 0",
]

# Use the container's name as its hostname, in preference over its ID.
#
container_name_as_hostname = true
15 changes: 15 additions & 0 deletions tests/run.bats
Original file line number Diff line number Diff line change
Expand Up @@ -997,3 +997,18 @@ _EOF
run_buildah ? bud --pull=false --layers .
expect_output --substring -- "-c requires an argument"
}

@test "container_name_as_hostname" {
skip_if_no_runtime

_prefetch alpine

name=alpine-working-container-for-test
run_buildah from --name :"$name": --cidfile ${TEST_SCRATCH_DIR}/cid alpine
cid="$output"
export CONTAINERS_CONF=${TEST_SOURCES}/containers.conf
run_buildah run $cid hostname
expect_output "$name"
run_buildah run $(cat ${TEST_SCRATCH_DIR}/cid) hostname
expect_output "$name"
}

0 comments on commit a433090

Please sign in to comment.