Skip to content

Commit 7b1ac07

Browse files
committed
deploy template via a config map
1 parent 4fa0f31 commit 7b1ac07

File tree

5 files changed

+148
-140
lines changed

5 files changed

+148
-140
lines changed
+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
user nginx;
2+
worker_processes auto;
3+
4+
error_log stderr debug;
5+
pid /etc/nginx/nginx.pid;
6+
7+
load_module /usr/lib/nginx/modules/ngx_http_js_module.so;
8+
9+
events {
10+
worker_connections 1024;
11+
}
12+
13+
14+
http {
15+
# include /etc/nginx/mime.types;
16+
default_type application/octet-stream;
17+
18+
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
19+
'$status $body_bytes_sent "$http_referer" '
20+
'"$http_user_agent" "$http_x_forwarded_for"';
21+
22+
access_log stdout main;
23+
24+
js_import /usr/lib/nginx/modules/njs/httpmatches.js;
25+
26+
server_names_hash_bucket_size 256;
27+
server_names_hash_max_size 1024;
28+
29+
sendfile on;
30+
#tcp_nopush on;
31+
32+
keepalive_timeout 65;
33+
34+
#gzip on;
35+
36+
#############
37+
# upstreams #
38+
#############
39+
40+
{{ range $u := .Upstreams }}
41+
upstream {{ $u.Name }} {
42+
random two least_conn;
43+
zone {{ $u.Name }} 512k;
44+
{{ range $server := $u.Servers }}
45+
server {{ $server.Address }};
46+
{{- end }}
47+
}
48+
{{ end }}
49+
50+
#################
51+
# split clients #
52+
#################
53+
54+
{{ range $sc := .SplitClients }}
55+
split_clients $request_id ${{ $sc.VariableName }} {
56+
{{- range $d := $sc.Distributions }}
57+
{{- if eq $d.Percent "0.00" }}
58+
# {{ $d.Percent }}% {{ $d.Value }};
59+
{{- else }}
60+
{{ $d.Percent }}% {{ $d.Value }};
61+
{{- end }}
62+
{{- end }}
63+
}
64+
{{ end }}
65+
66+
###########
67+
# servers #
68+
###########
69+
70+
{{ range $s := .Servers -}}
71+
{{ if $s.IsDefaultSSL -}}
72+
server {
73+
listen 443 ssl default_server;
74+
75+
ssl_reject_handshake on;
76+
}
77+
{{- else if $s.IsDefaultHTTP }}
78+
server {
79+
listen 80 default_server;
80+
81+
default_type text/html;
82+
return 404;
83+
}
84+
{{- else }}
85+
server {
86+
{{- if $s.SSL }}
87+
listen 443 ssl;
88+
ssl_certificate {{ $s.SSL.Certificate }};
89+
ssl_certificate_key {{ $s.SSL.CertificateKey }};
90+
91+
if ($ssl_server_name != $host) {
92+
return 421;
93+
}
94+
{{- end }}
95+
96+
server_name {{ $s.ServerName }};
97+
98+
{{ range $l := $s.Locations }}
99+
location {{ if $l.Exact }}= {{ end }}{{ $l.Path }} {
100+
{{ if $l.Internal -}}
101+
internal;
102+
{{ end }}
103+
104+
{{- if $l.Return -}}
105+
return {{ $l.Return.Code }} "{{ $l.Return.Body }}";
106+
{{ end }}
107+
108+
{{- if $l.HTTPMatchVar -}}
109+
set $http_matches {{ $l.HTTPMatchVar | printf "%q" }};
110+
js_content httpmatches.redirect;
111+
{{ end }}
112+
113+
{{- if $l.ProxyPass -}}
114+
proxy_set_header Host $host;
115+
proxy_pass {{ $l.ProxyPass }}$request_uri;
116+
{{- end }}
117+
}
118+
{{ end }}
119+
}
120+
{{- end }}
121+
{{ end }}
122+
server {
123+
listen unix:/var/lib/nginx/nginx-502-server.sock;
124+
access_log off;
125+
126+
return 502;
127+
}
128+
129+
server {
130+
listen unix:/var/lib/nginx/nginx-500-server.sock;
131+
access_log off;
132+
133+
return 500;
134+
}
135+
}

deploy/manifests/nginx-gateway.yaml

+5
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ spec:
9494
- name: njs-modules
9595
configMap:
9696
name: njs-modules
97+
- name: nginx-config-template
98+
configMap:
99+
name: nginx-config-template
97100
initContainers:
98101
- image: busybox:1.34 # FIXME(pleshakov): use gateway container to init the Config with proper main config
99102
name: nginx-config-initializer
@@ -108,6 +111,8 @@ spec:
108111
volumeMounts:
109112
- name: nginx-config
110113
mountPath: /etc/nginx
114+
- name: nginx-config-template
115+
mountPath: /etc/nginx-config-template
111116
securityContext:
112117
runAsUser: 1001
113118
# FIXME(pleshakov) - figure out which capabilities are required

docs/installation.md

+6
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ This guide walks you through how to install NGINX Kubernetes Gateway on a generi
3434
```
3535
kubectl create configmap njs-modules --from-file=internal/nginx/modules/src/httpmatches.js -n nginx-gateway
3636
```
37+
38+
1. Create the NGINX config template ConfigMap:
39+
40+
```
41+
kubectl create configmap nginx-config-template --from-file=deploy/manifests/nginx-config-template.tmpl -n nginx-gateway
42+
```
3743
3844
1. Create the GatewayClass resource:
3945

internal/nginx/config/generator.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ type GeneratorImpl struct {
2323

2424
// NewGeneratorImpl creates a new GeneratorImpl.
2525
func NewGeneratorImpl() GeneratorImpl {
26-
t, err := template.New("nginx").Parse(mainTemplate)
26+
// t, err := template.New("nginx").Parse(mainTemplate)
27+
t, err := template.ParseFiles("/etc/nginx-config-template/nginx-config-template.tmpl")
2728
if err != nil {
2829
panic(err)
2930
}

internal/nginx/config/template.go

-139
This file was deleted.

0 commit comments

Comments
 (0)