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

fix: volume metrics on Windows csi-proxy v1beta #1622

Merged
merged 1 commit into from
Nov 29, 2022

Conversation

andyzhangx
Copy link
Member

@andyzhangx andyzhangx commented Nov 28, 2022

What type of PR is this?
/kind bug

What this PR does / why we need it:
fix: volume metrics on Windows csi-proxy v1beta

Which issue(s) this PR fixes:

Fixes #

Requirements:

Special notes for your reviewer:

  • correct NodeGetVolumeStats response
I1128 13:06:41.438471    6376 utils.go:77] GRPC call: /csi.v1.Node/NodeGetVolumeStats
I1128 13:06:41.438998    6376 utils.go:78] GRPC request: {"volume_id":"/subscriptions/0e46bd28-a80f-4d3a-8200-d9eb8d80cb2e/resourceGroups/kubetest-5zrwevie/providers/Microsoft.Compute/disks/pvc-b7977a80-d0b0-4b7a-bf70-10e16ae07123","volume_path":"c:\\var\\lib\\kubelet\\pods\\e94d925a-9045-4fe8-a834-d7403a5dade2\\volumes\\kubernetes.io~csi\\pvc-b7977a80-d0b0-4b7a-bf70-10e16ae07123\\mount"}
I1128 13:06:41.982617    6376 safe_mounter_v1beta_windows.go:333] GetVolumeStats(c:\var\lib\kubelet\pods\e94d925a-9045-4fe8-a834-d7403a5dade2\volumes\kubernetes.io~csi\pvc-b7977a80-d0b0-4b7a-bf70-10e16ae07123\mount) returned volumeID(\\?\Volume{2e73efca-0000-0000-0000-100000000000}\)
I1128 13:06:44.452604    6376 utils.go:84] GRPC response: {"usage":[{"available":10692947968,"total":10735316992,"unit":1,"used":42369024}]}
// https://github.com/kubernetes-csi/csi-proxy/blob/v0.2.2/internal/os/volume/api.go#L157-L180
// VolumeStats - retrieves the volume stats for a given volume
func (VolAPIImplementor) VolumeStats(volumeID string) (int64, int64, error) {
        // get the size and sizeRemaining for the volume
        cmd := fmt.Sprintf("(Get-Volume -UniqueId \"%s\" | Select SizeRemaining,Size) | ConvertTo-Json", volumeID)
        out, err := runExec(cmd)

        if err != nil {
                return -1, -1, fmt.Errorf("error getting capacity and used size of volume. cmd: %s, output: %s, error: %v", cmd, string(out), err)
        }

        var getVolume map[string]int64
        outString := string(out)
        err = json.Unmarshal([]byte(outString), &getVolume)
        if err != nil {
                return -1, -1, fmt.Errorf("out %v outstring %v err %v", out, outString, err)
        }
        var volumeSizeRemaining int64
        var volumeSize int64

        volumeSize = getVolume["Size"]
        volumeSizeRemaining = getVolume["SizeRemaining"]

        volumeUsedSize := volumeSize - volumeSizeRemaining
        return volumeSizeRemaining, volumeUsedSize, nil
}

// https://github.com/kubernetes-csi/csi-proxy/blob/v0.2.2/internal/server/volume/server.go#L195
func (s *Server) VolumeStats(context context.Context, request *internal.VolumeStatsRequest, version apiversion.Version) (*internal.VolumeStatsResponse, error) {
        klog.V(4).Infof("calling VolumeStats with request: %+v", request)
        minimumVersion := apiversion.NewVersionOrPanic("v1beta1")
        if version.Compare(minimumVersion) < 0 {
                return nil, fmt.Errorf("VolumeStats requires CSI-Proxy API version v1beta1 or greater")
        }

        volumeId := request.VolumeId
        if volumeId == "" {
                return nil, fmt.Errorf("volume id empty")
        }

        capacity, used, err := s.hostAPI.VolumeStats(request.VolumeId)

        if err != nil {
                klog.Errorf("failed VolumeStats %v", err)
                return nil, err
        }

        klog.V(5).Infof("VolumeStats: returned: Capacity %v Used %v", capacity, used)

        response := &internal.VolumeStatsResponse{
                VolumeSize:     capacity,
                VolumeUsedSize: used,
        }

        return response, nil
}

https://github.com/kubernetes-csi/csi-proxy/blob/v1.0.0/pkg/os/volume/api.go
// GetVolumeStats - retrieves the volume stats for a given volume
func (VolumeAPI) GetVolumeStats(volumeID string) (int64, int64, error) {
	// get the size and sizeRemaining for the volume
	cmd := fmt.Sprintf("(Get-Volume -UniqueId \"%s\" | Select SizeRemaining,Size) | ConvertTo-Json", volumeID)
	out, err := runExec(cmd)

	if err != nil {
		return -1, -1, fmt.Errorf("error getting capacity and used size of volume. cmd: %s, output: %s, error: %v", cmd, string(out), err)
	}

	var getVolume map[string]int64
	outString := string(out)
	err = json.Unmarshal([]byte(outString), &getVolume)
	if err != nil {
		return -1, -1, fmt.Errorf("out %v outstring %v err %v", out, outString, err)
	}
	var volumeSizeRemaining int64
	var volumeSize int64

	volumeSize = getVolume["Size"]
	volumeSizeRemaining = getVolume["SizeRemaining"]

	volumeUsedSize := volumeSize - volumeSizeRemaining
	return volumeSize, volumeUsedSize, nil
}

https://github.com/kubernetes-csi/csi-proxy/blob/v1.0.0/pkg/server/volume/server.go#L184-L187
func (s *Server) GetVolumeStats(context context.Context, request *internal.GetVolumeStatsRequest, version apiversion.Version) (*internal.GetVolumeStatsResponse, error) {
	klog.V(2).Infof("GetVolumeStats: Request: %+v", request)
	volumeID := request.VolumeId
	if volumeID == "" {
		return nil, fmt.Errorf("volume id empty")
	}

	totalBytes, usedBytes, err := s.hostAPI.GetVolumeStats(volumeID)
	if err != nil {
		klog.Errorf("failed GetVolumeStats %v", err)
		return nil, err
	}

	klog.V(2).Infof("VolumeStats: returned: Capacity %v Used %v", totalBytes, usedBytes)

	response := &internal.GetVolumeStatsResponse{
		TotalBytes: totalBytes,
		UsedBytes:  usedBytes,
	}

	return response, nil
}

Release note:

fix: volume metrics on Windows csi-proxy v1beta

@k8s-ci-robot k8s-ci-robot added kind/bug Categorizes issue or PR as related to a bug. do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Nov 28, 2022
@k8s-ci-robot k8s-ci-robot added approved Indicates a PR has been approved by an approver from all required OWNERS files. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Nov 28, 2022
@andyzhangx andyzhangx removed the request for review from edreed November 28, 2022 11:12
@andyzhangx
Copy link
Member Author

/test pull-azuredisk-csi-driver-e2e-migration-windows

@andyzhangx andyzhangx changed the title [WIP]fix: volume metrics on Windows csi-proxy v1beta fix: volume metrics on Windows csi-proxy v1beta Nov 29, 2022
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Nov 29, 2022
Copy link
Member

@ZeroMagic ZeroMagic left a comment

Choose a reason for hiding this comment

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

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Nov 29, 2022
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: andyzhangx, ZeroMagic

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@andyzhangx
Copy link
Member Author

/retest

1 similar comment
@andyzhangx
Copy link
Member Author

/retest

@k8s-ci-robot k8s-ci-robot merged commit 77a7a42 into kubernetes-sigs:master Nov 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/bug Categorizes issue or PR as related to a bug. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants