segmentio / topicctl

Tool for declarative management of Kafka topics
MIT License
580 stars 54 forks source link

fix issues from get subcmd refactor #151

Closed petedannemann closed 10 months ago

petedannemann commented 10 months ago

https://github.com/segmentio/topicctl/pull/148 introduced two new (known) bugs that are fixed in this PR:

  1. The debug command broke for the get subcommand. This is due to PersistentPreRun only traversing one level of subcommands. This is a known issue with cobra
  2. Zookeeper get commands broke. This commit closes the adminclient before the function returns with the defer command, which is not what we want to do. We must keep the adminclient open until the end of the command so this PR reverts that commit which added the buggy getCliRunnerAndCtx function

Verification:

  1. Debug flag fix

Before:

go run cmd/topicctl/main.go get topics --cluster-config examples/local-cluster/cluster.yaml --debug
[2023-09-18 08:55:33]  INFO Topics:
-------+------------+-------------+-----------+------------
  NAME | PARTITIONS | REPLICATION | RETENTION |   RACKS
       |            |             |   MINS    | (MIN,MAX)
-------+------------+-------------+-----------+------------
-------+------------+-------------+-----------+------------

After:

$ go run cmd/topicctl/main.go get topics --cluster-config examples/local-cluster/cluster.yaml --debug
[2023-09-18 08:55:29] DEBUG No ZK addresses provided, using broker admin client
[2023-09-18 08:55:29] DEBUG Connecting to cluster on address localhost:9092 with TLS enabled=false, SASL enabled=false
[2023-09-18 08:55:29] DEBUG Getting supported API versions
...
  1. Zk Fix

Before:

$ go run cmd/topicctl/main.go get brokers --zk-addr localhost:2181
panic: send on closed channel

goroutine 1 [running]:
github.com/segmentio/topicctl/pkg/zk.(*PooledClient).Children(0xc00017c570, {0x1ba3770, 0xc000122000}, {0xc00062e0c0, 0xc})
        /Users/pdannemann/src/topicctl/pkg/zk/client.go:182 +0x12e
github.com/segmentio/topicctl/pkg/admin.(*ZKAdminClient).GetBrokerIDs(0xc000212280, {0x1ba3770, 0xc000122000})
        /Users/pdannemann/src/topicctl/pkg/admin/zkclient.go:274 +0xb6
github.com/segmentio/topicctl/pkg/admin.(*ZKAdminClient).GetBrokers(0xc000212280, {0x1ba3770, 0xc000122000}, {0x0?, 0x0?, 0xc0004dc480?})
        /Users/pdannemann/src/topicctl/pkg/admin/zkclient.go:174 +0x53
github.com/segmentio/topicctl/pkg/cli.(*CLIRunner).GetBrokers(0xc0004dc480, {0x1ba3770?, 0xc000122000?}, 0x0?)
        /Users/pdannemann/src/topicctl/pkg/cli/cli.go:69 +0x85
github.com/segmentio/topicctl/cmd/topicctl/subcmd.brokersCmd.func1(0xc0004fe000?, {0xc0004dc200?, 0x2?, 0x2?})
        /Users/pdannemann/src/topicctl/cmd/topicctl/subcmd/get.go:121 +0x49
github.com/spf13/cobra.(*Command).execute(0xc0004fe000, {0xc0004dc1e0, 0x2, 0x2})
        /Users/pdannemann/dev/pkg/mod/github.com/spf13/cobra@v1.5.0/command.go:872 +0x694
github.com/spf13/cobra.(*Command).ExecuteC(0x1feae60)
        /Users/pdannemann/dev/pkg/mod/github.com/spf13/cobra@v1.5.0/command.go:990 +0x3b4
github.com/spf13/cobra.(*Command).Execute(...)
        /Users/pdannemann/dev/pkg/mod/github.com/spf13/cobra@v1.5.0/command.go:918
github.com/segmentio/topicctl/cmd/topicctl/subcmd.Execute({0x1838289?, 0xc0000021a0?})
        /Users/pdannemann/src/topicctl/cmd/topicctl/subcmd/root.go:49 +0xb1
main.main()
        /Users/pdannemann/src/topicctl/cmd/topicctl/main.go:13 +0x27

After:

$ go run cmd/topicctl/main.go get brokers --zk-addr localhost:2181
[2023-09-18 08:56:22]  INFO Brokers:
-----+-----------+------+------+-----------------------
  ID |   HOST    | PORT | RACK |      TIMESTAMP
-----+-----------+------+------+-----------------------
  1  | localhost | 9092 |      | 2023-09-16T17:06:33Z
-----+-----------+------+------+-----------------------
[2023-09-18 08:56:22]  INFO Brokers per rack:
-------+--------------
  RACK | NUM BROKERS
-------+--------------
       | 1
-------+--------------