Skip to content

Commit 27299cb

Browse files
neonvm: apply code review fixes
Co-authored-by: Em Sharnoff <[email protected]> Signed-off-by: Misha Sakhnov <[email protected]>
1 parent af1e76d commit 27299cb

File tree

4 files changed

+30
-18
lines changed

4 files changed

+30
-18
lines changed

neonvm-daemon/cmd/main.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,15 @@ type cpuServer struct {
5151
func (s *cpuServer) handleGetCPUStatus(w http.ResponseWriter) {
5252
s.cpuOperationsMutex.Lock()
5353
defer s.cpuOperationsMutex.Unlock()
54+
5455
activeCPUs, err := s.cpuScaler.ActiveCPUsCount()
5556
if err != nil {
5657
s.logger.Error("could not get active CPUs count", zap.Error(err))
5758
w.WriteHeader(http.StatusInternalServerError)
5859
return
5960
}
6061
w.WriteHeader(http.StatusOK)
62+
6163
if _, err := w.Write([]byte(fmt.Sprintf("%d", activeCPUs*1000))); err != nil {
6264
s.logger.Error("could not write response", zap.Error(err))
6365
}
@@ -66,20 +68,24 @@ func (s *cpuServer) handleGetCPUStatus(w http.ResponseWriter) {
6668
func (s *cpuServer) handleSetCPUStatus(w http.ResponseWriter, r *http.Request) {
6769
s.cpuOperationsMutex.Lock()
6870
defer s.cpuOperationsMutex.Unlock()
71+
6972
body, err := io.ReadAll(r.Body)
7073
if err != nil {
7174
s.logger.Error("could not read request body", zap.Error(err))
7275
w.WriteHeader(http.StatusBadRequest)
7376
return
7477
}
78+
defer r.Body.Close()
79+
7580
updateInt, err := strconv.Atoi(string(body))
76-
update := vmv1.MilliCPU(updateInt)
7781
if err != nil {
7882
s.logger.Error("could not unmarshal request body", zap.Error(err))
7983
w.WriteHeader(http.StatusBadRequest)
8084
return
8185
}
82-
s.logger.Info("Setting CPU status", zap.String("body", string(body)))
86+
87+
s.logger.Debug("Setting CPU status", zap.String("body", string(body)))
88+
update := vmv1.MilliCPU(updateInt)
8389
if err := s.cpuScaler.ReconcileOnlineCPU(int(update.RoundedUp())); err != nil {
8490
s.logger.Error("could not ensure online CPUs", zap.Error(err))
8591
w.WriteHeader(http.StatusInternalServerError)

pkg/neonvm/controllers/vm_controller.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ type VMReconciler struct {
7878
Scheme *runtime.Scheme
7979
Recorder record.EventRecorder
8080
Config *ReconcilerConfig
81-
Metrics ReconcilerMetrics `exhaustruct:"optional"`
81+
82+
Metrics ReconcilerMetrics `exhaustruct:"optional"`
8283
}
8384

8485
// The following markers are used to generate the rules permissions (RBAC) on config/rbac using controller-gen
@@ -162,9 +163,9 @@ func (r *VMReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Re
162163
}
163164

164165
// examine cpuScalingMode and set it to the default value if it is not set
165-
if vm.Spec.CpuScalingMode == nil || *vm.Spec.CpuScalingMode == "" {
166+
if vm.Spec.CpuScalingMode == nil {
166167
log.Info("Setting default CPU scaling mode", "default", r.Config.DefaultCPUScalingMode)
167-
vm.Spec.CpuScalingMode = lo.ToPtr(vmv1.CpuScalingMode(r.Config.DefaultCPUScalingMode))
168+
vm.Spec.CpuScalingMode = lo.ToPtr(r.Config.DefaultCPUScalingMode)
168169
if err := r.tryUpdateVM(ctx, &vm); err != nil {
169170
log.Error(err, "Failed to set default CPU scaling mode")
170171
return ctrl.Result{}, err
@@ -576,10 +577,8 @@ func (r *VMReconciler) doReconcile(ctx context.Context, vm *vmv1.VirtualMachine)
576577

577578
switch *vm.Spec.CpuScalingMode {
578579
case vmv1.CpuScalingModeSysfs:
579-
log.Info("CPU usage check based on cgroups", "CpuScalingMode", *vm.Spec.CpuScalingMode)
580580
pluggedCPU = cgroupUsage.VCPUs.RoundedUp()
581581
case vmv1.CpuScalingModeQMP:
582-
log.Info("CPU usage check based on QMP", "CpuScalingMode", *vm.Spec.CpuScalingMode)
583582
cpuSlotsPlugged, _, err := QmpGetCpus(QmpAddr(vm))
584583
if err != nil {
585584
log.Error(err, "Failed to get CPU details from VirtualMachine", "VirtualMachine", vm.Name)

pkg/neonvm/controllers/vm_controller_cpu_scaling.go

-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ func (r *VMReconciler) handleCPUScalingQMP(ctx context.Context, vm *vmv1.Virtual
8686
return false, err
8787
}
8888
} else {
89-
log.Info("No need to plug or unplug CPU")
9089
hotPlugCPUScaled = true
9190
}
9291

pkg/neonvm/cpuscaling/sysfs.go

+18-10
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,29 @@ func (cs *cpuSysfsState) SetState(cpuNum int, cpuState cpuState) error {
3232
}
3333

3434
func (cs *cpuSysfsState) GetState(cpuNum int) (cpuState, error) {
35-
data, err := os.ReadFile(filepath.Join(cpuPath, "online"))
35+
onlineCPUs, err := cs.getAllOnlineCPUs()
3636
if err != nil {
3737
return cpuOffline, err
3838
}
39+
if slices.Contains(onlineCPUs, cpuNum) {
40+
return cpuOnline, nil
41+
}
3942

40-
onlineCPUs, err := cs.parseMultipleCPURange(string(data))
43+
return cpuOffline, nil
44+
}
45+
46+
func (cs *cpuSysfsState) getAllOnlineCPUs() ([]int, error) {
47+
data, err := os.ReadFile(filepath.Join(cpuPath, "online"))
4148
if err != nil {
42-
return cpuOffline, err
49+
return nil, err
4350
}
4451

45-
if slices.Contains(onlineCPUs, cpuNum) {
46-
return cpuOnline, nil
52+
onlineCPUs, err := cs.parseMultipleCPURange(string(data))
53+
if err != nil {
54+
return onlineCPUs, err
4755
}
4856

49-
return cpuOffline, nil
57+
return onlineCPUs, nil
5058
}
5159

5260
// PossibleCPUs returns the start and end indexes of all possible CPUs.
@@ -59,7 +67,7 @@ func (cs *cpuSysfsState) PossibleCPUs() (int, int, error) {
5967
return cs.parseCPURange(string(data))
6068
}
6169

62-
// parseCPURange parses the CPU range string (e.g., "0-3") and returns a list of CPUs.
70+
// parseCPURange parses the CPU range string (e.g., "0-3") and returns start and end indexes.
6371
func (cs *cpuSysfsState) parseCPURange(cpuRange string) (int, int, error) {
6472
cpuRange = strings.TrimSpace(cpuRange)
6573
parts := strings.Split(cpuRange, "-")
@@ -86,9 +94,9 @@ func (cs *cpuSysfsState) parseCPURange(cpuRange string) (int, int, error) {
8694
}
8795

8896
// parseMultipleCPURange parses the multiple CPU range string (e.g., "0-3,5-7") and returns a list of CPUs.
89-
func (cs *cpuSysfsState) parseMultipleCPURange(cpuRange string) ([]int, error) {
90-
cpuRange = strings.TrimSpace(cpuRange)
91-
parts := strings.Split(cpuRange, ",")
97+
func (cs *cpuSysfsState) parseMultipleCPURange(cpuRanges string) ([]int, error) {
98+
cpuRanges = strings.TrimSpace(cpuRanges)
99+
parts := strings.Split(cpuRanges, ",")
92100

93101
var cpus []int
94102
for _, part := range parts {

0 commit comments

Comments
 (0)