Skip to content

Commit bbcf56f

Browse files
committed
More fixes; add some unit tests
1 parent da36f73 commit bbcf56f

File tree

38 files changed

+2137
-234
lines changed

38 files changed

+2137
-234
lines changed

charts/nginx-gateway-fabric/templates/tmp-nginx-agent-conf.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,21 @@ data:
2323
{{- end }}
2424
log:
2525
level: debug
26+
collector:
27+
receivers:
28+
host_metrics:
29+
collection_interval: 1m0s
30+
initial_delay: 1s
31+
scrapers:
32+
cpu: {}
33+
memory: {}
34+
disk: {}
35+
network: {}
36+
filesystem: {}
37+
processors:
38+
batch: {}
39+
exporters:
40+
prometheus_exporter:
41+
server:
42+
host: "0.0.0.0"
43+
port: 9113

charts/nginx-gateway-fabric/templates/tmp-nginx-deployment.yaml

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,37 @@ spec:
1313
labels:
1414
app.kubernetes.io/name: tmp-nginx-deployment
1515
app.kubernetes.io/instance: {{ .Release.Name }}
16+
annotations:
17+
{{- if .Values.metrics.enable }}
18+
prometheus.io/scrape: "true"
19+
prometheus.io/port: "{{ .Values.metrics.port }}"
20+
{{- if .Values.metrics.secure }}
21+
prometheus.io/scheme: "https"
22+
{{- end }}
23+
{{- end }}
1624
spec:
1725
initContainers:
18-
- name: sleep # wait for a bit for control plane to be ready
19-
image: {{ .Values.nginxGateway.image.repository }}:{{ default .Chart.AppVersion .Values.nginxGateway.image.tag }}
20-
imagePullPolicy: {{ .Values.nginxGateway.image.pullPolicy }}
21-
command:
22-
- /usr/bin/gateway
23-
- sleep
24-
- --duration=5s
2526
- name: init
2627
image: {{ .Values.nginxGateway.image.repository }}:{{ default .Chart.AppVersion .Values.nginxGateway.image.tag }}
2728
imagePullPolicy: {{ .Values.nginxGateway.image.pullPolicy }}
2829
command:
2930
- /usr/bin/gateway
3031
- initialize
3132
- --source
33+
- /agent/nginx-agent.conf
34+
- --destination
35+
- /etc/nginx-agent
36+
- --source
3237
- /includes/main.conf
38+
- --destination
39+
- /etc/nginx/main-includes
3340
{{- if .Values.nginx.plus }}
3441
- --source
3542
- /includes/mgmt.conf
3643
- --nginx-plus
37-
{{- end }}
3844
- --destination
3945
- /etc/nginx/main-includes
46+
{{- end }}
4047
env:
4148
- name: POD_UID
4249
valueFrom:
@@ -52,6 +59,10 @@ spec:
5259
runAsUser: 101
5360
runAsGroup: 1001
5461
volumeMounts:
62+
- name: nginx-agent-config
63+
mountPath: /agent
64+
- name: nginx-agent
65+
mountPath: /etc/nginx-agent
5566
- name: nginx-includes-bootstrap
5667
mountPath: /includes
5768
- name: nginx-main-includes
@@ -69,6 +80,8 @@ spec:
6980
name: http
7081
- containerPort: 443
7182
name: https
83+
- name: metrics
84+
containerPort: {{ .Values.metrics.port }}
7285
securityContext:
7386
seccompProfile:
7487
type: RuntimeDefault
@@ -83,6 +96,8 @@ spec:
8396
volumeMounts:
8497
- name: nginx-agent
8598
mountPath: /etc/nginx-agent
99+
- name: nginx-agent-log
100+
mountPath: /var/log/nginx-agent
86101
- name: nginx-conf
87102
mountPath: /etc/nginx/conf.d
88103
- name: nginx-stream-conf
@@ -139,8 +154,12 @@ spec:
139154
{{- end }}
140155
volumes:
141156
- name: nginx-agent
157+
emptyDir: {}
158+
- name: nginx-agent-config
142159
configMap:
143160
name: nginx-agent-config
161+
- name: nginx-agent-log
162+
emptyDir: {}
144163
- name: nginx-conf
145164
emptyDir: {}
146165
- name: nginx-stream-conf

cmd/gateway/commands.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -519,14 +519,14 @@ func createInitializeCommand() *cobra.Command {
519519

520520
// flag values
521521
var srcFiles []string
522-
var dest string
522+
var destDirs []string
523523
var plus bool
524524

525525
cmd := &cobra.Command{
526526
Use: "initialize",
527527
Short: "Write initial configuration files",
528528
RunE: func(_ *cobra.Command, _ []string) error {
529-
if err := validateCopyArgs(srcFiles, dest); err != nil {
529+
if err := validateCopyArgs(srcFiles, destDirs); err != nil {
530530
return err
531531
}
532532

@@ -546,7 +546,7 @@ func createInitializeCommand() *cobra.Command {
546546
logger.Info(
547547
"Starting init container",
548548
"source filenames to copy", srcFiles,
549-
"destination directory", dest,
549+
"destination directories", destDirs,
550550
"nginx-plus",
551551
plus,
552552
)
@@ -558,16 +558,21 @@ func createInitializeCommand() *cobra.Command {
558558
Logger: logger.WithName("deployCtxCollector"),
559559
})
560560

561+
files := make([]fileToCopy, 0, len(srcFiles))
562+
for i, src := range srcFiles {
563+
files = append(files, fileToCopy{
564+
destDirName: destDirs[i],
565+
srcFileName: src,
566+
})
567+
}
568+
561569
return initialize(initializeConfig{
562570
fileManager: file.NewStdLibOSFileManager(),
563571
fileGenerator: ngxConfig.NewGeneratorImpl(plus, nil, logger.WithName("generator")),
564572
logger: logger,
565573
plus: plus,
566574
collector: dcc,
567-
copy: copyFiles{
568-
srcFileNames: srcFiles,
569-
destDirName: dest,
570-
},
575+
copy: files,
571576
})
572577
},
573578
}
@@ -579,11 +584,11 @@ func createInitializeCommand() *cobra.Command {
579584
"The source files to be copied",
580585
)
581586

582-
cmd.Flags().StringVar(
583-
&dest,
587+
cmd.Flags().StringSliceVar(
588+
&destDirs,
584589
destFlag,
585-
"",
586-
"The destination directory for the source files to be copied to",
590+
[]string{},
591+
"The destination directories for the source files at the same array index to be copied to",
587592
)
588593

589594
cmd.Flags().BoolVar(

cmd/gateway/initialize.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,23 @@ const (
1818
collectDeployCtxTimeout = 10 * time.Second
1919
)
2020

21-
type copyFiles struct {
22-
destDirName string
23-
srcFileNames []string
21+
type fileToCopy struct {
22+
destDirName string
23+
srcFileName string
2424
}
2525

2626
type initializeConfig struct {
2727
collector licensing.Collector
2828
fileManager file.OSFileManager
2929
fileGenerator config.Generator
3030
logger logr.Logger
31-
copy copyFiles
31+
copy []fileToCopy
3232
plus bool
3333
}
3434

3535
func initialize(cfg initializeConfig) error {
36-
for _, src := range cfg.copy.srcFileNames {
37-
if err := copyFile(cfg.fileManager, src, cfg.copy.destDirName); err != nil {
36+
for _, f := range cfg.copy {
37+
if err := copyFile(cfg.fileManager, f.srcFileName, f.destDirName); err != nil {
3838
return err
3939
}
4040
}

cmd/gateway/initialize_test.go

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,15 @@ func TestInitialize_OSS(t *testing.T) {
2828
ic := initializeConfig{
2929
fileManager: fakeFileMgr,
3030
logger: zap.New(),
31-
copy: copyFiles{
32-
destDirName: "destDir",
33-
srcFileNames: []string{"src1", "src2"},
31+
copy: []fileToCopy{
32+
{
33+
destDirName: "destDir",
34+
srcFileName: "src1",
35+
},
36+
{
37+
destDirName: "destDir2",
38+
srcFileName: "src2",
39+
},
3440
},
3541
plus: false,
3642
}
@@ -56,9 +62,15 @@ func TestInitialize_OSS_Error(t *testing.T) {
5662
ic := initializeConfig{
5763
fileManager: fakeFileMgr,
5864
logger: zap.New(),
59-
copy: copyFiles{
60-
destDirName: "destDir",
61-
srcFileNames: []string{"src1", "src2"},
65+
copy: []fileToCopy{
66+
{
67+
destDirName: "destDir",
68+
srcFileName: "src1",
69+
},
70+
{
71+
destDirName: "destDir2",
72+
srcFileName: "src2",
73+
},
6274
},
6375
plus: false,
6476
}
@@ -114,9 +126,15 @@ func TestInitialize_Plus(t *testing.T) {
114126
logger: zap.New(),
115127
collector: fakeCollector,
116128
fileGenerator: fakeGenerator,
117-
copy: copyFiles{
118-
destDirName: "destDir",
119-
srcFileNames: []string{"src1", "src2"},
129+
copy: []fileToCopy{
130+
{
131+
destDirName: "destDir",
132+
srcFileName: "src1",
133+
},
134+
{
135+
destDirName: "destDir2",
136+
srcFileName: "src2",
137+
},
120138
},
121139
plus: true,
122140
}

cmd/gateway/validation.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,15 @@ func ensureNoPortCollisions(ports ...int) error {
206206
return nil
207207
}
208208

209-
// validateCopyArgs ensures that arguments to the sleep command are set.
210-
func validateCopyArgs(srcFiles []string, dest string) error {
209+
// validateCopyArgs ensures that arguments to the initialize command are set.
210+
func validateCopyArgs(srcFiles []string, destDirs []string) error {
211+
if len(srcFiles) != len(destDirs) {
212+
return errors.New("source and destination must have the same number of elements")
213+
}
211214
if len(srcFiles) == 0 {
212215
return errors.New("source must not be empty")
213216
}
214-
if len(dest) == 0 {
217+
if len(destDirs) == 0 {
215218
return errors.New("destination must not be empty")
216219
}
217220

cmd/gateway/validation_test.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -554,41 +554,47 @@ func TestEnsureNoPortCollisions(t *testing.T) {
554554
g.Expect(ensureNoPortCollisions(9113, 9113)).ToNot(Succeed())
555555
}
556556

557-
func TestValidateSleepArgs(t *testing.T) {
557+
func TestValidateInitializeArgs(t *testing.T) {
558558
t.Parallel()
559559

560560
tests := []struct {
561561
name string
562-
dest string
562+
destDirs []string
563563
srcFiles []string
564564
expErr bool
565565
}{
566566
{
567567
name: "valid values",
568-
dest: "/dest/file",
568+
destDirs: []string{"/dest/"},
569569
srcFiles: []string{"/src/file"},
570570
expErr: false,
571571
},
572572
{
573573
name: "invalid dest",
574-
dest: "",
574+
destDirs: []string{},
575575
srcFiles: []string{"/src/file"},
576576
expErr: true,
577577
},
578578
{
579579
name: "invalid src",
580-
dest: "/dest/file",
580+
destDirs: []string{"/dest/"},
581581
srcFiles: []string{},
582582
expErr: true,
583583
},
584+
{
585+
name: "different lengths",
586+
destDirs: []string{"/dest/"},
587+
srcFiles: []string{"src1", "src2"},
588+
expErr: true,
589+
},
584590
}
585591

586592
for _, tc := range tests {
587593
t.Run(tc.name, func(t *testing.T) {
588594
t.Parallel()
589595
g := NewWithT(t)
590596

591-
err := validateCopyArgs(tc.srcFiles, tc.dest)
597+
err := validateCopyArgs(tc.srcFiles, tc.destDirs)
592598
if !tc.expErr {
593599
g.Expect(err).ToNot(HaveOccurred())
594600
} else {

0 commit comments

Comments
 (0)