Skip to content

Commit

Permalink
score/hpa: support autoscaling/v2
Browse files Browse the repository at this point in the history
  • Loading branch information
ReuDa committed Dec 19, 2023
1 parent 891b4c9 commit 531c3e1
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 2 deletions.
22 changes: 22 additions & 0 deletions parser/internal/hpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package internal

import (
autoscalingv1 "k8s.io/api/autoscaling/v1"
autoscalingv2 "k8s.io/api/autoscaling/v2"
autoscalingv2beta1 "k8s.io/api/autoscaling/v2beta1"
autoscalingv2beta2 "k8s.io/api/autoscaling/v2beta2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -71,3 +72,24 @@ func (d HPAv2beta2) GetObjectMeta() metav1.ObjectMeta {
func (d HPAv2beta2) HpaTarget() autoscalingv1.CrossVersionObjectReference {
return autoscalingv1.CrossVersionObjectReference(d.Spec.ScaleTargetRef)
}

type HPAv2 struct {
autoscalingv2.HorizontalPodAutoscaler
Location ks.FileLocation
}

func (d HPAv2) FileLocation() ks.FileLocation {
return d.Location
}

func (d HPAv2) GetTypeMeta() metav1.TypeMeta {
return d.TypeMeta
}

func (d HPAv2) GetObjectMeta() metav1.ObjectMeta {
return d.ObjectMeta
}

func (d HPAv2) HpaTarget() autoscalingv1.CrossVersionObjectReference {
return autoscalingv1.CrossVersionObjectReference(d.Spec.ScaleTargetRef)
}
8 changes: 8 additions & 0 deletions parser/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
appsv1beta1 "k8s.io/api/apps/v1beta1"
appsv1beta2 "k8s.io/api/apps/v1beta2"
autoscalingv1 "k8s.io/api/autoscaling/v1"
autoscalingv2 "k8s.io/api/autoscaling/v2"
autoscalingv2beta1 "k8s.io/api/autoscaling/v2beta1"
autoscalingv2beta2 "k8s.io/api/autoscaling/v2beta2"
batchv1 "k8s.io/api/batch/v1"
Expand Down Expand Up @@ -409,6 +410,13 @@ func (p *Parser) decodeItem(cnf config.Configuration, s *parsedObjects, detected
FileLocationer: h,
})

case autoscalingv2.SchemeGroupVersion.WithKind("HorizontalPodAutoscaler"):
var hpa autoscalingv2.HorizontalPodAutoscaler
errs.AddIfErr(p.decode(fileContents, &hpa))
h := internal.HPAv2{HorizontalPodAutoscaler: hpa, Location: fileLocation}
s.hpaTargeters = append(s.hpaTargeters, h)
s.bothMetas = append(s.bothMetas, ks.BothMeta{TypeMeta: hpa.TypeMeta, ObjectMeta: hpa.ObjectMeta, FileLocationer: h})

default:
if cnf.VerboseOutput > 1 {
log.Printf("Unknown datatype: %s", detectedVersion.String())
Expand Down
9 changes: 7 additions & 2 deletions score/hpa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ import (
"github.com/zegl/kube-score/scorecard"
)

func TestHorizontalPodAutoscalerTargetsDeployment(t *testing.T) {
func TestHorizontalPodAutoscalerV1TargetsDeployment(t *testing.T) {
t.Parallel()
testExpectedScore(t, "hpa-targets-deployment.yaml", "HorizontalPodAutoscaler has target", scorecard.GradeAllOK)
testExpectedScore(t, "hpa-autoscalingv1-targets-deployment.yaml", "HorizontalPodAutoscaler has target", scorecard.GradeAllOK)
}

func TestHorizontalPodAutoscalerV2TargetsDeployment(t *testing.T) {
t.Parallel()
testExpectedScore(t, "hpa-autoscalingv2-targets-deployment.yaml", "HorizontalPodAutoscaler has target", scorecard.GradeAllOK)
}

func TestHorizontalPodAutoscalerHasNoTarget(t *testing.T) {
Expand Down
File renamed without changes.
31 changes: 31 additions & 0 deletions score/testdata/hpa-autoscalingv2-targets-deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: php-apache
namespace: default
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: php-apache
minReplicas: 1
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 50
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: php-apache
namespace: default
spec:
template:
spec:
containers:
- name: foo
image: foo:latest

0 comments on commit 531c3e1

Please sign in to comment.