Skip to content
This repository was archived by the owner on Mar 9, 2022. It is now read-only.

Commit ed68cfd

Browse files
authored
Merge pull request #901 from Random-Liu/fix-hostname-env
Fix hostname env.
2 parents cfdf872 + f08a90f commit ed68cfd

5 files changed

Lines changed: 75 additions & 0 deletions

File tree

pkg/os/os.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ type OS interface {
4343
Mount(source string, target string, fstype string, flags uintptr, data string) error
4444
Unmount(target string) error
4545
LookupMount(path string) (mount.Info, error)
46+
Hostname() (string, error)
4647
}
4748

4849
// RealOS is used to dispatch the real system level operations.
@@ -134,3 +135,8 @@ func Unmount(target string) error {
134135

135136
return err
136137
}
138+
139+
// Hostname will call os.Hostname to get the hostname of the host.
140+
func (RealOS) Hostname() (string, error) {
141+
return os.Hostname()
142+
}

pkg/os/testing/fake_os.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ type FakeOS struct {
5151
MountFn func(source string, target string, fstype string, flags uintptr, data string) error
5252
UnmountFn func(target string) error
5353
LookupMountFn func(path string) (containerdmount.Info, error)
54+
HostnameFn func() (string, error)
5455
calls []CalledDetail
5556
errors map[string]error
5657
}
@@ -254,3 +255,16 @@ func (f *FakeOS) LookupMount(path string) (containerdmount.Info, error) {
254255
}
255256
return containerdmount.Info{}, nil
256257
}
258+
259+
// Hostname is a fake call that invokes HostnameFn or just return nil.
260+
func (f *FakeOS) Hostname() (string, error) {
261+
f.appendCalls("Hostname")
262+
if err := f.getError("Hostname"); err != nil {
263+
return "", err
264+
}
265+
266+
if f.HostnameFn != nil {
267+
return f.HostnameFn()
268+
}
269+
return "", nil
270+
}

pkg/server/container_create.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,17 @@ func (c *criService) generateContainerSpec(id string, sandboxID string, sandboxP
334334
g.AddProcessEnv("TERM", "xterm")
335335
}
336336

337+
// Add HOSTNAME env.
338+
hostname := sandboxConfig.GetHostname()
339+
if sandboxConfig.GetLinux().GetSecurityContext().GetNamespaceOptions().GetNetwork() == runtime.NamespaceMode_NODE &&
340+
hostname == "" {
341+
hostname, err = c.os.Hostname()
342+
if err != nil {
343+
return nil, err
344+
}
345+
}
346+
g.AddProcessEnv(hostnameEnv, hostname)
347+
337348
// Apply envs from image config first, so that envs from container config
338349
// can override them.
339350
if err := addImageEnvs(&g, imageConfig.Env); err != nil {

pkg/server/container_create_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -979,3 +979,45 @@ func TestMaskedAndReadonlyPaths(t *testing.T) {
979979
assert.Equal(t, test.expectedReadonly, spec.Linux.ReadonlyPaths)
980980
}
981981
}
982+
983+
func TestHostname(t *testing.T) {
984+
testID := "test-id"
985+
testSandboxID := "sandbox-id"
986+
testPid := uint32(1234)
987+
config, sandboxConfig, imageConfig, specCheck := getCreateContainerTestData()
988+
c := newTestCRIService()
989+
c.os.(*ostesting.FakeOS).HostnameFn = func() (string, error) {
990+
return "real-hostname", nil
991+
}
992+
for desc, test := range map[string]struct {
993+
hostname string
994+
networkNs runtime.NamespaceMode
995+
expectedEnv string
996+
}{
997+
"should add HOSTNAME=sandbox.Hostname for pod network namespace": {
998+
hostname: "test-hostname",
999+
networkNs: runtime.NamespaceMode_POD,
1000+
expectedEnv: "HOSTNAME=test-hostname",
1001+
},
1002+
"should add HOSTNAME=sandbox.Hostname for host network namespace": {
1003+
hostname: "test-hostname",
1004+
networkNs: runtime.NamespaceMode_NODE,
1005+
expectedEnv: "HOSTNAME=test-hostname",
1006+
},
1007+
"should add HOSTNAME=os.Hostname for host network namespace if sandbox.Hostname is not set": {
1008+
hostname: "",
1009+
networkNs: runtime.NamespaceMode_NODE,
1010+
expectedEnv: "HOSTNAME=real-hostname",
1011+
},
1012+
} {
1013+
t.Logf("TestCase %q", desc)
1014+
sandboxConfig.Hostname = test.hostname
1015+
sandboxConfig.Linux.SecurityContext = &runtime.LinuxSandboxSecurityContext{
1016+
NamespaceOptions: &runtime.NamespaceOption{Network: test.networkNs},
1017+
}
1018+
spec, err := c.generateContainerSpec(testID, testSandboxID, testPid, config, sandboxConfig, imageConfig, nil)
1019+
require.NoError(t, err)
1020+
specCheck(t, testID, testSandboxID, testPid, spec)
1021+
assert.Contains(t, spec.Process.Env, test.expectedEnv)
1022+
}
1023+
}

pkg/server/helpers.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ const (
9797
etcHosts = "/etc/hosts"
9898
// resolvConfPath is the abs path of resolv.conf on host or container.
9999
resolvConfPath = "/etc/resolv.conf"
100+
// hostnameEnv is the key for HOSTNAME env.
101+
hostnameEnv = "HOSTNAME"
100102
)
101103

102104
const (

0 commit comments

Comments
 (0)