Skip to content

Commit b8e035b

Browse files
nowjeansjberman
authored andcommitted
Add control/data plane HPA
Problem: I want the NGF control plane and nginx data plane to scale automatically based on resource consumption or other metrics. Solution: Allow for configuring a HorizontalPodAutoscaler (HPA) through the helm chart and NginxProxy resource for both Deployments.
1 parent 6c410e5 commit b8e035b

File tree

18 files changed

+1937
-31
lines changed

18 files changed

+1937
-31
lines changed

apis/v1alpha2/nginxproxy_types.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package v1alpha2
22

33
import (
4+
autoscalingv2 "k8s.io/api/autoscaling/v2"
45
corev1 "k8s.io/api/core/v1"
56
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
67
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -440,6 +441,11 @@ type DeploymentSpec struct {
440441
// +optional
441442
Replicas *int32 `json:"replicas,omitempty"`
442443

444+
// Autoscaling defines the configuration for Horizontal Pod Autoscaling.
445+
//
446+
// +optional
447+
Autoscaling HPASpec `json:"autoscaling"`
448+
443449
// Pod defines Pod-specific fields.
444450
//
445451
// +optional
@@ -469,6 +475,53 @@ type DaemonSetSpec struct {
469475
Patches []Patch `json:"patches,omitempty"`
470476
}
471477

478+
// HPASpec is the configuration for the Horizontal Pod Autoscaling.
479+
//
480+
// +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))"
481+
// +kubebuilder:validation:XValidation:message="minReplicas must be less than or equal to maxReplicas",rule="self.minReplicas <= self.maxReplicas"
482+
//
483+
//nolint:lll
484+
type HPASpec struct {
485+
// Behavior configures the scaling behavior of the target
486+
// in both Up and Down directions (scaleUp and scaleDown fields respectively).
487+
// If not set, the default HPAScalingRules for scale up and scale down are used.
488+
//
489+
// +optional
490+
Behavior *autoscalingv2.HorizontalPodAutoscalerBehavior `json:"behavior,omitempty"`
491+
492+
// AutoscalingTemplate configures the additional scaling option.
493+
//
494+
// +optional
495+
AutoscalingTemplate []autoscalingv2.MetricSpec `json:"autoscalingTemplate,omitempty"`
496+
497+
// Target cpu utilization percentage of HPA.
498+
//
499+
// +optional
500+
// +kubebuilder:validation:Minimum=1
501+
// +kubebuilder:validation:Maximum=100
502+
TargetCPUUtilizationPercentage *int32 `json:"targetCPUUtilizationPercentage,omitempty"`
503+
504+
// Target memory utilization percentage of HPA.
505+
//
506+
// +optional
507+
// +kubebuilder:validation:Minimum=1
508+
// +kubebuilder:validation:Maximum=100
509+
TargetMemoryUtilizationPercentage *int32 `json:"targetMemoryUtilizationPercentage,omitempty"`
510+
511+
// Minimum number of replicas.
512+
//
513+
// +optional
514+
// +kubebuilder:validation:Minimum=1
515+
MinReplicas *int32 `json:"minReplicas,omitempty"`
516+
517+
// Maximum number of replicas.
518+
// +kubebuilder:validation:Minimum=1
519+
MaxReplicas int32 `json:"maxReplicas"`
520+
521+
// Enable or disable Horizontal Pod Autoscaler
522+
Enabled bool `json:"enabled"`
523+
}
524+
472525
// PodSpec defines Pod-specific fields.
473526
type PodSpec struct {
474527
// TerminationGracePeriodSeconds is the optional duration in seconds the pod needs to terminate gracefully.

apis/v1alpha2/zz_generated.deepcopy.go

Lines changed: 44 additions & 0 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: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,12 +264,12 @@ The following table lists the configurable parameters of the NGINX Gateway Fabri
264264
| `certGenerator.ttlSecondsAfterFinished` | How long to wait after the cert generator job has finished before it is removed by the job controller. | int | `30` |
265265
| `clusterDomain` | The DNS cluster domain of your Kubernetes cluster. | string | `"cluster.local"` |
266266
| `gateways` | A list of Gateway objects. View https://gateway-api.sigs.k8s.io/reference/spec/#gateway for full Gateway reference. | list | `[]` |
267-
| `nginx` | The nginx section contains the configuration for all NGINX data plane deployments installed by the NGINX Gateway Fabric control plane. | object | `{"config":{},"container":{"hostPorts":[],"lifecycle":{},"readinessProbe":{},"resources":{},"volumeMounts":[]},"debug":false,"image":{"pullPolicy":"Always","repository":"ghcr.io/nginx/nginx-gateway-fabric/nginx","tag":"edge"},"imagePullSecret":"","imagePullSecrets":[],"kind":"deployment","nginxOneConsole":{"dataplaneKeySecretName":"","endpointHost":"agent.connect.nginx.com","endpointPort":443,"skipVerify":false},"plus":false,"pod":{},"replicas":1,"service":{"externalTrafficPolicy":"Local","loadBalancerClass":"","loadBalancerIP":"","loadBalancerSourceRanges":[],"nodePorts":[],"type":"LoadBalancer"},"usage":{"caSecretName":"","clientSSLSecretName":"","endpoint":"","resolver":"","secretName":"nplus-license","skipVerify":false}}` |
267+
| `nginx` | The nginx section contains the configuration for all NGINX data plane deployments installed by the NGINX Gateway Fabric control plane. | object | `{"autoscaling":{"enabled":false},"autoscalingTemplate":[],"config":{},"container":{"hostPorts":[],"lifecycle":{},"readinessProbe":{},"resources":{},"volumeMounts":[]},"debug":false,"image":{"pullPolicy":"Always","repository":"ghcr.io/nginx/nginx-gateway-fabric/nginx","tag":"edge"},"imagePullSecret":"","imagePullSecrets":[],"kind":"deployment","nginxOneConsole":{"dataplaneKeySecretName":"","endpointHost":"agent.connect.nginx.com","endpointPort":443,"skipVerify":false},"plus":false,"pod":{},"replicas":1,"service":{"externalTrafficPolicy":"Local","loadBalancerClass":"","loadBalancerIP":"","loadBalancerSourceRanges":[],"nodePorts":[],"type":"LoadBalancer"},"usage":{"caSecretName":"","clientSSLSecretName":"","endpoint":"","resolver":"","secretName":"nplus-license","skipVerify":false}}` |
268268
| `nginx.config` | The configuration for the data plane that is contained in the NginxProxy resource. This is applied globally to all Gateways managed by this instance of NGINX Gateway Fabric. | object | `{}` |
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. | 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"` |
@@ -283,7 +283,7 @@ The following table lists the configurable parameters of the NGINX Gateway Fabri
283283
| `nginx.nginxOneConsole.skipVerify` | Skip TLS verification for NGINX One Console connections. | bool | `false` |
284284
| `nginx.plus` | Is NGINX Plus image being used. | bool | `false` |
285285
| `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 | `{}` |
286-
| `nginx.replicas` | The number of replicas of the NGINX Deployment. | int | `1` |
286+
| `nginx.replicas` | The number of replicas of the NGINX Deployment. This value is ignored if autoscaling.enabled is true. | int | `1` |
287287
| `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"}` |
288288
| `nginx.service.externalTrafficPolicy` | The externalTrafficPolicy of the service. The value Local preserves the client source IP. | string | `"Local"` |
289289
| `nginx.service.loadBalancerClass` | LoadBalancerClass is the class of the load balancer implementation this Service belongs to. Requires nginx.service.type set to LoadBalancer. | string | `""` |
@@ -297,7 +297,7 @@ The following table lists the configurable parameters of the NGINX Gateway Fabri
297297
| `nginx.usage.resolver` | The nameserver used to resolve the NGINX Plus usage reporting endpoint. Used with NGINX Instance Manager. | string | `""` |
298298
| `nginx.usage.secretName` | The name of the Secret containing the JWT for NGINX Plus usage reporting. Must exist in the same namespace that the NGINX Gateway Fabric control plane is running in (default namespace: nginx-gateway). | string | `"nplus-license"` |
299299
| `nginx.usage.skipVerify` | Disable client verification of the NGINX Plus usage reporting server certificate. | bool | `false` |
300-
| `nginxGateway` | The nginxGateway section contains configuration for the NGINX Gateway Fabric control plane deployment. | object | `{"affinity":{},"config":{"logging":{"level":"info"}},"configAnnotations":{},"extraVolumeMounts":[],"extraVolumes":[],"gatewayClassAnnotations":{},"gatewayClassName":"nginx","gatewayControllerName":"gateway.nginx.org/nginx-gateway-controller","gwAPIExperimentalFeatures":{"enable":false},"image":{"pullPolicy":"Always","repository":"ghcr.io/nginx/nginx-gateway-fabric","tag":"edge"},"kind":"deployment","labels":{},"leaderElection":{"enable":true,"lockName":""},"lifecycle":{},"metrics":{"enable":true,"port":9113,"secure":false},"name":"","nodeSelector":{},"podAnnotations":{},"productTelemetry":{"enable":true},"readinessProbe":{"enable":true,"initialDelaySeconds":3,"port":8081},"replicas":1,"resources":{},"service":{"annotations":{},"labels":{}},"serviceAccount":{"annotations":{},"imagePullSecret":"","imagePullSecrets":[],"name":""},"snippetsFilters":{"enable":false},"terminationGracePeriodSeconds":30,"tolerations":[],"topologySpreadConstraints":[]}` |
300+
| `nginxGateway` | The nginxGateway section contains configuration for the NGINX Gateway Fabric control plane deployment. | object | `{"affinity":{},"autoscaling":{"enabled":false},"autoscalingTemplate":[],"config":{"logging":{"level":"info"}},"configAnnotations":{},"extraVolumeMounts":[],"extraVolumes":[],"gatewayClassAnnotations":{},"gatewayClassName":"nginx","gatewayControllerName":"gateway.nginx.org/nginx-gateway-controller","gwAPIExperimentalFeatures":{"enable":false},"image":{"pullPolicy":"Always","repository":"ghcr.io/nginx/nginx-gateway-fabric","tag":"edge"},"kind":"deployment","labels":{},"leaderElection":{"enable":true,"lockName":""},"lifecycle":{},"metrics":{"enable":true,"port":9113,"secure":false},"name":"","nodeSelector":{},"podAnnotations":{},"productTelemetry":{"enable":true},"readinessProbe":{"enable":true,"initialDelaySeconds":3,"port":8081},"replicas":1,"resources":{},"service":{"annotations":{},"labels":{}},"serviceAccount":{"annotations":{},"imagePullSecret":"","imagePullSecrets":[],"name":""},"snippetsFilters":{"enable":false},"terminationGracePeriodSeconds":30,"tolerations":[],"topologySpreadConstraints":[]}` |
301301
| `nginxGateway.affinity` | The affinity of the NGINX Gateway Fabric control plane pod. | object | `{}` |
302302
| `nginxGateway.config.logging.level` | Log level. | string | `"info"` |
303303
| `nginxGateway.configAnnotations` | Set of custom annotations for NginxGateway objects. | object | `{}` |
@@ -324,7 +324,7 @@ The following table lists the configurable parameters of the NGINX Gateway Fabri
324324
| `nginxGateway.readinessProbe.enable` | Enable the /readyz endpoint on the control plane. | bool | `true` |
325325
| `nginxGateway.readinessProbe.initialDelaySeconds` | The number of seconds after the Pod has started before the readiness probes are initiated. | int | `3` |
326326
| `nginxGateway.readinessProbe.port` | Port in which the readiness endpoint is exposed. | int | `8081` |
327-
| `nginxGateway.replicas` | The number of replicas of the NGINX Gateway Fabric Deployment. | int | `1` |
327+
| `nginxGateway.replicas` | The number of replicas of the NGINX Gateway Fabric Deployment. This value is ignored if autoscaling.enabled is true. | int | `1` |
328328
| `nginxGateway.resources` | The resource requests and/or limits of the nginx-gateway container. | object | `{}` |
329329
| `nginxGateway.service` | The service configuration for the NGINX Gateway Fabric control plane. | object | `{"annotations":{},"labels":{}}` |
330330
| `nginxGateway.service.annotations` | The annotations of the NGINX Gateway Fabric control plane service. | object | `{}` |

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,19 @@ rules:
88
- apiGroups:
99
- ""
1010
- apps
11+
{{- if or .Values.nginx.autoscaling.enabled .Values.nginxGateway.autoscaling.enabled }}
12+
- autoscaling
13+
{{- end }}
1114
resources:
1215
- secrets
1316
- configmaps
1417
- serviceaccounts
1518
- services
1619
- deployments
1720
- daemonsets
21+
{{- if or .Values.nginx.autoscaling.enabled .Values.nginxGateway.autoscaling.enabled }}
22+
- horizontalpodautoscalers
23+
{{- end }}
1824
verbs:
1925
- create
2026
- update
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{{- if and (eq .Values.nginxGateway.kind "deployment") .Values.nginxGateway.autoscaling.enabled (.Capabilities.APIVersions.Has "autoscaling/v2") -}}
2+
apiVersion: autoscaling/v2
3+
kind: HorizontalPodAutoscaler
4+
metadata:
5+
{{- with .Values.nginxGateway.autoscaling.annotations }}
6+
annotations: {{ toYaml . | nindent 4 }}
7+
{{- end }}
8+
labels:
9+
{{- include "nginx-gateway.labels" . | nindent 4 }}
10+
{{- with .Values.nginxGateway.labels }}
11+
{{- toYaml . | nindent 4 }}
12+
{{- end }}
13+
name: {{ include "nginx-gateway.fullname" . }}
14+
namespace: {{ .Release.Namespace }}
15+
spec:
16+
scaleTargetRef:
17+
apiVersion: apps/v1
18+
kind: Deployment
19+
name: {{ include "nginx-gateway.fullname" . }}
20+
{{- if .Values.nginxGateway.autoscaling.minReplicas }}
21+
minReplicas: {{ .Values.nginxGateway.autoscaling.minReplicas }}
22+
{{- end }}
23+
maxReplicas: {{ .Values.nginxGateway.autoscaling.maxReplicas }}
24+
metrics:
25+
{{- with .Values.nginxGateway.autoscaling.targetMemoryUtilizationPercentage }}
26+
- type: Resource
27+
resource:
28+
name: memory
29+
target:
30+
type: Utilization
31+
averageUtilization: {{ . }}
32+
{{- end }}
33+
{{- with .Values.nginxGateway.autoscaling.targetCPUUtilizationPercentage }}
34+
- type: Resource
35+
resource:
36+
name: cpu
37+
target:
38+
type: Utilization
39+
averageUtilization: {{ . }}
40+
{{- end }}
41+
{{- with .Values.autoscalingTemplate }}
42+
{{- toYaml . | nindent 2 }}
43+
{{- end }}
44+
{{- with .Values.nginxGateway.autoscaling.behavior }}
45+
behavior:
46+
{{- toYaml . | nindent 4 }}
47+
{{- end }}
48+
{{- end }}

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,31 @@ spec:
1212
kubernetes:
1313
{{- if eq .Values.nginx.kind "deployment" }}
1414
deployment:
15+
{{- if .Values.nginx.replicas }}
1516
replicas: {{ .Values.nginx.replicas }}
17+
{{- end }}
18+
{{- if .Values.nginx.autoscaling.enabled }}
19+
autoscaling:
20+
enabled: {{ .Values.nginx.autoscaling.enabled }}
21+
{{- if .Values.nginx.autoscaling.minReplicas }}
22+
minReplicas: {{ .Values.nginx.autoscaling.minReplicas }}
23+
{{- end }}
24+
maxReplicas: {{ .Values.nginx.autoscaling.maxReplicas }}
25+
{{- if .Values.nginx.autoscaling.targetCPUUtilizationPercentage }}
26+
targetCPUUtilizationPercentage: {{ .Values.nginx.autoscaling.targetCPUUtilizationPercentage }}
27+
{{- end }}
28+
{{- if .Values.nginx.autoscaling.targetMemoryUtilizationPercentage }}
29+
targetMemoryUtilizationPercentage: {{ .Values.nginx.autoscaling.targetMemoryUtilizationPercentage }}
30+
{{- end }}
31+
{{- if .Values.nginx.autoscaling.behavior }}
32+
behavior:
33+
{{- toYaml .Values.nginx.autoscaling.behavior | nindent 10 }}
34+
{{- end }}
35+
{{- if .Values.nginx.autoscalingTemplate }}
36+
autoscalingTemplate:
37+
{{- toYaml .Values.nginx.autoscalingTemplate | nindent 8 }}
38+
{{- end }}
39+
{{- end }}
1640
{{- if .Values.nginx.pod }}
1741
pod:
1842
{{- toYaml .Values.nginx.pod | nindent 8 }}

0 commit comments

Comments
 (0)