Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add include defaults when describing topics #184

Merged
merged 3 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/describe/describe-topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func newDescribeTopicCmd() *cobra.Command {
cmdDescribeTopic.Flags().StringVarP(&flags.OutputFormat, "output", "o", flags.OutputFormat, "output format. One of: json|yaml|wide")
cmdDescribeTopic.Flags().BoolVarP(&flags.PrintConfigs, "print-configs", "c", true, "print configs")
cmdDescribeTopic.Flags().BoolVarP(&flags.SkipEmptyPartitions, "skip-empty", "s", false, "show only partitions that have a messages")
cmdDescribeTopic.Flags().BoolVarP(&flags.IncludeDefaults, "include-defaults", "i", false, "include default configs")

return cmdDescribeTopic
}
4 changes: 2 additions & 2 deletions internal/broker/broker-operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (operation *Operation) GetBrokers(flags GetBrokersFlags) error {
Name: fmt.Sprint(broker.ID()),
}

if configs, err = internal.ListConfigs(&admin, brokerConfig); err != nil {
if configs, err = internal.ListConfigs(&admin, brokerConfig, false); err != nil {
return err
}

Expand Down Expand Up @@ -153,7 +153,7 @@ func (operation *Operation) DescribeBroker(id int32, flags DescribeBrokerFlags)
Name: fmt.Sprint(broker.ID()),
}

if configs, err = internal.ListConfigs(&admin, brokerConfig); err != nil {
if configs, err = internal.ListConfigs(&admin, brokerConfig, false); err != nil {
return err
}

Expand Down
13 changes: 9 additions & 4 deletions internal/common-operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,10 +309,9 @@ func TopicExists(client *sarama.Client, name string) (bool, error) {
return false, nil
}

func ListConfigs(admin *sarama.ClusterAdmin, resource sarama.ConfigResource) ([]Config, error) {
func ListConfigs(admin *sarama.ClusterAdmin, resource sarama.ConfigResource, includeDefaults bool) ([]Config, error) {

var (
configs = make([]Config, 0)
configEntries []sarama.ConfigEntry
err error
)
Expand All @@ -321,15 +320,21 @@ func ListConfigs(admin *sarama.ClusterAdmin, resource sarama.ConfigResource) ([]
return nil, errors.Wrap(err, fmt.Sprintf("failed to describe %v config", getResourceTypeName(resource.Type)))
}

return listConfigsFromEntries(configEntries, includeDefaults), nil
d-rk marked this conversation as resolved.
Show resolved Hide resolved
}

func listConfigsFromEntries(configEntries []sarama.ConfigEntry, includeDefaults bool) []Config {
var configs = make([]Config, 0)

for _, configEntry := range configEntries {

if !configEntry.Default && configEntry.Source != sarama.SourceDefault {
if includeDefaults || (!configEntry.Default && configEntry.Source != sarama.SourceDefault) {
entry := Config{Name: configEntry.Name, Value: configEntry.Value}
configs = append(configs, entry)
}
}

return configs, nil
return configs
}

func getResourceTypeName(resourceType sarama.ConfigResourceType) string {
Expand Down
83 changes: 83 additions & 0 deletions internal/common-operation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package internal

import (
"reflect"
"testing"

"github.com/IBM/sarama"
)

func TestListConfigsFromEntries(t *testing.T) {
testCases := []struct {
name string
entries []sarama.ConfigEntry
includeDefaults bool
configs []Config
}{
{
name: "not include defaults, empty entries",
entries: []sarama.ConfigEntry{},
configs: []Config{},
},
{
name: "not include defaults",
entries: []sarama.ConfigEntry{
{
Name: "non_default",
Value: "ND",
Default: false,
Source: sarama.SourceUnknown,
},
{
Name: "default",
Value: "D",
Default: true,
Source: sarama.SourceDefault,
},
},
configs: []Config{
{Name: "non_default", Value: "ND"},
},
},
{
name: "include defaults, empty entries",
entries: []sarama.ConfigEntry{},
configs: []Config{},
includeDefaults: true,
},
{
name: "include defaults",
entries: []sarama.ConfigEntry{
{
Name: "non_default",
Value: "ND",
Default: false,
Source: sarama.SourceUnknown,
},
{
Name: "default",
Value: "D",
Default: true,
Source: sarama.SourceDefault,
},
},
configs: []Config{
{Name: "non_default", Value: "ND"},
{Name: "default", Value: "D"},
},
includeDefaults: true,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
configs := listConfigsFromEntries(tc.entries, tc.includeDefaults)

if len(configs) > 0 &&
len(tc.configs) > 0 &&
!reflect.DeepEqual(configs, tc.configs) {
t.Fatalf("expect: %v, got %v", tc.configs, configs)
}
})
}
}
21 changes: 13 additions & 8 deletions internal/topic/topic-operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ type Partition struct {
}

type requestedTopicFields struct {
partitionID bool
partitionOffset bool
partitionLeader bool
partitionReplicas bool
partitionISRs bool
config bool
partitionID bool
partitionOffset bool
partitionLeader bool
partitionReplicas bool
partitionISRs bool
config bool
configIncludeDefaults bool
}

var allFields = requestedTopicFields{partitionID: true, partitionOffset: true, partitionLeader: true, partitionReplicas: true, partitionISRs: true, config: true}
Expand All @@ -64,6 +65,7 @@ type DescribeTopicFlags struct {
PrintConfigs bool
SkipEmptyPartitions bool
OutputFormat string
IncludeDefaults bool
}

type Operation struct {
Expand Down Expand Up @@ -161,7 +163,10 @@ func (operation *Operation) DescribeTopic(topic string, flags DescribeTopicFlags
return errors.Wrap(err, "failed to create cluster admin")
}

if t, err = readTopic(&client, &admin, topic, allFields); err != nil {
fields := allFields
fields.configIncludeDefaults = flags.IncludeDefaults

if t, err = readTopic(&client, &admin, topic, fields); err != nil {
return errors.Wrap(err, "failed to read topic")
}

Expand Down Expand Up @@ -732,7 +737,7 @@ func readTopic(client *sarama.Client, admin *sarama.ClusterAdmin, name string, r
Name: name,
}

if top.Configs, err = internal.ListConfigs(admin, topicConfig); err != nil {
if top.Configs, err = internal.ListConfigs(admin, topicConfig, requestedFields.configIncludeDefaults); err != nil {
return top, err
}
}
Expand Down