Thanks to visit codestin.com
Credit goes to github.com

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

Commit 616d154

Browse files
committed
Fix /etc/hostname backward compatibility issue for in-place upgrade.
Signed-off-by: Lantao Liu <[email protected]>
1 parent 8e7ca12 commit 616d154

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

pkg/server/container_create.go

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

483491
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"
@@ -542,6 +544,7 @@ func TestGenerateVolumeMounts(t *testing.T) {
542544
func TestGenerateContainerMounts(t *testing.T) {
543545
const testSandboxID = "test-id"
544546
for desc, test := range map[string]struct {
547+
statFn func(string) (os.FileInfo, error)
545548
criMounts []*runtime.Mount
546549
securityContext *runtime.LinuxContainerSecurityContext
547550
expectedMounts []*runtime.Mount
@@ -647,6 +650,30 @@ func TestGenerateContainerMounts(t *testing.T) {
647650
securityContext: &runtime.LinuxContainerSecurityContext{},
648651
expectedMounts: nil,
649652
},
653+
"should skip hostname mount if the old sandbox doesn't have hostname file": {
654+
statFn: func(path string) (os.FileInfo, error) {
655+
assert.Equal(t, filepath.Join(testRootDir, sandboxesDir, testSandboxID, "hostname"), path)
656+
return nil, errors.New("random error")
657+
},
658+
securityContext: &runtime.LinuxContainerSecurityContext{},
659+
expectedMounts: []*runtime.Mount{
660+
{
661+
ContainerPath: "/etc/hosts",
662+
HostPath: filepath.Join(testRootDir, sandboxesDir, testSandboxID, "hosts"),
663+
Readonly: false,
664+
},
665+
{
666+
ContainerPath: resolvConfPath,
667+
HostPath: filepath.Join(testRootDir, sandboxesDir, testSandboxID, "resolv.conf"),
668+
Readonly: false,
669+
},
670+
{
671+
ContainerPath: "/dev/shm",
672+
HostPath: filepath.Join(testStateDir, sandboxesDir, testSandboxID, "shm"),
673+
Readonly: false,
674+
},
675+
},
676+
},
650677
} {
651678
config := &runtime.ContainerConfig{
652679
Metadata: &runtime.ContainerMetadata{
@@ -659,6 +686,7 @@ func TestGenerateContainerMounts(t *testing.T) {
659686
},
660687
}
661688
c := newTestCRIService()
689+
c.os.(*ostesting.FakeOS).StatFn = test.statFn
662690
mounts := c.generateContainerMounts(testSandboxID, config)
663691
assert.Equal(t, test.expectedMounts, mounts, desc)
664692
}

0 commit comments

Comments
 (0)