Skip to content

Commit

Permalink
fix: auto-generate rla namespace when using Consumer Groups
Browse files Browse the repository at this point in the history
  • Loading branch information
mheap authored and GGabriele committed Apr 10, 2024
1 parent f13a65e commit ed12ee5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
25 changes: 25 additions & 0 deletions convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,16 @@ func generateAutoFields(content *file.Content) error {
}
}

for _, consumerGroup := range content.ConsumerGroups {
for _, plugin := range consumerGroup.Plugins {
if *plugin.Name == rateLimitingAdvancedPluginName {
if err := autoGenerateNamespaceForRLAPluginConsumerGroups(plugin); err != nil {
return err
}
}
}
}

return nil
}

Expand All @@ -237,6 +247,21 @@ func autoGenerateNamespaceForRLAPlugin(plugin *file.FPlugin) error {
return nil
}

func autoGenerateNamespaceForRLAPluginConsumerGroups(plugin *kong.ConsumerGroupPlugin) error {
if plugin.Config != nil {
ns, ok := plugin.Config["namespace"]
if !ok || ns == nil {
// namespace is not set, generate one.
randomNamespace, err := randomString(rlaNamespaceDefaultLength)
if err != nil {
return fmt.Errorf("error generating random namespace: %w", err)
}
plugin.Config["namespace"] = randomNamespace
}
}
return nil
}

func migrateRoutesPathFieldPre300(route *file.FRoute) (*file.FRoute, bool) {
var hasChanged bool
for _, path := range route.Paths {
Expand Down
16 changes: 16 additions & 0 deletions convert/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,19 @@ func Test_convertAutoFields(t *testing.T) {
},
},
},
ConsumerGroups: []file.FConsumerGroupObject{
{
ConsumerGroup: kong.ConsumerGroup{
Name: kong.String("my_consumer_group"),
},
Plugins: []*kong.ConsumerGroupPlugin{
{
Name: kong.String("rate-limiting-advanced"),
Config: kong.Configuration{},
},
},
},
},
Plugins: []file.FPlugin{
{
Plugin: kong.Plugin{
Expand All @@ -562,4 +575,7 @@ func Test_convertAutoFields(t *testing.T) {

consumerPluginConfig := got.Consumers[0].Plugins[0].Config
assert.NotEmpty(t, consumerPluginConfig["namespace"])

consumerGroupPluginConfig := got.ConsumerGroups[0].Plugins[0].Config
assert.NotEmpty(t, consumerGroupPluginConfig["namespace"])
}

0 comments on commit ed12ee5

Please sign in to comment.