HorizontalPodAutoscalerV2 generates invalid metrics block in Kubernetes manifest
- Dominant language
- C#
- Stars
- 6.3k
- Forks
- 991
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 196
Description
Description:
When using PublishAsKubernetesService to generate a HorizontalPodAutoscalerV2 resource, the resulting manifest includes an invalid metrics block. Specifically, multiple metric types (containerResource, external, pods, object) are nested under a single metrics entry, which violates the Kubernetes schema for autoscaling/v2.
Expected Manifest Output:
```
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: datanex-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: datanex-deployment
minReplicas: 3
maxReplicas: 12
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 80
```
Code Used to Generate the Resource:
```
service.PublishAsKubernetesService(resource =>
{
var scaler = new HorizontalPodAutoscalerV2()
{
Metadata = new() { Name = resource.Name + "-hpa" },
Spec = new()
{
ScaleTargetRef = new()
{
ApiVersion = "apps/v1",
Kind = "Deployment",
Name = "datanex-deployment"
},
MinReplicas = 3,
MaxReplicas = 12,
}
};
scaler.Spec.Metrics.Add(new MetricSpecV2
{
Type = "Resource",
Resource = new()
{
Name = "cpu",
Target = new()
{
Type = "Utilization",
AverageUtilization = 80
}
}
});
resource.AdditionalResources.Add(scaler);
});
```
aspire output:
```
---
apiVersion: "autoscaling/v2"
kind: "HorizontalPodAutoscaler"
metadata:
name: "datanex-hpa"
spec:
scaleTargetRef:
apiVersion: "apps/v1"
kind: "Deployment"
metadata: {}
name: "datanex-deployment"
behavior:
scaleDown: {}
scaleUp: {}
maxReplicas: 12
metrics:
- containerResource:
target: {}
resource:
name: "cpu"
target:
type: "Utilization"
averageUtilization: 80
type: "Resource"
external:
metric:
selector: {}
target: {}
pods:
metric:
selector: {}
target: {}
object:
metric:
selector: {}
describedObject:
apiVersion: "v2"
kind: "ObjectReference"
metadata: {}
target: {}
minReplicas: 3
```
Problem:
The generated manifest includes empty or invalid blocks for other metric types (external, pods, object, etc.) even though only a Resource metric was defined. This causes deployment failures due to schema violations.
Contributor guide
Assessment
This issue has not been assessed yet.