Skip to content

Commit

Permalink
Refactor disk collector to use homedir
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidNix committed Aug 29, 2023
1 parent 02e9765 commit 7c5d4fb
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
4 changes: 2 additions & 2 deletions internal/fullnode/pvc_disk_usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

// DiskUsager fetches disk usage statistics
type DiskUsager interface {
DiskUsage(ctx context.Context, host string) (healthcheck.DiskUsageResponse, error)
DiskUsage(ctx context.Context, host, homeDir string) (healthcheck.DiskUsageResponse, error)
}

type PVCDiskUsage struct {
Expand Down Expand Up @@ -66,7 +66,7 @@ func (c DiskUsageCollector) CollectDiskUsage(ctx context.Context, crd *cosmosv1.
pod := pods.Items[i]
cctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
resp, err := c.diskClient.DiskUsage(cctx, "http://"+pod.Status.PodIP)
resp, err := c.diskClient.DiskUsage(cctx, "http://"+pod.Status.PodIP, ChainHomeDir(crd))
if err != nil {
errs[i] = fmt.Errorf("pod %s %s: %w", pod.Name, resp.Dir, err)
return nil
Expand Down
47 changes: 39 additions & 8 deletions internal/fullnode/pvc_disk_usage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
)

type mockDiskUsager func(ctx context.Context, host string) (healthcheck.DiskUsageResponse, error)
type mockDiskUsager func(ctx context.Context, host, homeDir string) (healthcheck.DiskUsageResponse, error)

func (fn mockDiskUsager) DiskUsage(ctx context.Context, host string) (healthcheck.DiskUsageResponse, error) {
return fn(ctx, host)
func (fn mockDiskUsager) DiskUsage(ctx context.Context, host, homeDir string) (healthcheck.DiskUsageResponse, error) {
return fn(ctx, host, homeDir)
}

func TestCollectDiskUsage(t *testing.T) {
Expand Down Expand Up @@ -55,7 +55,10 @@ func TestCollectDiskUsage(t *testing.T) {
},
}

diskClient := mockDiskUsager(func(ctx context.Context, host string) (healthcheck.DiskUsageResponse, error) {
diskClient := mockDiskUsager(func(ctx context.Context, host, homeDir string) (healthcheck.DiskUsageResponse, error) {
if homeDir != "/home/operator/cosmos" {
return healthcheck.DiskUsageResponse{}, fmt.Errorf("unexpected homeDir: %s", homeDir)
}
var free uint64
switch host {
case "http://10.0.0.0":
Expand Down Expand Up @@ -111,9 +114,37 @@ func TestCollectDiskUsage(t *testing.T) {
require.Equal(t, resource.MustParse("500Gi"), result.Capacity)
})

t.Run("custom home dir", func(t *testing.T) {
var reader mockReader
reader.ObjectList = corev1.PodList{Items: validPods}
reader.Object = corev1.PersistentVolumeClaim{
Status: corev1.PersistentVolumeClaimStatus{
Capacity: corev1.ResourceList{corev1.ResourceStorage: resource.MustParse("500Gi")},
},
}

diskClient := mockDiskUsager(func(ctx context.Context, host, homeDir string) (healthcheck.DiskUsageResponse, error) {
if homeDir != "/home/operator/.gaia" {
return healthcheck.DiskUsageResponse{}, fmt.Errorf("unexpected homeDir: %s", homeDir)
}
return healthcheck.DiskUsageResponse{
AllBytes: 1000,
FreeBytes: 900,
}, nil
})

coll := NewDiskUsageCollector(diskClient, &reader)

ccrd := crd.DeepCopy()
ccrd.Spec.ChainSpec.HomeDir = ".gaia"
_, err := coll.CollectDiskUsage(ctx, ccrd)

require.NoError(t, err)
})

t.Run("no pods found", func(t *testing.T) {
var reader mockReader
diskClient := mockDiskUsager(func(ctx context.Context, host string) (healthcheck.DiskUsageResponse, error) {
diskClient := mockDiskUsager(func(ctx context.Context, host, homeDir string) (healthcheck.DiskUsageResponse, error) {
panic("should not be called")
})

Expand All @@ -130,7 +161,7 @@ func TestCollectDiskUsage(t *testing.T) {
{ObjectMeta: metav1.ObjectMeta{Name: "pod-1"}, Status: corev1.PodStatus{PodIP: "10.0.0.1"}},
}}
reader.ListErr = errors.New("boom")
diskClient := mockDiskUsager(func(ctx context.Context, host string) (healthcheck.DiskUsageResponse, error) {
diskClient := mockDiskUsager(func(ctx context.Context, host, homeDir string) (healthcheck.DiskUsageResponse, error) {
panic("should not be called")
})

Expand All @@ -145,7 +176,7 @@ func TestCollectDiskUsage(t *testing.T) {
var reader mockReader
reader.ObjectList = corev1.PodList{Items: validPods}

diskClient := mockDiskUsager(func(ctx context.Context, host string) (healthcheck.DiskUsageResponse, error) {
diskClient := mockDiskUsager(func(ctx context.Context, host, homeDir string) (healthcheck.DiskUsageResponse, error) {
if host == "http://10.0.0.1" {
return healthcheck.DiskUsageResponse{}, errors.New("boom")
}
Expand Down Expand Up @@ -174,7 +205,7 @@ func TestCollectDiskUsage(t *testing.T) {
{ObjectMeta: metav1.ObjectMeta{Name: "2"}, Status: corev1.PodStatus{PodIP: "10.0.0.2"}},
}}

diskClient := mockDiskUsager(func(ctx context.Context, host string) (healthcheck.DiskUsageResponse, error) {
diskClient := mockDiskUsager(func(ctx context.Context, host, homeDir string) (healthcheck.DiskUsageResponse, error) {
return healthcheck.DiskUsageResponse{Dir: "/some/dir"}, errors.New("boom")
})

Expand Down

0 comments on commit 7c5d4fb

Please sign in to comment.