Skip to content

Commit

Permalink
feat: add default_lookup_tags support for routes
Browse files Browse the repository at this point in the history
  • Loading branch information
GGabriele committed Jan 22, 2024
1 parent fb8f906 commit 56f7136
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 2 deletions.
35 changes: 34 additions & 1 deletion cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func syncMain(ctx context.Context, filenames []string, dry bool, parallelism,

dumpConfig.LookUpSelectorTagsConsumers, err = determineLookUpSelectorTagsConsumers(*targetContent)
if err != nil {
return fmt.Errorf("error determining lookup selector tags: %w", err)
return fmt.Errorf("error determining lookup selector tags for consumers: %w", err)
}

if dumpConfig.LookUpSelectorTagsConsumers != nil {
Expand All @@ -256,6 +256,24 @@ func syncMain(ctx context.Context, filenames []string, dry bool, parallelism,
}
}

dumpConfig.LookUpSelectorTagsRoutes, err = determineLookUpSelectorTagsRoutes(*targetContent)
if err != nil {
return fmt.Errorf("error determining lookup selector tags for routes: %w", err)
}

if dumpConfig.LookUpSelectorTagsRoutes != nil {
routesGlobal, err := dump.GetAllRoutes(ctx, kongClient, dumpConfig.LookUpSelectorTagsRoutes)
if err != nil {
return fmt.Errorf("error retrieving global routes via lookup selector tags: %w", err)
}
for _, r := range routesGlobal {
targetContent.Routes = append(targetContent.Routes, file.FRoute{Route: *r})
if err != nil {
return fmt.Errorf("error adding global route %v: %w", r.FriendlyName(), err)
}
}
}

if utils.Kong340Version.LTE(parsedKongVersion) {
dumpConfig.IsConsumerGroupScopedPluginSupported = true
}
Expand Down Expand Up @@ -354,6 +372,21 @@ func determineLookUpSelectorTagsConsumers(targetContent file.Content) ([]string,
return nil, nil
}

func determineLookUpSelectorTagsRoutes(targetContent file.Content) ([]string, error) {
if targetContent.Info != nil &&
targetContent.Info.LookUpSelectorTags != nil &&
targetContent.Info.LookUpSelectorTags.Routes != nil {
if len(targetContent.Info.LookUpSelectorTags.Routes) == 0 {
return nil, fmt.Errorf("global routes specified but no global tags")
}
utils.RemoveDuplicates(&targetContent.Info.LookUpSelectorTags.Routes)
sort.Strings(targetContent.Info.LookUpSelectorTags.Routes)
return targetContent.Info.LookUpSelectorTags.Routes, nil

}
return nil, nil
}

func determineSelectorTag(targetContent file.Content, config dump.Config) ([]string, error) {
if targetContent.Info != nil {
if len(targetContent.Info.SelectorTags) > 0 {
Expand Down
21 changes: 20 additions & 1 deletion cmd/gateway_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func executeValidate(cmd *cobra.Command, _ []string) error {
// if this is an online validation, we need to look up upstream consumers if required.
lookUpSelectorTagsConsumers, err := determineLookUpSelectorTagsConsumers(*targetContent)
if err != nil {
return fmt.Errorf("error determining lookup selector tags: %w", err)
return fmt.Errorf("error determining lookup selector tags for consumers: %w", err)
}

if lookUpSelectorTagsConsumers != nil {
Expand All @@ -66,6 +66,25 @@ func executeValidate(cmd *cobra.Command, _ []string) error {
}
}
}

// if this is an online validation, we need to look up upstream routes if required.
lookUpSelectorTagsRoutes, err := determineLookUpSelectorTagsRoutes(*targetContent)
if err != nil {
return fmt.Errorf("error determining lookup selector tags for routes: %w", err)
}

if lookUpSelectorTagsRoutes != nil {
routesGlobal, err := dump.GetAllRoutes(ctx, kongClient, lookUpSelectorTagsRoutes)
if err != nil {
return fmt.Errorf("error retrieving global routes via lookup selector tags: %w", err)
}
for _, r := range routesGlobal {
targetContent.Routes = append(targetContent.Routes, file.FRoute{Route: *r})
if err != nil {
return fmt.Errorf("error adding global route %v: %w", r.FriendlyName(), err)
}
}
}
}

rawState, err := file.Get(ctx, targetContent, file.RenderConfig{
Expand Down
38 changes: 38 additions & 0 deletions tests/integration/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4825,3 +4825,41 @@ func Test_Sync_ConsumerGroupConsumersWithCustomID(t *testing.T) {
require.NoError(t, sync("testdata/sync/028-consumer-group-consumers-custom_id/kong.yaml"))
testKongState(t, client, false, expectedState, nil)
}

// Test_Sync_LookupRoutesTags tests that existing behavior when referencing
// routes from plugins is preserved:
// - if a referenced route is not present in the state file, the sync fails
// - if a referenced route is present in the state file, the sync succeeds
//
// This test also tests that the new behavior is implemented correctly:
// - if a referenced route is not present in the state file, but is present
// in Kong when using the new lookup selector tags, the sync succeeds
// - if a referenced route is not present in the state file and neither in
// Kong when using the new lookup selector tags, the sync fails
func Test_Sync_LookupRoutesTags(t *testing.T) {
runWhen(t, "enterprise", ">=3.0.0")
setup(t)

// test that reference to non-existing route fails.
pluginsNoLookupStateFile := "testdata/sync/030-lookup-tags-routes/plugins_no_lookup.yaml"
err := sync(pluginsNoLookupStateFile)
require.Error(t, err)
require.EqualError(t, err, "building state: route foo for plugin rate-limiting-advanced: entity not found")

// test that reference to existing local route succeeds.
pluginsAndRoutesStateFile := "testdata/sync/030-lookup-tags-routes/plugins_and_routes.yaml"
require.NoError(t, sync(pluginsAndRoutesStateFile))
reset(t)

// test that reference to existing global route succeeds via lookup tags.
globalRoutesStateFile := "testdata/sync/030-lookup-tags-routes/global_routes.yaml"
require.NoError(t, sync(globalRoutesStateFile))
// sync plugins with lookup reference to global routes.
pluginsLookupStateFile := "testdata/sync/030-lookup-tags-routes/plugins_lookup.yaml"
require.NoError(t, sync(pluginsLookupStateFile))
reset(t)

// test that reference to non-existing global route fails via lookup tags.
require.Error(t, sync(pluginsLookupStateFile))
require.EqualError(t, err, "building state: route foo for plugin rate-limiting-advanced: entity not found")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
_format_version: "3.0"
_info:
select_tags:
- global-routes
routes:
- name: foo
paths:
- /foo
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
_format_version: "3.0"
plugins:
- name: rate-limiting-advanced
config:
limit:
- 10
window_size:
- 60
namespace: foo
sync_rate: -1
route: foo
routes:
- name: foo
paths:
- /foo
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
_format_version: "3.0"
_info:
select_tags:
- managed-by-deck
default_lookup_tags:
routes:
- global-routes
plugins:
- name: rate-limiting-advanced
config:
limit:
- 10
window_size:
- 60
namespace: foo
sync_rate: -1
route: foo
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
_format_version: "3.0"
_info:
select_tags:
- managed-by-deck
plugins:
- name: rate-limiting-advanced
config:
limit:
- 10
window_size:
- 60
namespace: foo
sync_rate: -1
route: foo

0 comments on commit 56f7136

Please sign in to comment.