Skip to content

Commit

Permalink
add targetCPUUTilization to HPA
Browse files Browse the repository at this point in the history
  • Loading branch information
moh-osman3 committed Aug 30, 2022
1 parent 81b51b1 commit 012fd4a
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 3 deletions.
4 changes: 4 additions & 0 deletions apis/v1alpha1/opentelemetrycollector_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ type OpenTelemetryCollectorSpec struct {
// MaxReplicas sets an upper bound to the autoscaling feature. If MaxReplicas is set autoscaling is enabled.
// +optional
MaxReplicas *int32 `json:"maxReplicas,omitempty"`
// TargetCPUUtilization sets the target average CPU used across all replicas.
// If average CPU exceeds this value, the HPA will scale up. Defaults to 90 percent.
// +optional
TargetCPUUtilization *int32 `json:"targetCPUUtilization,omitempty"`
// SecurityContext will be set as the container security context.
// +optional
SecurityContext *v1.SecurityContext `json:"securityContext,omitempty"`
Expand Down
4 changes: 4 additions & 0 deletions apis/v1alpha1/opentelemetrycollector_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ func (r *OpenTelemetryCollector) validateCRDSpec() error {
return fmt.Errorf("the OpenTelemetry Spec autoscale configuration is incorrect, minReplicas should be one or more")
}

if r.Spec.TargetCPUUtilization != nil && (*r.Spec.TargetCPUUtilization < int32(1) || *r.Spec.TargetCPUUtilization > int32(99)) {
return fmt.Errorf("the OpenTelemetry Spec autoscale configuration is incorrect, targetCPUUtilization should be greater than 0 and less than 100")
}

}

return nil
Expand Down
5 changes: 5 additions & 0 deletions apis/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,12 @@ spec:
service account to use with this instance.
type: string
type: object
targetCPUUtilization:
description: TargetCPUUtilization sets the target average CPU used
across all replicas. If average CPU exceeds this value, the HPA
will scale up. Defaults to 90 percent.
format: int32
type: integer
tolerations:
description: Toleration to schedule OpenTelemetry Collector pods.
This is only relevant to daemonset, statefulset, and deployment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,12 @@ spec:
service account to use with this instance.
type: string
type: object
targetCPUUtilization:
description: TargetCPUUtilization sets the target average CPU used
across all replicas. If average CPU exceeds this value, the HPA
will scale up. Defaults to 90 percent.
format: int32
type: integer
tolerations:
description: Toleration to schedule OpenTelemetry Collector pods.
This is only relevant to daemonset, statefulset, and deployment
Expand Down
10 changes: 10 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1825,6 +1825,16 @@ OpenTelemetryCollectorSpec defines the desired state of OpenTelemetryCollector.
TargetAllocator indicates a value which determines whether to spawn a target allocation resource or not.<br/>
</td>
<td>false</td>
</tr><tr>
<td><b>targetCPUUtilization</b></td>
<td>integer</td>
<td>
TargetCPUUtilization sets the target average CPU used across all replicas.
If average CPU exceeds this value, the HPA will scale up. Defaults to 90 percent<br/>
<br/>
<i>Format</i>: int32<br/>
</td>
<td>false</td>
</tr><tr>
<td><b><a href="#opentelemetrycollectorspectolerationsindex">tolerations</a></b></td>
<td>[]object</td>
Expand Down
7 changes: 6 additions & 1 deletion pkg/collector/horizontalpodautoscaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ func HorizontalPodAutoscaler(cfg config.Config, logger logr.Logger, otelcol v1al
labels["app.kubernetes.io/name"] = naming.Collector(otelcol)

annotations := Annotations(otelcol)
cpuTarget := defaultCPUTarget
var cpuTarget int32
if otelcol.Spec.TargetCPUUtilization != nil {
cpuTarget = *otelcol.Spec.TargetCPUUtilization
} else {
cpuTarget = defaultCPUTarget
}
var result client.Object

objectMeta := metav1.ObjectMeta{
Expand Down
11 changes: 11 additions & 0 deletions pkg/collector/reconcile/horizontalpodautoscaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ func expectedHorizontalPodAutoscalers(ctx context.Context, params Params, expect

func setAutoscalerSpec(params Params, autoscalingVersion autodetect.AutoscalingVersion, updated client.Object) {
one := int32(1)
ninety := int32(90)
if params.Instance.Spec.MaxReplicas != nil {
if autoscalingVersion == autodetect.AutoscalingVersionV2Beta2 {
updated.(*autoscalingv2beta2.HorizontalPodAutoscaler).Spec.MaxReplicas = *params.Instance.Spec.MaxReplicas
Expand All @@ -119,13 +120,23 @@ func setAutoscalerSpec(params Params, autoscalingVersion autodetect.AutoscalingV
} else {
updated.(*autoscalingv2beta2.HorizontalPodAutoscaler).Spec.MinReplicas = &one
}
if params.Instance.Spec.TargetCPUUtilization != nil {
updated.(*autoscalingv2beta2.HorizontalPodAutoscaler).Spec.Metrics[0].Resource.Target.AverageUtilization = params.Instance.Spec.TargetCPUUtilization
} else {
updated.(*autoscalingv2beta2.HorizontalPodAutoscaler).Spec.Metrics[0].Resource.Target.AverageUtilization = &ninety
}
} else {
updated.(*autoscalingv2.HorizontalPodAutoscaler).Spec.MaxReplicas = *params.Instance.Spec.MaxReplicas
if params.Instance.Spec.MinReplicas != nil {
updated.(*autoscalingv2.HorizontalPodAutoscaler).Spec.MinReplicas = params.Instance.Spec.MinReplicas
} else {
updated.(*autoscalingv2.HorizontalPodAutoscaler).Spec.MinReplicas = &one
}
if params.Instance.Spec.TargetCPUUtilization != nil {
updated.(*autoscalingv2.HorizontalPodAutoscaler).Spec.Metrics[0].Resource.Target.AverageUtilization = params.Instance.Spec.TargetCPUUtilization
} else {
updated.(*autoscalingv2.HorizontalPodAutoscaler).Spec.Metrics[0].Resource.Target.AverageUtilization = &ninety
}
}
}
}
Expand Down
10 changes: 8 additions & 2 deletions tests/e2e/autoscale/00-assert.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@ status:
readyReplicas: 1

---
apiVersion: autoscaling/v1
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
name: simplest-collector
spec:
minReplicas: 1
maxReplicas: 2

metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 50
1 change: 1 addition & 0 deletions tests/e2e/autoscale/00-install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ metadata:
spec:
minReplicas: 1
maxReplicas: 2
targetCPUUtilization: 50
resources:
limits:
cpu: 500m
Expand Down

0 comments on commit 012fd4a

Please sign in to comment.