jthomperoo / predictive-horizontal-pod-autoscaler

Horizontal Pod Autoscaler built with predictive abilities using statistical models
Apache License 2.0
330 stars 30 forks source link

Nil pointers when using Pod metric #57

Closed jaredhancock31 closed 2 years ago

jaredhancock31 commented 2 years ago

Describe the bug

I'm exporting custom metrics using prometheu under type: Pod and am getting panics from nil pointers.

It's unclear if such metrics are supported here or if I'm just misconfiguring the spec. My HPA (autoscaling/v2beta1) is able to track this metric without issue on EKS

To Reproduce

apiVersion: custompodautoscaler.com/v1
kind: CustomPodAutoscaler
metadata:
  name: linear-cpa
spec:
  template:
    spec:
      containers:
        - name: cpa
          image: jthomperoo/predictive-horizontal-pod-autoscaler:latest
          imagePullPolicy: IfNotPresent
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: mything
  roleRequiresMetricsServer: true
  config:
    - name: minReplicas
      value: "1"
    - name: maxReplicas
      value: "3"
    - name: predictiveConfig
      value: |
        models:
        - type: Linear
          name: LinearPrediction
          perInterval: 1
          linear:
            lookAhead: 10000
            storedValues: 20
        decisionType: "mean"
        metrics:
        - type: Pods
          pods:
            metric:
              name: numSessions
            target:
              type: AverageValue
              averageValue: "75"
    - name: interval
      value: "10000"
    - name: downscaleStabilization
      value: "300"

I also tried using a different spec, since I know K8s has tweaked it a lot over the last few releases. Something like:

  metrics:
    - type: Pods
      pods:
        metricName: numSessions
        targetAverageValue: '75'

observed logs:

I1006 19:51:48.228280       1 shell.go:90] Shell command failed, stderr: panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0x6528c2]

goroutine 1 [running]:
k8s.io/apimachinery/pkg/api/resource.(*Quantity).ScaledValue(0x0, 0xfffffffd, 0x0)
    /home/runner/work/predictive-horizontal-pod-autoscaler/predictive-horizontal-pod-autoscaler/vendor/k8s.io/apimachinery/pkg/api/resource/quantity.go:744 +0x22
k8s.io/apimachinery/pkg/api/resource.(*Quantity).MilliValue(...)
    /home/runner/work/predictive-horizontal-pod-autoscaler/predictive-horizontal-pod-autoscaler/vendor/k8s.io/apimachinery/pkg/api/resource/quantity.go:736
github.com/jthomperoo/horizontal-pod-autoscaler/evaluate/pods.(*Evaluate).GetEvaluation(0xc0001253b0, 0xc000000001, 0xc000390720, 0x14363e0)
    /home/runner/work/predictive-horizontal-pod-autoscaler/predictive-horizontal-pod-autoscaler/vendor/github.com/jthomperoo/horizontal-pod-autoscaler/evaluate/pods/pods.go:52 +0x3f
github.com/jthomperoo/horizontal-pod-autoscaler/evaluate.(*Evaluate).getEvaluation(0xc0004aaa80, 0xc000000001, 0xc000390720, 0x0, 0x0, 0x0)
    /home/runner/work/predictive-horizontal-pod-autoscaler/predictive-horizontal-pod-autoscaler/vendor/github.com/jthomperoo/horizontal-pod-autoscaler/evaluate/evaluate.go:119 +0xfe
github.com/jthomperoo/horizontal-pod-autoscaler/evaluate.(*Evaluate).GetEvaluation(0xc0004aaa80, 0xc00003c580, 0x1, 0x4, 0x10, 0x8, 0x8826d1)

Expected behavior No nil pointers

Kubernetes Details (kubectl version):

Client Version: version.Info{Major:"1", Minor:"21", GitVersion:"v1.21.2", GitCommit:"092fbfbf53427de67cac1e9fa54aaa09a28371d7", GitTreeState:"clean", BuildDate:"2021-06-16T12:52:14Z", GoVersion:"go1.16.5", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"21+", GitVersion:"v1.21.2-eks-0389ca3", GitCommit:"8a4e27b9d88142bbdd21b997b532eb6d493df6d2", GitTreeState:"clean", BuildDate:"2021-07-31T01:34:46Z", GoVersion:"go1.16.5", Compiler:"gc", Platform:"linux/amd64"}

Additional context Add any other context about the problem here.

Skillshot commented 2 years ago

Initially got same problems with the metric not able to fetch with error something like: invalid metrics (1 invalid out of 1), first error is: failed to get pods metric: unable to get custom metric.

Was able to get it working by using the following spec:

metrics:
  - type: Pods
    pods:
      metric:
        name: numSessions
      target:
        type: AverageValue
        value: 75

The cause of Nil pointer issue in the original question seem to be value field not defined.

jthomperoo commented 2 years ago

Thanks for raising this, and thanks for pointing out a workaround! So this is in fact still a bug, for the Pod metrics it should be looking at the averageValue field, rather than the value field. I'm just going to open this in the underlying repository this project relies on and get this fixed, so as a heads up in the next release it will use the correct averageValue rather than the value field.

Issue tracked here: jthomperoo/horizontal-pod-autoscaler#26

Thanks!