Skip to content

Commit

Permalink
OCI runtime: make hostname logic more compatible (#7106)
Browse files Browse the repository at this point in the history
  • Loading branch information
bduffany authored Jul 29, 2024
1 parent ad34546 commit 268d019
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ var (
// These can be overridden either by the image or the command.
baseEnv = []string{
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"HOSTNAME=localhost",
}
)

Expand Down Expand Up @@ -216,8 +217,13 @@ func (c *ociContainer) configPath() string {
return filepath.Join(c.bundlePath(), "config.json")
}

func (c *ociContainer) hostname() string {
return c.cid
// containerName returns the container short-name.
func (c *ociContainer) containerName() string {
const cidPrefixLen = 12
if len(c.cid) <= cidPrefixLen {
return c.cid
}
return c.cid[:cidPrefixLen]
}

// createBundle creates the OCI bundle directory, which includes the OCI spec
Expand All @@ -229,7 +235,7 @@ func (c *ociContainer) createBundle(ctx context.Context, cmd *repb.Command) erro
}

hostnamePath := filepath.Join(c.bundlePath(), "hostname")
if err := os.WriteFile(hostnamePath, []byte(c.hostname()), 0644); err != nil {
if err := os.WriteFile(hostnamePath, []byte("localhost"), 0644); err != nil {
return fmt.Errorf("write %s: %w", hostnamePath, err)
}
// TODO: append 'hosts.container.internal' and <cid> host to match podman?
Expand Down Expand Up @@ -579,7 +585,7 @@ func (c *ociContainer) createSpec(cmd *repb.Command) (*specs.Spec, error) {
Path: c.rootfsPath(),
Readonly: false,
},
Hostname: c.hostname(),
Hostname: c.containerName(),
Mounts: []specs.Mount{
{
Destination: "/proc",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func TestRunWithImage(t *testing.T) {
cmd := &repb.Command{
Arguments: []string{"sh", "-c", `
echo "$GREETING world!"
env | grep -v 'HOSTNAME' | sort
env | sort
`},
EnvironmentVariables: []*repb.Command_EnvironmentVariable{
{Name: "GREETING", Value: "Hello"},
Expand All @@ -211,6 +211,7 @@ func TestRunWithImage(t *testing.T) {
assert.Equal(t, `Hello world!
GREETING=Hello
HOME=/root
HOSTNAME=localhost
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/test/bin
PWD=/buildbuddy-execroot
SHLVL=1
Expand Down Expand Up @@ -356,7 +357,7 @@ func TestPullCreateExecRemove(t *testing.T) {
Arguments: []string{"sh", "-ec", `
touch /bin/foo.txt
pwd
env | grep -v HOSTNAME | sort
env | sort
`},
EnvironmentVariables: []*repb.Command_EnvironmentVariable{
{Name: "GREETING", Value: "Hello"},
Expand All @@ -371,6 +372,7 @@ func TestPullCreateExecRemove(t *testing.T) {
assert.Equal(t, `/buildbuddy-execroot
GREETING=Hello
HOME=/root
HOSTNAME=localhost
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/test/bin
PWD=/buildbuddy-execroot
SHLVL=1
Expand Down

0 comments on commit 268d019

Please sign in to comment.