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 (machine) : crc daemon /status api should return correct preset value for okd cluster (#4478) #4480

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 31 additions & 23 deletions pkg/crc/machine/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"github.com/pkg/errors"
)

type openShiftStatusSupplierFunc func(context.Context, string) types.OpenshiftStatus

func (client *client) Status() (*types.ClusterStatusResult, error) {
vm, err := loadVirtualMachine(client.name, client.useVSock())
if err != nil {
Expand All @@ -33,45 +35,51 @@ func (client *client) Status() (*types.ClusterStatusResult, error) {
return nil, errors.Wrap(err, "Cannot get machine state")
}

ip, err := vm.IP()
if err != nil {
return nil, errors.Wrap(err, "Error getting ip")
}
ramSize, ramUse := client.getRAMStatus(vm)
diskSize, diskUse := client.getDiskDetails(vm)
pvSize, pvUse := client.getPVCSize(vm)
var openShiftStatusSupplier = getOpenShiftStatus
if vm.bundle.IsMicroshift() {
openShiftStatusSupplier = getMicroShiftStatus
}

return createClusterStatusResult(vmStatus, vm.bundle.GetBundleType(), vm.bundle.GetVersion(), ip, ramSize, ramUse, diskSize, diskUse, pvSize, pvUse, openShiftStatusSupplier)
}

func createClusterStatusResult(vmStatus state.State, bundleType preset.Preset, vmBundleVersion, vmIP string, diskSize, diskUse, ramSize, ramUse int64, pvUse, pvSize int, openShiftStatusSupplier openShiftStatusSupplierFunc) (*types.ClusterStatusResult, error) {
clusterStatusResult := &types.ClusterStatusResult{
CrcStatus: vmStatus,
CrcStatus: vmStatus,
OpenshiftVersion: vmBundleVersion,
OpenshiftStatus: types.OpenshiftStopped,
}
switch {
case vm.bundle.IsMicroshift():
clusterStatusResult.OpenshiftStatus = types.OpenshiftStopped
clusterStatusResult.OpenshiftVersion = vm.bundle.GetVersion()
switch bundleType {
case preset.Microshift:
clusterStatusResult.Preset = preset.Microshift
case preset.OKD:
clusterStatusResult.Preset = preset.OKD
default:
clusterStatusResult.OpenshiftStatus = types.OpenshiftStopped
clusterStatusResult.OpenshiftVersion = vm.bundle.GetVersion()
clusterStatusResult.Preset = preset.OpenShift
}

if vmStatus != state.Running {
return clusterStatusResult, nil
}

ip, err := vm.IP()
if err != nil {
return nil, errors.Wrap(err, "Error getting ip")
}

diskSize, diskUse := client.getDiskDetails(vm)
clusterStatusResult.CrcStatus = state.Running
clusterStatusResult.DiskUse = diskUse
clusterStatusResult.DiskSize = diskSize

switch {
case vm.bundle.IsMicroshift():
clusterStatusResult.OpenshiftStatus = getMicroShiftStatus(context.Background(), ip)
clusterStatusResult.PersistentVolumeUse, clusterStatusResult.PersistentVolumeSize = client.getPVCSize(vm)
case vm.bundle.IsOpenShift():
clusterStatusResult.OpenshiftStatus = getOpenShiftStatus(context.Background(), ip)
}

ramSize, ramUse := client.getRAMStatus(vm)
clusterStatusResult.RAMSize = ramSize
clusterStatusResult.RAMUse = ramUse
clusterStatusResult.OpenshiftStatus = openShiftStatusSupplier(context.Background(), vmIP)

if bundleType == preset.Microshift {
clusterStatusResult.PersistentVolumeUse = pvUse
clusterStatusResult.PersistentVolumeSize = pvSize
}

return clusterStatusResult, nil
}
Expand Down
252 changes: 252 additions & 0 deletions pkg/crc/machine/status_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
package machine

import (
"context"
"testing"

"github.com/crc-org/crc/v2/pkg/crc/machine/state"
"github.com/crc-org/crc/v2/pkg/crc/machine/types"
"github.com/crc-org/crc/v2/pkg/crc/preset"
"github.com/stretchr/testify/assert"
)

func TestCreateClusterStatusResultShouldSetOpenShiftStatusAsExpected(t *testing.T) {
tests := []struct {
name string
vmStatus state.State
vmBundleType preset.Preset
expectedClusterStatus types.ClusterStatusResult
}{
{
"MicroShift cluster running", state.Running, preset.Microshift, types.ClusterStatusResult{
CrcStatus: "Running",
OpenshiftStatus: "Running",
OpenshiftVersion: "v4.5.1",
DiskUse: int64(16),
DiskSize: int64(32),
RAMUse: int64(8),
RAMSize: int64(12),
PersistentVolumeUse: 16,
PersistentVolumeSize: 32,
Preset: preset.Microshift,
},
},
{
"MicroShift cluster stopped", state.Stopped, preset.Microshift, types.ClusterStatusResult{
CrcStatus: "Stopped",
OpenshiftStatus: "Stopped",
OpenshiftVersion: "v4.5.1",
Preset: preset.Microshift,
DiskUse: int64(0),
DiskSize: int64(0),
RAMUse: int64(0),
RAMSize: int64(0),
PersistentVolumeUse: 0,
PersistentVolumeSize: 0,
},
},
{
"MicroShift cluster error state", state.Error, preset.Microshift, types.ClusterStatusResult{
CrcStatus: "Error",
OpenshiftStatus: "Stopped",
OpenshiftVersion: "v4.5.1",
Preset: preset.Microshift,
DiskUse: int64(0),
DiskSize: int64(0),
RAMUse: int64(0),
RAMSize: int64(0),
PersistentVolumeUse: 0,
PersistentVolumeSize: 0,
},
},
{
"MicroShift cluster stopping state", state.Stopping, preset.Microshift, types.ClusterStatusResult{
CrcStatus: "Stopping",
OpenshiftStatus: "Stopped",
OpenshiftVersion: "v4.5.1",
Preset: preset.Microshift,
DiskUse: int64(0),
DiskSize: int64(0),
RAMUse: int64(0),
RAMSize: int64(0),
PersistentVolumeUse: 0,
PersistentVolumeSize: 0,
},
},
{
"MicroShift cluster starting state", state.Starting, preset.Microshift, types.ClusterStatusResult{
CrcStatus: "Starting",
OpenshiftStatus: "Stopped",
OpenshiftVersion: "v4.5.1",
Preset: preset.Microshift,
DiskUse: int64(0),
DiskSize: int64(0),
RAMUse: int64(0),
RAMSize: int64(0),
PersistentVolumeUse: 0,
PersistentVolumeSize: 0,
},
},
{
"OpenShift cluster running", state.Running, preset.OpenShift, types.ClusterStatusResult{
CrcStatus: "Running",
OpenshiftStatus: "Running",
OpenshiftVersion: "v4.5.1",
Preset: preset.OpenShift,
DiskUse: int64(16),
DiskSize: int64(32),
RAMUse: int64(8),
RAMSize: int64(12),
PersistentVolumeUse: 0,
PersistentVolumeSize: 0,
},
},
{
"OpenShift cluster stopped", state.Stopped, preset.OpenShift, types.ClusterStatusResult{
CrcStatus: "Stopped",
OpenshiftStatus: "Stopped",
OpenshiftVersion: "v4.5.1",
Preset: preset.OpenShift,
DiskUse: int64(0),
DiskSize: int64(0),
RAMUse: int64(0),
RAMSize: int64(0),
PersistentVolumeUse: 0,
PersistentVolumeSize: 0,
},
},
{
"OpenShift cluster errored", state.Error, preset.OpenShift, types.ClusterStatusResult{
CrcStatus: "Error",
OpenshiftStatus: "Stopped",
OpenshiftVersion: "v4.5.1",
Preset: preset.OpenShift,
DiskUse: int64(0),
DiskSize: int64(0),
RAMUse: int64(0),
RAMSize: int64(0),
PersistentVolumeUse: 0,
PersistentVolumeSize: 0,
},
},
{
"OpenShift cluster stopping state", state.Stopping, preset.OpenShift, types.ClusterStatusResult{
CrcStatus: "Stopping",
OpenshiftStatus: "Stopped",
OpenshiftVersion: "v4.5.1",
Preset: preset.OpenShift,
DiskUse: int64(0),
DiskSize: int64(0),
RAMUse: int64(0),
RAMSize: int64(0),
PersistentVolumeUse: 0,
PersistentVolumeSize: 0,
},
},
{
"OpenShift cluster starting state", state.Starting, preset.OpenShift, types.ClusterStatusResult{
CrcStatus: "Starting",
OpenshiftStatus: "Stopped",
OpenshiftVersion: "v4.5.1",
Preset: preset.OpenShift,
DiskUse: int64(0),
DiskSize: int64(0),
RAMUse: int64(0),
RAMSize: int64(0),
PersistentVolumeUse: 0,
PersistentVolumeSize: 0,
},
},
{
"OpenShift/OKD cluster running", state.Running, preset.OKD, types.ClusterStatusResult{
CrcStatus: "Running",
OpenshiftStatus: "Running",
OpenshiftVersion: "v4.5.1",
Preset: preset.OKD,
DiskUse: int64(16),
DiskSize: int64(32),
RAMUse: int64(8),
RAMSize: int64(12),
PersistentVolumeUse: 0,
PersistentVolumeSize: 0,
},
},
{
"OpenShift/OKD cluster stopped", state.Stopped, preset.OKD, types.ClusterStatusResult{
CrcStatus: "Stopped",
OpenshiftStatus: "Stopped",
OpenshiftVersion: "v4.5.1",
Preset: preset.OKD,
DiskUse: int64(0),
DiskSize: int64(0),
RAMUse: int64(0),
RAMSize: int64(0),
PersistentVolumeUse: 0,
PersistentVolumeSize: 0,
},
},
{
"OpenShift/OKD cluster errored", state.Error, preset.OKD, types.ClusterStatusResult{
CrcStatus: "Error",
OpenshiftStatus: "Stopped",
OpenshiftVersion: "v4.5.1",
Preset: preset.OKD,
DiskUse: int64(0),
DiskSize: int64(0),
RAMUse: int64(0),
RAMSize: int64(0),
PersistentVolumeUse: 0,
PersistentVolumeSize: 0,
},
},
{
"OpenShift/OKD cluster stopping state", state.Stopping, preset.OKD, types.ClusterStatusResult{
CrcStatus: "Stopping",
OpenshiftStatus: "Stopped",
OpenshiftVersion: "v4.5.1",
Preset: preset.OKD,
DiskUse: int64(0),
DiskSize: int64(0),
RAMUse: int64(0),
RAMSize: int64(0),
PersistentVolumeUse: 0,
PersistentVolumeSize: 0,
},
},
{
"OpenShift/OKD cluster starting state", state.Starting, preset.OKD, types.ClusterStatusResult{
CrcStatus: "Starting",
OpenshiftStatus: "Stopped",
OpenshiftVersion: "v4.5.1",
Preset: preset.OKD,
DiskUse: int64(0),
DiskSize: int64(0),
RAMUse: int64(0),
RAMSize: int64(0),
PersistentVolumeUse: 0,
PersistentVolumeSize: 0,
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Given
// When
actualClusterStatusResult, err := createClusterStatusResult(tt.vmStatus, tt.vmBundleType, "v4.5.1", "127.0.0.1", 32, 16, 12, 8, 16, 32, func(context.Context, string) types.OpenshiftStatus { return types.OpenshiftRunning })

// Then
assert.NoError(t, err)
assert.Equal(t, tt.expectedClusterStatus.CrcStatus, actualClusterStatusResult.CrcStatus)
assert.Equal(t, tt.expectedClusterStatus.OpenshiftStatus, actualClusterStatusResult.OpenshiftStatus)
assert.Equal(t, tt.expectedClusterStatus.Preset, actualClusterStatusResult.Preset)
assert.Equal(t, tt.expectedClusterStatus.OpenshiftVersion, actualClusterStatusResult.OpenshiftVersion)
assert.Equal(t, tt.expectedClusterStatus.RAMSize, actualClusterStatusResult.RAMSize)
assert.Equal(t, tt.expectedClusterStatus.RAMUse, actualClusterStatusResult.RAMUse)
assert.Equal(t, tt.expectedClusterStatus.DiskSize, actualClusterStatusResult.DiskSize)
assert.Equal(t, tt.expectedClusterStatus.DiskUse, actualClusterStatusResult.DiskUse)
assert.Equal(t, tt.expectedClusterStatus.PersistentVolumeSize, actualClusterStatusResult.PersistentVolumeSize)
assert.Equal(t, tt.expectedClusterStatus.PersistentVolumeUse, actualClusterStatusResult.PersistentVolumeUse)
})
}
}
Loading