Skip to content

Commit dd2d712

Browse files
committed
Code Review for status.desiredReplicas
1 parent 444740c commit dd2d712

File tree

11 files changed

+75
-52
lines changed

11 files changed

+75
-52
lines changed

apis/v1alpha2/nginxproxy_types.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -466,12 +466,10 @@ type DaemonSetSpec struct {
466466
Patches []Patch `json:"patches,omitempty"`
467467
}
468468

469+
// HPASpec is the configuration for the Horizontal Pod Autoscaling.
470+
//
469471
// +kubebuilder:validation:XValidation:message="at least one metric must be specified when autoscaling is enabled",rule="!self.enabled || (has(self.targetCPUUtilizationPercentage) || has(self.targetMemoryUtilizationPercentage) || (has(self.autoscalingTemplate) && size(self.autoscalingTemplate) > 0))"
470472
// +kubebuilder:validation:XValidation:message="minReplicas must be less than or equal to maxReplicas",rule="self.minReplicas <= self.maxReplicas"
471-
// +kubebuilder:validation:XValidation:message="CPU utilization must be between 1 and 100",rule="!has(self.targetCPUUtilizationPercentage) || (self.targetCPUUtilizationPercentage >= 1 && self.targetCPUUtilizationPercentage <= 100)"
472-
// +kubebuilder:validation:XValidation:message="memory utilization must be between 1 and 100",rule="!has(self.targetMemoryUtilizationPercentage) || (self.targetMemoryUtilizationPercentage >= 1 && self.targetMemoryUtilizationPercentage <= 100)"
473-
//
474-
// HPASpec is the configuration for the Horizontal Pod Autoscaling.
475473
//
476474
//nolint:lll
477475
type HPASpec struct {
@@ -485,24 +483,30 @@ type HPASpec struct {
485483
// AutoscalingTemplate configures the additional scaling option.
486484
//
487485
// +optional
488-
AutoscalingTemplate *[]autoscalingv2.MetricSpec `json:"autoscalingTemplate,omitempty"`
486+
AutoscalingTemplate []autoscalingv2.MetricSpec `json:"autoscalingTemplate,omitempty"`
489487

490488
// Target cpu utilization percentage of HPA.
491489
//
492490
// +optional
491+
// +kubebuilder:validation:Minimum=1
492+
// +kubebuilder:validation:Maximum=100
493493
TargetCPUUtilizationPercentage *int32 `json:"targetCPUUtilizationPercentage,omitempty"`
494494

495495
// Target memory utilization percentage of HPA.
496496
//
497497
// +optional
498+
// +kubebuilder:validation:Minimum=1
499+
// +kubebuilder:validation:Maximum=100
498500
TargetMemoryUtilizationPercentage *int32 `json:"targetMemoryUtilizationPercentage,omitempty"`
499501

500502
// Minimum number of replicas.
501503
//
502504
// +optional
505+
// +kubebuilder:validation:Minimum=1
503506
MinReplicas *int32 `json:"minReplicas,omitempty"`
504507

505508
// Maximum number of replicas.
509+
// +kubebuilder:validation:Minimum=1
506510
MaxReplicas int32 `json:"maxReplicas"`
507511

508512
// Enable or disable Horizontal Pod Autoscaler

apis/v1alpha2/zz_generated.deepcopy.go

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

charts/nginx-gateway-fabric/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ The following table lists the configurable parameters of the NGINX Gateway Fabri
269269
| `nginx.container` | The container configuration for the NGINX container. This is applied globally to all Gateways managed by this instance of NGINX Gateway Fabric. | object | `{"hostPorts":[],"lifecycle":{},"readinessProbe":{},"resources":{},"volumeMounts":[]}` |
270270
| `nginx.container.hostPorts` | A list of HostPorts to expose on the host. This configuration allows containers to bind to a specific port on the host node, enabling external network traffic to reach the container directly through the host's IP address and port. Use this option when you need to expose container ports on the host for direct access, such as for debugging, legacy integrations, or when NodePort/LoadBalancer services are not suitable. Note: Using hostPort may have security and scheduling implications, as it ties pods to specific nodes and ports. | list | `[]` |
271271
| `nginx.container.lifecycle` | The lifecycle of the NGINX container. | object | `{}` |
272-
| `nginx.container.resources` | The resource requirements of the NGINX container. You should be set this value, If you want to use dataplane Autoscaling(HPA). | object | `{}` |
272+
| `nginx.container.resources` | The resource requirements of the NGINX container. You should set this value if you want to use dataplane Autoscaling(HPA). | object | `{}` |
273273
| `nginx.container.volumeMounts` | volumeMounts are the additional volume mounts for the NGINX container. | list | `[]` |
274274
| `nginx.debug` | Enable debugging for NGINX. Uses the nginx-debug binary. The NGINX error log level should be set to debug in the NginxProxy resource. | bool | `false` |
275275
| `nginx.image.repository` | The NGINX image to use. | string | `"ghcr.io/nginx/nginx-gateway-fabric/nginx"` |
@@ -278,7 +278,7 @@ The following table lists the configurable parameters of the NGINX Gateway Fabri
278278
| `nginx.kind` | The kind of NGINX deployment. | string | `"deployment"` |
279279
| `nginx.plus` | Is NGINX Plus image being used. | bool | `false` |
280280
| `nginx.pod` | The pod configuration for the NGINX data plane pod. This is applied globally to all Gateways managed by this instance of NGINX Gateway Fabric. | object | `{}` |
281-
| `nginx.replicas` | The number of replicas of the NGINX Deployment. | int | `1` |
281+
| `nginx.replicas` | The number of replicas of the NGINX Deployment. This value is ignored if autoscaling.enabled is true. | int | `1` |
282282
| `nginx.service` | The service configuration for the NGINX data plane. This is applied globally to all Gateways managed by this instance of NGINX Gateway Fabric. | object | `{"externalTrafficPolicy":"Local","loadBalancerClass":"","loadBalancerIP":"","loadBalancerSourceRanges":[],"nodePorts":[],"type":"LoadBalancer"}` |
283283
| `nginx.service.externalTrafficPolicy` | The externalTrafficPolicy of the service. The value Local preserves the client source IP. | string | `"Local"` |
284284
| `nginx.service.loadBalancerClass` | LoadBalancerClass is the class of the load balancer implementation this Service belongs to. Requires nginx.service.type set to LoadBalancer. | string | `""` |
@@ -319,7 +319,7 @@ The following table lists the configurable parameters of the NGINX Gateway Fabri
319319
| `nginxGateway.readinessProbe.enable` | Enable the /readyz endpoint on the control plane. | bool | `true` |
320320
| `nginxGateway.readinessProbe.initialDelaySeconds` | The number of seconds after the Pod has started before the readiness probes are initiated. | int | `3` |
321321
| `nginxGateway.readinessProbe.port` | Port in which the readiness endpoint is exposed. | int | `8081` |
322-
| `nginxGateway.replicas` | The number of replicas of the NGINX Gateway Fabric Deployment. | int | `1` |
322+
| `nginxGateway.replicas` | The number of replicas of the NGINX Gateway Fabric Deployment. This value is ignored if autoscaling.enabled is true. | int | `1` |
323323
| `nginxGateway.resources` | The resource requests and/or limits of the nginx-gateway container. | object | `{}` |
324324
| `nginxGateway.service` | The service configuration for the NGINX Gateway Fabric control plane. | object | `{"annotations":{},"labels":{}}` |
325325
| `nginxGateway.service.annotations` | The annotations of the NGINX Gateway Fabric control plane service. | object | `{}` |

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ spec:
1717
apiVersion: apps/v1
1818
kind: Deployment
1919
name: {{ include "nginx-gateway.fullname" . }}
20+
{{- if .Values.nginxGateway.autoscaling.minReplicas }}
2021
minReplicas: {{ .Values.nginxGateway.autoscaling.minReplicas }}
22+
{{- end }}
2123
maxReplicas: {{ .Values.nginxGateway.autoscaling.maxReplicas }}
2224
metrics:
2325
{{- with .Values.nginxGateway.autoscaling.targetMemoryUtilizationPercentage }}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ spec:
1818
{{- if .Values.nginx.autoscaling.enabled }}
1919
autoscaling:
2020
enabled: {{ .Values.nginx.autoscaling.enabled }}
21+
{{- if .Values.nginx.autoscaling.minReplicas }}
2122
minReplicas: {{ .Values.nginx.autoscaling.minReplicas }}
23+
{{- end }}
2224
maxReplicas: {{ .Values.nginx.autoscaling.maxReplicas }}
2325
{{- if .Values.nginx.autoscaling.targetCPUUtilizationPercentage }}
2426
targetCPUUtilizationPercentage: {{ .Values.nginx.autoscaling.targetCPUUtilizationPercentage }}

charts/nginx-gateway-fabric/values.schema.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@
380380
"type": "object"
381381
},
382382
"resources": {
383-
"description": "The resource requirements of the NGINX container. You should be set this value, If you want to use dataplane Autoscaling(HPA).",
383+
"description": "The resource requirements of the NGINX container. You should set this value if you want to use dataplane Autoscaling(HPA).",
384384
"required": [],
385385
"title": "resources",
386386
"type": "object"
@@ -407,7 +407,7 @@
407407
"type": "boolean"
408408
},
409409
"image": {
410-
"description": "Custom or additional autoscaling metrics\nref: https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/#support-for-custom-metrics\n- type: Pods\n pods:\n metric:\n name: nginx_gateway_fabric_nginx_process_requests_total\n target:\n type: AverageValue\n averageValue: 10000m",
410+
"description": "Custom or additional autoscaling metrics\nref: https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/#support-for-custom-metrics\n- type: Pods\n pods:\n metric:\n name: nginx_gateway_fabric_nginx_process_requests_total\n target:\n type: nginx_http_requests_total\n averageValue: 400",
411411
"properties": {
412412
"pullPolicy": {
413413
"default": "Always",
@@ -478,7 +478,7 @@
478478
},
479479
"replicas": {
480480
"default": 1,
481-
"description": "The number of replicas of the NGINX Deployment.",
481+
"description": "The number of replicas of the NGINX Deployment. This value is ignored if autoscaling.enabled is true.",
482482
"required": [],
483483
"title": "replicas",
484484
"type": "integer"
@@ -897,7 +897,7 @@
897897
},
898898
"replicas": {
899899
"default": 1,
900-
"description": "The number of replicas of the NGINX Gateway Fabric Deployment.",
900+
"description": "The number of replicas of the NGINX Gateway Fabric Deployment. This value is ignored if autoscaling.enabled is true.",
901901
"required": [],
902902
"title": "replicas",
903903
"type": "integer"

charts/nginx-gateway-fabric/values.yaml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ nginxGateway:
8080
# Secrets must exist in the same namespace as the helm release.
8181
imagePullSecrets: []
8282

83-
# -- The number of replicas of the NGINX Gateway Fabric Deployment.
83+
# -- The number of replicas of the NGINX Gateway Fabric Deployment. This value is ignored if autoscaling.enabled is true.
8484
replicas: 1
8585

8686
# The configuration for leader election.
@@ -184,10 +184,10 @@ nginxGateway:
184184
# - type: Pods
185185
# pods:
186186
# metric:
187-
# name: nginx_gateway_fabric_nginx_process_requests_total
187+
# name: container_memory_usage_bytes
188188
# target:
189189
# type: AverageValue
190-
# averageValue: 10000m
190+
# averageValue: "400Mi"
191191

192192
metrics:
193193
# -- Enable exposing metrics in the Prometheus format.
@@ -226,7 +226,7 @@ nginx:
226226
# -- The kind of NGINX deployment.
227227
kind: deployment
228228

229-
# -- The number of replicas of the NGINX Deployment.
229+
# -- The number of replicas of the NGINX Deployment. This value is ignored if autoscaling.enabled is true.
230230
replicas: 1
231231

232232
autoscaling:
@@ -257,8 +257,8 @@ nginx:
257257
# metric:
258258
# name: nginx_gateway_fabric_nginx_process_requests_total
259259
# target:
260-
# type: AverageValue
261-
# averageValue: 10000m
260+
# type: nginx_http_requests_total
261+
# averageValue: 400
262262
image:
263263
# -- The NGINX image to use.
264264
repository: ghcr.io/nginx/nginx-gateway-fabric/nginx
@@ -494,7 +494,7 @@ nginx:
494494
# - port: 80
495495
# containerPort: 80
496496

497-
# -- The resource requirements of the NGINX container. You should be set this value, If you want to use dataplane Autoscaling(HPA).
497+
# -- The resource requirements of the NGINX container. You should set this value if you want to use dataplane Autoscaling(HPA).
498498
resources: {}
499499

500500
# -- The lifecycle of the NGINX container.

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4175,18 +4175,24 @@ spec:
41754175
maxReplicas:
41764176
description: Maximum number of replicas.
41774177
format: int32
4178+
minimum: 1
41784179
type: integer
41794180
minReplicas:
41804181
description: Minimum number of replicas.
41814182
format: int32
4183+
minimum: 1
41824184
type: integer
41834185
targetCPUUtilizationPercentage:
41844186
description: Target cpu utilization percentage of HPA.
41854187
format: int32
4188+
maximum: 100
4189+
minimum: 1
41864190
type: integer
41874191
targetMemoryUtilizationPercentage:
41884192
description: Target memory utilization percentage of HPA.
41894193
format: int32
4194+
maximum: 100
4195+
minimum: 1
41904196
type: integer
41914197
required:
41924198
- enabled
@@ -4200,12 +4206,6 @@ spec:
42004206
&& size(self.autoscalingTemplate) > 0))'
42014207
- message: minReplicas must be less than or equal to maxReplicas
42024208
rule: self.minReplicas <= self.maxReplicas
4203-
- message: CPU utilization must be between 1 and 100
4204-
rule: '!has(self.targetCPUUtilizationPercentage) || (self.targetCPUUtilizationPercentage
4205-
>= 1 && self.targetCPUUtilizationPercentage <= 100)'
4206-
- message: memory utilization must be between 1 and 100
4207-
rule: '!has(self.targetMemoryUtilizationPercentage) || (self.targetMemoryUtilizationPercentage
4208-
>= 1 && self.targetMemoryUtilizationPercentage <= 100)'
42094209
container:
42104210
description: Container defines container fields for the NGINX
42114211
container.

deploy/crds.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4760,18 +4760,24 @@ spec:
47604760
maxReplicas:
47614761
description: Maximum number of replicas.
47624762
format: int32
4763+
minimum: 1
47634764
type: integer
47644765
minReplicas:
47654766
description: Minimum number of replicas.
47664767
format: int32
4768+
minimum: 1
47674769
type: integer
47684770
targetCPUUtilizationPercentage:
47694771
description: Target cpu utilization percentage of HPA.
47704772
format: int32
4773+
maximum: 100
4774+
minimum: 1
47714775
type: integer
47724776
targetMemoryUtilizationPercentage:
47734777
description: Target memory utilization percentage of HPA.
47744778
format: int32
4779+
maximum: 100
4780+
minimum: 1
47754781
type: integer
47764782
required:
47774783
- enabled
@@ -4785,12 +4791,6 @@ spec:
47854791
&& size(self.autoscalingTemplate) > 0))'
47864792
- message: minReplicas must be less than or equal to maxReplicas
47874793
rule: self.minReplicas <= self.maxReplicas
4788-
- message: CPU utilization must be between 1 and 100
4789-
rule: '!has(self.targetCPUUtilizationPercentage) || (self.targetCPUUtilizationPercentage
4790-
>= 1 && self.targetCPUUtilizationPercentage <= 100)'
4791-
- message: memory utilization must be between 1 and 100
4792-
rule: '!has(self.targetMemoryUtilizationPercentage) || (self.targetMemoryUtilizationPercentage
4793-
>= 1 && self.targetMemoryUtilizationPercentage <= 100)'
47944794
container:
47954795
description: Container defines container fields for the NGINX
47964796
container.

internal/controller/provisioner/objects.go

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -614,8 +614,28 @@ func (p *NginxProvisioner) buildNginxDeployment(
614614
}
615615
}
616616

617+
var replicas *int32
617618
if deploymentCfg.Replicas != nil {
618-
deployment.Spec.Replicas = deploymentCfg.Replicas
619+
replicas = deploymentCfg.Replicas
620+
}
621+
622+
if isAutoscalingEnabled(&deploymentCfg) {
623+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
624+
defer cancel()
625+
626+
hpa := &autoscalingv2.HorizontalPodAutoscaler{}
627+
err := p.k8sClient.Get(ctx, types.NamespacedName{
628+
Namespace: objectMeta.Namespace,
629+
Name: objectMeta.Name,
630+
}, hpa)
631+
if err == nil && hpa.Status.DesiredReplicas > 0 {
632+
// overwrite with HPA's desiredReplicas
633+
replicas = helpers.GetPointer(hpa.Status.DesiredReplicas)
634+
}
635+
}
636+
637+
if replicas != nil {
638+
deployment.Spec.Replicas = replicas
619639
}
620640

621641
return deployment, nil
@@ -1101,12 +1121,10 @@ func buildNginxDeploymentHPA(
11011121
})
11021122
}
11031123

1104-
if autoscalingTemplate != nil {
1105-
for _, additionalAutoscaling := range *autoscalingTemplate {
1106-
metric := buildAdditionalMetric(additionalAutoscaling)
1107-
if metric != nil {
1108-
metrics = append(metrics, *metric)
1109-
}
1124+
for _, additionalAutoscaling := range autoscalingTemplate {
1125+
metric := buildAdditionalMetric(additionalAutoscaling)
1126+
if metric != nil {
1127+
metrics = append(metrics, *metric)
11101128
}
11111129
}
11121130

@@ -1193,11 +1211,11 @@ func (p *NginxProvisioner) buildNginxResourceObjectsForDeletion(deploymentNSName
11931211
// order to delete:
11941212
// deployment/daemonset
11951213
// service
1214+
// hpa
11961215
// role/binding (if openshift)
11971216
// serviceaccount
11981217
// configmaps
11991218
// secrets
1200-
// hpa
12011219

12021220
objectMeta := metav1.ObjectMeta{
12031221
Name: deploymentNSName.Name,
@@ -1219,12 +1237,6 @@ func (p *NginxProvisioner) buildNginxResourceObjectsForDeletion(deploymentNSName
12191237

12201238
objects := []client.Object{deployment, daemonSet, service, hpa}
12211239

1222-
// objects := []client.Object{deployment, daemonSet, service}
1223-
1224-
// // if hpa := p.buildHPA(objectMeta, nProxyCfg); hpa != nil {
1225-
// // objects = append(objects, hpa)
1226-
// // }
1227-
12281240
if p.isOpenshift {
12291241
role := &rbacv1.Role{
12301242
ObjectMeta: objectMeta,

0 commit comments

Comments
 (0)