8000 Merge branch 'main' into fix/server-snippets-newlines · nginx/kubernetes-ingress@32b3a52 · GitHub
[go: up one dir, main page]

Skip to content

Commit 32b3a52

Browse files
author
Jim Ryan
authored
Merge branch 'main' into fix/server-snippets-newlines
2 parents d677b38 + 95f8da6 commit 32b3a52

File tree

11 files changed

+266
-14
lines changed

11 files changed

+266
-14
lines changed

cmd/nginx-ingress/main.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func main() {
7979
appProtectVersion = getAppProtectVersionInfo()
8080
}
8181

82-
updateSelfWithVersionInfo(kubeClient, version, nginxVersion, appProtectVersion)
82+
updateSelfWithVersionInfo(kubeClient, version, nginxVersion.String(), appProtectVersion)
8383

8484
templateExecutor, templateExecutorV2 := createTemplateExecutors()
8585

@@ -118,6 +118,7 @@ func main() {
118118
EnableCertManager: *enableCertManager,
119119
DynamicSSLReload: *enableDynamicSSLReload,
120120
StaticSSLPath: nginxManager.GetSecretsDir(),
121+
NginxVersion: nginxVersion,
121122
}
122123

123124
processNginxConfig(staticCfgParams, cfgParams, templateExecutor, nginxManager)
@@ -146,6 +147,7 @@ func main() {
146147
IsPrometheusEnabled: *enablePrometheusMetrics,
147148
IsLatencyMetricsEnabled: *enableLatencyMetrics,
148149
IsDynamicSSLReloadEnabled: *enableDynamicSSLReload,
150+
NginxVersion: nginxVersion,
149151
})
150152

151153
controllerNamespace := os.Getenv("POD_NAMESPACE")
@@ -400,17 +402,16 @@ func createNginxManager(managerCollector collectors.ManagerCollector) (nginx.Man
400402
return nginxManager, useFakeNginxManager
401403
}
402404

403-
func getNginxVersionInfo(nginxManager nginx.Manager) string {
404-
nginxVersion := nginxManager.Version()
405-
isPlus := strings.Contains(nginxVersion, "plus")
406-
glog.Infof("Using %s", nginxVersion)
405+
func getNginxVersionInfo(nginxManager nginx.Manager) nginx.Version {
406+
nginxInfo := nginxManager.Version()
407+
glog.Infof("Using %s", nginxInfo.String())
407408

408-
if *nginxPlus && !isPlus {
409+
if *nginxPlus && !nginxInfo.IsPlus {
409410
glog.Fatal("NGINX Plus flag enabled (-nginx-plus) without NGINX Plus binary")
410-
} else if !*nginxPlus && isPlus {
411+
} else if !*nginxPlus && nginxInfo.IsPlus {
411412
glog.Fatal("NGINX Plus binary found without NGINX Plus flag (-nginx-plus)")
412413
}
413-
return nginxVersion
414+
return nginxInfo
414415
}
415416

416417
func getAppProtectVersionInfo() string {

internal/configs/config_params.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package configs
22

3-
import conf_v1 "github.com/nginxinc/kubernetes-ingress/pkg/apis/configuration/v1"
3+
import (
4+
"github.com/nginxinc/kubernetes-ingress/internal/nginx"
5+
conf_v1 "github.com/nginxinc/kubernetes-ingress/pkg/apis/configuration/v1"
6+
)
47

58
// ConfigParams holds NGINX configuration parameters that affect the main NGINX config
69
// as well as configs for Ingress resources.
@@ -136,6 +139,7 @@ type StaticConfigParams struct {
136139
EnableCertManager bool
137140
DynamicSSLReload bool
138141
StaticSSLPath string
142+
NginxVersion nginx.Version
139143
}
140144

141145
// GlobalConfigParams holds global configuration parameters. For now, it only holds listeners.

internal/configs/configmaps.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,7 @@ func GenerateNginxMainConfig(staticCfgParams *StaticConfigParams, config *Config
581581
OIDC: staticCfgParams.EnableOIDC,
582582
DynamicSSLReloadEnabled: staticCfgParams.DynamicSSLReload,
583583
StaticSSLPath: staticCfgParams.StaticSSLPath,
584+
NginxVersion: staticCfgParams.NginxVersion,
584585
}
585586
return nginxCfg
586587
}

internal/configs/configurator.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ type ConfiguratorParams struct {
145145
IsWildcardEnabled bool
146146
IsLatencyMetricsEnabled bool
147147
IsDynamicSSLReloadEnabled bool
148+
NginxVersion nginx.Version
148149
}
149150

150151
// NewConfigurator creates a new Configurator.

internal/configs/configurator_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ func createTestStaticConfigParams() *StaticConfigParams {
2525
NginxStatusAllowCIDRs: []string{"127.0.0.1"},
2626
NginxStatusPort: 8080,
2727
StubStatusOverUnixSocketForOSS: false,
28+
NginxVersion: nginx.NewVersion("nginx version: nginx/1.25.3 (nginx-plus-r31)"),
2829
}
2930
}
3031

@@ -53,6 +54,7 @@ func createTestConfigurator(t *testing.T) *Configurator {
5354
IsWildcardEnabled: false,
5455
IsPrometheusEnabled: false,
5556
IsLatencyMetricsEnabled: false,
57+
NginxVersion: nginx.NewVersion("nginx version: nginx/1.25.3 (nginx-plus-r31)"),
5658
})
5759
cnf.isReloadsEnabled = true
5860
return cnf

internal/configs/version1/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package version1
22

3+
import "github.com/nginxinc/kubernetes-ingress/internal/nginx"
4+
35
// UpstreamLabels describes the Prometheus labels for an NGINX upstream.
46
type UpstreamLabels struct {
57
Service string
@@ -234,6 +236,7 @@ type MainConfig struct {
234236
OIDC bool
235237
DynamicSSLReloadEnabled bool
236238
StaticSSLPath string
239+
NginxVersion nginx.Version
237240
}
238241

239242
// NewUpstreamWithDefaultServer creates an upstream with the default server.

internal/configs/version1/nginx-plus.tmpl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,3 +346,9 @@ stream {
346346

347347
include /etc/nginx/stream-conf.d/*.conf;
348348
}
349+
350+
{{- if (.NginxVersion.PlusGreaterThanOrEqualTo "nginx-plus-r31") }}
351+
mgmt {
352+
usage_report interval=0s;
353+
}
354+
{{- end}}

internal/configs/version1/template_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"strings"
66
"testing"
77
"text/template"
8+
9+
"github.com/nginxinc/kubernetes-ingress/internal/nginx"
810
)
911

1012
func TestExecuteMainTemplateForNGINXPlus(t *testing.T) {
@@ -20,6 +22,19 @@ func TestExecuteMainTemplateForNGINXPlus(t *testing.T) {
2022
t.Log(buf.String())
2123
}
2224

25+
func TestExecuteMainTemplateForNGINXPlusR31(t *testing.T) {
26+
t.Parallel()
27+
28+
tmpl := newNGINXPlusMainTmpl(t)
29+
buf := &bytes.Buffer{}
30+
31+
err := tmpl.Execute(buf, mainCfgR31)
32+
if err != nil {
33+
t.Error(err)
34+
}
35+
t.Log(buf.String())
36+
}
37+
2338
func TestExecuteMainTemplateForNGINX(t *testing.T) {
2439
t.Parallel()
2540

@@ -1291,6 +1306,33 @@ var (
12911306
KeepaliveRequests: 100,
12921307
VariablesHashBucketSize: 256,
12931308
VariablesHashMaxSize: 1024,
1309+
NginxVersion: nginx.NewVersion("nginx version: nginx/1.25.3 (nginx-plus-r31)"),
1310+
}
1311+
1312+
mainCfgR31 = MainConfig{
1313+
DefaultHTTPListenerPort: 80,
1314+
DefaultHTTPSListenerPort: 443,
1315+
ServerNamesHashMaxSize: "512",
1316+
ServerTokens: "off",
1317+
WorkerProcesses: "auto",
1318+
WorkerCPUAffinity: "auto",
1319+
WorkerShutdownTimeout: "1m",
1320+
WorkerConnections: "1024",
1321+
WorkerRlimitNofile: "65536",
1322+
LogFormat: []string{"$remote_addr", "$remote_user"},
1323+
LogFormatEscaping: "default",
1324+
StreamSnippets: []string{"# comment"},
1325+
StreamLogFormat: []string{"$remote_addr", "$remote_user"},
1326+
StreamLogFormatEscaping: "none",
1327+
ResolverAddresses: []string{"example.com", "127.0.0.1"},
1328+
ResolverIPV6: false,
1329+
ResolverValid: "10s",
1330+
ResolverTimeout: "15s",
1331+
KeepaliveTimeout: "65s",
1332+
KeepaliveRequests: 100,
1333+
VariablesHashBucketSize: 256,
1334+
VariablesHashMaxSize: 1024,
1335+
NginxVersion: nginx.NewVersion("nginx version: nginx/1.25.3 (nginx-plus-r31)"),
12941336
}
12951337

12961338
mainCfgHTTP2On = MainConfig{
@@ -1317,6 +1359,7 @@ var (
13171359
KeepaliveRequests: 100,
13181360
VariablesHashBucketSize: 256,
13191361
VariablesHashMaxSize: 1024,
1362+
NginxVersion: nginx.NewVersion("nginx version: nginx/1.25.3 (nginx-plus-r31)"),
13201363
}
13211364

13221365
mainCfgCustomTLSPassthroughPort = MainConfig{
@@ -1342,6 +1385,7 @@ var (
13421385
VariablesHashMaxSize: 1024,
13431386
TLSPassthrough: true,
13441387
TLSPassthroughPort: 8443,
1388+
NginxVersion: nginx.NewVersion("nginx version: nginx/1.25.3 (nginx-plus-r31)"),
13451389
}
13461390

13471391
mainCfgWithoutTLSPassthrough = MainConfig{
@@ -1367,6 +1411,7 @@ var (
13671411
VariablesHashMaxSize: 1024,
13681412
TLSPassthrough: false,
13691413
TLSPassthroughPort: 8443,
1414+
NginxVersion: nginx.NewVersion("nginx version: nginx/1.25.3 (nginx-plus-r31)"),
13701415
}
13711416

13721417
mainCfgDefaultTLSPassthroughPort = MainConfig{
@@ -1392,6 +1437,7 @@ var (
13921437
VariablesHashMaxSize: 1024,
13931438
TLSPassthrough: true,
13941439
TLSPassthroughPort: 443,
1440+
NginxVersion: nginx.NewVersion("nginx version: nginx/1.25.3 (nginx-plus-r31)"),
13951441
}
13961442

13971443
mainCfgCustomDefaultHTTPAndHTTPSListenerPorts = MainConfig{
@@ -1417,6 +1463,7 @@ var (
14171463
KeepaliveRequests: 100,
14181464
VariablesHashBucketSize: 256,
14191465
VariablesHashMaxSize: 1024,
1466+
NginxVersion: nginx.NewVersion("nginx version: nginx/1.25.3 (nginx-plus-r31)"),
14201467
}
14211468

14221469
mainCfgCustomDefaultHTTPListenerPort = MainConfig{
@@ -1442,6 +1489,7 @@ var (
14421489
KeepaliveRequests: 100,
14431490
VariablesHashBucketSize: 256,
14441491
VariablesHashMaxSize: 1024,
1492+
NginxVersion: nginx.NewVersion("nginx version: nginx/1.25.3 (nginx-plus-r31)"),
14451493
}
14461494

14471495
mainCfgCustomDefaultHTTPSListenerPort = MainConfig{
@@ -1467,6 +1515,7 @@ var (
14671515
KeepaliveRequests: 100,
14681516
VariablesHashBucketSize: 256,
14691517
VariablesHashMaxSize: 1024,
1518+
NginxVersion: nginx.NewVersion("nginx version: nginx/1.25.3 (nginx-plus-r31)"),
14701519
}
14711520

14721521
// Vars for Mergable Ingress Master - Minion tests

internal/nginx/fake_manager.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ func (fm *FakeManager) CreateDHParam(_ string) (string, error) {
101101
}
102102

103103
// Version provides a fake implementation of Version.
104-
func (*FakeManager) Version() string {
104+
func (*FakeManager) Version() Version {
105105
glog.V(3).Info("Printing nginx version")
106-
return "fake version plus"
106+
return Version{}
107107
}
108108

109109
// Start provides a fake implementation of Start.

internal/nginx/manager.go

Lines changed: 94 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"os/exec"
99
"path"
1010
"path/filepath"
11+
"regexp"
1112
"strconv"
1213
"strings"
1314
"time"
@@ -46,6 +47,19 @@ const (
4647
appProtectDosAgentStartDebugCmd = "/usr/bin/admd -d --standalone --log debug"
4748
)
4849

50+
var (
51+
re = regexp.MustCompile(`(?P<name>\S+)/(?P<version>\S+)`)
52+
plusre = regexp.MustCompile(`(?P<name>\S+)/(?P<version>\S+).\((?P<plus>\S+plus\S+)\)`)
53+
)
54+
55+
// Version holds the parsed output from `nginx -v`
56+
type Version struct {
57+
raw string
58+
OSS string
59+
IsPlus bool
60+
Plus string
61+
}
62+
4963
// ServerConfig holds the config data for an upstream server in NGINX Plus.
5064
type ServerConfig struct {
5165
MaxFails int
@@ -72,7 +86,7 @@ type Manager interface {
7286
CreateDHParam(content string) (string, error)
7387
CreateOpenTracingTracerConfig(content string) error
7488
Start(done chan error)
75-
Version() string
89+
Version() Version
7690
Reload(isEndpointsUpdate bool) error
7791
Quit()
7892
UpdateConfigVersionFile(openTracing bool)
@@ -334,13 +348,13 @@ func (lm *LocalManager) Quit() {
334348
}
335349

336350
// Version returns NGINX version
337-
func (lm *LocalManager) Version() string {
351+
func (lm *LocalManager) Version() Version {
338352
binaryFilename := getBinaryFileName(lm.debug)
339353
out, err := exec.Command(binaryFilename, "-v").CombinedOutput()
340354
if err != nil {
341355
glog.Fatalf("Failed to get nginx version: %v", err)
342356
}
343-
return string(out)
357+
return NewVersion(string(out))
344358
}
345359

346360
// UpdateConfigVersionFile writes the config version file.
@@ -432,6 +446,83 @@ func (lm *LocalManager) CreateOpenTracingTracerConfig(content string) error {
432446
return nil
433447
}
434448

449+
// Return the raw Nginx version string from `nginx -v`
450+
func (v *Version) String() string {
451+
return v.raw
452+
}
453+
454+
// PlusGreaterThanOrEqualTo compares the supplied nginx-plus version string with the Version{} struct
455+
func (v Version) PlusGreaterThanOrEqualTo(target string) (bool, error) {
456+
r, p, err := extractPlusVersionValues(v.String())
457+
if err != nil {
458+
return false, err
459+
}
460+
tr, tp, err := extractPlusVersionValues(target)
461+
if err != nil {
462+
return false, err
463+
}
464+
465+
return (r > tr || (r == tr && p >= tp)), nil
466+
}
467+
468+
// NewVersion will take the output from `nginx -v` and explodes it into the `nginx.Version` struct
469+
func NewVersion(line string) Version {
470+
matches := re.FindStringSubmatch(line)
471+
plusmatches := plusre.FindStringSubmatch(line)
472+
nv := Version{
473+
raw: line,
474+
}
475+
476+
if len(plusmatches) > 0 {
477+
subNames := plusre.SubexpNames()
478+
nv.IsPlus = true
479+
for i, v := range plusmatches {
480+
switch subNames[i] {
481+
case "plus":
482+
nv.Plus = v
483+
case "version":
484+
nv.OSS = v
485+
}
486+
}
487+
}
488+
489+
if len(matches) > 0 {
490+
for i, key := range re.SubexpNames() {
491+
val := matches[i]
492+
if key == "version" {
493+
nv.OSS = val
494+
}
495+
}
496+
}
497+
498+
return nv
499+
}
500+
501+
// extractPlusVersionValues
502+
func extractPlusVersionValues(input string) (int, int, error) {
503+
var rValue, pValue int
504+
re := regexp.MustCompile(`-r(\d+)(?:-p(\d+))?`)
505+
matches := re.FindStringSubmatch(input)
506+
507+
if len(matches) < 2 {
508+
return 0, 0, fmt.Errorf("no matches found in the input string")
509+
}
510+
511+
rValue, err := strconv.Atoi(matches[1])
512+
if err != nil {
513+
return 0, 0, fmt.Errorf("failed to convert rValue to integer: %w", err)
514+
}
515+
516+
if len(matches) > 2 && len(matches[2]) > 0 {
517+
pValue, err = strconv.Atoi(matches[2])
518+
if err != nil {
519+
return 0, 0, fmt.Errorf("failed to convert pValue to integer: %w", err)
520+
}
521+
}
522+
523+
return rValue, pValue, nil
524+
}
525+
435526
// verifyConfigVersion is used to check if the worker process that the API client is connected
436527
// to is using the latest version of nginx config. This way we avoid making changes on
437528
// a worker processes that is being shut down.

0 commit comments

Comments
 (0)
0