Skip to content

Commit 046d9e9

Browse files
committed
Clean up and fix templates
1 parent 3574a4c commit 046d9e9

File tree

8 files changed

+410
-504
lines changed

8 files changed

+410
-504
lines changed

apis/v1alpha2/nginxproxy_types.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -477,8 +477,7 @@ type DaemonSetSpec struct {
477477

478478
// HPASpec is the configuration for the Horizontal Pod Autoscaling.
479479
//
480-
// +kubebuilder:validation:XValidation:message="at least one metric must be specified when autoscaling is enabled",rule="!self.enable || (has(self.targetCPUUtilizationPercentage) || has(self.targetMemoryUtilizationPercentage) || (has(self.autoscalingTemplate) && size(self.autoscalingTemplate) > 0))"
481-
// +kubebuilder:validation:XValidation:message="minReplicas must be less than or equal to maxReplicas",rule="self.minReplicas <= self.maxReplicas"
480+
// +kubebuilder:validation:XValidation:message="minReplicas must be less than or equal to maxReplicas",rule="(!has(self.minReplicas)) || (self.minReplicas <= self.maxReplicas)"
482481
//
483482
//nolint:lll
484483
type HPASpec struct {
@@ -509,10 +508,10 @@ type HPASpec struct {
509508
// +kubebuilder:validation:Minimum=1
510509
MinReplicas *int32 `json:"minReplicas,omitempty"`
511510

512-
// AutoscalingTemplate configures the additional scaling option.
511+
// Templates configures additional metrics options.
513512
//
514513
// +optional
515-
AutoscalingTemplate []autoscalingv2.MetricSpec `json:"autoscalingTemplate,omitempty"`
514+
Templates []autoscalingv2.MetricSpec `json:"templates,omitempty"`
516515

517516
// Maximum number of replicas.
518517
//

apis/v1alpha2/zz_generated.deepcopy.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

charts/nginx-gateway-fabric/templates/hpa.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ spec:
3838
type: Utilization
3939
averageUtilization: {{ . }}
4040
{{- end }}
41-
{{- with .Values.nginxGateway.autoscalingTemplate }}
41+
{{- with .Values.nginxGateway.autoscaling.templates }}
4242
{{- toYaml . | nindent 2 }}
4343
{{- end }}
4444
{{- with .Values.nginxGateway.autoscaling.behavior }}

charts/nginx-gateway-fabric/templates/nginxproxy.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ spec:
3232
behavior:
3333
{{- toYaml .Values.nginx.autoscaling.behavior | nindent 10 }}
3434
{{- end }}
35-
{{- if .Values.nginx.autoscalingTemplate }}
36-
autoscalingTemplate:
37-
{{- toYaml .Values.nginx.autoscalingTemplate | nindent 8 }}
35+
{{- if .Values.nginx.autoscaling.templates }}
36+
templates:
37+
{{- toYaml .Values.nginx.autoscaling.templates | nindent 8 }}
3838
{{- end }}
3939
{{- end }}
4040
{{- if .Values.nginx.pod }}

charts/nginx-gateway-fabric/values.yaml

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -179,16 +179,15 @@ nginxGateway:
179179
# - type: Pods
180180
# value: 2
181181
# periodSeconds: 60
182-
# Custom or additional autoscaling metrics
183-
# autoscalingTemplate: []
184-
# ref: https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/#support-for-custom-metrics
185-
# - type: Pods
186-
# pods:
187-
# metric:
188-
# name: container_memory_usage_bytes
189-
# target:
190-
# type: AverageValue
191-
# averageValue: "400Mi"
182+
# Custom or additional autoscaling metrics. https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/#support-for-custom-metrics
183+
# templates: []
184+
# - type: Pods
185+
# pods:
186+
# metric:
187+
# name: container_memory_usage_bytes
188+
# target:
189+
# type: AverageValue
190+
# averageValue: "400Mi"
192191

193192
metrics:
194193
# -- Enable exposing metrics in the Prometheus format.
@@ -251,16 +250,15 @@ nginx:
251250
# - type: Pods
252251
# value: 2
253252
# periodSeconds: 60
254-
# Custom or additional autoscaling metrics
255-
# autoscalingTemplate: []
256-
# ref: https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/#support-for-custom-metrics
257-
# - type: Pods
258-
# pods:
259-
# metric:
260-
# name: nginx_gateway_fabric_nginx_process_requests_total
261-
# target:
262-
# type: nginx_http_requests_total
263-
# averageValue: 400
253+
# Custom or additional autoscaling metrics. https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/#support-for-custom-metrics
254+
# templates: []
255+
# - type: Pods
256+
# pods:
257+
# metric:
258+
# name: nginx_gateway_fabric_nginx_process_requests_total
259+
# target:
260+
# type: AverageValue
261+
# averageValue: 400
264262
image:
265263
# -- The NGINX image to use.
266264
repository: ghcr.io/nginx/nginx-gateway-fabric/nginx

config/crd/bases/gateway.nginx.org_nginxproxies.yaml

Lines changed: 186 additions & 192 deletions
Large diffs are not rendered by default.

deploy/crds.yaml

Lines changed: 186 additions & 192 deletions
Large diffs are not rendered by default.

internal/controller/provisioner/objects.go

Lines changed: 11 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,40 +1117,26 @@ func (p *NginxProvisioner) buildImage(nProxyCfg *graph.EffectiveNginxProxy) (str
11171117
return fmt.Sprintf("%s:%s", image, tag), pullPolicy
11181118
}
11191119

1120-
func getMetricTargetByType(target autoscalingv2.MetricTarget) autoscalingv2.MetricTarget {
1121-
switch target.Type {
1122-
case autoscalingv2.UtilizationMetricType:
1123-
return autoscalingv2.MetricTarget{
1124-
Type: autoscalingv2.UtilizationMetricType,
1125-
AverageUtilization: target.AverageUtilization,
1126-
}
1127-
case autoscalingv2.AverageValueMetricType:
1128-
return autoscalingv2.MetricTarget{
1129-
Type: autoscalingv2.AverageValueMetricType,
1130-
AverageValue: target.AverageValue,
1131-
}
1132-
case autoscalingv2.ValueMetricType:
1133-
return autoscalingv2.MetricTarget{
1134-
Type: autoscalingv2.ValueMetricType,
1135-
Value: target.Value,
1136-
}
1137-
default:
1138-
return autoscalingv2.MetricTarget{}
1139-
}
1140-
}
1141-
11421120
func buildNginxDeploymentHPA(
11431121
objectMeta metav1.ObjectMeta,
11441122
autoScaling *ngfAPIv1alpha2.HPASpec,
11451123
) *autoscalingv2.HorizontalPodAutoscaler {
11461124
if !autoScaling.Enable {
11471125
return nil
11481126
}
1149-
var metrics []autoscalingv2.MetricSpec
11501127

11511128
cpuUtil := autoScaling.TargetCPUUtilizationPercentage
11521129
memUtil := autoScaling.TargetMemoryUtilizationPercentage
1153-
autoscalingTemplate := autoScaling.AutoscalingTemplate
1130+
1131+
metricsLen := len(autoScaling.Templates)
1132+
if cpuUtil != nil {
1133+
metricsLen++
1134+
}
1135+
if memUtil != nil {
1136+
metricsLen++
1137+
}
1138+
1139+
metrics := make([]autoscalingv2.MetricSpec, 0, metricsLen)
11541140

11551141
if cpuUtil != nil {
11561142
metrics = append(metrics, autoscalingv2.MetricSpec{
@@ -1178,17 +1164,7 @@ func buildNginxDeploymentHPA(
11781164
})
11791165
}
11801166

1181-
for _, additionalAutoscaling := range autoscalingTemplate {
1182-
metric := buildAdditionalMetric(additionalAutoscaling)
1183-
if metric != nil {
1184-
metrics = append(metrics, *metric)
1185-
}
1186-
}
1187-
1188-
if len(metrics) == 0 {
1189-
// No metrics configured, skip HPA creation
1190-
return nil
1191-
}
1167+
metrics = append(metrics, autoScaling.Templates...)
11921168

11931169
return &autoscalingv2.HorizontalPodAutoscaler{
11941170
ObjectMeta: objectMeta,
@@ -1206,61 +1182,6 @@ func buildNginxDeploymentHPA(
12061182
}
12071183
}
12081184

1209-
func buildAdditionalMetric(additionalAutoscaling autoscalingv2.MetricSpec) *autoscalingv2.MetricSpec {
1210-
switch additionalAutoscaling.Type {
1211-
case autoscalingv2.ResourceMetricSourceType:
1212-
return &autoscalingv2.MetricSpec{
1213-
Type: additionalAutoscaling.Type,
1214-
Resource: &autoscalingv2.ResourceMetricSource{
1215-
Name: additionalAutoscaling.Resource.Name,
1216-
Target: getMetricTargetByType(additionalAutoscaling.Resource.Target),
1217-
},
1218-
}
1219-
case autoscalingv2.PodsMetricSourceType:
1220-
return &autoscalingv2.MetricSpec{
1221-
Type: additionalAutoscaling.Type,
1222-
Pods: &autoscalingv2.PodsMetricSource{
1223-
Metric: additionalAutoscaling.Pods.Metric,
1224-
Target: getMetricTargetByType(additionalAutoscaling.Pods.Target),
1225-
},
1226-
}
1227-
case autoscalingv2.ContainerResourceMetricSourceType:
1228-
return &autoscalingv2.MetricSpec{
1229-
Type: additionalAutoscaling.Type,
1230-
ContainerResource: &autoscalingv2.ContainerResourceMetricSource{
1231-
Name: additionalAutoscaling.ContainerResource.Name,
1232-
Target: getMetricTargetByType(additionalAutoscaling.ContainerResource.Target),
1233-
Container: additionalAutoscaling.ContainerResource.Container,
1234-
},
1235-
}
1236-
case autoscalingv2.ObjectMetricSourceType:
1237-
return &autoscalingv2.MetricSpec{
1238-
Type: additionalAutoscaling.Type,
1239-
Object: &autoscalingv2.ObjectMetricSource{
1240-
DescribedObject: additionalAutoscaling.Object.DescribedObject,
1241-
Target: getMetricTargetByType(additionalAutoscaling.Object.Target),
1242-
Metric: autoscalingv2.MetricIdentifier{
1243-
Name: additionalAutoscaling.Object.Metric.Name,
1244-
Selector: additionalAutoscaling.Object.Metric.Selector,
1245-
},
1246-
},
1247-
}
1248-
case autoscalingv2.ExternalMetricSourceType:
1249-
return &autoscalingv2.MetricSpec{
1250-
Type: additionalAutoscaling.Type,
1251-
External: &autoscalingv2.ExternalMetricSource{
1252-
Metric: autoscalingv2.MetricIdentifier{
1253-
Name: additionalAutoscaling.External.Metric.Name,
1254-
Selector: additionalAutoscaling.External.Metric.Selector,
1255-
},
1256-
Target: getMetricTargetByType(additionalAutoscaling.External.Target),
1257-
},
1258-
}
1259-
default:
1260-
return nil
1261-
}
1262-
}
1263-
12641185
// TODO(sberman): see about how this can be made more elegant. Maybe create some sort of Object factory
12651186
// that can better store/build all the objects we need, to reduce the amount of duplicate object lists that we
12661187
// have everywhere.

0 commit comments

Comments
 (0)