Skip to content

Commit

Permalink
Merge pull request #197 from digitalocean/implement-get-volume-stats-rpc
Browse files Browse the repository at this point in the history
Implement NodeGetVolumeStats RPC
  • Loading branch information
Timo Reimann authored Nov 12, 2019
2 parents 6494e90 + e817678 commit 67cd8b3
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 16 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## unreleased

* Implement NodeGetVolumeStats RPC
[[GH-197](https://github.com/digitalocean/csi-digitalocean/pull/197)]

## v1.1.2 - 2019.09.17

* Improve error messages for incorrectly attached volumes
Expand Down
27 changes: 23 additions & 4 deletions driver/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ func TestDriverSuite(t *testing.T) {
nodeId: strconv.Itoa(nodeID),
doTag: doTag,
region: "nyc3",
mounter: &fakeMounter{},
log: logrus.New().WithField("test_enabed", true),
mounter: &fakeMounter{
mounted: map[string]string{},
},
log: logrus.New().WithField("test_enabed", true),

storage: &fakeStorageDriver{
volumes: volumes,
Expand Down Expand Up @@ -360,25 +362,42 @@ func (f *fakeSnapshotsDriver) Delete(context.Context, string) (*godo.Response, e
panic("not implemented")
}

type fakeMounter struct{}
type fakeMounter struct {
mounted map[string]string
}

func (f *fakeMounter) Format(source string, fsType string) error {
return nil
}

func (f *fakeMounter) Mount(source string, target string, fsType string, options ...string) error {
f.mounted[target] = source
return nil
}

func (f *fakeMounter) Unmount(target string) error {
delete(f.mounted, target)
return nil
}

func (f *fakeMounter) IsFormatted(source string) (bool, error) {
return true, nil
}
func (f *fakeMounter) IsMounted(target string) (bool, error) {
return true, nil
_, ok := f.mounted[target]
return ok, nil
}

func (f *fakeMounter) GetStatistics(volumePath string) (volumeStatistics, error) {
return volumeStatistics{
availableBytes: 3 * giB,
totalBytes: 10 * giB,
usedBytes: 7 * giB,

availableInodes: 3000,
totalInodes: 10000,
usedInodes: 7000,
}, nil
}

func godoResponse() *godo.Response {
Expand Down
33 changes: 33 additions & 0 deletions driver/mounter.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"strings"

"github.com/sirupsen/logrus"
"golang.org/x/sys/unix"
)

type findmntResponse struct {
Expand All @@ -38,7 +39,14 @@ type fileSystem struct {
Options string `json:"options"`
}

type volumeStatistics struct {
availableBytes, totalBytes, usedBytes int64
availableInodes, totalInodes, usedInodes int64
}

// Mounter is responsible for formatting and mounting volumes
// TODO(timoreimann): find a more suitable name since the interface encompasses
// more than just mounting functionality by now.
type Mounter interface {
// Format formats the source with the given filesystem type
Format(source, fsType string) error
Expand All @@ -57,6 +65,10 @@ type Mounter interface {
// propagated). It returns true if it's mounted. An error is returned in
// case of system errors or if it's mounted incorrectly.
IsMounted(target string) (bool, error)

// GetStatistics returns capacity-related volume statistics for the given
// volume path.
GetStatistics(volumePath string) (volumeStatistics, error)
}

// TODO(arslan): this is Linux only for now. Refactor this into a package with
Expand Down Expand Up @@ -272,3 +284,24 @@ func (m *mounter) IsMounted(target string) (bool, error) {

return targetFound, nil
}

func (m *mounter) GetStatistics(volumePath string) (volumeStatistics, error) {
var statfs unix.Statfs_t
// See http://man7.org/linux/man-pages/man2/statfs.2.html for details.
err := unix.Statfs(volumePath, &statfs)
if err != nil {
return volumeStatistics{}, err
}

volStats := volumeStatistics{
availableBytes: int64(statfs.Bavail) * int64(statfs.Bsize),
totalBytes: int64(statfs.Blocks) * int64(statfs.Bsize),
usedBytes: (int64(statfs.Blocks) - int64(statfs.Bfree)) * int64(statfs.Bsize),

availableInodes: int64(statfs.Ffree),
totalInodes: int64(statfs.Files),
usedInodes: int64(statfs.Files) - int64(statfs.Ffree),
}

return volStats, nil
}
77 changes: 65 additions & 12 deletions driver/node.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ require (
github.com/sirupsen/logrus v1.2.0
github.com/spf13/pflag v1.0.2 // indirect
golang.org/x/oauth2 v0.0.0-20190402181905-9f3314589c9a
golang.org/x/sys v0.0.0-20190312061237-fead79001313
golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2 // indirect
google.golang.org/genproto v0.0.0-20180409222037-51d0944304c3 // indirect
google.golang.org/grpc v1.13.0
Expand Down

0 comments on commit 67cd8b3

Please sign in to comment.