Skip to content

Commit 401f695

Browse files
committed
Add NGINX plus Dockerfile, add make command, add metrics (nginx#1394)
* Add nginx plus Dockerfile, add make command, add metrics
1 parent 3b3c3f7 commit 401f695

File tree

19 files changed

+224
-22
lines changed

19 files changed

+224
-22
lines changed

.github/workflows/ci.yml

+1
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ jobs:
215215
build-args: |
216216
NJS_DIR=internal/mode/static/nginx/modules/src
217217
NGINX_CONF_DIR=internal/mode/static/nginx/conf
218+
BUILD_AGENT=gha
218219
219220
- name: Deploy Kubernetes
220221
id: k8s

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,7 @@ internal/mode/static/nginx/modules/coverage
3737

3838
# MacOS Finder
3939
.DS_Store
40+
41+
# Certs and keys
42+
*.crt
43+
*.key

Makefile

+11-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ MANIFEST_DIR = $(shell pwd)/deploy/manifests
66
CHART_DIR = $(shell pwd)/deploy/helm-chart
77
NGINX_CONF_DIR = internal/mode/static/nginx/conf
88
NJS_DIR = internal/mode/static/nginx/modules/src
9+
NGINX_DOCKER_BUILD_PLUS_ARGS = --secret id=nginx-repo.crt,src=nginx-repo.crt --secret id=nginx-repo.key,src=nginx-repo.key
10+
BUILD_AGENT=local
911

1012
# go build flags - should not be overridden by the user
1113
GO_LINKER_FlAGS_VARS = -X main.version=${VERSION} -X main.commit=${GIT_COMMIT} -X main.date=${DATE}
@@ -15,6 +17,7 @@ GO_LINKER_FLAGS = $(GO_LINKER_FLAGS_OPTIMIZATIONS) $(GO_LINKER_FlAGS_VARS)
1517
# variables that can be overridden by the user
1618
PREFIX ?= nginx-gateway-fabric## The name of the NGF image. For example, nginx-gateway-fabric
1719
NGINX_PREFIX ?= $(PREFIX)/nginx## The name of the nginx image. For example: nginx-gateway-fabric/nginx
20+
NGINX_PLUS_PREFIX ?= $(PREFIX)/nginxplus## The name of the nginx plus image. For example: nginx-gateway-fabric/nginxplus
1821
TAG ?= $(VERSION:v%=%)## The tag of the image. For example, 0.3.0
1922
TARGET ?= local## The target of the build. Possible values: local and container
2023
KIND_KUBE_CONFIG=$${HOME}/.kube/kind/config## The location of the kind kubeconfig
@@ -23,7 +26,7 @@ GOARCH ?= amd64## The architecture of the image and/or binary. For example: amd6
2326
GOOS ?= linux## The OS of the image and/or binary. For example: linux or darwin
2427
override HELM_TEMPLATE_COMMON_ARGS += --set creator=template --set nameOverride=nginx-gateway## The common options for the Helm template command.
2528
override HELM_TEMPLATE_EXTRA_ARGS_FOR_ALL_MANIFESTS_FILE += --set service.create=false## The options to be passed to the full Helm templating command only.
26-
override NGINX_DOCKER_BUILD_OPTIONS += --build-arg NJS_DIR=$(NJS_DIR) --build-arg NGINX_CONF_DIR=$(NGINX_CONF_DIR)
29+
override NGINX_DOCKER_BUILD_OPTIONS += --build-arg NJS_DIR=$(NJS_DIR) --build-arg NGINX_CONF_DIR=$(NGINX_CONF_DIR) --build-arg BUILD_AGENT=$(BUILD_AGENT)
2730
.DEFAULT_GOAL := help
2831

2932
.PHONY: help
@@ -34,6 +37,9 @@ help: Makefile ## Display this help
3437
.PHONY: build-images
3538
build-images: build-ngf-image build-nginx-image ## Build the NGF and nginx docker images
3639

40+
.PHONY: build-images-with-plus
41+
build-images-with-plus: build-ngf-image build-nginx-plus-image ## Build the NGF and NGINX Plus docker images
42+
3743
.PHONY: build-ngf-image
3844
build-ngf-image: check-for-docker build ## Build the NGF docker image
3945
docker build --platform linux/$(GOARCH) --target $(strip $(TARGET)) -f build/Dockerfile -t $(strip $(PREFIX)):$(strip $(TAG)) .
@@ -42,6 +48,10 @@ build-ngf-image: check-for-docker build ## Build the NGF docker image
4248
build-nginx-image: check-for-docker ## Build the custom nginx image
4349
docker build --platform linux/$(GOARCH) $(strip $(NGINX_DOCKER_BUILD_OPTIONS)) -f build/Dockerfile.nginx -t $(strip $(NGINX_PREFIX)):$(strip $(TAG)) .
4450

51+
.PHONY: build-nginx-plus-image
52+
build-nginx-plus-image: check-for-docker ## Build the custom nginx plus image
53+
docker build --platform linux/$(GOARCH) $(strip $(NGINX_DOCKER_BUILD_OPTIONS)) $(strip $(NGINX_DOCKER_BUILD_PLUS_ARGS)) -f build/Dockerfile.nginxplus -t $(strip $(NGINX_PLUS_PREFIX)):$(strip $(TAG)) .
54+
4555
.PHONY: check-for-docker
4656
check-for-docker: ## Check if Docker is installed
4757
@docker -v || (code=$$?; printf "\033[0;31mError\033[0m: there was a problem with Docker\n"; exit $$code)

README.md

+11-11
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,17 @@ the [Issue Lifecycle](ISSUE_LIFECYCLE.md) document for information on issue crea
6262

6363
The following table lists the software versions NGINX Gateway Fabric supports.
6464

65-
| NGINX Gateway Fabric | Gateway API | Kubernetes | NGINX OSS |
66-
|----------------------|-------------|------------|-----------|
67-
| Edge | 1.0.0 | 1.23+ | 1.25.3 |
68-
| 1.1.0 | 1.0.0 | 1.23+ | 1.25.3 |
69-
| 1.0.0 | 0.8.1 | 1.23+ | 1.25.2 |
70-
| 0.6.0 | 0.8.0 | 1.23+ | 1.25.2 |
71-
| 0.5.0 | 0.7.1 | 1.21+ | 1.25.x * |
72-
| 0.4.0 | 0.7.1 | 1.21+ | 1.25.x * |
73-
| 0.3.0 | 0.6.2 | 1.21+ | 1.23.x * |
74-
| 0.2.0 | 0.5.1 | 1.21+ | 1.21.x * |
75-
| 0.1.0 | 0.5.0 | 1.19+ | 1.21.3 |
65+
| NGINX Gateway Fabric | Gateway API | Kubernetes | NGINX OSS | NGINX Plus |
66+
|----------------------|-------------|------------|-----------|------------|
67+
| Edge | 1.0.0 | 1.23+ | 1.25.3 | R30 |
68+
| 1.1.0 | 1.0.0 | 1.23+ | 1.25.3 | n/a |
69+
| 1.0.0 | 0.8.1 | 1.23+ | 1.25.2 | n/a |
70+
| 0.6.0 | 0.8.0 | 1.23+ | 1.25.2 | n/a |
71+
| 0.5.0 | 0.7.1 | 1.21+ | 1.25.x * | n/a |
72+
| 0.4.0 | 0.7.1 | 1.21+ | 1.25.x * | n/a |
73+
| 0.3.0 | 0.6.2 | 1.21+ | 1.23.x * | n/a |
74+
| 0.2.0 | 0.5.1 | 1.21+ | 1.21.x * | n/a |
75+
| 0.1.0 | 0.5.0 | 1.19+ | 1.21.3 | n/a |
7676

7777
\*the installation manifests use the minor version of NGINX container image (e.g. 1.25) and the patch version is not
7878
specified. This means that the latest available patch version is used.

build/Dockerfile.nginx

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ FROM nginx:1.25.3-alpine
33

44
ARG NJS_DIR
55
ARG NGINX_CONF_DIR
6+
ARG BUILD_AGENT
67

78
RUN apk update && apk upgrade && apk add --no-cache libcap \
89
&& mkdir -p /var/lib/nginx /usr/lib/nginx/modules \
@@ -15,4 +16,6 @@ COPY ${NGINX_CONF_DIR}/nginx.conf /etc/nginx/nginx.conf
1516

1617
RUN chown -R 101:1001 /etc/nginx /var/cache/nginx /var/lib/nginx
1718

19+
LABEL org.nginx.ngf.image.build.agent="${BUILD_AGENT}"
20+
1821
USER 101:1001

build/Dockerfile.nginxplus

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# syntax=docker/dockerfile:1.4
2+
FROM alpine:3.18
3+
4+
ARG NGINX_PLUS_VERSION=R30
5+
ARG NJS_DIR
6+
ARG NGINX_CONF_DIR
7+
ARG BUILD_AGENT
8+
9+
RUN --mount=type=secret,id=nginx-repo.crt,dst=/etc/apk/cert.pem,mode=0644 \
10+
--mount=type=secret,id=nginx-repo.key,dst=/etc/apk/cert.key,mode=0644 \
11+
addgroup -g 1001 -S nginx \
12+
&& adduser -S -D -H -u 101 -h /var/cache/nginx -s /sbin/nologin -G nginx -g nginx nginx \
13+
&& wget -nv -O /etc/apk/keys/nginx_signing.rsa.pub https://cs.nginx.com/static/keys/nginx_signing.rsa.pub \
14+
&& printf "%s\n" "https://pkgs.nginx.com/plus/${NGINX_PLUS_VERSION}/alpine/v$(grep -E -o '^[0-9]+\.[0-9]+' /etc/alpine-release)/main" >> /etc/apk/repositories \
15+
&& apk add --no-cache nginx-plus nginx-plus-module-njs libcap \
16+
&& ldconfig /usr/local/lib/ \
17+
&& mkdir -p /var/lib/nginx /usr/lib/nginx/modules \
18+
&& setcap 'cap_net_bind_service=+ep' /usr/sbin/nginx \
19+
&& setcap -v 'cap_net_bind_service=+ep' /usr/sbin/nginx \
20+
&& apk del libcap \
21+
# forward request and error logs to docker log collector
22+
&& ln -sf /dev/stdout /var/log/nginx/access.log \
23+
&& ln -sf /dev/stderr /var/log/nginx/error.log
24+
25+
COPY ${NJS_DIR}/httpmatches.js /usr/lib/nginx/modules/njs/httpmatches.js
26+
COPY ${NGINX_CONF_DIR}/nginx-plus.conf /etc/nginx/nginx.conf
27+
28+
RUN chown -R 101:1001 /etc/nginx /var/cache/nginx /var/lib/nginx
29+
30+
USER 101:1001
31+
32+
LABEL org.nginx.ngf.image.build.agent="${BUILD_AGENT}"
33+
34+
CMD ["nginx", "-g", "daemon off;"]

cmd/gateway/commands.go

+11
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func createStaticModeCommand() *cobra.Command {
5555
healthPortFlag = "health-port"
5656
leaderElectionDisableFlag = "leader-election-disable"
5757
leaderElectionLockNameFlag = "leader-election-lock-name"
58+
plusFlag = "nginx-plus"
5859
)
5960

6061
// flag values
@@ -92,6 +93,8 @@ func createStaticModeCommand() *cobra.Command {
9293
validator: validateResourceName,
9394
value: "nginx-gateway-leader-election-lock",
9495
}
96+
97+
plus bool
9598
)
9699

97100
cmd := &cobra.Command{
@@ -160,6 +163,7 @@ func createStaticModeCommand() *cobra.Command {
160163
LockName: leaderElectionLockName.String(),
161164
Identity: podName,
162165
},
166+
Plus: plus,
163167
}
164168

165169
if err := static.StartManager(conf); err != nil {
@@ -266,6 +270,13 @@ func createStaticModeCommand() *cobra.Command {
266270
"A Lease object with this name will be created in the same Namespace as the controller.",
267271
)
268272

273+
cmd.Flags().BoolVar(
274+
&plus,
275+
plusFlag,
276+
false,
277+
"Use NGINX Plus",
278+
)
279+
269280
return cmd
270281
}
271282

deploy/helm-chart/README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
- [Installing the Chart via Sources](#installing-the-chart-via-sources)
1010
- [Pulling the Chart](#pulling-the-chart)
1111
- [Installing the Chart](#installing-the-chart-1)
12+
- [Custom installation options](#custom-installation-options)
13+
- [Service type](#service-type)
1214
- [Upgrading the Chart](#upgrading-the-chart)
1315
- [Upgrading the Gateway Resources](#upgrading-the-gateway-resources)
1416
- [Upgrading the CRDs](#upgrading-the-crds)
@@ -275,7 +277,7 @@ kubectl delete -f https://github.com./kubernetes-sigs/gateway-api/releases/downlo
275277
The following tables lists the configurable parameters of the NGINX Gateway Fabric chart and their default values.
276278

277279
| Parameter | Description | Default Value |
278-
|---------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------|
280+
| ------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------- |
279281
| `nginxGateway.image.repository` | The repository for the NGINX Gateway Fabric image. | ghcr.io/nginxinc/nginx-gateway-fabric |
280282
| `nginxGateway.image.tag` | The tag for the NGINX Gateway Fabric image. | edge |
281283
| `nginxGateway.image.pullPolicy` | The `imagePullPolicy` for the NGINX Gateway Fabric image. | Always |
@@ -294,6 +296,7 @@ The following tables lists the configurable parameters of the NGINX Gateway Fabr
294296
| `nginx.image.repository` | The repository for the NGINX image. | ghcr.io/nginxinc/nginx-gateway-fabric/nginx |
295297
| `nginx.image.tag` | The tag for the NGINX image. | edge |
296298
| `nginx.image.pullPolicy` | The `imagePullPolicy` for the NGINX image. | Always |
299+
| `nginx.plus` | Is NGINX Plus image being used | false |
297300
| `nginx.lifecycle` | The `lifecycle` of the nginx container. | {} |
298301
| `nginx.extraVolumeMounts` | Extra `volumeMounts` for the nginx container. | {} |
299302
| `terminationGracePeriodSeconds` | The termination grace period of the NGINX Gateway Fabric pod. | 30 |

deploy/helm-chart/templates/deployment.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ spec:
3131
- --gatewayclass={{ .Values.nginxGateway.gatewayClassName }}
3232
- --config={{ include "nginx-gateway.config-name" . }}
3333
- --service={{ include "nginx-gateway.fullname" . }}
34+
{{- if .Values.nginx.plus }}
35+
- --nginx-plus
36+
{{- end }}
3437
{{- if .Values.metrics.enable }}
3538
- --metrics-port={{ .Values.metrics.port }}
3639
{{- if .Values.metrics.secure }}

deploy/helm-chart/values.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ nginx:
5858
tag: edge
5959
pullPolicy: Always
6060

61+
## Is NGINX Plus image being used
62+
plus: false
63+
6164
## The lifecycle of the nginx container.
6265
lifecycle: {}
6366

docs/developer/quickstart.md

+36-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Follow these steps to set up your development environment.
4848
make deps
4949
```
5050

51-
## Build the Binary and Image
51+
## Build the Binary and Images
5252

5353
### Build the Binary
5454

@@ -70,6 +70,19 @@ make TAG=$(whoami) build-images
7070
7171
This will build the docker images `nginx-gateway-fabric:<your-user>` and `nginx-gateway-fabric/nginx:<your-user>`.
7272
73+
### Build the Images with NGINX Plus
74+
75+
> Note: You will need a valid NGINX Plus license certificate and key named `nginx-repo.crt` and `nginx-repo.key` in the
76+
> root of this repo to build the NGINX Plus image.
77+
78+
To build the NGINX Gateway Fabric and NGINX Plus container images from source run the following make command:
79+
80+
```makefile
81+
make TAG=$(whoami) build-images-with-plus
82+
```
83+
84+
This will build the docker images `nginx-gateway-fabric:<your-user>` and `nginx-gateway-fabric/nginxplus:<your-user>`.
85+
7386
## Deploy on Kind
7487
7588
1. Create a `kind` cluster:
@@ -84,6 +97,12 @@ This will build the docker images `nginx-gateway-fabric:<your-user>` and `nginx-
8497
kind load docker-image nginx-gateway-fabric:$(whoami) nginx-gateway-fabric/nginx:$(whoami)
8598
```
8699
100+
or
101+
102+
```shell
103+
kind load docker-image nginx-gateway-fabric:$(whoami) nginx-gateway-fabric/nginxplus:$(whoami)
104+
```
105+
87106
3. Install Gateway API CRDs:
88107
89108
```shell
@@ -98,7 +117,13 @@ This will build the docker images `nginx-gateway-fabric:<your-user>` and `nginx-
98117
helm install my-release ./deploy/helm-chart --create-namespace --wait --set service.type=NodePort --set nginxGateway.image.repository=nginx-gateway-fabric --set nginxGateway.image.tag=$(whoami) --set nginxGateway.image.pullPolicy=Never --set nginx.image.repository=nginx-gateway-fabric/nginx --set nginx.image.tag=$(whoami) --set nginx.image.pullPolicy=Never -n nginx-gateway
99118
```
100119
101-
> For more information on helm configuration options see the Helm [README](../../deploy/helm-chart/README.md).
120+
- To install NGINX Plus with Helm (where your release name is `my-release`):
121+
122+
```shell
123+
helm install my-release ./deploy/helm-chart --create-namespace --wait --set service.type=NodePort --set nginxGateway.image.repository=nginx-gateway-fabric --set nginxGateway.image.tag=$(whoami) --set nginxGateway.image.pullPolicy=Never --set nginx.image.repository=nginx-gateway-fabric/nginxplus --set nginx.image.tag=$(whoami) --set nginx.image.pullPolicy=Never --set nginx.plus=true -n nginx-gateway
124+
```
125+
126+
> For more information on Helm configuration options see the Helm [README](../../deploy/helm-chart/README.md).
102127
103128
- To install with manifests:
104129
@@ -109,6 +134,15 @@ This will build the docker images `nginx-gateway-fabric:<your-user>` and `nginx-
109134
kubectl apply -f deploy/manifests/service/nodeport.yaml
110135
```
111136
137+
- To install NGINX Plus with manifests:
138+
139+
```shell
140+
make generate-manifests HELM_TEMPLATE_COMMON_ARGS="--set nginxGateway.image.repository=nginx-gateway-fabric --set nginxGateway.image.tag=$(whoami) --set nginxGateway.image.pullPolicy=Never --set nginx.image.repository=nginx-gateway-fabric/nginxplus --set nginx.image.tag=$(whoami) --set nginx.image.pullPolicy=Never --set nginx.plus=true"
141+
kubectl apply -f deploy/manifests/crds
142+
kubectl apply -f deploy/manifests/nginx-gateway.yaml
143+
kubectl apply -f deploy/manifests/service/nodeport.yaml
144+
```
145+
112146
### Run Examples
113147
114148
To make sure NGF is running properly, try out the [examples](/examples).

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com./go-logr/logr v1.3.0
1010
github.com./google/go-cmp v0.6.0
1111
github.com./maxbrunsfeld/counterfeiter/v6 v6.7.0
12+
github.com./nginxinc/nginx-plus-go-client v0.10.0
1213
github.com./nginxinc/nginx-prometheus-exporter v0.11.0
1314
github.com./onsi/ginkgo/v2 v2.13.2
1415
github.com./onsi/gomega v1.30.0
@@ -61,7 +62,6 @@ require (
6162
github.com./modern-go/reflect2 v1.0.2 // indirect
6263
github.com./munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
6364
github.com./mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect
64-
github.com./nginxinc/nginx-plus-go-client v0.10.0 // indirect
6565
github.com./pkg/errors v0.9.1 // indirect
6666
github.com./pmezard/go-difflib v1.0.0 // indirect
6767
github.com./prometheus/client_model v0.5.0 // indirect

internal/mode/static/config/config.go

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ type Config struct {
2626
LeaderElection LeaderElection
2727
// UpdateGatewayClassStatus enables updating the status of the GatewayClass resource.
2828
UpdateGatewayClassStatus bool
29+
// Plus indicates whether NGINX Plus is being used.
30+
Plus bool
2931
// MetricsConfig specifies the metrics config.
3032
MetricsConfig MetricsConfig
3133
// HealthConfig specifies the health probe config.

internal/mode/static/manager.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ func init() {
6363
utilruntime.Must(apiext.AddToScheme(scheme))
6464
}
6565

66+
// nolint:gocyclo
6667
func StartManager(cfg config.Config) error {
6768
options := manager.Options{
6869
Scheme: scheme,
@@ -147,7 +148,12 @@ func StartManager(cfg config.Config) error {
147148

148149
if cfg.MetricsConfig.Enabled {
149150
constLabels := map[string]string{"class": cfg.GatewayClassName}
150-
ngxCollector, err := collectors.NewNginxMetricsCollector(constLabels)
151+
var ngxCollector prometheus.Collector
152+
if cfg.Plus {
153+
ngxCollector, err = collectors.NewNginxPlusMetricsCollector(constLabels)
154+
} else {
155+
ngxCollector, err = collectors.NewNginxMetricsCollector(constLabels)
156+
}
151157
if err != nil {
152158
return fmt.Errorf("cannot create nginx metrics collector: %w", err)
153159
}

0 commit comments

Comments
 (0)