diff --git a/parser/internal/hpa.go b/parser/internal/hpa.go index a32c4e7d..bd619d18 100644 --- a/parser/internal/hpa.go +++ b/parser/internal/hpa.go @@ -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" @@ -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) +} diff --git a/parser/parse.go b/parser/parse.go index f1ae9bbb..601ae82f 100644 --- a/parser/parse.go +++ b/parser/parse.go @@ -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" @@ -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()) diff --git a/score/hpa_test.go b/score/hpa_test.go index 110323ef..6b0307ac 100644 --- a/score/hpa_test.go +++ b/score/hpa_test.go @@ -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) { diff --git a/score/testdata/hpa-targets-deployment.yaml b/score/testdata/hpa-autoscalingv1-targets-deployment.yaml similarity index 100% rename from score/testdata/hpa-targets-deployment.yaml rename to score/testdata/hpa-autoscalingv1-targets-deployment.yaml diff --git a/score/testdata/hpa-autoscalingv2-targets-deployment.yaml b/score/testdata/hpa-autoscalingv2-targets-deployment.yaml new file mode 100644 index 00000000..9a7c03cf --- /dev/null +++ b/score/testdata/hpa-autoscalingv2-targets-deployment.yaml @@ -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 \ No newline at end of file