Skip to content

Commit

Permalink
fix(vmcpur): fixed reconciliation bugs and rbac (#29)
Browse files Browse the repository at this point in the history
fix(vmcpu): added nodes to rbac
fix(vmcpu): fixed model name field

---------

Signed-off-by: Isteb4k <[email protected]>
  • Loading branch information
Isteb4k authored Mar 21, 2024
1 parent 1576d18 commit 11d83f1
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 18 deletions.
2 changes: 1 addition & 1 deletion api/core/v1alpha2/virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ const (
)

type CPUSpec struct {
Model string `json:"model"`
ModelName string `json:"modelName"`
Cores int `json:"cores"`
CoreFraction string `json:"coreFraction"`
}
Expand Down

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

Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (r *VMCPUReconciler) UpdateStatus(_ context.Context, _ reconcile.Request, s
case virtv2.Host:
case virtv2.Model:
state.VMCPU.Changed().Status.Nodes = &modelNodes
isReady = containAll(modelNodes, state.VMCPU.Current().Spec.Model)
isReady = len(modelNodes) > 0
case virtv2.Features:
state.VMCPU.Changed().Status.Nodes = &modelNodes
state.VMCPU.Changed().Status.Features = &features
Expand All @@ -116,13 +116,13 @@ func (r *VMCPUReconciler) UpdateStatus(_ context.Context, _ reconcile.Request, s
}

func (r *VMCPUReconciler) FilterAttachedVM(vm *virtv2.VirtualMachine) bool {
return vm.Spec.CPU.Model != ""
return vm.Spec.CPU.ModelName != ""
}

func (r *VMCPUReconciler) EnqueueFromAttachedVM(vm *virtv2.VirtualMachine) []reconcile.Request {
return []reconcile.Request{{
NamespacedName: types.NamespacedName{
Name: vm.Spec.CPU.Model,
Name: vm.Spec.CPU.ModelName,
},
}}
}
Expand All @@ -132,7 +132,7 @@ func (r *VMCPUReconciler) getModelNodes(model string, nodes []corev1.Node) []str

for _, node := range nodes {
for label, enabled := range node.Labels {
if !strings.HasPrefix(label, v1.CPUModelLabel+model) || enabled != "true" {
if label != v1.CPUModelLabel+model || enabled != "true" {
continue
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,19 @@ func (state *VMCPUReconcilerState) ShouldReconcile(log logr.Logger) bool {
return false
}

return state.AttacheeState.ShouldReconcile(log)
if state.AttacheeState.ShouldReconcile(log) {
return true
}

return true
}

func (state *VMCPUReconcilerState) IsAttachedToVM(vm virtv2.VirtualMachine) bool {
if state.VMCPU.IsEmpty() {
return false
}

return state.VMCPU.Name().Name == vm.Spec.CPU.Model
return state.VMCPU.Name().Name == vm.Spec.CPU.ModelName
}

func (state *VMCPUReconcilerState) isDeletion() bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (state *VMReconcilerState) Reload(ctx context.Context, req reconcile.Reques
return fmt.Errorf("unable to get Claim %s: %w", claimKey, err)
}

vmcpuKey := types.NamespacedName{Name: state.VM.Current().Spec.CPU.Model}
vmcpuKey := types.NamespacedName{Name: state.VM.Current().Spec.CPU.ModelName}
state.CPUModel, err = helper.FetchObject(ctx, vmcpuKey, state.Client, &virtv2.VirtualMachineCPUModel{})
if err != nil {
return fmt.Errorf("unable to get cpu model %s: %w", claimKey, err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ var _ = Describe("VM", func() {
EnableParavirtualization: true,
OsType: virtv2.GenericOs,
CPU: virtv2.CPUSpec{
Model: "test-vmcpu",
Cores: 2,
ModelName: "test-vmcpu",
Cores: 2,
},
Memory: virtv2.MemorySpec{
Size: "2Gi",
Expand Down Expand Up @@ -408,8 +408,8 @@ var _ = Describe("Apply VM changes", func() {
EnableParavirtualization: true,
OsType: virtv2.GenericOs,
CPU: virtv2.CPUSpec{
Model: vmcpuName,
Cores: 2,
ModelName: vmcpuName,
Cores: 2,
},
Memory: virtv2.MemorySpec{
Size: "2Gi",
Expand Down Expand Up @@ -560,7 +560,7 @@ var _ = Describe("Apply VM changes with manual approval", func() {
EnableParavirtualization: true,
OsType: virtv2.GenericOs,
CPU: virtv2.CPUSpec{
Model: vmcpuName,
ModelName: vmcpuName,
Cores: cpuStartingCores,
CoreFraction: cpuStartingCoreFraction,
},
Expand Down Expand Up @@ -697,12 +697,12 @@ var _ = Describe("Apply VM changes with manual approval", func() {
"path": Equal("cpu"),
"operation": Equal(string(vmchange.ChangeReplace)),
"currentValue": MatchAllKeys(Keys{
"model": BeEquivalentTo(vmcpuName),
"modelName": BeEquivalentTo(vmcpuName),
"cores": BeEquivalentTo(cpuStartingCores),
"coreFraction": Equal(cpuStartingCoreFraction),
}),
"desiredValue": MatchAllKeys(Keys{
"model": BeEquivalentTo(vmcpuName),
"modelName": BeEquivalentTo(vmcpuName),
"cores": BeEquivalentTo(cpuNewCores),
"coreFraction": Equal(cpuNewCoreFraction),
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func compareCPU(current, desired *v1alpha2.VirtualMachineSpec) []FieldChange {
return fractionChanges
}

modelChanges := compareStrings("cpu.model", current.CPU.Model, desired.CPU.Model, "", ActionRestart)
modelChanges := compareStrings("cpu.model", current.CPU.ModelName, desired.CPU.ModelName, "", ActionRestart)
if HasChanges(modelChanges) {
return modelChanges
}
Expand Down
7 changes: 7 additions & 0 deletions templates/virtualization-controller/rbac-for-us.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ rules:
- update
- patch
- delete
- apiGroups:
- ""
resources:
- nodes
verbs:
- list
- watch
- apiGroups:
- ""
resources:
Expand Down

0 comments on commit 11d83f1

Please sign in to comment.