support nsmounter when running in containerized mode

Signed-off-by: Huamin Chen <hchen@redhat.com>
This commit is contained in:
Huamin Chen 2018-10-15 14:59:41 +00:00
parent d5b7543565
commit 3436a094f7
5 changed files with 38 additions and 16 deletions

View File

@ -13,6 +13,7 @@ spec:
spec: spec:
serviceAccount: csi-nodeplugin serviceAccount: csi-nodeplugin
hostNetwork: true hostNetwork: true
hostPID: true
# to use e.g. Rook orchestrated cluster, and mons' FQDN is # to use e.g. Rook orchestrated cluster, and mons' FQDN is
# resolved through k8s service, set dns policy to cluster first # resolved through k8s service, set dns policy to cluster first
dnsPolicy: ClusterFirstWithHostNet dnsPolicy: ClusterFirstWithHostNet
@ -49,9 +50,10 @@ spec:
- "--endpoint=$(CSI_ENDPOINT)" - "--endpoint=$(CSI_ENDPOINT)"
- "--v=5" - "--v=5"
- "--drivername=csi-rbdplugin" - "--drivername=csi-rbdplugin"
- "--containerized=true"
env: env:
- name: HOST_ROOTFS - name: HOST_ROOTFS
value: "/host" value: "/rootfs"
- name: NODE_ID - name: NODE_ID
valueFrom: valueFrom:
fieldRef: fieldRef:
@ -67,7 +69,7 @@ spec:
mountPropagation: "Bidirectional" mountPropagation: "Bidirectional"
- mountPath: /dev - mountPath: /dev
name: host-dev name: host-dev
- mountPath: /host - mountPath: /rootfs
name: host-rootfs name: host-rootfs
- mountPath: /sys - mountPath: /sys
name: host-sys name: host-sys

View File

@ -25,6 +25,8 @@ Option | Default value | Description
`--endpoint` | `unix://tmp/csi.sock` | CSI endpoint, must be a UNIX socket `--endpoint` | `unix://tmp/csi.sock` | CSI endpoint, must be a UNIX socket
`--drivername` | `csi-cephfsplugin` | name of the driver (Kubernetes: `provisioner` field in StorageClass must correspond to this value) `--drivername` | `csi-cephfsplugin` | name of the driver (Kubernetes: `provisioner` field in StorageClass must correspond to this value)
`--nodeid` | _empty_ | This node's ID `--nodeid` | _empty_ | This node's ID
`--containerized` | true | Whether running in containerized mode
**Available environmental variables:** **Available environmental variables:**
`HOST_ROOTFS`: rbdplugin searches `/proc` directory under the directory set by `HOST_ROOTFS`. `HOST_ROOTFS`: rbdplugin searches `/proc` directory under the directory set by `HOST_ROOTFS`.

View File

@ -35,6 +35,7 @@ import (
type nodeServer struct { type nodeServer struct {
*csicommon.DefaultNodeServer *csicommon.DefaultNodeServer
mounter mount.Interface
} }
func (ns *nodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublishVolumeRequest) (*csi.NodePublishVolumeResponse, error) { func (ns *nodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublishVolumeRequest) (*csi.NodePublishVolumeResponse, error) {
@ -46,7 +47,7 @@ func (ns *nodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublis
s := strings.Split(strings.TrimSuffix(targetPath, "/mount"), "/") s := strings.Split(strings.TrimSuffix(targetPath, "/mount"), "/")
volName := s[len(s)-1] volName := s[len(s)-1]
notMnt, err := mount.New("").IsLikelyNotMountPoint(targetPath) notMnt, err := ns.mounter.IsLikelyNotMountPoint(targetPath)
if err != nil { if err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {
if err = os.MkdirAll(targetPath, 0750); err != nil { if err = os.MkdirAll(targetPath, 0750); err != nil {
@ -86,7 +87,7 @@ func (ns *nodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublis
options = append(options, "ro") options = append(options, "ro")
} }
diskMounter := &mount.SafeFormatAndMount{Interface: mount.New(""), Exec: mount.NewOsExec()} diskMounter := &mount.SafeFormatAndMount{Interface: ns.mounter, Exec: mount.NewOsExec()}
if err := diskMounter.FormatAndMount(devicePath, targetPath, fsType, options); err != nil { if err := diskMounter.FormatAndMount(devicePath, targetPath, fsType, options); err != nil {
return nil, err return nil, err
} }
@ -96,9 +97,8 @@ func (ns *nodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublis
func (ns *nodeServer) NodeUnpublishVolume(ctx context.Context, req *csi.NodeUnpublishVolumeRequest) (*csi.NodeUnpublishVolumeResponse, error) { func (ns *nodeServer) NodeUnpublishVolume(ctx context.Context, req *csi.NodeUnpublishVolumeRequest) (*csi.NodeUnpublishVolumeResponse, error) {
targetPath := req.GetTargetPath() targetPath := req.GetTargetPath()
mounter := mount.New("")
notMnt, err := mounter.IsLikelyNotMountPoint(targetPath) notMnt, err := ns.mounter.IsLikelyNotMountPoint(targetPath)
if err != nil { if err != nil {
return nil, status.Error(codes.Internal, err.Error()) return nil, status.Error(codes.Internal, err.Error())
} }
@ -106,13 +106,13 @@ func (ns *nodeServer) NodeUnpublishVolume(ctx context.Context, req *csi.NodeUnpu
return nil, status.Error(codes.NotFound, "Volume not mounted") return nil, status.Error(codes.NotFound, "Volume not mounted")
} }
devicePath, cnt, err := mount.GetDeviceNameFromMount(mounter, targetPath) devicePath, cnt, err := mount.GetDeviceNameFromMount(ns.mounter, targetPath)
if err != nil { if err != nil {
return nil, status.Error(codes.Internal, err.Error()) return nil, status.Error(codes.Internal, err.Error())
} }
// Unmounting the image // Unmounting the image
err = mounter.Unmount(targetPath) err = ns.mounter.Unmount(targetPath)
if err != nil { if err != nil {
return nil, status.Error(codes.Internal, err.Error()) return nil, status.Error(codes.Internal, err.Error())
} }

View File

@ -27,6 +27,10 @@ import (
"github.com/container-storage-interface/spec/lib/go/csi/v0" "github.com/container-storage-interface/spec/lib/go/csi/v0"
"github.com/kubernetes-csi/drivers/pkg/csi-common" "github.com/kubernetes-csi/drivers/pkg/csi-common"
"k8s.io/kubernetes/pkg/util/mount"
"k8s.io/kubernetes/pkg/util/nsenter"
"k8s.io/utils/exec"
) )
// PluginFolder defines the location of rbdplugin // PluginFolder defines the location of rbdplugin
@ -156,13 +160,23 @@ func NewControllerServer(d *csicommon.CSIDriver) *controllerServer {
} }
} }
func NewNodeServer(d *csicommon.CSIDriver) *nodeServer { func NewNodeServer(d *csicommon.CSIDriver, containerized bool) (*nodeServer, error) {
mounter := mount.New("")
if containerized {
ne, err := nsenter.NewNsenter(nsenter.DefaultHostRootFsPath, exec.New())
if err != nil {
return nil, err
}
mounter = mount.NewNsenterMounter("", ne)
}
return &nodeServer{ return &nodeServer{
DefaultNodeServer: csicommon.NewDefaultNodeServer(d), DefaultNodeServer: csicommon.NewDefaultNodeServer(d),
} mounter: mounter,
}, nil
} }
func (rbd *rbd) Run(driverName, nodeID, endpoint string) { func (rbd *rbd) Run(driverName, nodeID, endpoint string, containerized bool) {
var err error
glog.Infof("Driver: %v version: %v", driverName, version) glog.Infof("Driver: %v version: %v", driverName, version)
// Initialize default library driver // Initialize default library driver
@ -180,7 +194,10 @@ func (rbd *rbd) Run(driverName, nodeID, endpoint string) {
// Create GRPC servers // Create GRPC servers
rbd.ids = NewIdentityServer(rbd.driver) rbd.ids = NewIdentityServer(rbd.driver)
rbd.ns = NewNodeServer(rbd.driver) rbd.ns, err = NewNodeServer(rbd.driver, containerized)
if err != nil {
glog.Fatalln("failed to start node server, err %v", err)
}
rbd.cs = NewControllerServer(rbd.driver) rbd.cs = NewControllerServer(rbd.driver)
s := csicommon.NewNonBlockingGRPCServer() s := csicommon.NewNonBlockingGRPCServer()
s.Start(endpoint, rbd.ids, rbd.cs, rbd.ns) s.Start(endpoint, rbd.ids, rbd.cs, rbd.ns)

View File

@ -33,6 +33,7 @@ var (
endpoint = flag.String("endpoint", "unix://tmp/csi.sock", "CSI endpoint") endpoint = flag.String("endpoint", "unix://tmp/csi.sock", "CSI endpoint")
driverName = flag.String("drivername", "csi-rbdplugin", "name of the driver") driverName = flag.String("drivername", "csi-rbdplugin", "name of the driver")
nodeID = flag.String("nodeid", "", "node id") nodeID = flag.String("nodeid", "", "node id")
containerized = flag.Bool("containerized", true, "whether run as containerized")
) )
func main() { func main() {
@ -53,7 +54,7 @@ func main() {
func handle() { func handle() {
driver := rbd.GetRBDDriver() driver := rbd.GetRBDDriver()
driver.Run(*driverName, *nodeID, *endpoint) driver.Run(*driverName, *nodeID, *endpoint, *containerized)
} }
func createPersistentStorage(persistentStoragePath string) error { func createPersistentStorage(persistentStoragePath string) error {