Skip to content

Commit

Permalink
neonvm-controller: rename runner cpu functions
Browse files Browse the repository at this point in the history
Signed-off-by: Misha Sakhnov <[email protected]>
  • Loading branch information
mikhail-sakhnov committed Oct 31, 2024
1 parent adbcc04 commit 97ff1f7
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 72 deletions.
79 changes: 79 additions & 0 deletions pkg/neonvm/controllers/runner_cpu_limits.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package controllers

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"time"

vmv1 "github.com/neondatabase/autoscaling/neonvm/apis/neonvm/v1"
"github.com/neondatabase/autoscaling/pkg/api"
)

func setRunnerCPULimits(ctx context.Context, vm *vmv1.VirtualMachine, cpu vmv1.MilliCPU) error {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()

url := fmt.Sprintf("http://%s:%d/cpu_change", vm.Status.PodIP, vm.Spec.RunnerPort)

update := api.VCPUChange{VCPUs: cpu}

data, err := json.Marshal(update)
if err != nil {
return err
}

req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewReader(data))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")

resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()

if resp.StatusCode != 200 {
return fmt.Errorf("setRunnerCgroup: unexpected status %s", resp.Status)
}
return nil
}

func getRunnerCPULimits(ctx context.Context, vm *vmv1.VirtualMachine) (*api.VCPUCgroup, error) {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()

url := fmt.Sprintf("http://%s:%d/cpu_current", vm.Status.PodIP, vm.Spec.RunnerPort)

req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return nil, err
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}

if resp.StatusCode != 200 {
return nil, fmt.Errorf("getRunnerCgroup: unexpected status %s", resp.Status)
}

body, err := io.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return nil, err
}
var result api.VCPUCgroup
err = json.Unmarshal(body, &result)
if err != nil {
return nil, err
}

return &result, nil
}
70 changes: 1 addition & 69 deletions pkg/neonvm/controllers/vm_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,13 @@ limitations under the License.
package controllers

import (
"bytes"
"context"
"crypto/ed25519"
"crypto/rand"
"encoding/base64"
"encoding/json"
"encoding/pem"
"fmt"
"io"
"net/http"
"os"
"reflect"
"strconv"
Expand Down Expand Up @@ -554,7 +551,7 @@ func (r *VMReconciler) doReconcile(ctx context.Context, vm *vmv1.VirtualMachine)
}

// get cgroups CPU details from runner pod
cgroupUsage, err := getRunnerCgroup(ctx, vm)
cgroupUsage, err := getRunnerCPULimits(ctx, vm)
if err != nil {
log.Error(err, "Failed to get CPU details from runner", "VirtualMachine", vm.Name)
return err
Expand Down Expand Up @@ -1238,71 +1235,6 @@ func affinityForVirtualMachine(vm *vmv1.VirtualMachine) *corev1.Affinity {
return a
}

func setRunnerCgroup(ctx context.Context, vm *vmv1.VirtualMachine, cpu vmv1.MilliCPU) error {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()

url := fmt.Sprintf("http://%s:%d/cpu_change", vm.Status.PodIP, vm.Spec.RunnerPort)

update := api.VCPUChange{VCPUs: cpu}

data, err := json.Marshal(update)
if err != nil {
return err
}

req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewReader(data))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")

resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()

if resp.StatusCode != 200 {
return fmt.Errorf("setRunnerCgroup: unexpected status %s", resp.Status)
}
return nil
}

func getRunnerCgroup(ctx context.Context, vm *vmv1.VirtualMachine) (*api.VCPUCgroup, error) {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()

url := fmt.Sprintf("http://%s:%d/cpu_current", vm.Status.PodIP, vm.Spec.RunnerPort)

req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return nil, err
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}

if resp.StatusCode != 200 {
return nil, fmt.Errorf("getRunnerCgroup: unexpected status %s", resp.Status)
}

body, err := io.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return nil, err
}
var result api.VCPUCgroup
err = json.Unmarshal(body, &result)
if err != nil {
return nil, err
}

return &result, nil
}

// imageForVirtualMachine gets the Operand image which is managed by this controller
// from the VM_RUNNER_IMAGE environment variable defined in the config/manager/manager.yaml
func imageForVmRunner() (string, error) {
Expand Down
6 changes: 3 additions & 3 deletions pkg/neonvm/controllers/vm_controller_cpu_scaling.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (r *VMReconciler) handleCPUScaling(ctx context.Context, vm *vmv1.VirtualMac
func (r *VMReconciler) handleCPUScalingQMP(ctx context.Context, vm *vmv1.VirtualMachine, vmRunner *corev1.Pod) (bool, error) {
log := log.FromContext(ctx)
specCPU := vm.Spec.Guest.CPUs.Use
cgroupUsage, err := getRunnerCgroup(ctx, vm)
cgroupUsage, err := getRunnerCPULimits(ctx, vm)
if err != nil {
log.Error(err, "Failed to get CPU details from runner", "VirtualMachine", vm.Name)
return false, err
Expand Down Expand Up @@ -92,7 +92,7 @@ func (r *VMReconciler) handleCPUScalingSysfs(ctx context.Context, vm *vmv1.Virtu
log := log.FromContext(ctx)
specCPU := vm.Spec.Guest.CPUs.Use

cgroupUsage, err := getRunnerCgroup(ctx, vm)
cgroupUsage, err := getRunnerCPULimits(ctx, vm)
if err != nil {
log.Error(err, "Failed to get CPU details from runner", "VirtualMachine", vm.Name)
return false, err
Expand All @@ -107,7 +107,7 @@ func (r *VMReconciler) handleCPUScalingSysfs(ctx context.Context, vm *vmv1.Virtu

func (r *VMReconciler) handleCgroupCPUUpdate(ctx context.Context, vm *vmv1.VirtualMachine, cgroupUsage *api.VCPUCgroup) (bool, error) {
specCPU := vm.Spec.Guest.CPUs.Use
if err := setRunnerCgroup(ctx, vm, specCPU); err != nil {
if err := setRunnerCPULimits(ctx, vm, specCPU); err != nil {
return false, err
}
reason := "ScaleDown"
Expand Down

0 comments on commit 97ff1f7

Please sign in to comment.