Skip to content

Commit e28d25f

Browse files
vyasgunpraveenkumar
authored andcommitted
Display PersistentVolumeUsage in crc status
- Enhance the crc status command to display the PersistentVolumeUsage and PersistentVolumeSize from the CRC VM. - Fetch and display the persistent volume usage value in the crc status output. - Add new fields to the ClusterStatus and CrcStatus structs to support this feature. - Update the CRC daemon to include these changes.
1 parent 7c63a82 commit e28d25f

File tree

6 files changed

+107
-48
lines changed

6 files changed

+107
-48
lines changed

cmd/crc/cmd/status.go

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,20 @@ var statusCmd = &cobra.Command{
3939
}
4040

4141
type status struct {
42-
Success bool `json:"success"`
43-
Error *crcErrors.SerializableError `json:"error,omitempty"`
44-
CrcStatus string `json:"crcStatus,omitempty"`
45-
OpenShiftStatus types.OpenshiftStatus `json:"openshiftStatus,omitempty"`
46-
OpenShiftVersion string `json:"openshiftVersion,omitempty"`
47-
DiskUsage int64 `json:"diskUsage,omitempty"`
48-
DiskSize int64 `json:"diskSize,omitempty"`
49-
CacheUsage int64 `json:"cacheUsage,omitempty"`
50-
CacheDir string `json:"cacheDir,omitempty"`
51-
RAMSize int64 `json:"ramSize,omitempty"`
52-
RAMUsage int64 `json:"ramUsage,omitempty"`
53-
Preset preset.Preset `json:"preset"`
42+
Success bool `json:"success"`
43+
Error *crcErrors.SerializableError `json:"error,omitempty"`
44+
CrcStatus string `json:"crcStatus,omitempty"`
45+
OpenShiftStatus types.OpenshiftStatus `json:"openshiftStatus,omitempty"`
46+
OpenShiftVersion string `json:"openshiftVersion,omitempty"`
47+
DiskUsage int64 `json:"diskUsage,omitempty"`
48+
DiskSize int64 `json:"diskSize,omitempty"`
49+
CacheUsage int64 `json:"cacheUsage,omitempty"`
50+
CacheDir string `json:"cacheDir,omitempty"`
51+
RAMSize int64 `json:"ramSize,omitempty"`
52+
RAMUsage int64 `json:"ramUsage,omitempty"`
53+
PersistentVolumeUse int `json:"persistentVolumeUsage,omitempty"`
54+
PersistentVolumeSize int `json:"persistentVolumeSize,omitempty"`
55+
Preset preset.Preset `json:"preset"`
5456
}
5557

5658
func runStatus(writer io.Writer, client *daemonclient.Client, cacheDir, outputFormat string, watch bool) error {
@@ -163,17 +165,19 @@ func getStatus(client *daemonclient.Client, cacheDir string) *status {
163165
}
164166

165167
return &status{
166-
Success: true,
167-
CrcStatus: clusterStatus.CrcStatus,
168-
OpenShiftStatus: types.OpenshiftStatus(clusterStatus.OpenshiftStatus),
169-
OpenShiftVersion: clusterStatus.OpenshiftVersion,
170-
DiskUsage: clusterStatus.DiskUse,
171-
DiskSize: clusterStatus.DiskSize,
172-
RAMSize: clusterStatus.RAMSize,
173-
RAMUsage: clusterStatus.RAMUse,
174-
CacheUsage: size,
175-
CacheDir: cacheDir,
176-
Preset: clusterStatus.Preset,
168+
Success: true,
169+
CrcStatus: clusterStatus.CrcStatus,
170+
OpenShiftStatus: types.OpenshiftStatus(clusterStatus.OpenshiftStatus),
171+
OpenShiftVersion: clusterStatus.OpenshiftVersion,
172+
DiskUsage: clusterStatus.DiskUse,
173+
DiskSize: clusterStatus.DiskSize,
174+
RAMSize: clusterStatus.RAMSize,
175+
RAMUsage: clusterStatus.RAMUse,
176+
CacheUsage: size,
177+
PersistentVolumeUse: clusterStatus.PersistentVolumeUse,
178+
PersistentVolumeSize: clusterStatus.PersistentVolumeSize,
179+
CacheDir: cacheDir,
180+
Preset: clusterStatus.Preset,
177181
}
178182
}
179183

@@ -209,7 +213,14 @@ func (s *status) prettyPrintTo(writer io.Writer) error {
209213
line{"Disk Usage", fmt.Sprintf(
210214
"%s of %s (Inside the CRC VM)",
211215
units.HumanSize(float64(s.DiskUsage)),
212-
units.HumanSize(float64(s.DiskSize)))},
216+
units.HumanSize(float64(s.DiskSize)))})
217+
if s.Preset == preset.Microshift {
218+
lines = append(lines, line{"Persistent Volume Usage", fmt.Sprintf(
219+
"%s of %s (Allocated)",
220+
units.HumanSize(float64(s.PersistentVolumeUse)),
221+
units.HumanSize(float64(s.PersistentVolumeSize)))})
222+
}
223+
lines = append(lines,
213224
line{"Cache Usage", units.HumanSize(float64(s.CacheUsage))},
214225
line{"Cache Directory", s.CacheDir})
215226

pkg/crc/api/client/types.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,16 @@ type StartResult struct {
2020
}
2121

2222
type ClusterStatusResult struct {
23-
CrcStatus string
24-
OpenshiftStatus string
25-
OpenshiftVersion string `json:"OpenshiftVersion,omitempty"`
26-
DiskUse int64
27-
DiskSize int64
28-
RAMUse int64
29-
RAMSize int64
30-
Preset preset.Preset
23+
CrcStatus string
24+
OpenshiftStatus string
25+
OpenshiftVersion string `json:"OpenshiftVersion,omitempty"`
26+
DiskUse int64
27+
DiskSize int64
28+
RAMUse int64
29+
RAMSize int64
30+
PersistentVolumeUse int `json:"PersistentVolumeUse,omitempty"`
31+
PersistentVolumeSize int `json:"PersistentVolumeSize,omitempty"`
32+
Preset preset.Preset
3133
}
3234

3335
type ConsoleResult struct {

pkg/crc/api/handlers.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,16 @@ func (h *Handler) Status(c *context) error {
6363
return err
6464
}
6565
return c.JSON(http.StatusOK, client.ClusterStatusResult{
66-
CrcStatus: string(res.CrcStatus),
67-
OpenshiftStatus: string(res.OpenshiftStatus),
68-
OpenshiftVersion: res.OpenshiftVersion,
69-
DiskUse: res.DiskUse,
70-
DiskSize: res.DiskSize,
71-
RAMSize: res.RAMSize,
72-
RAMUse: res.RAMUse,
73-
Preset: res.Preset,
66+
CrcStatus: string(res.CrcStatus),
67+
OpenshiftStatus: string(res.OpenshiftStatus),
68+
OpenshiftVersion: res.OpenshiftVersion,
69+
DiskUse: res.DiskUse,
70+
DiskSize: res.DiskSize,
71+
RAMSize: res.RAMSize,
72+
RAMUse: res.RAMUse,
73+
PersistentVolumeUse: res.PersistentVolumeUse,
74+
PersistentVolumeSize: res.PersistentVolumeSize,
75+
Preset: res.Preset,
7476
})
7577
}
7678

pkg/crc/cluster/cluster.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,29 @@ func GetCPUUsage(sshRunner *ssh.Runner) ([]int64, error) {
130130

131131
}
132132

133+
func GetPVCUsage(sshRunner *ssh.Runner) (int, error) {
134+
cmd := `#!/bin/bash
135+
mountpoints=$(lsblk --output=mountpoints | grep pvc | uniq | tr '\n' ' ')
136+
if [ -z "$mountpoints" ]; then
137+
exit 0
138+
fi
139+
sudo df -B1 --output=size $mountpoints | awk ' { sum += $1 } END { printf "%d", sum} '
140+
`
141+
out, _, err := sshRunner.Run("/bin/bash", "-c", cmd)
142+
if err != nil {
143+
return 0, err
144+
}
145+
out = strings.TrimSpace(out)
146+
if len(out) == 0 {
147+
return 0, nil
148+
}
149+
ans, err := strconv.Atoi(out)
150+
if err != nil {
151+
return 0, err
152+
}
153+
return ans, nil
154+
}
155+
133156
func EnsureSSHKeyPresentInTheCluster(ctx context.Context, ocConfig oc.Config, sshPublicKeyPath string) error {
134157
sshPublicKeyByte, err := os.ReadFile(sshPublicKeyPath)
135158
if err != nil {

pkg/crc/machine/status.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ import (
55
"fmt"
66

77
"github.com/crc-org/crc/v2/pkg/crc/cluster"
8+
"github.com/crc-org/crc/v2/pkg/crc/config"
89
"github.com/crc-org/crc/v2/pkg/crc/constants"
910
"github.com/crc-org/crc/v2/pkg/crc/logging"
1011
"github.com/crc-org/crc/v2/pkg/crc/machine/state"
1112
"github.com/crc-org/crc/v2/pkg/crc/machine/types"
1213
"github.com/crc-org/crc/v2/pkg/crc/preset"
14+
"github.com/docker/go-units"
1315
"github.com/pkg/errors"
1416
)
1517

@@ -62,6 +64,7 @@ func (client *client) Status() (*types.ClusterStatusResult, error) {
6264
switch {
6365
case vm.bundle.IsMicroshift():
6466
clusterStatusResult.OpenshiftStatus = getMicroShiftStatus(context.Background(), ip)
67+
clusterStatusResult.PersistentVolumeUse, clusterStatusResult.PersistentVolumeSize = client.getPVCSize(vm)
6568
case vm.bundle.IsOpenShift():
6669
clusterStatusResult.OpenshiftStatus = getOpenShiftStatus(context.Background(), ip)
6770
}
@@ -198,3 +201,19 @@ func (client *client) getCPUStatus(vm *virtualMachine) []int64 {
198201
return cpuUsage
199202

200203
}
204+
205+
func (client *client) getPVCSize(vm *virtualMachine) (int, int) {
206+
sshRunner, err := vm.SSHRunner()
207+
if err != nil {
208+
logging.Debugf("Cannot get SSH runner: %v", err)
209+
return 0, 0
210+
}
211+
total := client.config.Get(config.PersistentVolumeSize)
212+
defer sshRunner.Close()
213+
used, err := cluster.GetPVCUsage(sshRunner)
214+
if err != nil {
215+
logging.Debugf("Cannot get PVC usage: %v", err)
216+
return 0, 0
217+
}
218+
return used, total.AsInt() * units.GB
219+
}

pkg/crc/machine/types/types.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,16 @@ type StopResult struct {
7272
}
7373

7474
type ClusterStatusResult struct {
75-
CrcStatus state.State
76-
OpenshiftStatus OpenshiftStatus
77-
OpenshiftVersion string
78-
DiskUse int64
79-
DiskSize int64
80-
RAMUse int64
81-
RAMSize int64
82-
Preset crcpreset.Preset
75+
CrcStatus state.State
76+
OpenshiftStatus OpenshiftStatus
77+
OpenshiftVersion string
78+
DiskUse int64
79+
DiskSize int64
80+
RAMUse int64
81+
RAMSize int64
82+
PersistentVolumeUse int
83+
PersistentVolumeSize int
84+
Preset crcpreset.Preset
8385
}
8486

8587
type ClusterLoadResult struct {

0 commit comments

Comments
 (0)