Skip to content
Open
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
21 changes: 21 additions & 0 deletions api/hypershift/v1beta1/kubevirt.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ const (

type QoSClass string

// CpuModelType represents the CPU model for KubeVirt VMs.
//
// +kubebuilder:validation:Enum=HostPassthrough
type CpuModelType string

const (
// CpuModelHostPassthrough configures the VM to use the same CPU model as the node.
CpuModelHostPassthrough CpuModelType = "HostPassthrough"
)

// KubevirtCompute contains values associated with the virtual compute hardware requested for the VM.
type KubevirtCompute struct {
// memory represents how much guest memory the VM should have
Expand All @@ -34,6 +44,17 @@ type KubevirtCompute struct {
// +kubebuilder:validation:Enum=Burstable;Guaranteed
// +kubebuilder:default=Burstable
QosClass *QoSClass `json:"qosClass,omitempty"`

// model specifies the CPU model for the KubeVirt VirtualMachineInstance.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'd recommend adding an explicit list of valid values in here

Suggested change
// model specifies the CPU model for the KubeVirt VirtualMachineInstance.
// model specifies the CPU model for the KubeVirt VirtualMachineInstance.
// Valid values are "HostPassthrough" and omitted.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

makes sense, thanks.
i've added it.

// Valid values are "HostPassthrough" and omitted.
// When not set, no explicit CPU model is configured and KubeVirt will use
// its default behavior.
// When set to "HostPassthrough", the VM will use the same CPU model as the
// host node, which provides the best performance but may limit live migration
// compatibility between nodes with different CPU types.
//
// +optional
Model CpuModelType `json:"model,omitempty"`
}

// +kubebuilder:validation:Enum=ReadWriteOnce;ReadWriteMany;ReadOnly;ReadWriteOncePod
Expand Down
135 changes: 135 additions & 0 deletions api/hypershift/v1beta1/kubevirt_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package v1beta1

import (
"encoding/json"
"reflect"
"testing"

"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/utils/ptr"
)

// kubevirtComputeNMinus1 represents the previous version of KubevirtCompute
// before the Model field was added. It is used to verify that JSON produced
// by the current type can be deserialized by previous versions of the code,
// and vice versa.
//
//nolint:kubeapilinter // N-1 compatibility fixture; intentionally mirrors the previous API version
type kubevirtComputeNMinus1 struct {
Memory *resource.Quantity `json:"memory,omitempty"`
Cores *uint32 `json:"cores,omitempty"`
QosClass *QoSClass `json:"qosClass,omitempty"`
}

func TestKubevirtComputeSerializationCompatibility(t *testing.T) {
burstable := QoSClassBurstable
guaranteed := QoSClassGuaranteed
mem8Gi := resource.MustParse("8Gi")

tests := []struct {
name string
current KubevirtCompute
expectedJSON string
nMinus1Result kubevirtComputeNMinus1
}{
{
name: "When all fields are zero-value, it should round-trip as empty object",
current: KubevirtCompute{},
expectedJSON: `{}`,
nMinus1Result: kubevirtComputeNMinus1{},
},
{
name: "When Model is empty, it should be omitted and N-1 should deserialize without it",
current: KubevirtCompute{
Memory: &mem8Gi,
Cores: ptr.To[uint32](4),
QosClass: &burstable,
},
expectedJSON: `{"memory":"8Gi","cores":4,"qosClass":"Burstable"}`,
nMinus1Result: kubevirtComputeNMinus1{
Memory: &mem8Gi,
Cores: ptr.To[uint32](4),
QosClass: &burstable,
},
},
{
name: "When Model is set to HostPassthrough, it should be included and N-1 should ignore it",
current: KubevirtCompute{
Memory: &mem8Gi,
Cores: ptr.To[uint32](4),
QosClass: &burstable,
Model: CpuModelHostPassthrough,
},
expectedJSON: `{"memory":"8Gi","cores":4,"qosClass":"Burstable","model":"HostPassthrough"}`,
nMinus1Result: kubevirtComputeNMinus1{
Memory: &mem8Gi,
Cores: ptr.To[uint32](4),
QosClass: &burstable,
},
},
{
name: "When only Model is set, it should serialize correctly and N-1 should get empty fields",
current: KubevirtCompute{
Model: CpuModelHostPassthrough,
},
expectedJSON: `{"model":"HostPassthrough"}`,
nMinus1Result: kubevirtComputeNMinus1{},
},
{
name: "When Model is set with Guaranteed QoS, it should round-trip correctly",
current: KubevirtCompute{
Memory: &mem8Gi,
Cores: ptr.To[uint32](8),
QosClass: &guaranteed,
Model: CpuModelHostPassthrough,
},
expectedJSON: `{"memory":"8Gi","cores":8,"qosClass":"Guaranteed","model":"HostPassthrough"}`,
nMinus1Result: kubevirtComputeNMinus1{
Memory: &mem8Gi,
Cores: ptr.To[uint32](8),
QosClass: &guaranteed,
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
data, err := json.Marshal(tt.current)
if err != nil {
t.Fatalf("failed to marshal current struct: %v", err)
}
if string(data) != tt.expectedJSON {
t.Errorf("unexpected JSON output: got %s, want %s", string(data), tt.expectedJSON)
}

var nMinus1 kubevirtComputeNMinus1
if err := json.Unmarshal(data, &nMinus1); err != nil {
t.Fatalf("N-1 failed to unmarshal JSON from N: %v", err)
}
if !reflect.DeepEqual(nMinus1, tt.nMinus1Result) {
t.Errorf("N-1 deserialization mismatch: got %+v, want %+v", nMinus1, tt.nMinus1Result)
}

nMinus1Data, err := json.Marshal(tt.nMinus1Result)
if err != nil {
t.Fatalf("failed to marshal N-1 struct: %v", err)
}
var roundTripped KubevirtCompute
if err := json.Unmarshal(nMinus1Data, &roundTripped); err != nil {
t.Fatalf("N failed to unmarshal JSON from N-1: %v", err)
}
if !reflect.DeepEqual(roundTripped.Memory, tt.nMinus1Result.Memory) {
t.Errorf("Memory mismatch after N-1 round-trip: got %v, want %v", roundTripped.Memory, tt.nMinus1Result.Memory)
}
if !reflect.DeepEqual(roundTripped.Cores, tt.nMinus1Result.Cores) {
t.Errorf("Cores mismatch after N-1 round-trip: got %v, want %v", roundTripped.Cores, tt.nMinus1Result.Cores)
}
if !reflect.DeepEqual(roundTripped.QosClass, tt.nMinus1Result.QosClass) {
t.Errorf("QosClass mismatch after N-1 round-trip: got %v, want %v", roundTripped.QosClass, tt.nMinus1Result.QosClass)
}
if roundTripped.Model != "" {
t.Errorf("Model should be empty after N-1 round-trip: got %v", roundTripped.Model)
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1139,6 +1139,18 @@ spec:
VM should have
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
model:
description: |-
model specifies the CPU model for the KubeVirt VirtualMachineInstance.
Valid values are "HostPassthrough" and omitted.
When not set, no explicit CPU model is configured and KubeVirt will use
its default behavior.
When set to "HostPassthrough", the VM will use the same CPU model as the
host node, which provides the best performance but may limit live migration
compatibility between nodes with different CPU types.
enum:
- HostPassthrough
type: string
qosClass:
default: Burstable
description: |-
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1407,6 +1407,18 @@ spec:
VM should have
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
model:
description: |-
model specifies the CPU model for the KubeVirt VirtualMachineInstance.
Valid values are "HostPassthrough" and omitted.
When not set, no explicit CPU model is configured and KubeVirt will use
its default behavior.
When set to "HostPassthrough", the VM will use the same CPU model as the
host node, which provides the best performance but may limit live migration
compatibility between nodes with different CPU types.
enum:
- HostPassthrough
type: string
qosClass:
default: Burstable
description: |-
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1172,6 +1172,18 @@ spec:
VM should have
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
model:
description: |-
model specifies the CPU model for the KubeVirt VirtualMachineInstance.
Valid values are "HostPassthrough" and omitted.
When not set, no explicit CPU model is configured and KubeVirt will use
its default behavior.
When set to "HostPassthrough", the VM will use the same CPU model as the
host node, which provides the best performance but may limit live migration
compatibility between nodes with different CPU types.
enum:
- HostPassthrough
type: string
qosClass:
default: Burstable
description: |-
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1139,6 +1139,18 @@ spec:
VM should have
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
model:
description: |-
model specifies the CPU model for the KubeVirt VirtualMachineInstance.
Valid values are "HostPassthrough" and omitted.
When not set, no explicit CPU model is configured and KubeVirt will use
its default behavior.
When set to "HostPassthrough", the VM will use the same CPU model as the
host node, which provides the best performance but may limit live migration
compatibility between nodes with different CPU types.
enum:
- HostPassthrough
type: string
qosClass:
default: Burstable
description: |-
Expand Down
15 changes: 12 additions & 3 deletions client/applyconfiguration/hypershift/v1beta1/kubevirtcompute.go

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

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

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

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

40 changes: 40 additions & 0 deletions docs/content/reference/aggregated-docs.md

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

Loading
Loading