Skip to content

Commit

Permalink
Merge pull request #5522 from twmb/config_set_slice_improvements
Browse files Browse the repository at this point in the history
rpk redpanda config set: improve setting arrays
  • Loading branch information
twmb committed Jul 20, 2022
2 parents 5743404 + 0604ee2 commit b08ac47
Show file tree
Hide file tree
Showing 4 changed files with 267 additions and 70 deletions.
30 changes: 24 additions & 6 deletions src/go/rpk/pkg/cli/cmd/redpanda/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,36 @@ func set(fs afero.Fs) *cobra.Command {
)
c := &cobra.Command{
Use: "set <key> <value>",
Short: "Set configuration values, such as the node IDs or the list of seed servers",
Long: `Set configuration values, such as the node IDs or the list of seed servers
Short: "Set configuration values, such as the redpanda node ID or the list of seed servers",
Long: `Set configuration values, such as the redpanda node ID or the list of seed servers
You can pass a key that represents the configuration property name, if it's a
nested property you should pass the group/category where it belongs, e.g:
This command modifies the redpanda.yaml you have locally on disk. The first
argument is the key within the yaml representing a property / field that you
would like to set. Nested fields can be accessed through a dot:
rpk redpanda config set redpanda.developer_mode true
if --format is not used, rpk will use yaml as default, you can also pass
partial json/yaml config objects:
The default format is to parse the value as yaml. Individual specific fields
can be set, or full structs:
rpk redpanda config set rpk.tune_disk_irq true
rpk redpanda config set redpanda.rpc_server '{address: 3.250.158.1, port: 9092}'
You can set an entire array by wrapping all items with braces, or by using one
struct:
rpk redpanda config set redpanda.advertised_kafka_api '[{address: 0.0.0.0, port: 9092}]'
rpk redpanda config set redpanda.advertised_kafka_api '{address: 0.0.0.0, port: 9092}' # same
Indexing can be used to set specific items in an array. You can index one past
the end of an array to extend it:
rpk redpanda config set redpanda.advertised_kafka_api[1] '{address: 0.0.0.0, port: 9092}'
The json format can be used to set values as json:
rpk redpanda config set redpanda.rpc_server '{"address":"0.0.0.0","port":33145}' --format json
`,
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
Expand Down
76 changes: 76 additions & 0 deletions src/go/rpk/pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ tune_cpu: true`,
require.Exactly(st, expected, c.Redpanda.AdvertisedKafkaAPI)
},
},

{
name: "set value of a slice",
key: "redpanda.admin.port",
Expand All @@ -265,6 +266,24 @@ tune_cpu: true`,
require.Exactly(st, 9641, c.Redpanda.AdminAPI[0].Port)
},
},

{
name: "set value of a slice with an index",
key: "redpanda.admin[0].port",
value: "9641",
check: func(st *testing.T, c *Config) {
require.Exactly(st, 9641, c.Redpanda.AdminAPI[0].Port)
},
},
{
name: "set value of a slice with an index at end extends slice",
key: "redpanda.admin[1].port",
value: "9648",
check: func(st *testing.T, c *Config) {
require.Exactly(st, 9648, c.Redpanda.AdminAPI[1].Port)
},
},

{
name: "set slice single values",
key: "redpanda.seed_servers.host.address",
Expand All @@ -274,6 +293,7 @@ tune_cpu: true`,
require.Exactly(st, "foo", c.Redpanda.SeedServers[0].Host.Address)
},
},

{
name: "set slice object",
key: "redpanda.seed_servers.host",
Expand All @@ -284,6 +304,37 @@ tune_cpu: true`,
require.Exactly(st, 80, c.Redpanda.SeedServers[0].Host.Port)
},
},

{
name: "set slice with object defaults to index 0",
key: "redpanda.advertised_kafka_api",
value: `{address: 3.250.158.1, port: 9092}`,
format: "yaml",
check: func(st *testing.T, c *Config) {
require.Exactly(st, "3.250.158.1", c.Redpanda.AdvertisedKafkaAPI[0].Address)
require.Exactly(st, 9092, c.Redpanda.AdvertisedKafkaAPI[0].Port)
},
},

{
name: "slices with one element works",
key: "rpk.kafka_api.brokers",
value: `127.0.0.0:9092`,
format: "yaml",
check: func(st *testing.T, c *Config) {
require.Exactly(st, "127.0.0.0:9092", c.Rpk.KafkaAPI.Brokers[0])
},
},
{
name: "slices with one element works with indexing",
key: "rpk.kafka_api.brokers[0]",
value: `127.0.0.0:9092`,
format: "yaml",
check: func(st *testing.T, c *Config) {
require.Exactly(st, "127.0.0.0:9092", c.Rpk.KafkaAPI.Brokers[0])
},
},

{
name: "fail if the value isn't well formatted (json)",
key: "redpanda",
Expand Down Expand Up @@ -318,6 +369,31 @@ tune_cpu: true`,
value: "foo",
expectErr: true,
},

{
name: "invalid negative index",
key: "redpanda.admin[-1].port",
value: "9641",
expectErr: true,
},
{
name: "invalid large index",
key: "redpanda.admin[12310293812093823094801].port",
value: "9641",
expectErr: true,
},
{
name: "invalid out of bounds index",
key: "redpanda.admin[2].port", // 0 is default, 1 is valid (extends by one), 2 is invalid
value: "9641",
expectErr: true,
},
{
name: "index into other (unknown) field",
key: "redpanda.fiz[0]",
value: "9641",
expectErr: true,
},
}

for _, tt := range tests {
Expand Down
Loading

0 comments on commit b08ac47

Please sign in to comment.