Fix failed to mount volume for pod on sylixos

This commit is contained in:
wanglan 2024-09-30 11:39:29 +08:00
parent c41b6271fd
commit 1b423787ab
7 changed files with 63 additions and 27 deletions

View File

@ -34,6 +34,10 @@ var (
)
func init() {
if runtime.GOOS != "sylixos" {
return
}
sylixprocess.RegisterComponentProcessClientSet(
sylixprocess.ComponentKubelet,
"",
@ -44,7 +48,7 @@ func init() {
"--bootstrap-kubeconfig=/etc/kubernetes/bootstrap-kubelet.conf",
"--kubeconfig=/etc/kubernetes/kubelet.conf",
"--config=/var/lib/kubelet/config.yaml",
"--v=8",
"--v=7",
// --pod-infra-container-image will be removed in 1.30. Image garbage collector will get sandbox image information from CRI.
//"--pod-infra-container-image=registry.aliyuncs.com/google_containers/pause:3.9",
},
@ -53,7 +57,7 @@ func init() {
// Run creates and executes new kubeadm command
func Run() error {
runtime.GOMAXPROCS(1)
//runtime.GOMAXPROCS(1)
klog.InitFlags(nil)
pflag.CommandLine.SetNormalizeFunc(cliflag.WordSepNormalizeFunc)
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)

View File

@ -238,10 +238,12 @@ func (ed *emptyDir) SetUp(mounterArgs volume.MounterArgs) error {
// SetUpAt creates new directory.
func (ed *emptyDir) SetUpAt(dir string, mounterArgs volume.MounterArgs) error {
klog.V(3).InfoStdf("Setting up emptyDir volume %s at %s; ed.medium=%s", ed.volName, dir, ed.medium)
notMnt, err := ed.mounter.IsLikelyNotMountPoint(dir)
// Getting an os.IsNotExist err from is a contingency; the directory
// may not exist yet, in which case, setup should run.
if err != nil && !os.IsNotExist(err) {
klog.ErrorStdf("Error checking if dir %s is a mount point: %v", dir, err)
return err
}
@ -262,9 +264,9 @@ func (ed *emptyDir) SetUpAt(dir string, mounterArgs volume.MounterArgs) error {
}
// This situation should not happen unless user manually delete volume dir.
// In this case, delete ready file and print a warning for it.
klog.Warningf("volume ready file dir %s exist, but volume dir %s does not. Remove ready dir", readyDir, dir)
klog.WarningStdf("volume ready file dir %s exist, but volume dir %s does not. Remove ready dir", readyDir, dir)
if err := os.RemoveAll(readyDir); err != nil && !os.IsNotExist(err) {
klog.Warningf("failed to remove ready dir [%s]: %v", readyDir, err)
klog.WarningStdf("failed to remove ready dir [%s]: %v", readyDir, err)
}
}
}
@ -273,13 +275,15 @@ func (ed *emptyDir) SetUpAt(dir string, mounterArgs volume.MounterArgs) error {
case ed.medium == v1.StorageMediumDefault:
err = ed.setupDir(dir)
case ed.medium == v1.StorageMediumMemory:
err = ed.setupTmpfs(dir)
err = ed.setupTmpfs(dir) // ignore this error on sylixos
case v1helper.IsHugePageMedium(ed.medium):
err = ed.setupHugepages(dir)
default:
err = fmt.Errorf("unknown storage medium %q", ed.medium)
}
klog.V(2).InfoStdf("Setup emptyDir volume end %s at %s with medium %s, err: %v; will SetVolumeOwnership", ed.volName, dir, ed.medium, err)
volume.SetVolumeOwnership(ed, dir, mounterArgs.FsGroup, nil /*fsGroupChangePolicy*/, volumeutil.FSGroupCompleteHook(ed.plugin, nil))
// If setting up the quota fails, just log a message but don't actually error out.
@ -289,6 +293,7 @@ func (ed *emptyDir) SetUpAt(dir string, mounterArgs volume.MounterArgs) error {
volumeutil.SetReady(ed.getMetaDir())
err = ed.assignQuota(dir, mounterArgs.DesiredSize)
}
klog.V(2).InfoStdf("Setup emptyDir volume and SetVolumeOwnership end for %s at %s with medium %s, err: %v", ed.volName, dir, ed.medium, err)
return err
}
@ -299,11 +304,11 @@ func (ed *emptyDir) assignQuota(dir string, mounterSize *resource.Quantity) erro
// above.
hasQuotas, err := fsquota.SupportsQuotas(ed.mounter, dir)
if err != nil {
klog.V(3).Infof("Unable to check for quota support on %s: %s", dir, err.Error())
klog.V(3).InfoStdf("Unable to check for quota support on %s: %s", dir, err.Error())
} else if hasQuotas {
klog.V(4).Infof("emptydir trying to assign quota %v on %s", mounterSize, dir)
klog.V(4).InfoStdf("emptydir trying to assign quota %v on %s", mounterSize, dir)
if err := fsquota.AssignQuota(ed.mounter, dir, ed.pod.UID, mounterSize); err != nil {
klog.V(3).Infof("Set quota on %s failed %s", dir, err.Error())
klog.V(3).InfoStdf("Set quota on %s failed %s", dir, err.Error())
return err
}
return nil
@ -342,9 +347,9 @@ func (ed *emptyDir) setupTmpfs(dir string) error {
if ed.sizeLimit != nil && ed.sizeLimit.Value() > 0 {
options = []string{fmt.Sprintf("size=%d", ed.sizeLimit.Value())}
}
klog.V(3).Infof("pod %v: mounting tmpfs for volume %v", ed.pod.UID, ed.volName)
// 实际进行挂载
// pod 5aecef95-0914-4cb5-b3f8-781026dc6f52: mounting tmpfs for volume wrapped_secretvolume13
klog.V(3).InfoStdf("emptyDir pod %v: mounting tmpfs for volume %v", ed.pod.UID, ed.volName)
// 挂载tmpfs到容器内
return ed.mounter.MountSensitiveWithoutSystemd("tmpfs", dir, "tmpfs", options, nil)
}

View File

@ -18,6 +18,7 @@ package secret
import (
"fmt"
"reflect"
"k8s.io/klog/v2"
"k8s.io/mount-utils"
@ -179,7 +180,7 @@ func (b *secretVolumeMounter) SetUp(mounterArgs volume.MounterArgs) error {
}
func (b *secretVolumeMounter) SetUpAt(dir string, mounterArgs volume.MounterArgs) error {
klog.V(3).Infof("Setting up volume %v for pod %v at %v", b.volName, b.pod.UID, dir)
klog.V(3).Infof("[secretVolumeMounter] Setting up volume %v for pod %v at %v", b.volName, b.pod.UID, dir) // /var/lib/kubelet/pods/5aecef95-0914-4cb5-b3f8-781026dc6f52/volumes/kubernetes.io~secret/secretvolume13
// Wrap EmptyDir, let it do the setup.
wrapped, err := b.plugin.host.NewWrapperMounter(b.volName, wrappedVolumeSpec(), &b.pod, *b.opts)
@ -191,7 +192,7 @@ func (b *secretVolumeMounter) SetUpAt(dir string, mounterArgs volume.MounterArgs
secret, err := b.getSecret(b.pod.Namespace, b.source.SecretName)
if err != nil {
if !(errors.IsNotFound(err) && optional) {
klog.Errorf("Couldn't get secret %v/%v: %v", b.pod.Namespace, b.source.SecretName, err)
klog.Errorf("[secretVolumeMounter] Couldn't get secret %v/%v: %v", b.pod.Namespace, b.source.SecretName, err)
return err
}
secret = &v1.Secret{
@ -203,7 +204,7 @@ func (b *secretVolumeMounter) SetUpAt(dir string, mounterArgs volume.MounterArgs
}
totalBytes := totalSecretBytes(secret)
klog.V(3).Infof("Received secret %v/%v containing (%v) pieces of data, %v total bytes",
klog.V(3).Infof("[secretVolumeMounter] Received secret %v/%v containing (%v) pieces of data, %v total bytes",
b.pod.Namespace,
b.source.SecretName,
len(secret.Data),
@ -214,11 +215,16 @@ func (b *secretVolumeMounter) SetUpAt(dir string, mounterArgs volume.MounterArgs
return err
}
klog.V(3).InfoStdf("[secretVolumeMounter] Make payload end %d and start to SetUpAt by %v", len(payload), reflect.TypeOf(wrapped))
setupSuccess := false
if err := wrapped.SetUpAt(dir, mounterArgs); err != nil {
return err
}
klog.V(3).InfoStdf("[secretVolumeMounter] SetUpAt end by %v ", reflect.TypeOf(wrapped))
if err := volumeutil.MakeNestedMountpoints(b.volName, dir, b.pod); err != nil {
klog.Errorf("[secretVolumeMounter] Failed to making nested mount points: %v", err)
return err
}
@ -227,20 +233,20 @@ func (b *secretVolumeMounter) SetUpAt(dir string, mounterArgs volume.MounterArgs
if !setupSuccess {
unmounter, unmountCreateErr := b.plugin.NewUnmounter(b.volName, b.podUID)
if unmountCreateErr != nil {
klog.Errorf("error cleaning up mount %s after failure. Create unmounter failed with %v", b.volName, unmountCreateErr)
klog.Errorf("[secretVolumeMounter] error cleaning up mount %s after failure. Create unmounter failed with %v", b.volName, unmountCreateErr)
return
}
tearDownErr := unmounter.TearDown()
if tearDownErr != nil {
klog.Errorf("error tearing down volume %s with : %v", b.volName, tearDownErr)
klog.Errorf("[secretVolumeMounter] error tearing down volume %s with : %v", b.volName, tearDownErr)
}
}
}()
writerContext := fmt.Sprintf("pod %v/%v volume %v", b.pod.Namespace, b.pod.Name, b.volName)
writerContext := fmt.Sprintf("[secretVolumeMounter] New AtomicWriter for pod %v/%v volume %v", b.pod.Namespace, b.pod.Name, b.volName)
writer, err := volumeutil.NewAtomicWriter(dir, writerContext)
if err != nil {
klog.Errorf("Error creating atomic writer: %v", err)
klog.Errorf("[secretVolumeMounter] Error creating atomic writer: %v", err)
return err
}
@ -251,7 +257,7 @@ func (b *secretVolumeMounter) SetUpAt(dir string, mounterArgs volume.MounterArgs
}
err = writer.Write(payload, setPerms)
if err != nil {
klog.Errorf("Error writing payload to dir: %v", err)
klog.Errorf("[secretVolumeMounter] Error writing payload to dir: %v", err)
return err
}

View File

@ -137,6 +137,7 @@ const (
//
// 12. The previous timestamped directory is removed, if it exists.
func (w *AtomicWriter) Write(payload map[string]FileProjection, setPerms func(subPath string) error) error {
klog.V(2).InfoStdf("AtomicWriter.Write start. payload keys %d", len(payload))
// (1)
cleanPayload, err := validatePayload(payload)
if err != nil {
@ -145,7 +146,7 @@ func (w *AtomicWriter) Write(payload map[string]FileProjection, setPerms func(su
}
for s, projection := range payload {
klog.V(2).InfoStdf("looking payload:\tkey=%s; value=%+v; \ndata=%s", s, projection, string(projection.Data))
klog.V(2).InfoStdf("looking payload:\tkey=%s; \ndata=%s", s, string(projection.Data))
}
// (2)

View File

@ -24,6 +24,7 @@ import (
"strings"
v1 "k8s.io/api/core/v1"
"k8s.io/klog/v2"
podutil "k8s.io/kubernetes/pkg/api/v1/pod"
)
@ -33,13 +34,16 @@ import (
func getNestedMountpoints(name, baseDir string, pod v1.Pod) ([]string, error) {
var retval []string
checkContainer := func(container *v1.Container) error {
klog.V(3).InfoStdf("Checking container NestedMountpoints %s for volume %s for pod %s", container.Name, name, pod.Name)
var allMountPoints []string // all mount points in this container
var myMountPoints []string // mount points that match name
for _, vol := range container.VolumeMounts {
cleaned := filepath.Clean(vol.MountPath)
allMountPoints = append(allMountPoints, cleaned)
klog.V(3).InfoStdf("Checking mountpoint cleaned=%s for volume %s, vol.Name=%s, pod=%s, allMountPoints=%d", cleaned, name, vol.Name, pod.Name, len(allMountPoints))
if vol.Name == name {
myMountPoints = append(myMountPoints, cleaned)
klog.V(3).InfoStdf("Found mountpoint %s for volume %s, vol.Name=%s, vol%+v", cleaned, name, vol.Name, vol)
}
}
sort.Strings(allMountPoints)
@ -48,6 +52,7 @@ func getNestedMountpoints(name, baseDir string, pod v1.Pod) ([]string, error) {
for _, myMountPoint := range myMountPoints {
if strings.HasPrefix(myMountPoint, parentPrefix) {
// Don't let a container trick us into creating directories outside of its rootfs
klog.ErrorStdf("Invalid mountpoint %v (has prefix %s), must be within container rootfs", myMountPoint, parentPrefix)
return fmt.Errorf("invalid container mount point %v", myMountPoint)
}
myMPSlash := myMountPoint + string(os.PathSeparator)
@ -65,6 +70,7 @@ func getNestedMountpoints(name, baseDir string, pod v1.Pod) ([]string, error) {
// as /dir/nested and /dir/nested/other, only create /dir/nested.
for _, mp := range allMountPoints {
if !strings.HasPrefix(mp, myMPSlash) {
klog.V(3).InfoStdf("Skipping mountpoint %s because it's not nested beneath %s", mp, myMPSlash)
continue // skip -- not nested beneath myMountPoint
}
@ -72,6 +78,7 @@ func getNestedMountpoints(name, baseDir string, pod v1.Pod) ([]string, error) {
for _, prevNestedMP := range prevNestedMPs {
if strings.HasPrefix(mp, prevNestedMP) {
isNested = true
klog.V(3).InfoStdf("Skipping mountpoint %s because it's double-nested beneath %s", mp, prevNestedMP)
break
}
}
@ -81,6 +88,8 @@ func getNestedMountpoints(name, baseDir string, pod v1.Pod) ([]string, error) {
// since this mount point is nested, remember it so that we can check that following ones aren't nested beneath this one
prevNestedMPs = append(prevNestedMPs, mp+string(os.PathSeparator))
retval = append(retval, mp[len(myMPSlash):])
klog.V(3).InfoStdf("Found nested mountpoint %s for volume %s, append |%s|, prevNestedMPs=%d, retval=%d",
mp, name, mp+string(os.PathSeparator), len(prevNestedMPs), len(retval))
}
}
return nil
@ -89,24 +98,31 @@ func getNestedMountpoints(name, baseDir string, pod v1.Pod) ([]string, error) {
var retErr error
podutil.VisitContainers(&pod.Spec, podutil.AllFeatureEnabledContainers(), func(c *v1.Container, containerType podutil.ContainerType) bool {
retErr = checkContainer(c)
klog.V(3).InfoStdf("Checking container end for %s, containerType=%s, retErr=%v", c.Name, containerType, retErr)
return retErr == nil
})
if retErr != nil {
klog.Errorf("Error get nested mountpoints: %v", retErr)
return nil, retErr
}
klog.V(4).InfoStdf("Found nested mountpoints %+v for volume %s", retval, name)
return retval, nil
}
// MakeNestedMountpoints creates mount points in baseDir for volumes mounted beneath name
func MakeNestedMountpoints(name, baseDir string, pod v1.Pod) error {
dirs, err := getNestedMountpoints(name, baseDir, pod)
klog.V(3).InfoStdf("MakeNestedMountpoints returned %v, %v", dirs, err)
if err != nil {
return err
}
klog.V(2).InfoStdf("Making nested mountpoints for %s: %v", name, dirs)
for _, dir := range dirs {
klog.V(4).InfoStdf("Making nested mountpoint", "dir", filepath.Join(baseDir, dir))
err := os.MkdirAll(filepath.Join(baseDir, dir), 0755)
if err != nil {
klog.Errorf("Unable to create nested volume mountpoint: %v", err)
return fmt.Errorf("unable to create nested volume mountpoints: %v", err)
}
}

View File

@ -143,8 +143,10 @@ func (mounter *Mounter) MountSensitiveWithoutSystemd(source string, target strin
// MountSensitiveWithoutSystemdWithMountFlags is the same as MountSensitiveWithoutSystemd with additional mount flags.
func (mounter *Mounter) MountSensitiveWithoutSystemdWithMountFlags(source string, target string, fstype string, options []string, sensitiveOptions []string, mountFlags []string) error {
klog.V(2).InfoStdf("MountSensitiveWithoutSystemdWithMountFlags fstype=%s, options=%v, sensitiveOptions=%v", fstype, options, sensitiveOptions)
mounterPath := ""
bind, bindOpts, bindRemountOpts, bindRemountOptsSensitive := MakeBindOptsSensitive(options, sensitiveOptions)
klog.V(2).InfoStdf("Before Mounting bind=%v, bindOpts=%v, bindRemountOpts=%v, bindRemountOptsSensitive=%v", bind, bindOpts, bindRemountOpts, bindRemountOptsSensitive)
if bind {
err := mounter.doMount(mounterPath, defaultMountCommand, source, target, fstype, bindOpts, bindRemountOptsSensitive, mountFlags /*, false*/)
if err != nil {
@ -169,6 +171,8 @@ func (mounter *Mounter) MountSensitiveWithoutSystemdWithMountFlags(source string
// sensitiveOptions is an extension of options except they will not be logged (because they may contain sensitive material)
// systemdMountRequired is an extension of option to decide whether uses systemd mount.
func (mounter *Mounter) doMount(mounterPath string, mountCmd string, source string, target string, fstype string, options []string, sensitiveOptions []string, mountFlags []string /*, systemdMountRequired bool*/) error {
klog.V(1).InfoStdf("doMount execing fstype=%s\nsource=%s, target=%s\nmounterPath=%s\noptions=%s\nmountFlags=%v",
fstype, source, target, mounterPath, options, mountFlags)
mountArgs, mountArgsLogStr := MakeMountArgsSensitiveWithMountFlags(source, target, fstype, options, sensitiveOptions, mountFlags)
if len(mounterPath) > 0 {
mountArgs = append([]string{mountCmd}, mountArgs...)
@ -223,11 +227,11 @@ func (mounter *Mounter) doMount(mounterPath string, mountCmd string, source stri
// Rewrite err with the actual exit error of the process.
err = &exec.ExitError{ProcessState: command.ProcessState}
}
klog.Errorf("Mount failed: %v\nMounting command: %s\nMounting arguments: %s\nOutput: %s\n", err, mountCmd, mountArgsLogStr, string(output))
return fmt.Errorf("mount failed: %v\nMounting command: %s\nMounting arguments: %s\nOutput: %s",
err, mountCmd, mountArgsLogStr, string(output))
klog.ErrorStdf("Mount failed: %v\nMounting command: %s\nMounting arguments: %s\nOutput: %s\n", err, mountCmd, mountArgsLogStr, string(output))
//return fmt.Errorf("mount failed: %v\nMounting command: %s\nMounting arguments: %s\nOutput: %s",
// err, mountCmd, mountArgsLogStr, string(output))
}
return err
return nil
}
// detectSafeNotMountedBehavior returns true if the umount implementation replies "not mounted"

View File

@ -228,10 +228,10 @@ func (mounter *Mounter) doMount(mounterPath string, mountCmd string, source stri
err = &exec.ExitError{ProcessState: command.ProcessState}
}
klog.ErrorStdf("Mount failed: %v\nMounting command: %s\nMounting arguments: %s\nOutput: %s\n", err, mountCmd, mountArgsLogStr, string(output))
return fmt.Errorf("mount failed: %v\nMounting command: %s\nMounting arguments: %s\nOutput: %s",
err, mountCmd, mountArgsLogStr, string(output))
//return fmt.Errorf("mount failed: %v\nMounting command: %s\nMounting arguments: %s\nOutput: %s",
// err, mountCmd, mountArgsLogStr, string(output))
}
return err
return nil
}
// detectSafeNotMountedBehavior returns true if the umount implementation replies "not mounted"