Skip to content

Commit 789ace3

Browse files
authored
Merge pull request #243 from 0x5d/rpk-fix-config-overwrite
rpk/config: Merge the new config onto the current one before write
2 parents efbdbb3 + b34e0c1 commit 789ace3

File tree

3 files changed

+159
-81
lines changed

3 files changed

+159
-81
lines changed

src/go/rpk/pkg/config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func defaultMap() map[string]interface{} {
9595
"port": 9644,
9696
},
9797
"node_id": 0,
98-
"seed_servers": []map[string]interface{}{},
98+
"seed_servers": []interface{}{},
9999
"developer_mode": true,
100100
},
101101
"rpk": map[string]interface{}{

src/go/rpk/pkg/config/config_test.go

Lines changed: 157 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -283,20 +283,21 @@ func TestWrite(t *testing.T) {
283283
wantErr: false,
284284
expected: `config_file: /etc/redpanda/redpanda.yaml
285285
redpanda:
286-
data_directory: /var/lib/redpanda/data
287-
rpc_server:
286+
admin:
288287
address: 0.0.0.0
289-
port: 33145
288+
port: 9644
290289
advertised_rpc_api:
291290
address: 174.32.64.2
292291
port: 33145
292+
data_directory: /var/lib/redpanda/data
293+
developer_mode: false
293294
kafka_api:
294295
address: 0.0.0.0
295296
port: 9092
296-
admin:
297-
address: 0.0.0.0
298-
port: 9644
299297
node_id: 0
298+
rpc_server:
299+
address: 0.0.0.0
300+
port: 33145
300301
seed_servers:
301302
- host:
302303
address: 127.0.0.1
@@ -306,24 +307,23 @@ redpanda:
306307
address: 127.0.0.1
307308
port: 33146
308309
node_id: 2
309-
developer_mode: false
310310
rpk:
311+
coredump_dir: /var/lib/redpanda/coredumps
312+
enable_memory_locking: true
311313
enable_usage_stats: true
312-
tune_network: true
313-
tune_disk_scheduler: true
314-
tune_disk_nomerges: true
315-
tune_disk_irq: true
316-
tune_fstrim: true
317-
tune_cpu: true
314+
overprovisioned: false
318315
tune_aio_events: true
319316
tune_clocksource: true
317+
tune_coredump: true
318+
tune_cpu: true
319+
tune_disk_irq: true
320+
tune_disk_nomerges: true
321+
tune_disk_scheduler: true
322+
tune_fstrim: true
323+
tune_network: true
320324
tune_swappiness: true
321325
tune_transparent_hugepages: true
322-
enable_memory_locking: true
323-
tune_coredump: true
324-
coredump_dir: /var/lib/redpanda/coredumps
325326
well_known_io: vendor:vm:storage
326-
overprovisioned: false
327327
`,
328328
},
329329
{
@@ -339,20 +339,21 @@ rpk:
339339
wantErr: false,
340340
expected: `config_file: /etc/redpanda/redpanda.yaml
341341
redpanda:
342-
data_directory: /var/lib/redpanda/data
343-
rpc_server:
344-
address: 0.0.0.0
345-
port: 33145
346-
kafka_api:
342+
admin:
347343
address: 0.0.0.0
348-
port: 9092
344+
port: 9644
349345
advertised_kafka_api:
350346
address: 174.32.64.2
351347
port: 9092
352-
admin:
348+
data_directory: /var/lib/redpanda/data
349+
developer_mode: false
350+
kafka_api:
353351
address: 0.0.0.0
354-
port: 9644
352+
port: 9092
355353
node_id: 0
354+
rpc_server:
355+
address: 0.0.0.0
356+
port: 33145
356357
seed_servers:
357358
- host:
358359
address: 127.0.0.1
@@ -362,24 +363,23 @@ redpanda:
362363
address: 127.0.0.1
363364
port: 33146
364365
node_id: 2
365-
developer_mode: false
366366
rpk:
367+
coredump_dir: /var/lib/redpanda/coredumps
368+
enable_memory_locking: true
367369
enable_usage_stats: true
368-
tune_network: true
369-
tune_disk_scheduler: true
370-
tune_disk_nomerges: true
371-
tune_disk_irq: true
372-
tune_fstrim: true
373-
tune_cpu: true
370+
overprovisioned: false
374371
tune_aio_events: true
375372
tune_clocksource: true
373+
tune_coredump: true
374+
tune_cpu: true
375+
tune_disk_irq: true
376+
tune_disk_nomerges: true
377+
tune_disk_scheduler: true
378+
tune_fstrim: true
379+
tune_network: true
376380
tune_swappiness: true
377381
tune_transparent_hugepages: true
378-
enable_memory_locking: true
379-
tune_coredump: true
380-
coredump_dir: /var/lib/redpanda/coredumps
381382
well_known_io: vendor:vm:storage
382-
overprovisioned: false
383383
`,
384384
},
385385
{
@@ -392,17 +392,18 @@ rpk:
392392
wantErr: false,
393393
expected: `config_file: /etc/redpanda/redpanda.yaml
394394
redpanda:
395-
data_directory: /var/lib/redpanda/data
396-
rpc_server:
395+
admin:
397396
address: 0.0.0.0
398-
port: 33145
397+
port: 9644
398+
data_directory: /var/lib/redpanda/data
399+
developer_mode: false
399400
kafka_api:
400401
address: 0.0.0.0
401402
port: 9092
402-
admin:
403-
address: 0.0.0.0
404-
port: 9644
405403
node_id: 0
404+
rpc_server:
405+
address: 0.0.0.0
406+
port: 33145
406407
seed_servers:
407408
- host:
408409
address: 127.0.0.1
@@ -412,22 +413,22 @@ redpanda:
412413
address: 127.0.0.1
413414
port: 33146
414415
node_id: 2
415-
developer_mode: false
416416
rpk:
417+
coredump_dir: /var/lib/redpanda/coredump
418+
enable_memory_locking: false
417419
enable_usage_stats: false
418-
tune_network: false
419-
tune_disk_scheduler: false
420-
tune_disk_nomerges: false
421-
tune_disk_irq: false
422-
tune_fstrim: false
423-
tune_cpu: false
420+
overprovisioned: false
424421
tune_aio_events: false
425422
tune_clocksource: false
423+
tune_coredump: false
424+
tune_cpu: false
425+
tune_disk_irq: false
426+
tune_disk_nomerges: false
427+
tune_disk_scheduler: false
428+
tune_fstrim: false
429+
tune_network: false
426430
tune_swappiness: false
427431
tune_transparent_hugepages: false
428-
enable_memory_locking: false
429-
tune_coredump: false
430-
overprovisioned: false
431432
`,
432433
},
433434
{
@@ -439,30 +440,24 @@ rpk:
439440
},
440441
wantErr: true,
441442
},
442-
{
443-
name: "shall fail if there's no redpanda config",
444-
conf: func() *Config {
445-
return &Config{}
446-
},
447-
wantErr: true,
448-
},
449443
{
450444
name: "shall write a valid config file with an rpk config object",
451445
conf: getValidConfig,
452446
wantErr: false,
453447
expected: `config_file: /etc/redpanda/redpanda.yaml
454448
redpanda:
455-
data_directory: /var/lib/redpanda/data
456-
rpc_server:
449+
admin:
457450
address: 0.0.0.0
458-
port: 33145
451+
port: 9644
452+
data_directory: /var/lib/redpanda/data
453+
developer_mode: false
459454
kafka_api:
460455
address: 0.0.0.0
461456
port: 9092
462-
admin:
463-
address: 0.0.0.0
464-
port: 9644
465457
node_id: 0
458+
rpc_server:
459+
address: 0.0.0.0
460+
port: 33145
466461
seed_servers:
467462
- host:
468463
address: 127.0.0.1
@@ -472,24 +467,23 @@ redpanda:
472467
address: 127.0.0.1
473468
port: 33146
474469
node_id: 2
475-
developer_mode: false
476470
rpk:
471+
coredump_dir: /var/lib/redpanda/coredumps
472+
enable_memory_locking: true
477473
enable_usage_stats: true
478-
tune_network: true
479-
tune_disk_scheduler: true
480-
tune_disk_nomerges: true
481-
tune_disk_irq: true
482-
tune_fstrim: true
483-
tune_cpu: true
474+
overprovisioned: false
484475
tune_aio_events: true
485476
tune_clocksource: true
477+
tune_coredump: true
478+
tune_cpu: true
479+
tune_disk_irq: true
480+
tune_disk_nomerges: true
481+
tune_disk_scheduler: true
482+
tune_fstrim: true
483+
tune_network: true
486484
tune_swappiness: true
487485
tune_transparent_hugepages: true
488-
enable_memory_locking: true
489-
tune_coredump: true
490-
coredump_dir: /var/lib/redpanda/coredumps
491486
well_known_io: vendor:vm:storage
492-
overprovisioned: false
493487
`,
494488
},
495489
{
@@ -532,6 +526,7 @@ redpanda:
532526
admin_api_doc_dir: /etc/redpanda/doc
533527
data_directory: /var/lib/redpanda/data
534528
default_window_sec: 100
529+
developer_mode: false
535530
kafka_api:
536531
address: 0.0.0.0
537532
port: 9092
@@ -549,13 +544,96 @@ redpanda:
549544
port: 33146
550545
node_id: 2
551546
target_quota_byte_rate: 1000000
547+
rpk:
548+
coredump_dir: /var/lib/redpanda/coredumps
549+
enable_memory_locking: true
550+
enable_usage_stats: true
551+
overprovisioned: false
552+
tune_aio_events: true
553+
tune_clocksource: true
554+
tune_coredump: true
555+
tune_cpu: true
556+
tune_disk_irq: true
557+
tune_disk_nomerges: true
558+
tune_disk_scheduler: true
559+
tune_fstrim: true
560+
tune_network: true
561+
tune_swappiness: true
562+
tune_transparent_hugepages: true
563+
well_known_io: vendor:vm:storage
552564
unrecognized_top_field:
553565
child: true
566+
`,
567+
},
568+
{
569+
name: "should merge the new config onto the current one, not just overwrite it",
570+
existingConf: `config_file: /etc/redpanda/redpanda.yaml
571+
redpanda:
572+
admin:
573+
address: 0.0.0.0
574+
port: 9644
575+
admin_api_doc_dir: /etc/redpanda/doc
576+
auto_create_topics_enabled: true
577+
data_directory: /var/lib/redpanda/data
578+
default_window_sec: 100
579+
kafka_api:
580+
address: 0.0.0.0
581+
port: 9092
582+
node_id: 0
583+
rpc_server:
584+
address: 0.0.0.0
585+
port: 33145
586+
target_quota_byte_rate: 1000000
587+
`,
588+
conf: func() *Config {
589+
conf := getValidConfig()
590+
conf.Redpanda.SeedServers = []SeedServer{}
591+
conf.Redpanda.Directory = "/different/path"
592+
return conf
593+
},
594+
wantErr: false,
595+
expected: `config_file: /etc/redpanda/redpanda.yaml
596+
redpanda:
597+
admin:
598+
address: 0.0.0.0
599+
port: 9644
600+
admin_api_doc_dir: /etc/redpanda/doc
601+
auto_create_topics_enabled: true
602+
data_directory: /different/path
603+
default_window_sec: 100
604+
developer_mode: false
605+
kafka_api:
606+
address: 0.0.0.0
607+
port: 9092
608+
node_id: 0
609+
rpc_server:
610+
address: 0.0.0.0
611+
port: 33145
612+
seed_servers: []
613+
target_quota_byte_rate: 1000000
614+
rpk:
615+
coredump_dir: /var/lib/redpanda/coredumps
616+
enable_memory_locking: true
617+
enable_usage_stats: true
618+
overprovisioned: false
619+
tune_aio_events: true
620+
tune_clocksource: true
621+
tune_coredump: true
622+
tune_cpu: true
623+
tune_disk_irq: true
624+
tune_disk_nomerges: true
625+
tune_disk_scheduler: true
626+
tune_fstrim: true
627+
tune_network: true
628+
tune_swappiness: true
629+
tune_transparent_hugepages: true
630+
well_known_io: vendor:vm:storage
554631
`,
555632
},
556633
}
557634
for _, tt := range tests {
558635
t.Run(tt.name, func(t *testing.T) {
636+
path := tt.conf().ConfigFile
559637
fs := afero.NewMemMapFs()
560638
mgr := NewManager(fs)
561639
if tt.existingConf != "" {
@@ -567,7 +645,9 @@ unrecognized_top_field:
567645
err := vyaml.Persist(fs, tt.conf(), path)
568646
require.NoError(t, err)
569647
}
570-
err := mgr.Write(tt.conf())
648+
_, err := mgr.Read(path)
649+
require.NoError(t, err)
650+
err = mgr.Write(tt.conf())
571651
if tt.wantErr {
572652
require.Error(t, err)
573653
return

0 commit comments

Comments
 (0)