-
Notifications
You must be signed in to change notification settings - Fork 743
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Kafka eventsource supports Sarama config customization (#2161)
Signed-off-by: Derek Wang <[email protected]>
- Loading branch information
Showing
11 changed files
with
594 additions
and
404 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package common | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
|
||
"github.com/Shopify/sarama" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
// GetSaramaConfigFromYAMLString parse yaml string to sarama.config. | ||
// Note: All the time.Duration config can not be correctly decoded because it does not implement the decode function. | ||
func GetSaramaConfigFromYAMLString(yaml string) (*sarama.Config, error) { | ||
v := viper.New() | ||
v.SetConfigType("yaml") | ||
if err := v.ReadConfig(bytes.NewBufferString(yaml)); err != nil { | ||
return nil, err | ||
} | ||
cfg := sarama.NewConfig() | ||
cfg.Producer.Return.Successes = true | ||
if err := v.Unmarshal(cfg); err != nil { | ||
return nil, fmt.Errorf("unable to decode into struct, %w", err) | ||
} | ||
if err := cfg.Validate(); err != nil { | ||
return nil, fmt.Errorf("failed validating sarama config, %w", err) | ||
} | ||
return cfg, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package common | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGetSaramaConfigFromYAMLString(t *testing.T) { | ||
t.Run("YAML Config", func(t *testing.T) { | ||
var yamlExample = string(` | ||
admin: | ||
retry: | ||
max: 105 | ||
producer: | ||
maxMessageBytes: 800 | ||
consumer: | ||
fetch: | ||
min: 2 | ||
net: | ||
MaxOpenRequests: 5 | ||
`) | ||
conf, err := GetSaramaConfigFromYAMLString(yamlExample) | ||
assert.NoError(t, err) | ||
assert.Equal(t, 800, conf.Producer.MaxMessageBytes) | ||
assert.Equal(t, 105, conf.Admin.Retry.Max) | ||
assert.Equal(t, int32(2), conf.Consumer.Fetch.Min) | ||
assert.Equal(t, 5, conf.Net.MaxOpenRequests) | ||
}) | ||
t.Run("Empty config", func(t *testing.T) { | ||
conf, err := GetSaramaConfigFromYAMLString("") | ||
assert.NoError(t, err) | ||
assert.Equal(t, 1000000, conf.Producer.MaxMessageBytes) | ||
assert.Equal(t, 5, conf.Admin.Retry.Max) | ||
assert.Equal(t, int32(1), conf.Consumer.Fetch.Min) | ||
assert.Equal(t, 5, conf.Net.MaxOpenRequests) | ||
}) | ||
|
||
t.Run("NON yaml config", func(t *testing.T) { | ||
_, err := GetSaramaConfigFromYAMLString("welcome") | ||
assert.Error(t, err) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.