Skip to content

Commit 5c0a00e

Browse files
msfidelisdkostyrev
authored andcommitted
feature(geoip2_autoreload): Enable GeoIP2 auto_reload config (kubernetes#11079)
* feature(geoip2_autoreload): GeoIP Autoreload feature(geoip2_autoreload): fix lint feature(geoip2_autoreload): changing flag interval feature(geoip2_autoreload): tests - up and running feature(geoip2_autoreload): tests - up and running feature(geoip2): testing feature(geoip2): remove typo feature(geoip2_autoreload): fixing tests * feature(geoip2_autoreload): working * feature(geoip2_autoreload): including tests on geoip2 test file
1 parent 600d81a commit 5c0a00e

File tree

4 files changed

+84
-0
lines changed

4 files changed

+84
-0
lines changed

docs/user-guide/nginx-configuration/configmap.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ The following table shows a configuration option's name, type, and the default v
101101
|[use-gzip](#use-gzip)|bool|"false"||
102102
|[use-geoip](#use-geoip)|bool|"true"||
103103
|[use-geoip2](#use-geoip2)|bool|"false"||
104+
|[geoip2-autoreload-in-minutes](#geoip2-autoreload-in-minutes)|int|"0"||
104105
|[enable-brotli](#enable-brotli)|bool|"false"||
105106
|[brotli-level](#brotli-level)|int|4||
106107
|[brotli-min-length](#brotli-min-length)|int|20||
@@ -737,6 +738,12 @@ Alternatively, it is possible to use a volume to mount the files `/etc/nginx/geo
737738

738739
_**default:**_ false
739740

741+
## geoip2-autoreload-in-minutes
742+
743+
Enables the [geoip2 module](https://github.com/leev/ngx_http_geoip2_module) autoreload in MaxMind databases setting the interval in minutes.
744+
745+
_**default:**_ 0
746+
740747
## enable-brotli
741748

742749
Enables or disables compression of HTTP responses using the ["brotli" module](https://github.com/google/ngx_brotli).

internal/ingress/controller/config/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,10 @@ type Configuration struct {
436436
// By default this is disabled
437437
UseGeoIP2 bool `json:"use-geoip2,omitempty"`
438438

439+
// GeoIP2AutoReloadMinutes enables autoreload on geoip2 setting the interval in minutes
440+
// By default this is disabled using 0
441+
GeoIP2AutoReloadMinutes int `json:"geoip2-autoreload-in-minutes,omitempty"`
442+
439443
// Enables or disables the use of the NGINX Brotli Module for compression
440444
// https://github.com/google/ngx_brotli
441445
EnableBrotli bool `json:"enable-brotli,omitempty"`
@@ -841,6 +845,7 @@ func NewDefault() Configuration {
841845
EnableAioWrite: true,
842846
UseGzip: false,
843847
UseGeoIP2: false,
848+
GeoIP2AutoReloadMinutes: 0,
844849
WorkerProcesses: strconv.Itoa(runtime.NumCPU()),
845850
WorkerShutdownTimeout: "240s",
846851
VariablesHashBucketSize: 256,

rootfs/etc/nginx/template/nginx.tmpl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ http {
172172
{{ range $index, $file := $all.MaxmindEditionFiles }}
173173
{{ if eq $file "GeoLite2-Country.mmdb" }}
174174
geoip2 /etc/ingress-controller/geoip/GeoLite2-Country.mmdb {
175+
{{ if (gt $cfg.GeoIP2AutoReloadMinutes 0) }}
176+
auto_reload {{ $cfg.GeoIP2AutoReloadMinutes }}m;
177+
{{ end }}
175178
$geoip2_country_code source=$remote_addr country iso_code;
176179
$geoip2_country_name source=$remote_addr country names en;
177180
$geoip2_country_geoname_id source=$remote_addr country geoname_id;
@@ -183,6 +186,9 @@ http {
183186

184187
{{ if eq $file "GeoIP2-Country.mmdb" }}
185188
geoip2 /etc/ingress-controller/geoip/GeoIP2-Country.mmdb {
189+
{{ if (gt $cfg.GeoIP2AutoReloadMinutes 0) }}
190+
auto_reload {{ $cfg.GeoIP2AutoReloadMinutes }}m;
191+
{{ end }}
186192
$geoip2_country_code source=$remote_addr country iso_code;
187193
$geoip2_country_name source=$remote_addr country names en;
188194
$geoip2_country_geoname_id source=$remote_addr country geoname_id;
@@ -194,6 +200,9 @@ http {
194200

195201
{{ if eq $file "GeoLite2-City.mmdb" }}
196202
geoip2 /etc/ingress-controller/geoip/GeoLite2-City.mmdb {
203+
{{ if (gt $cfg.GeoIP2AutoReloadMinutes 0) }}
204+
auto_reload {{ $cfg.GeoIP2AutoReloadMinutes }}m;
205+
{{ end }}
197206
$geoip2_city_country_code source=$remote_addr country iso_code;
198207
$geoip2_city_country_name source=$remote_addr country names en;
199208
$geoip2_city_country_geoname_id source=$remote_addr country geoname_id;
@@ -217,6 +226,9 @@ http {
217226

218227
{{ if eq $file "GeoIP2-City.mmdb" }}
219228
geoip2 /etc/ingress-controller/geoip/GeoIP2-City.mmdb {
229+
{{ if (gt $cfg.GeoIP2AutoReloadMinutes 0) }}
230+
auto_reload {{ $cfg.GeoIP2AutoReloadMinutes }}m;
231+
{{ end }}
220232
$geoip2_city_country_code source=$remote_addr country iso_code;
221233
$geoip2_city_country_name source=$remote_addr country names en;
222234
$geoip2_city_country_geoname_id source=$remote_addr country geoname_id;
@@ -240,20 +252,29 @@ http {
240252

241253
{{ if eq $file "GeoLite2-ASN.mmdb" }}
242254
geoip2 /etc/ingress-controller/geoip/GeoLite2-ASN.mmdb {
255+
{{ if (gt $cfg.GeoIP2AutoReloadMinutes 0) }}
256+
auto_reload {{ $cfg.GeoIP2AutoReloadMinutes }}m;
257+
{{ end }}
243258
$geoip2_asn source=$remote_addr autonomous_system_number;
244259
$geoip2_org source=$remote_addr autonomous_system_organization;
245260
}
246261
{{ end }}
247262

248263
{{ if eq $file "GeoIP2-ASN.mmdb" }}
249264
geoip2 /etc/ingress-controller/geoip/GeoIP2-ASN.mmdb {
265+
{{ if (gt $cfg.GeoIP2AutoReloadMinutes 0) }}
266+
auto_reload {{ $cfg.GeoIP2AutoReloadMinutes }}m;
267+
{{ end }}
250268
$geoip2_asn source=$remote_addr autonomous_system_number;
251269
$geoip2_org source=$remote_addr autonomous_system_organization;
252270
}
253271
{{ end }}
254272

255273
{{ if eq $file "GeoIP2-ISP.mmdb" }}
256274
geoip2 /etc/ingress-controller/geoip/GeoIP2-ISP.mmdb {
275+
{{ if (gt $cfg.GeoIP2AutoReloadMinutes 0) }}
276+
auto_reload {{ $cfg.GeoIP2AutoReloadMinutes }}m;
277+
{{ end }}
257278
$geoip2_isp source=$remote_addr isp;
258279
$geoip2_isp_org source=$remote_addr organization;
259280
$geoip2_asn source=$remote_addr default=0 autonomous_system_number;
@@ -268,6 +289,9 @@ http {
268289

269290
{{ if eq $file "GeoIP2-Anonymous-IP.mmdb" }}
270291
geoip2 /etc/ingress-controller/geoip/GeoIP2-Anonymous-IP.mmdb {
292+
{{ if (gt $cfg.GeoIP2AutoReloadMinutes 0) }}
293+
auto_reload {{ $cfg.GeoIP2AutoReloadMinutes }}m;
294+
{{ end }}
271295
$geoip2_is_anon source=$remote_addr is_anonymous;
272296
$geoip2_is_anonymous source=$remote_addr default=0 is_anonymous;
273297
$geoip2_is_anonymous_vpn source=$remote_addr default=0 is_anonymous_vpn;

test/e2e/settings/geoip2.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,52 @@ var _ = framework.DescribeSetting("Geoip2", func() {
124124
Expect().
125125
Status(http.StatusOK)
126126
})
127+
128+
ginkgo.It("should up and running nginx controller using autoreload flag", func() {
129+
edition := "GeoLite2-Country"
130+
131+
err := f.UpdateIngressControllerDeployment(func(deployment *appsv1.Deployment) error {
132+
args := deployment.Spec.Template.Spec.Containers[0].Args
133+
args = append(args, "--maxmind-edition-ids="+edition)
134+
deployment.Spec.Template.Spec.Containers[0].Args = args
135+
_, err := f.KubeClientSet.AppsV1().Deployments(f.Namespace).Update(context.TODO(), deployment, metav1.UpdateOptions{})
136+
return err
137+
})
138+
assert.Nil(ginkgo.GinkgoT(), err, "updating ingress controller deployment flags")
139+
140+
filename := fmt.Sprintf("/etc/ingress-controller/geoip/%s.mmdb", edition)
141+
exec, err := f.ExecIngressPod(fmt.Sprintf(`sh -c "mkdir -p '%s' && wget -O '%s' '%s' 2>&1"`, filepath.Dir(filename), filename, testdataURL))
142+
framework.Logf(exec)
143+
assert.Nil(ginkgo.GinkgoT(), err, fmt.Sprintln("error downloading test geoip2 db", filename))
144+
145+
f.SetNginxConfigMapData(map[string]string{
146+
"use-geoip2": "true",
147+
"geoip2-autoreload-in-minutes": "5",
148+
})
149+
150+
// Check Configmap Autoreload Patterns
151+
f.WaitForNginxConfiguration(
152+
func(cfg string) bool {
153+
return strings.Contains(cfg, fmt.Sprintf("geoip2 %s", filename)) &&
154+
strings.Contains(cfg, "auto_reload 5m;")
155+
},
156+
)
157+
158+
// Check if Nginx could up, running and routing with auto_reload configs
159+
host := "ping.com"
160+
ing := framework.NewSingleIngress(host, "/", host, f.Namespace, framework.EchoService, 80, nil)
161+
f.EnsureIngress(ing)
162+
163+
f.WaitForNginxServer(host,
164+
func(server string) bool {
165+
return strings.Contains(server, host) &&
166+
strings.Contains(server, "location /")
167+
})
168+
169+
f.HTTPTestClient().
170+
GET("/").
171+
WithHeader("Host", host).
172+
Expect().
173+
Status(http.StatusOK)
174+
})
127175
})

0 commit comments

Comments
 (0)