Skip to content

Commit

Permalink
feat: add consumer-groups scoped plugins support to Konnect
Browse files Browse the repository at this point in the history
  • Loading branch information
GGabriele committed Jul 26, 2023
1 parent fce04dc commit 1123668
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 51 deletions.
30 changes: 1 addition & 29 deletions dump/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"net/http"

"github.com/kong/deck/konnect"
"github.com/kong/deck/utils"
"github.com/kong/go-kong/kong"
"golang.org/x/sync/errgroup"
Expand Down Expand Up @@ -78,11 +77,7 @@ func getConsumerGroupsConfiguration(ctx context.Context, group *errgroup.Group,
err error
consumerGroups []*kong.ConsumerGroupObject
)
if config.KonnectRuntimeGroup != "" {
consumerGroups, err = GetAllKonnectConsumerGroups(ctx, client, config.SelectorTags)
} else {
consumerGroups, err = GetAllConsumerGroups(ctx, client, config.SelectorTags)
}
consumerGroups, err = GetAllConsumerGroups(ctx, client, config.SelectorTags)
if err != nil {
if kong.IsNotFoundErr(err) || kong.IsForbiddenErr(err) {
return nil
Expand Down Expand Up @@ -532,29 +527,6 @@ func GetAllUpstreams(ctx context.Context,
return upstreams, nil
}

// GetAllConsumerGroups queries Konnect for all the ConsumerGroups using client.
func GetAllKonnectConsumerGroups(ctx context.Context,
client *kong.Client, tags []string,
) ([]*kong.ConsumerGroupObject, error) {
var consumerGroupObjects []*kong.ConsumerGroupObject
opt := newOpt(tags)
cgs, err := konnect.ListAllConsumerGroups(ctx, client, opt.Tags)
if err != nil {
return nil, err
}
if err := ctx.Err(); err != nil {
return nil, err
}
for _, cg := range cgs {
r, err := konnect.GetConsumerGroupObject(ctx, client, cg.ID)
if err != nil {
return nil, err
}
consumerGroupObjects = append(consumerGroupObjects, r)
}
return consumerGroupObjects, nil
}

// GetAllConsumerGroups queries Kong for all the ConsumerGroups using client.
func GetAllConsumerGroups(ctx context.Context,
client *kong.Client, tags []string,
Expand Down
2 changes: 1 addition & 1 deletion file/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (b *stateBuilder) build() (*utils.KongRawState, *utils.KonnectRawState, err
b.checkRoutePaths = true
}

if utils.Kong340Version.LTE(b.kongVersion) {
if utils.Kong340Version.LTE(b.kongVersion) || b.isKonnect {
b.isConsumerGroupScopedPluginSupported = true
}

Expand Down
72 changes: 51 additions & 21 deletions tests/integration/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -623,20 +623,6 @@ var (
Username: kong.String("bar"),
},
},
Plugins: []*kong.ConsumerGroupPlugin{
{
Name: kong.String("rate-limiting-advanced"),
Config: kong.Configuration{
"limit": []*int32{int32p(7)},
"retry_after_jitter_max": int32p(1),
"window_size": []*int32{int32p(60)},
"window_type": kong.String("sliding"),
},
ConsumerGroup: &kong.ConsumerGroup{
ID: kong.String("521a90ad-36cb-4e31-a5db-1d979aee40d1"),
},
},
},
},
{
ConsumerGroup: &kong.ConsumerGroup{
Expand Down Expand Up @@ -3231,12 +3217,54 @@ func Test_Sync_ConsumerGroupsKonnect(t *testing.T) {
},
},
{
name: "creates consumer groups and plugin",
kongFile: "testdata/sync/016-consumer-groups-and-plugins/kong3x.yaml",
kongFileInitial: "testdata/sync/016-consumer-groups-and-plugins/kong3x-initial.yaml",
kongFile: "testdata/sync/023-consumer-groups-scoped-plugins/kong3x.yaml",
expectedState: utils.KongRawState{
Consumers: consumerGroupsConsumers,
ConsumerGroups: consumerGroupsWithRLAKonnect,
Consumers: consumerGroupsConsumers,
ConsumerGroups: []*kong.ConsumerGroupObject{
{
ConsumerGroup: &kong.ConsumerGroup{
Name: kong.String("silver"),
},
Consumers: []*kong.Consumer{
{
Username: kong.String("bar"),
},
},
},
{
ConsumerGroup: &kong.ConsumerGroup{
Name: kong.String("gold"),
},
Consumers: []*kong.Consumer{
{
Username: kong.String("foo"),
},
},
},
},
Plugins: consumerGroupScopedPlugins,
Services: svc1_207,
Routes: route1_20x,
KeyAuths: []*kong.KeyAuth{
{
Consumer: &kong.Consumer{
ID: kong.String("87095815-5395-454e-8c18-a11c9bc0ef04"),
},
Key: kong.String("i-am-special"),
},
{
Consumer: &kong.Consumer{
ID: kong.String("5a5b9369-baeb-4faa-a902-c40ccdc2928e"),
},
Key: kong.String("i-am-not-so-special"),
},
{
Consumer: &kong.Consumer{
ID: kong.String("e894ea9e-ad08-4acf-a960-5a23aa7701c7"),
},
Key: kong.String("i-am-just-average"),
},
},
},
},
}
Expand All @@ -3247,7 +3275,9 @@ func Test_Sync_ConsumerGroupsKonnect(t *testing.T) {
defer teardown(t)

// set up initial state
sync(tc.kongFileInitial)
if tc.kongFileInitial != "" {
sync(tc.kongFileInitial)
}
// update with desired final state
sync(tc.kongFile)

Expand Down Expand Up @@ -3966,7 +3996,7 @@ func Test_Sync_ConsumerGroupsScopedPlugins_Pre340(t *testing.T) {
kongFile: "testdata/sync/023-consumer-groups-scoped-plugins/kong3x.yaml",
expectedError: errors.New(
"a rate-limiting-advanced plugin with config.consumer_groups\n" +
"and/or config.enforced_consumer_group was found. Please use Consumer Groups scoped\n" +
"and/or config.enforce_consumer_group was found. Please use Consumer Groups scoped\n" +
"Plugins when running against Kong Enterprise 3.4.0 and above.\n\n" +
"Check DOC_LINK for more information"),
},
Expand Down

0 comments on commit 1123668

Please sign in to comment.