Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

oss: support CoCo direct volume feature #882

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 137 additions & 0 deletions pkg/oss/coco.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package oss

import (
"context"
"encoding/json"
"fmt"
"strings"

"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/utils/kata/directvolume"
log "github.com/sirupsen/logrus"
)

const (
optDirectAssigned = "direct"
optAnnotations = "annotations"
optEncrypted = "encrypted"
optEncPasswd = "encPasswd"
optKmsKeyId = "kmsKeyId"
fsTypeSecureMount = "secure_mount"
volumeTypeOSS = "alibaba-cloud-oss"
sealedSecretPrefix = "sealed."
)

func (ns *nodeServer) publishDirectVolume(ctx context.Context, req *csi.NodePublishVolumeRequest, opt *Options) (*csi.NodePublishVolumeResponse, error) {
logger := log.WithFields(map[string]interface{}{
"NodeServer": "OSSNodeServer",
"VolumeId": req.VolumeId,
"TargetPath": req.TargetPath,
})

volumePath := req.TargetPath
if isDirectVolumePath(volumePath) {
logger.Infof("NodePublishVolume: The mount info for DirectVolume is already exist: %s", volumePath)
return &csi.NodePublishVolumeResponse{}, nil
}

device := req.TargetPath
volumeType := volumeTypeOSS
fsType := fsTypeSecureMount
var encrypted, encPasswd, kmsKeyId string
annotationsObj := map[string]string{}

if v := req.Secrets[optEncPasswd]; v != "" {
encPasswd = v
}
for key, value := range req.VolumeContext {
switch key {
case optEncrypted:
encrypted = strings.TrimSpace(value)
case optEncPasswd:
encPasswd = strings.TrimSpace(value)
case optKmsKeyId:
kmsKeyId = strings.TrimSpace(value)
case optAnnotations:
json.Unmarshal([]byte(value), &annotationsObj)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it intentional to ignore the errors here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. This option is optional, and the invalid value can be ignored.

}
}

if annotationsObj["kata_fs_type"] != "" {
fsType = annotationsObj["kata_fs_type"]
}
if annotationsObj["kata_volume_type"] != "" {
volumeType = annotationsObj["kata_volume_type"]
}
if annotationsObj["kata_device"] != "" {
device = annotationsObj["kata_device"]
}
annotations, _ := json.Marshal(annotationsObj)
annotationsStr := string(annotations)
if len(annotations) == 0 || annotationsStr == "null" || annotationsStr == "" {
annotationsStr = "{}"
}
metadata := map[string]string{
"bucket": opt.Bucket,
"url": opt.URL,
"otherOpts": opt.OtherOpts,
"path": opt.Path,
"encrypted": encrypted,
"kmsKeyId": kmsKeyId,
"annotations": annotationsStr,
"volumeId": req.GetVolumeId(),
"readonly": fmt.Sprintf("%v", req.Readonly),
"targetPath": req.GetTargetPath(),
}
if opt.AkSecret != "" && strings.HasPrefix(opt.AkSecret, sealedSecretPrefix) {
metadata[AkID] = opt.AkID
metadata[AkSecret] = opt.AkSecret
}
if encPasswd != "" && strings.HasPrefix(encPasswd, sealedSecretPrefix) {
metadata[optEncPasswd] = encPasswd
}

mountInfo := directvolume.MountInfo{
VolumeType: volumeType,
Device: device,
FsType: fsType,
Metadata: metadata,
Options: strings.Fields(opt.OtherOpts),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

by using strings.Fields(), we dropped the support for space in the options, e.g.: -o key="hello world" will not work.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the ossfs have such cases.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe. But this breaks the current interface. We are parsing the options with shell now, e.g., -o 'key' is parsed as ["-o", "key"], not ["-o", "'key'"]

}

logger.Info("NodePublishVolume:: Starting add mount info for DirectVolume")
err := directvolume.AddMountInfo(volumePath, mountInfo)
if err != nil {
logger.Errorf("NodePublishVolume:: Add mount info for DirectVolume failed: %v", err)
return nil, err
}
logger.Info("NodePublishVolume:: Add mount info for DirectVolume is successfully")

return &csi.NodePublishVolumeResponse{}, nil
}

func (ns *nodeServer) unPublishDirectVolume(ctx context.Context, req *csi.NodeUnpublishVolumeRequest) (*csi.NodeUnpublishVolumeResponse, error) {
logger := log.WithFields(map[string]interface{}{
"NodeServer": "OSSNodeServer",
"VolumeId": req.VolumeId,
"TargetPath": req.TargetPath,
})

volumePath := req.TargetPath
err := directvolume.Remove(volumePath)
if err != nil {
logger.Errorf("NodeUnPublishVolume:: Remove mount info for DirectVolume failed: %v", err)
return nil, err
}

logger.Info("NodeUnPublishVolume:: Remove mount info for DirectVolume is successfully")
return &csi.NodeUnpublishVolumeResponse{}, nil
}

func isDirectVolumePath(volumePath string) bool {
_, err := directvolume.VolumeMountInfo(volumePath)
if err != nil {
return false
}
return true
}
122 changes: 122 additions & 0 deletions pkg/oss/coco_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package oss

import (
"context"
"testing"

"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/utils/kata/directvolume"
"github.com/stretchr/testify/assert"
)

func Test_nodeServer_publishDirectVolume(t *testing.T) {
ns := &nodeServer{}
req := &csi.NodePublishVolumeRequest{
VolumeId: "test-id",
PublishContext: nil,
StagingTargetPath: "",
TargetPath: "/foo/bar/path/for/publish",
VolumeCapability: nil,
Readonly: false,
Secrets: nil,
VolumeContext: map[string]string{
"encrypted": "local_encrypt",
"kmsKeyId": "foo-key-id",
},
}
opts := &Options{
URL: "https://oss-cn-hangzhou.aliyuncs.com",
Bucket: "test-bucket",
Path: "bucket/path",
OtherOpts: "-o test -w abc",
}
resp, err := ns.publishDirectVolume(context.TODO(), req, opts)
assert.NoError(t, err)
assert.NotNil(t, resp)

defer directvolume.Remove(req.TargetPath)
ret := isDirectVolumePath(req.TargetPath)
assert.True(t, ret)

info, err := directvolume.VolumeMountInfo(req.TargetPath)
assert.NoError(t, err)
assert.NotNil(t, info)
assert.Equal(t, "alibaba-cloud-oss", info.VolumeType)
assert.Equal(t, "secure_mount", info.FsType)
assert.Equal(t, []string{"-o", "test", "-w", "abc"}, info.Options)
assert.Equal(t, opts.URL, info.Metadata["url"])
assert.Equal(t, opts.Bucket, info.Metadata["bucket"])
assert.Equal(t, opts.Path, info.Metadata["path"])
assert.Equal(t, req.TargetPath, info.Metadata["targetPath"])
assert.Equal(t, req.VolumeContext["kmsKeyId"], info.Metadata["kmsKeyId"])
assert.Equal(t, req.VolumeContext["encrypted"], info.Metadata["encrypted"])
assert.Equal(t, opts.OtherOpts, info.Metadata["otherOpts"])

// publish twice
resp, err = ns.publishDirectVolume(context.TODO(), req, opts)
assert.NoError(t, err)
assert.NotNil(t, resp)
}

func Test_nodeServer_publishDirectVolume_overwrite_annotations(t *testing.T) {
ns := &nodeServer{}
req := &csi.NodePublishVolumeRequest{
VolumeId: "test-id",
PublishContext: nil,
StagingTargetPath: "",
TargetPath: "/foo/bar/path/for/publish",
VolumeCapability: nil,
Readonly: false,
Secrets: nil,
VolumeContext: map[string]string{
"annotations": `{"kata_fs_type": "type_v2", "kata_volume_type": "volume_type_v", "kata_device": "kata_device_vv" }`,
},
}
opts := &Options{}
resp, err := ns.publishDirectVolume(context.TODO(), req, opts)
assert.NoError(t, err)
assert.NotNil(t, resp)

defer directvolume.Remove(req.TargetPath)
ret := isDirectVolumePath(req.TargetPath)
assert.True(t, ret)

info, err := directvolume.VolumeMountInfo(req.TargetPath)
assert.NoError(t, err)
assert.NotNil(t, info)
assert.Equal(t, "volume_type_v", info.VolumeType)
assert.Equal(t, "type_v2", info.FsType)
assert.Equal(t, "kata_device_vv", info.Device)

// publish twice
resp, err = ns.publishDirectVolume(context.TODO(), req, opts)
assert.NoError(t, err)
assert.NotNil(t, resp)
}

func Test_nodeServer_unPublishDirectVolume(t *testing.T) {
ns := &nodeServer{}
req := &csi.NodeUnpublishVolumeRequest{
VolumeId: "test-id",
TargetPath: "/foo/bar/path/for/unpublish",
}

err := directvolume.AddMountInfo(req.TargetPath, directvolume.MountInfo{})
assert.NoError(t, err)
info, err := directvolume.VolumeMountInfo(req.TargetPath)
assert.NoError(t, err)
assert.NotNil(t, info)

resp, err := ns.unPublishDirectVolume(context.TODO(), req)
assert.NoError(t, err)
assert.NotNil(t, resp)

info, err = directvolume.VolumeMountInfo(req.TargetPath)
assert.Error(t, err)
assert.Nil(t, info)

// unpublish twice
resp, err = ns.unPublishDirectVolume(context.TODO(), req)
assert.NoError(t, err)
assert.NotNil(t, resp)
}
10 changes: 10 additions & 0 deletions pkg/oss/nodeserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ type nodeServer struct {

// Options contains options for target oss
type Options struct {
directAssigned bool

Bucket string `json:"bucket"`
URL string `json:"url"`
OtherOpts string `json:"otherOpts"`
Expand Down Expand Up @@ -130,6 +132,8 @@ func (ns *nodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublis
opt.MetricsTop = strings.ToLower(strings.TrimSpace(value))
} else if key == "containernetworkfilesystem" {
cnfsName = value
} else if key == optDirectAssigned {
opt.directAssigned, _ = strconv.ParseBool(strings.TrimSpace(value))
}
}

Expand Down Expand Up @@ -180,6 +184,9 @@ func (ns *nodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublis
argStr := fmt.Sprintf("Bucket: %s, url: %s, , OtherOpts: %s, Path: %s, UseSharedPath: %s, authType: %s", opt.Bucket, opt.URL, opt.OtherOpts, opt.Path, strconv.FormatBool(opt.UseSharedPath), opt.AuthType)
log.Infof("NodePublishVolume:: Starting Oss Mount: %s", argStr)

if opt.directAssigned {
return ns.publishDirectVolume(ctx, req, opt)
}
if IsOssfsMounted(mountPath) {
log.Infof("NodePublishVolume: The mountpoint is mounted: %s", mountPath)
return &csi.NodePublishVolumeResponse{}, nil
Expand Down Expand Up @@ -390,6 +397,9 @@ func (ns *nodeServer) NodeUnpublishVolume(ctx context.Context, req *csi.NodeUnpu
if err != nil {
return nil, err
}
if isDirectVolumePath(mountPoint) {
return ns.unPublishDirectVolume(ctx, req)
}

Comment on lines +400 to 403
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not idempotent. I suppose isDirectVolumePath would return false on a second unpublish? But it might be OK, because the remaining logic would still determine the volume as not mounted, then return success.

// check mount point with IsLikelyNotMountPoint first
notmounted, err := ns.k8smounter.IsLikelyNotMountPoint(mountPoint)
Expand Down
83 changes: 83 additions & 0 deletions pkg/utils/kata/directvolume/direct_volume.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright (c) 2022 Databricks Inc.
//
// SPDX-License-Identifier: Apache-2.0
//

package directvolume

// base on https://github.com/kata-containers/kata-containers/blob/c800d0739fe42414a15ac8967f3fa5ccb98c28d7/src/runtime/pkg/direct-volume/utils.go

import (
b64 "encoding/base64"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
)

const (
mountInfoFileName = "mountInfo.json"
)

var kataDirectVolumeRootPath = "/run/kata-containers/shared/direct-volumes"

// MountInfo contains the information needed by Kata to consume a host block device and mount it as a filesystem inside the guest VM.
type MountInfo struct {
// The type of the volume (ie. block)
VolumeType string `json:"volume-type"`
// The device backing the volume.
Device string `json:"device"`
// The filesystem type to be mounted on the volume.
FsType string `json:"fstype"`
// Additional metadata to pass to the agent regarding this volume.
Metadata map[string]string `json:"metadata,omitempty"`
// Additional mount options.
Options []string `json:"options,omitempty"`
}

// Add writes the mount info of a direct volume into a filesystem path known to Kata Container.
func Add(volumePath string, mountInfo string) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pass mountInfo as []byte?

Add is only called from AddMountInfo. Combine them to one func?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. then how about just importing it? That is a one file package.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't just import it. That will cause we depend on the whole kata runtime project.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that is not a big deal. We are using vendor mode, which will only introduce one go file into the vendor directory. But I've tried that, this requires several upgrades to other dependencies. So, it is a pity. Maybe we can import this after we sort out our go.mod, and upgrade our dependencies.

volumeDir := filepath.Join(kataDirectVolumeRootPath, b64.URLEncoding.EncodeToString([]byte(volumePath)))
stat, err := os.Stat(volumeDir)
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
return err
}
if err := os.MkdirAll(volumeDir, 0700); err != nil {
return err
}
}
if stat != nil && !stat.IsDir() {
return fmt.Errorf("%s should be a directory", volumeDir)
}

Comment on lines +42 to +54
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about just call os.MkdirAll? It will do the Stat and the checks for you.

Suggested change
stat, err := os.Stat(volumeDir)
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
return err
}
if err := os.MkdirAll(volumeDir, 0700); err != nil {
return err
}
}
if stat != nil && !stat.IsDir() {
return fmt.Errorf("%s should be a directory", volumeDir)
}
if err := os.MkdirAll(volumeDir, 0700); err != nil {
return err
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

var deserialized MountInfo
if err := json.Unmarshal([]byte(mountInfo), &deserialized); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why unmarshal it?

return err
}

return os.WriteFile(filepath.Join(volumeDir, mountInfoFileName), []byte(mountInfo), 0600)
}

// Remove deletes the direct volume path including all the files inside it.
func Remove(volumePath string) error {
return os.RemoveAll(filepath.Join(kataDirectVolumeRootPath, b64.URLEncoding.EncodeToString([]byte(volumePath))))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove each file using os.Remove? It's safer.

}

// VolumeMountInfo retrieves the mount info of a direct volume.
func VolumeMountInfo(volumePath string) (*MountInfo, error) {
mountInfoFilePath := filepath.Join(kataDirectVolumeRootPath, b64.URLEncoding.EncodeToString([]byte(volumePath)), mountInfoFileName)
if _, err := os.Stat(mountInfoFilePath); err != nil {
return nil, err
}
buf, err := os.ReadFile(mountInfoFilePath)
if err != nil {
return nil, err
}
var mountInfo MountInfo
if err := json.Unmarshal(buf, &mountInfo); err != nil {
return nil, err
}
return &mountInfo, nil
}
9 changes: 9 additions & 0 deletions pkg/utils/kata/directvolume/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package directvolume

import "encoding/json"

func AddMountInfo(volumePath string, mountInfo MountInfo) error {
data, _ := json.Marshal(mountInfo)

return Add(volumePath, string(data))
}