Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
andyzhangx committed Aug 13, 2023
1 parent 60f88f9 commit fc7cf11
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 62 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ require (
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.2.0
github.com/jongio/azidext/go/azidext v0.4.0
github.com/onsi/ginkgo/v2 v2.11.0
golang.org/x/sys v0.10.0
k8s.io/pod-security-admission v0.27.3
)

Expand Down Expand Up @@ -132,7 +133,6 @@ require (
golang.org/x/crypto v0.11.0 // indirect
golang.org/x/oauth2 v0.0.0-20220223155221-ee480838109b // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/term v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8 // indirect
Expand Down
5 changes: 5 additions & 0 deletions pkg/azurefile/azure_common_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,8 @@ func preparePublishPath(path string, m *mount.SafeFormatAndMount) error {
func prepareStagePath(path string, m *mount.SafeFormatAndMount) error {
return nil
}

// GetVolumeStats returns volume stats based on the given path.
func GetVolumeStats(path string) (*csi.NodeGetVolumeStatsResponse, error) {
return nil, status.Errorf(codes.Internal, "GetVolumeStats is not supported on darwin")
}
57 changes: 57 additions & 0 deletions pkg/azurefile/azure_common_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ limitations under the License.
package azurefile

import (
"github.com/container-storage-interface/spec/lib/go/csi"

"k8s.io/kubernetes/pkg/volume"
mount "k8s.io/mount-utils"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

func SMBMount(m *mount.SafeFormatAndMount, source, target, fsType string, options, sensitiveMountOptions []string) error {
Expand All @@ -38,3 +44,54 @@ func preparePublishPath(path string, m *mount.SafeFormatAndMount) error {
func prepareStagePath(path string, m *mount.SafeFormatAndMount) error {
return nil
}

// GetVolumeStats returns volume stats based on the given path.
func GetVolumeStats(path string) (*csi.NodeGetVolumeStatsResponse, error) {
volumeMetrics, err := volume.NewMetricsStatFS(path).GetMetrics()
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get metrics: %v", err)
}

available, ok := volumeMetrics.Available.AsInt64()
if !ok {
return nil, status.Errorf(codes.Internal, "failed to transform volume available size(%v)", volumeMetrics.Available)
}
capacity, ok := volumeMetrics.Capacity.AsInt64()
if !ok {
return nil, status.Errorf(codes.Internal, "failed to transform volume capacity size(%v)", volumeMetrics.Capacity)
}
used, ok := volumeMetrics.Used.AsInt64()
if !ok {
return nil, status.Errorf(codes.Internal, "failed to transform volume used size(%v)", volumeMetrics.Used)
}

inodesFree, ok := volumeMetrics.InodesFree.AsInt64()
if !ok {
return nil, status.Errorf(codes.Internal, "failed to transform disk inodes free(%v)", volumeMetrics.InodesFree)
}
inodes, ok := volumeMetrics.Inodes.AsInt64()
if !ok {
return nil, status.Errorf(codes.Internal, "failed to transform disk inodes(%v)", volumeMetrics.Inodes)
}
inodesUsed, ok := volumeMetrics.InodesUsed.AsInt64()
if !ok {
return nil, status.Errorf(codes.Internal, "failed to transform disk inodes used(%v)", volumeMetrics.InodesUsed)
}

return &csi.NodeGetVolumeStatsResponse{
Usage: []*csi.VolumeUsage{
{
Unit: csi.VolumeUsage_BYTES,
Available: available,
Total: capacity,
Used: used,
},
{
Unit: csi.VolumeUsage_INODES,
Available: inodesFree,
Total: inodes,
Used: inodesUsed,
},
},
}, nil
}
32 changes: 32 additions & 0 deletions pkg/azurefile/azure_common_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ package azurefile
import (
"fmt"

"github.com/container-storage-interface/spec/lib/go/csi"
"golang.org/x/sys/windows"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"k8s.io/klog/v2"
mount "k8s.io/mount-utils"
"sigs.k8s.io/azurefile-csi-driver/pkg/mounter"
Expand Down Expand Up @@ -69,3 +73,31 @@ func preparePublishPath(path string, m *mount.SafeFormatAndMount) error {
func prepareStagePath(path string, m *mount.SafeFormatAndMount) error {
return removeDir(path, m)
}

// GetFreeSpace returns the free space of the volume in bytes, total size of the volume in bytes and the used space of the volume in bytes
func GetFreeSpace(path string) (int64, int64, int64, error) {
var totalNumberOfBytes, totalNumberOfFreeBytes uint64
dirName := windows.StringToUTF16Ptr(path)
if err := windows.GetDiskFreeSpaceEx(dirName, nil, &totalNumberOfBytes, &totalNumberOfFreeBytes); err != nil {
return 0, 0, 0, err
}
return int64(totalNumberOfFreeBytes), int64(totalNumberOfBytes), int64(totalNumberOfBytes - totalNumberOfFreeBytes), nil
}

// GetVolumeStats returns volume stats based on the given path.
func GetVolumeStats(path string) (*csi.NodeGetVolumeStatsResponse, error) {
freeBytesAvailable, totalBytes, totalBytesUsed, err := GetFreeSpace(path)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get free space on path %s: %v", path, err)
}
return &csi.NodeGetVolumeStatsResponse{
Usage: []*csi.VolumeUsage{
{
Unit: csi.VolumeUsage_BYTES,
Available: freeBytesAvailable,
Total: totalBytes,
Used: totalBytesUsed,
},
},
}, nil
}
62 changes: 1 addition & 61 deletions pkg/azurefile/nodeserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,12 @@ import (

"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/klog/v2"
"k8s.io/kubernetes/pkg/volume"
"k8s.io/kubernetes/pkg/volume/util"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"golang.org/x/net/context"
"golang.org/x/sys/windows"
)

// NodePublishVolume mount the volume from staging to target path
Expand Down Expand Up @@ -433,65 +431,7 @@ func (d *Driver) NodeGetVolumeStats(ctx context.Context, req *csi.NodeGetVolumeS
return nil, status.Errorf(codes.Internal, "failed to stat file %s: %v", req.VolumePath, err)
}

volumeMetrics, err := volume.NewMetricsStatFS(req.VolumePath).GetMetrics()
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get metrics: %v", err)
}

available, ok := volumeMetrics.Available.AsInt64()
if !ok {
return nil, status.Errorf(codes.Internal, "failed to transform volume available size(%v)", volumeMetrics.Available)
}
capacity, ok := volumeMetrics.Capacity.AsInt64()
if !ok {
return nil, status.Errorf(codes.Internal, "failed to transform volume capacity size(%v)", volumeMetrics.Capacity)
}
used, ok := volumeMetrics.Used.AsInt64()
if !ok {
return nil, status.Errorf(codes.Internal, "failed to transform volume used size(%v)", volumeMetrics.Used)
}

if runtime.GOOS == "windows" {
var totalNumberOfBytes, totalNumberOfFreeBytes uint64
dirName := windows.StringToUTF16Ptr(req.VolumePath)
if err := windows.GetDiskFreeSpaceEx(dirName, nil, &totalNumberOfBytes, &totalNumberOfFreeBytes); err != nil {
return nil, status.Errorf(codes.Internal, "run GetDiskFreeSpaceEx(%s) failed with %v", req.VolumePath, err)
}
available = int64(totalNumberOfFreeBytes)
capacity = int64(totalNumberOfBytes)
used = int64(totalNumberOfBytes - totalNumberOfFreeBytes)
klog.V(2).Infof("NodeGetVolumeStats: volume %s available %d capacity %d used %d", req.VolumePath, available, capacity, used)
}

inodesFree, ok := volumeMetrics.InodesFree.AsInt64()
if !ok {
return nil, status.Errorf(codes.Internal, "failed to transform disk inodes free(%v)", volumeMetrics.InodesFree)
}
inodes, ok := volumeMetrics.Inodes.AsInt64()
if !ok {
return nil, status.Errorf(codes.Internal, "failed to transform disk inodes(%v)", volumeMetrics.Inodes)
}
inodesUsed, ok := volumeMetrics.InodesUsed.AsInt64()
if !ok {
return nil, status.Errorf(codes.Internal, "failed to transform disk inodes used(%v)", volumeMetrics.InodesUsed)
}

return &csi.NodeGetVolumeStatsResponse{
Usage: []*csi.VolumeUsage{
{
Unit: csi.VolumeUsage_BYTES,
Available: available,
Total: capacity,
Used: used,
},
{
Unit: csi.VolumeUsage_INODES,
Available: inodesFree,
Total: inodes,
Used: inodesUsed,
},
},
}, nil
return GetVolumeStats(req.VolumePath)
}

// NodeExpandVolume node expand volume
Expand Down

0 comments on commit fc7cf11

Please sign in to comment.