Skip to content

Commit

Permalink
rpk: make set command to only write desired value
Browse files Browse the repository at this point in the history
there was a bug that made rpk redpanda config set
to write unset defaults to the config file even
if the user didn't request it
  • Loading branch information
r-vasquez committed Jul 12, 2022
1 parent b559bc5 commit 3201e51
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/go/rpk/pkg/cli/cmd/redpanda/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ partial json/yaml config objects:
p := config.ParamsFromCommand(cmd)
cfg, err := p.Load(fs)
out.MaybeDie(err, "unable to load config: %v", err)
cfg = cfg.FileOrDefaults()

if format == "single" {
fmt.Println("'--format single' is deprecated, either remove it or use yaml/json")
Expand Down
148 changes: 148 additions & 0 deletions src/go/rpk/pkg/cli/cmd/redpanda/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,151 @@ func TestInitNode(t *testing.T) {
})
}
}

// This is a top level command test, individual cases for set are
// tested in 'rpk/pkg/config/config_test.go'.
func TestSetCommand(t *testing.T) {
for _, test := range []struct {
name string
cfgFile string
exp string
args []string
}{
{
name: "set without config file on disk",
exp: `config_file: /etc/redpanda/redpanda.yaml
redpanda:
data_directory: /var/lib/redpanda/data
node_id: 0
rack: redpanda-rack
seed_servers: []
rpc_server:
address: 0.0.0.0
port: 33145
kafka_api:
- address: 0.0.0.0
port: 9092
admin:
- address: 0.0.0.0
port: 9644
developer_mode: true
rpk:
enable_usage_stats: false
tune_network: false
tune_disk_scheduler: false
tune_disk_nomerges: false
tune_disk_write_cache: false
tune_disk_irq: false
tune_fstrim: false
tune_cpu: false
tune_aio_events: false
tune_clocksource: false
tune_swappiness: false
tune_transparent_hugepages: false
enable_memory_locking: false
tune_coredump: false
coredump_dir: /var/lib/redpanda/coredump
tune_ballast_file: false
overprovisioned: false
pandaproxy: {}
schema_registry: {}
`,
args: []string{"redpanda.rack", "redpanda-rack"},
},
{
name: "set with loaded config",
cfgFile: `config_file: /etc/redpanda/redpanda.yaml
redpanda:
data_directory: ""
node_id: 0
rack: redpanda-rack
seed_servers: []
rpc_server:
address: 0.0.0.0
port: 33145
kafka_api:
- address: 0.0.0.0
port: 9092
admin:
- address: 0.0.0.0
port: 9644
developer_mode: true
rpk:
enable_usage_stats: false
tune_network: false
tune_disk_scheduler: false
tune_disk_nomerges: false
tune_disk_write_cache: false
tune_disk_irq: false
tune_fstrim: false
tune_cpu: false
tune_aio_events: false
tune_clocksource: false
tune_swappiness: false
tune_transparent_hugepages: false
enable_memory_locking: false
tune_coredump: false
tune_ballast_file: false
overprovisioned: false
`,
exp: `config_file: /etc/redpanda/redpanda.yaml
redpanda:
node_id: 0
rack: redpanda-rack
seed_servers: []
rpc_server:
address: 0.0.0.0
port: 33145
kafka_api:
- address: 0.0.0.0
port: 9092
admin:
- address: 0.0.0.0
port: 9644
developer_mode: true
rpk:
enable_usage_stats: true
tune_network: false
tune_disk_scheduler: false
tune_disk_nomerges: false
tune_disk_write_cache: false
tune_disk_irq: false
tune_fstrim: false
tune_cpu: false
tune_aio_events: false
tune_clocksource: false
tune_swappiness: false
tune_transparent_hugepages: false
enable_memory_locking: false
tune_coredump: false
tune_ballast_file: false
overprovisioned: false
`,
args: []string{"rpk.enable_usage_stats", "true"},
},
} {
fs := afero.NewMemMapFs()

// We create a config file in default redpanda location
if test.cfgFile != "" {
err := afero.WriteFile(fs, "/etc/redpanda/redpanda.yaml", []byte(test.cfgFile), 0o644)
if err != nil {
t.Errorf("unexpected failure writing passed config file: %v", err)
}
}

c := set(fs)
c.SetArgs(test.args)
err := c.Execute()
if err != nil {
t.Errorf("error during command execution: %v", err)
}

// Read back from that default location and compare.
file, err := afero.ReadFile(fs, "/etc/redpanda/redpanda.yaml")
if err != nil {
t.Errorf("unexpected failure reading config file: %v", err)
}
require.Equal(t, test.exp, string(file))
}
}
15 changes: 15 additions & 0 deletions src/go/rpk/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,21 @@ func AvailableModes() []string {
}
}

// FileOrDefaults return the configuration as read from the file or
// the default configuration if there is no file loaded.
func (c *Config) FileOrDefaults() *Config {
if c.File() != nil {
cfg := c.File()
cfg.loadedPath = c.loadedPath
cfg.ConfigFile = c.ConfigFile
return cfg
} else {
cfg := Default()
cfg.ConfigFile = c.ConfigFile // preserve ConfigFile property.
return cfg // no file, write the defaults
}
}

// Check checks if the redpanda and rpk configuration is valid before running
// the tuners. See: redpanda_checkers.
func (c *Config) Check() (bool, []error) {
Expand Down

0 comments on commit 3201e51

Please sign in to comment.