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

feat(nas): automatic CNFS switching when mounting NAS volumes #1262

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
24 changes: 24 additions & 0 deletions pkg/cnfs/v1beta1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ type ContainerNetworkFileSystemStatus struct {
Conditions []ContainerNetworkFileSystemCondition `json:"conditions,omitempty"`
}

const (
StatusPending = "Pending"
StatusCreating = "Creating"
StatusInit = "Initialization"
StatusAvailable = "Available"
StatusUnavailable = "Unavailable"
StatusTerminating = "Terminating"
StatusFatal = "Fatal"
)

// ContainerNetworkFileSystemCondition define cnfs condition field
type ContainerNetworkFileSystemCondition struct {
LastProbeTime string `json:"lastProbeTime,omitempty"`
Expand All @@ -37,12 +47,26 @@ type ContainerNetworkFileSystemCondition struct {

// ContainerNetworkFileSystemSpec define cnfs spec field
type ContainerNetworkFileSystemSpec struct {
Fallback Fallback `json:"fallback,omitempty"`
StorageType string `json:"type,omitempty"`
ReclaimPolicy string `json:"reclaimPolicy,omitempty"`
Description string `json:"description,omitempty"`
Parameters Parameters `json:"parameters,omitempty"`
}

type Fallback struct {
Name string `json:"name,omitempty"`
Strategy FallbackStrategy `json:"strategy,omitempty"`
}

type FallbackStrategy string

const (
FallbackStrategyAlways FallbackStrategy = "Always"
FallbackStrategyIfConnectFailed FallbackStrategy = "IfConnectFailed"
FallbackStrategyIfMountTargetUnhealthy FallbackStrategy = "IfMountTargetUnhealthy"
)

// FsAttributes define cnfs status FsAttributes field
type FsAttributes struct {
RegionID string `json:"regionId,omitempty"`
Expand Down
74 changes: 66 additions & 8 deletions pkg/nas/nodeserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,17 @@ import (
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/utils/rund/directvolume"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
v1 "k8s.io/api/core/v1"
"k8s.io/client-go/tools/record"
"k8s.io/klog/v2"
mountutils "k8s.io/mount-utils"
)

type nodeServer struct {
config *internal.NodeConfig
mounter mountutils.Interface
locks *utils.VolumeLocks
config *internal.NodeConfig
mounter mountutils.Interface
locks *utils.VolumeLocks
recorder record.EventRecorder
common.GenericNodeServer
}

Expand All @@ -59,9 +62,10 @@ func newNodeServer(config *internal.NodeConfig) *nodeServer {
klog.Errorf("failed to config /proc/sys/sunrpc/tcp_slot_table_entries: %v", err)
}
return &nodeServer{
config: config,
mounter: NewNasMounter(),
locks: utils.NewVolumeLocks(),
config: config,
mounter: NewNasMounter(),
locks: utils.NewVolumeLocks(),
recorder: utils.NewEventRecorder(),
GenericNodeServer: common.GenericNodeServer{
NodeID: config.NodeName,
},
Expand Down Expand Up @@ -124,7 +128,10 @@ const (
//NFSClient
NFSClient = "nfsclient"
//NativeClient
NativeClient = "nativeclient"
NativeClient = "nativeclient"
cnfsAlwaysFallbackEventTmpl = "CNFS automatically switched from %s to %s."
cnfsIfConnectFailedFallbackEventTmpl = "Due to network issues, CNFS automatically switched from %s to %s."
cnfsIfMountTargetUnhealthyFallbackEventTmpl = "Due to mount target inactive, CNFS automatically switched from %s to %s."
)

func validateNodePublishVolumeRequest(req *csi.NodePublishVolumeRequest) error {
Expand Down Expand Up @@ -225,7 +232,7 @@ func (ns *nodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublis
}

if cnfsName != "" {
cnfs, err := ns.config.CNFSGetter.GetCNFS(ctx, cnfsName)
cnfs, err := ns.getCNFS(ctx, req, cnfsName)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -455,6 +462,57 @@ func (ns *nodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublis
return &csi.NodePublishVolumeResponse{}, nil
}

func (ns *nodeServer) getCNFS(ctx context.Context, req *csi.NodePublishVolumeRequest, name string) (*v1beta1.ContainerNetworkFileSystem, error) {
cnfs, err := ns.config.CNFSGetter.GetCNFS(ctx, name)
if err != nil {
return nil, err
}
if cnfs.Spec.Fallback.Name == "" || !cnfsNeedsFallback(cnfs) {
return cnfs, nil
}
return ns.fallbackCNFSAndRecord(ctx, req, cnfs)
}

func cnfsNeedsFallback(cnfs *v1beta1.ContainerNetworkFileSystem) bool {
if cnfs == nil {
return false
}
switch cnfs.Spec.Fallback.Strategy {
case v1beta1.FallbackStrategyAlways:
return true
case v1beta1.FallbackStrategyIfConnectFailed:
server := cnfs.Spec.Parameters.Server
dialer := net.Dialer{Timeout: 5 * time.Second}
_, err := dialer.Dial("tcp", server+":2049")
Copy link
Contributor

Choose a reason for hiding this comment

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

Better to use DialContext and explicitly close the returned conn object.

return err != nil
case v1beta1.FallbackStrategyIfMountTargetUnhealthy:
return cnfs.Status.Status == v1beta1.StatusUnavailable
}
return false
}

func (ns *nodeServer) fallbackCNFSAndRecord(ctx context.Context, req *csi.NodePublishVolumeRequest, cnfs *v1beta1.ContainerNetworkFileSystem) (*v1beta1.ContainerNetworkFileSystem, error) {
oldName, newName := cnfs.Name, cnfs.Spec.Fallback.Name
pod, err := utils.GetPodFromContextOrK8s(ctx, ns.config.KubeClient, req)
if 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.

I think we can just skip the event if this fails?

return nil, err
}
fallbackCNFS, err := ns.config.CNFSGetter.GetCNFS(ctx, newName)
if err != nil {
return nil, err
}

switch cnfs.Spec.Fallback.Strategy {
case v1beta1.FallbackStrategyAlways:
ns.recorder.Eventf(pod, v1.EventTypeWarning, "CNFSFallback", cnfsAlwaysFallbackEventTmpl, oldName, newName)
case v1beta1.FallbackStrategyIfConnectFailed:
ns.recorder.Eventf(pod, v1.EventTypeWarning, "CNFSFallback", cnfsIfConnectFailedFallbackEventTmpl, oldName, newName)
case v1beta1.FallbackStrategyIfMountTargetUnhealthy:
ns.recorder.Eventf(pod, v1.EventTypeWarning, "CNFSFallback", cnfsIfMountTargetUnhealthyFallbackEventTmpl, oldName, newName)
}
return fallbackCNFS, err
}

// /var/lib/kubelet/pods/5e03c7f7-2946-4ee1-ad77-2efbc4fdb16c/volumes/kubernetes.io~csi/nas-f5308354-725a-4fd3-b613-0f5b384bd00e/mount
func (ns *nodeServer) mountLosetupPv(mountPoint string, opt *Options, volumeID string) error {
pathList := strings.Split(mountPoint, "/")
Expand Down
202 changes: 202 additions & 0 deletions pkg/nas/nodeserver_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
package nas

import (
"context"
"fmt"
"net"
"testing"

"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/cnfs/v1beta1"
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/nas/internal"
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/utils"
"github.com/stretchr/testify/assert"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/tools/record"
)

type fakeCNFSGetter struct {
cnfsMap map[string]*v1beta1.ContainerNetworkFileSystem
}

func newFakeCNFSGetter(cnfsList ...*v1beta1.ContainerNetworkFileSystem) *fakeCNFSGetter {
cnfsMap := make(map[string]*v1beta1.ContainerNetworkFileSystem)
for _, cnfs := range cnfsList {
if cnfs != nil {
cnfsMap[cnfs.Name] = cnfs
}
}
return &fakeCNFSGetter{cnfsMap}
}

func (f *fakeCNFSGetter) GetCNFS(_ context.Context, name string) (*v1beta1.ContainerNetworkFileSystem, error) {
if cnfs, ok := f.cnfsMap[name]; ok {
return cnfs, nil
}
return nil, fmt.Errorf("CNFS %s not found", name)
}

func startListeningFor(addr string) {
listener, err := net.Listen("tcp", addr)
if err != nil {
panic(err)
}
defer listener.Close()

for {
conn, _ := listener.Accept()
if conn != nil {
conn.Close()
}
}
Comment on lines +48 to +53
Copy link
Contributor

Choose a reason for hiding this comment

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

This will not terminate after the test case finishes. How about making use of t.Cleanup()?

}

func fakeCNFS(name, status, server, fallbackName string, fallbackStrategy v1beta1.FallbackStrategy) *v1beta1.ContainerNetworkFileSystem {
return &v1beta1.ContainerNetworkFileSystem{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Status: v1beta1.ContainerNetworkFileSystemStatus{
Status: status,
},
Spec: v1beta1.ContainerNetworkFileSystemSpec{
Fallback: v1beta1.Fallback{
Name: fallbackName,
Strategy: fallbackStrategy,
},
Parameters: v1beta1.Parameters{
Server: server,
},
},
}
}

func TestCNFSNeedsFallback(t *testing.T) {
tests := []struct {
name string
cnfs *v1beta1.ContainerNetworkFileSystem
expected bool
}{
{
name: "Nil CNFS",
cnfs: nil,
expected: false,
},
{
name: "Always fallback strategy",
cnfs: fakeCNFS("", "", "", "", v1beta1.FallbackStrategyAlways),
expected: true,
},
{
name: "IfConnectFailed fallback strategy-server reachable",
cnfs: fakeCNFS("", "", "localhost", "", v1beta1.FallbackStrategyIfConnectFailed),
expected: false,
},
{
name: "IfMountTargetUnhealthy fallback strategy-status Available",
cnfs: fakeCNFS("", v1beta1.StatusAvailable, "", "", v1beta1.FallbackStrategyIfMountTargetUnhealthy),
expected: false,
},
{
name: "IfMountTargetUnhealthy fallback strategy-status Unavailable",
cnfs: fakeCNFS("", v1beta1.StatusUnavailable, "", "", v1beta1.FallbackStrategyIfMountTargetUnhealthy),
expected: true,
},
}

go startListeningFor("localhost:2049")
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual := cnfsNeedsFallback(tt.cnfs)
assert.Equal(t, tt.expected, actual)
})
}
}

func TestFallbackCNFSAndRecord(t *testing.T) {
client := fake.NewSimpleClientset(&v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "pod1",
Namespace: "default",
},
})
req := &csi.NodePublishVolumeRequest{
VolumeContext: map[string]string{
utils.PodNameKey: "pod1",
utils.PodNamespaceKey: "default",
},
}
eventRecorder := record.NewFakeRecorder(5)
ctx, _ := utils.WithPodInfo(context.Background(), client, req)

primaryCNFSName, fallbackCNFSName := "primary", "fallback"
tests := []struct {
name string
primaryCNFS *v1beta1.ContainerNetworkFileSystem
fallbackCNFS *v1beta1.ContainerNetworkFileSystem
expectFallback bool
expectErr bool
}{
{
name: "Always fallback strategy",
primaryCNFS: fakeCNFS(primaryCNFSName, "", "", fallbackCNFSName, v1beta1.FallbackStrategyAlways),
fallbackCNFS: fakeCNFS(fallbackCNFSName, "", "", "", ""),
expectFallback: true,
},
{
name: "IfConnectFailed fallback strategy",
primaryCNFS: fakeCNFS(primaryCNFSName, "", "", fallbackCNFSName, v1beta1.FallbackStrategyIfConnectFailed),
fallbackCNFS: fakeCNFS(fallbackCNFSName, "", "", "", ""),
expectFallback: true,
},
{
name: "IfMountTargetUnhealthy fallback strategy",
primaryCNFS: fakeCNFS(primaryCNFSName, v1beta1.StatusUnavailable, "", fallbackCNFSName, v1beta1.FallbackStrategyIfMountTargetUnhealthy),
fallbackCNFS: fakeCNFS(fallbackCNFSName, "", "", "", ""),
expectFallback: true,
},
{
name: "Non-existent fallback CNFS",
primaryCNFS: fakeCNFS("primary", "", "", "non-existent-cnfs", ""),
fallbackCNFS: fakeCNFS(fallbackCNFSName, "", "", "", ""),
expectErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cnfsGetter := newFakeCNFSGetter(tt.fallbackCNFS)
server := nodeServer{
config: &internal.NodeConfig{
KubeClient: client,
CNFSGetter: cnfsGetter,
},
recorder: eventRecorder,
}
actual, err := server.fallbackCNFSAndRecord(ctx, req, tt.primaryCNFS)
if tt.expectErr {
assert.Error(t, err)
} else {
if tt.expectFallback {
assert.Equal(t, *tt.fallbackCNFS, *actual)
assert.Len(t, eventRecorder.Events, 1)

oldCNFSName, newCNFSName := tt.primaryCNFS.Name, tt.fallbackCNFS.Name
msg := <-eventRecorder.Events
switch tt.primaryCNFS.Spec.Fallback.Strategy {
case v1beta1.FallbackStrategyAlways:
assert.Contains(t, msg, fmt.Sprintf(cnfsAlwaysFallbackEventTmpl, oldCNFSName, newCNFSName))
case v1beta1.FallbackStrategyIfConnectFailed:
assert.Contains(t, msg, fmt.Sprintf(cnfsIfConnectFailedFallbackEventTmpl, oldCNFSName, newCNFSName))
case v1beta1.FallbackStrategyIfMountTargetUnhealthy:
assert.Contains(t, msg, fmt.Sprintf(cnfsIfMountTargetUnhealthyFallbackEventTmpl, oldCNFSName, newCNFSName))
}
Comment on lines +188 to +195
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 add a expectedEvent field to tests, instead of copy pasting a large segment of code?

} else {
assert.Equal(t, *tt.primaryCNFS, *actual)
}
}
})
}
}
6 changes: 3 additions & 3 deletions pkg/utils/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ func GetPodRunTime(ctx context.Context, req *csi.NodePublishVolumeRequest, clien
}

func WithPodInfo(ctx context.Context, client kubernetes.Interface, req *csi.NodePublishVolumeRequest) (context.Context, *v1.Pod) {
pod, err := getPodFromK8s(ctx, client, req)
pod, err := getPodFromK8s(client, req)
if err != nil {
klog.Errorf("WithPodInfo: failed to get pod: %v", err)
return ctx, nil
Expand All @@ -478,10 +478,10 @@ func GetPodFromContextOrK8s(ctx context.Context, client kubernetes.Interface, re
if ok {
return pod, nil
}
return getPodFromK8s(ctx, client, req)
return getPodFromK8s(client, req)
}

func getPodFromK8s(ctx context.Context, client kubernetes.Interface, req *csi.NodePublishVolumeRequest) (*v1.Pod, error) {
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 passing the ctx to the Get() below, instead of removing it?

func getPodFromK8s(client kubernetes.Interface, req *csi.NodePublishVolumeRequest) (*v1.Pod, error) {
name, namespace := req.VolumeContext[PodNameKey], req.VolumeContext[PodNamespaceKey]
if name == "" || namespace == "" {
return nil, fmt.Errorf("empty pod name or namespace: '%s', '%s'", name, namespace)
Expand Down