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

Commit 134c2f3

Browse files
committed
Fix /etc/hostname backward compatibility issue for in-place upgrade.
Signed-off-by: Lantao Liu <[email protected]>
1 parent 5b8046c commit 134c2f3

2 files changed

Lines changed: 41 additions & 5 deletions

File tree

pkg/server/container_create.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -466,11 +466,19 @@ func (c *criService) generateContainerMounts(sandboxID string, config *runtime.C
466466
var mounts []*runtime.Mount
467467
securityContext := config.GetLinux().GetSecurityContext()
468468
if !isInCRIMounts(etcHostname, config.GetMounts()) {
469-
mounts = append(mounts, &runtime.Mount{
470-
ContainerPath: etcHostname,
471-
HostPath: c.getSandboxHostname(sandboxID),
472-
Readonly: securityContext.GetReadonlyRootfs(),
473-
})
469+
// /etc/hostname is added since 1.1.6, 1.2.4 and 1.3.
470+
// For in-place upgrade, the old sandbox doesn't have the hostname file,
471+
// do not mount this in that case.
472+
// TODO(random-liu): Remove the check and always mount this when
473+
// containerd 1.1 and 1.2 are deprecated.
474+
hostpath := c.getSandboxHostname(sandboxID)
475+
if _, err := c.os.Stat(hostpath); err == nil {
476+
mounts = append(mounts, &runtime.Mount{
477+
ContainerPath: etcHostname,
478+
HostPath: hostpath,
479+
Readonly: securityContext.GetReadonlyRootfs(),
480+
})
481+
}
474482
}
475483

476484
if !isInCRIMounts(etcHosts, config.GetMounts()) {

pkg/server/container_create_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package server
1818

1919
import (
20+
"os"
2021
"path/filepath"
2122
"reflect"
2223
"strings"
@@ -29,6 +30,7 @@ import (
2930
imagespec "github.com/opencontainers/image-spec/specs-go/v1"
3031
runtimespec "github.com/opencontainers/runtime-spec/specs-go"
3132
"github.com/opencontainers/runtime-tools/generate"
33+
"github.com/pkg/errors"
3234
"github.com/stretchr/testify/assert"
3335
"github.com/stretchr/testify/require"
3436
runtime "k8s.io/kubernetes/pkg/kubelet/apis/cri/runtime/v1alpha2"
@@ -541,6 +543,7 @@ func TestGenerateVolumeMounts(t *testing.T) {
541543
func TestGenerateContainerMounts(t *testing.T) {
542544
const testSandboxID = "test-id"
543545
for desc, test := range map[string]struct {
546+
statFn func(string) (os.FileInfo, error)
544547
criMounts []*runtime.Mount
545548
securityContext *runtime.LinuxContainerSecurityContext
546549
expectedMounts []*runtime.Mount
@@ -646,6 +649,30 @@ func TestGenerateContainerMounts(t *testing.T) {
646649
securityContext: &runtime.LinuxContainerSecurityContext{},
647650
expectedMounts: nil,
648651
},
652+
"should skip hostname mount if the old sandbox doesn't have hostname file": {
653+
statFn: func(path string) (os.FileInfo, error) {
654+
assert.Equal(t, filepath.Join(testRootDir, sandboxesDir, testSandboxID, "hostname"), path)
655+
return nil, errors.New("random error")
656+
},
657+
securityContext: &runtime.LinuxContainerSecurityContext{},
658+
expectedMounts: []*runtime.Mount{
659+
{
660+
ContainerPath: "/etc/hosts",
661+
HostPath: filepath.Join(testRootDir, sandboxesDir, testSandboxID, "hosts"),
662+
Readonly: false,
663+
},
664+
{
665+
ContainerPath: resolvConfPath,
666+
HostPath: filepath.Join(testRootDir, sandboxesDir, testSandboxID, "resolv.conf"),
667+
Readonly: false,
668+
},
669+
{
670+
ContainerPath: "/dev/shm",
671+
HostPath: filepath.Join(testStateDir, sandboxesDir, testSandboxID, "shm"),
672+
Readonly: false,
673+
},
674+
},
675+
},
649676
} {
650677
config := &runtime.ContainerConfig{
651678
Metadata: &runtime.ContainerMetadata{
@@ -658,6 +685,7 @@ func TestGenerateContainerMounts(t *testing.T) {
658685
},
659686
}
660687
c := newTestCRIService()
688+
c.os.(*ostesting.FakeOS).StatFn = test.statFn
661689
mounts := c.generateContainerMounts(testSandboxID, config)
662690
assert.Equal(t, test.expectedMounts, mounts, desc)
663691
}

0 commit comments

Comments
 (0)