Skip to content

Commit

Permalink
Improve GetMountPropagation (#639)
Browse files Browse the repository at this point in the history
  • Loading branch information
HomayoonAlimohammadi authored Aug 30, 2024
1 parent 8432941 commit bea0632
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 19 deletions.
6 changes: 3 additions & 3 deletions src/k8s/pkg/k8sd/features/cilium/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,16 @@ func ApplyNetwork(ctx context.Context, snap snap.Snap, cfg types.Network, _ type
"hostRoot": cgrMnt,
}
} else {
p, err := utils.GetMountPropagation("/sys")
pt, err := utils.GetMountPropagationType("/sys")
if err != nil {
err = fmt.Errorf("failed to get mount propagation for %s: %w", p, err)
err = fmt.Errorf("failed to get mount propagation type for /sys: %w", err)
return types.FeatureStatus{
Enabled: false,
Version: ciliumAgentImageTag,
Message: fmt.Sprintf(networkDeployFailedMsgTmpl, err),
}, err
}
if p == "private" {
if pt == utils.MountPropagationPrivate {
onLXD, err := snap.OnLXD(ctx)
if err != nil {
log.FromContext(ctx).Error(err, "Failed to check if running on LXD")
Expand Down
24 changes: 16 additions & 8 deletions src/k8s/pkg/utils/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,21 +139,29 @@ func GetMountPath(fsType string) (string, error) {
return mounts[0].Mountpoint, nil
}

// GetMountPropagation returns the propagation type (shared or private)
// GetMountPropagation returns ErrUnkownMount if the mount path does not exist.
func GetMountPropagation(path string) (string, error) {
type MountPropagationType string

const (
MountPropagationShared MountPropagationType = "shared"
MountPropagationPrivate MountPropagationType = "private"
MountPropagationUnknown MountPropagationType = "unknown"
)

// GetMountPropagationType returns the propagation type (shared or private)
// GetMountPropagationType returns ErrUnkownMount if the mount path does not exist.
func GetMountPropagationType(path string) (MountPropagationType, error) {
mounts, err := mountinfo.GetMounts(mountinfo.SingleEntryFilter(path))
if err != nil {
return "", fmt.Errorf("failed to get mounts: %w", err)
return MountPropagationUnknown, fmt.Errorf("failed to get mounts: %w", err)
}

if len(mounts) == 0 {
return "", ErrUnknownMount
return MountPropagationUnknown, ErrUnknownMount
}

mount := mounts[0]
if strings.Contains(mount.Optional, "shared") {
return "shared", nil
if strings.Contains(mount.Optional, string(MountPropagationShared)) {
return MountPropagationShared, nil
}
return "private", nil
return MountPropagationPrivate, nil
}
15 changes: 7 additions & 8 deletions src/k8s/pkg/utils/file_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package utils_test

import (
"errors"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -172,14 +171,14 @@ func TestFileExists(t *testing.T) {
g.Expect(fileExists).To(BeFalse())
}

func TestGetMountPropagation(t *testing.T) {
func TestGetMountPropagationType(t *testing.T) {
g := NewWithT(t)

mountType, err := utils.GetMountPropagation("/randommount")
g.Expect(errors.Is(err, utils.ErrUnknownMount)).To(BeTrue())
g.Expect(mountType).To(Equal(""))
mountType, err := utils.GetMountPropagationType("/randommount")
g.Expect(err).To(MatchError(utils.ErrUnknownMount))
g.Expect(mountType).To(Equal(utils.MountPropagationUnknown))

mountType, err = utils.GetMountPropagation("/sys")
g.Expect(err).To(BeNil())
g.Expect(mountType).To(Equal("shared"))
mountType, err = utils.GetMountPropagationType("/sys")
g.Expect(err).ToNot(HaveOccurred())
g.Expect(mountType).To(Equal(utils.MountPropagationShared))
}

0 comments on commit bea0632

Please sign in to comment.