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

feat: adding consumer-group policy overrides support in deck #1518

Merged
merged 6 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 14 additions & 0 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ func syncMain(ctx context.Context, filenames []string, dry bool, parallelism,
}
}

dumpConfig.IsConsumerGroupPolicyOverrideSet = determinePolicyOverride(*targetContent, dumpConfig)

dumpConfig.SelectorTags, err = determineSelectorTag(*targetContent, dumpConfig)
if err != nil {
return err
Expand Down Expand Up @@ -420,6 +422,18 @@ func syncMain(ctx context.Context, filenames []string, dry bool, parallelism,
return nil
}

func determinePolicyOverride(targetContent file.Content, config dump.Config) bool {
if config.IsConsumerGroupPolicyOverrideSet {
return true
}

if targetContent.Info != nil && targetContent.Info.ConsumerGroupPolicyOverrides {
return targetContent.Info.ConsumerGroupPolicyOverrides
}

return false
}

func determineLookUpSelectorTagsConsumerGroups(targetContent file.Content) ([]string, error) {
if targetContent.Info != nil &&
targetContent.Info.LookUpSelectorTags != nil &&
Expand Down
4 changes: 4 additions & 0 deletions cmd/gateway_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ that will be created, updated, or deleted.
false, "do not diff CA certificates.")
diffCmd.Flags().BoolVar(&diffJSONOutput, "json-output",
false, "generate command execution report in a JSON format")
diffCmd.Flags().BoolVar(&dumpConfig.IsConsumerGroupPolicyOverrideSet, "consumer-group-policy-overrides",
false, "allow deck to diff consumer-group policy overrides.\n"+
"This allows policy overrides to work with Kong GW versions >= 3.4\n"+
"Warning: do not mix with consumer-group scoped plugins")
addSilenceEventsFlag(diffCmd.Flags())
return diffCmd
}
30 changes: 18 additions & 12 deletions cmd/gateway_dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,13 @@ func executeDump(cmd *cobra.Command, _ []string) error {
}

if err := file.KongStateToFile(ks, file.WriteConfig{
SelectTags: dumpConfig.SelectorTags,
Workspace: workspace,
Filename: workspace,
FileFormat: format,
WithID: dumpWithID,
KongVersion: kongVersion,
SelectTags: dumpConfig.SelectorTags,
Workspace: workspace,
Filename: workspace,
FileFormat: format,
WithID: dumpWithID,
KongVersion: kongVersion,
IsConsumerGroupPolicyOverrideSet: dumpConfig.IsConsumerGroupPolicyOverrideSet,
}); err != nil {
return err
}
Expand Down Expand Up @@ -132,12 +133,13 @@ func executeDump(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("building state: %w", err)
}
return file.KongStateToFile(ks, file.WriteConfig{
SelectTags: dumpConfig.SelectorTags,
Workspace: dumpWorkspace,
Filename: dumpCmdKongStateFile,
FileFormat: format,
WithID: dumpWithID,
KongVersion: kongVersion,
SelectTags: dumpConfig.SelectorTags,
Workspace: dumpWorkspace,
Filename: dumpCmdKongStateFile,
FileFormat: format,
WithID: dumpWithID,
KongVersion: kongVersion,
IsConsumerGroupPolicyOverrideSet: dumpConfig.IsConsumerGroupPolicyOverrideSet,
})
}

Expand Down Expand Up @@ -197,6 +199,10 @@ configure Kong.`,
false, "do not show the association between consumer and consumer-group.\n"+
"If set to true, deck skips listing consumers with consumer-groups,\n"+
"thus gaining some performance with large configs. This flag is not valid with Konnect.")
dumpCmd.Flags().BoolVar(&dumpConfig.IsConsumerGroupPolicyOverrideSet, "consumer-group-policy-overrides",
false, "allow deck to dump consumer-group policy overrides.\n"+
"This allows policy overrides to work with Kong GW versions >= 3.4\n"+
"Warning: do not mix with consumer-group scoped plugins")
if deprecated {
dumpCmd.Flags().StringVarP(&dumpCmdKongStateFileDeprecated, "output-file", "o",
fileOutDefault, "file to which to write Kong's configuration."+
Expand Down
4 changes: 4 additions & 0 deletions cmd/gateway_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ to get Kong's state in sync with the input state.`,
"See `db_update_propagation` in kong.conf.")
syncCmd.Flags().BoolVar(&dumpConfig.SkipCACerts, "skip-ca-certificates",
false, "do not sync CA certificates.")
syncCmd.Flags().BoolVar(&dumpConfig.IsConsumerGroupPolicyOverrideSet, "consumer-group-policy-overrides",
false, "allow deck to sync consumer-group policy overrides.\n"+
"This allows policy overrides to work with Kong GW versions >= 3.4\n"+
"Warning: do not mix with consumer-group scoped plugins")
syncCmd.Flags().BoolVar(&syncJSONOutput, "json-output",
false, "generate command execution report in a JSON format")
addSilenceEventsFlag(syncCmd.Flags())
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/fatih/color v1.17.0
github.com/google/go-cmp v0.6.0
github.com/kong/go-apiops v0.1.41
github.com/kong/go-database-reconciler v1.19.3
github.com/kong/go-database-reconciler v1.20.1
github.com/kong/go-kong v0.63.0
github.com/mitchellh/go-homedir v1.1.0
github.com/spf13/cobra v1.8.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,8 @@ github.com/klauspost/cpuid/v2 v2.2.5 h1:0E5MSMDEoAulmXNFquVs//DdoomxaoTY1kUhbc/q
github.com/klauspost/cpuid/v2 v2.2.5/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
github.com/kong/go-apiops v0.1.41 h1:1KXbQqyhO2E4nEnXJNqUDmHZU/LQ1U7Zoi+MAlcM2P0=
github.com/kong/go-apiops v0.1.41/go.mod h1:sATq9Tz+ivzHKZU+tDXkRtEZnf64xroU3lgv3yXqP4I=
github.com/kong/go-database-reconciler v1.19.3 h1:wcvRIL1lt21ZDCGH0OaGbyTWxKv8tiGpbSCP4hUp6Ew=
github.com/kong/go-database-reconciler v1.19.3/go.mod h1:crxjFGiSSKWtP1AqFZz/IEnlxTX/hFkcBdMdwQbEpmk=
github.com/kong/go-database-reconciler v1.20.1 h1:mXS4sThlsZAJda+jX3Gf2JCdqQZx7FWSuhMIuwVjKg0=
github.com/kong/go-database-reconciler v1.20.1/go.mod h1:crxjFGiSSKWtP1AqFZz/IEnlxTX/hFkcBdMdwQbEpmk=
github.com/kong/go-kong v0.63.0 h1:8ECLgkgDqON61qCMq/M0gTwZKYxg55Oy692dRDOOBiU=
github.com/kong/go-kong v0.63.0/go.mod h1:ma9GWnhkxtrXZlLFfED955HjVzmUojYEHet3lm+PDik=
github.com/kong/go-slugify v1.0.0 h1:vCFAyf2sdoSlBtLcrmDWUFn0ohlpKiKvQfXZkO5vSKY=
Expand Down
63 changes: 63 additions & 0 deletions tests/integration/dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,3 +450,66 @@ func Test_SkipConsumersWithConsumerGroups(t *testing.T) {
})
}
}

func Test_Dump_ConsumerGroupPlugin_PolicyOverrides(t *testing.T) {
tests := []struct {
name string
stateFile string
expectedFile string
errorExpected bool
errorString string
runWhen func(t *testing.T)
}{
{
name: "dump with flag --consumer-group-policy-overrides set: >=3.4.0 <3.8.0",
stateFile: "testdata/sync/037-consumer-group-policy-overrides/kong34x-no-info.yaml",
expectedFile: "testdata/sync/037-consumer-group-policy-overrides/kong34x.yaml",
errorExpected: false,
runWhen: func(t *testing.T) { runWhen(t, "enterprise", ">=3.4.0 <3.8.0") },
},
{
name: "dump with flag --consumer-group-policy-overrides set: >=3.8.0 <3.9.0",
stateFile: "testdata/sync/037-consumer-group-policy-overrides/kong38x-no-info.yaml",
expectedFile: "testdata/sync/037-consumer-group-policy-overrides/kong38x.yaml",
errorExpected: false,
runWhen: func(t *testing.T) { runWhen(t, "enterprise", ">=3.8.0 <3.9.0") },
},
{
name: "dump with flag --consumer-group-policy-overrides set: >=3.9.0",
stateFile: "testdata/sync/037-consumer-group-policy-overrides/kong39x-no-info.yaml",
expectedFile: "testdata/sync/037-consumer-group-policy-overrides/kong39x.yaml",
errorExpected: false,
runWhen: func(t *testing.T) { runWhen(t, "enterprise", ">=3.9.0") },
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
tc.runWhen(t)
setup(t)

require.NoError(t, sync(context.Background(), tc.stateFile, "--consumer-group-policy-overrides"))

var (
output string
err error
)

output, err = dump(
"--consumer-group-policy-overrides",
"-o", "-",
)

if tc.errorExpected {
assert.Equal(t, err.Error(), tc.errorString)
return
}

require.NoError(t, err)

expected, err := readFile(tc.expectedFile)
require.NoError(t, err)
assert.Equal(t, expected, output)
})
}
}
20 changes: 16 additions & 4 deletions tests/integration/reset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func Test_Reset_SkipCACert_2x(t *testing.T) {

require.NoError(t, sync(context.Background(), tc.kongFile))
reset(t, "--skip-ca-certificates")
testKongState(t, client, false, tc.expectedState, nil)
testKongState(t, client, false, false, tc.expectedState, nil)
})
}
}
Expand Down Expand Up @@ -100,19 +100,31 @@ func Test_Reset_SkipCACert_3x(t *testing.T) {

require.NoError(t, sync(context.Background(), tc.kongFile))
reset(t, "--skip-ca-certificates")
testKongState(t, client, false, tc.expectedState, nil)
testKongState(t, client, false, false, tc.expectedState, nil)
})
}
}

func Test_Reset_ConsumerGroupConsumersWithCustomID(t *testing.T) {
runWhenEnterpriseOrKonnect(t, ">=3.0.0")
runWhen(t, "enterprise", ">=3.0.0")
setup(t)

client, err := getTestClient()
require.NoError(t, err)

require.NoError(t, sync(context.Background(), "testdata/sync/028-consumer-group-consumers-custom_id/kong.yaml"))
reset(t)
testKongState(t, client, false, utils.KongRawState{}, nil)
testKongState(t, client, false, false, utils.KongRawState{}, nil)
}

func Test_Reset_ConsumerGroupConsumersWithCustomID_Konnect(t *testing.T) {
runWhenKonnect(t)
setup(t)

client, err := getTestClient()
require.NoError(t, err)

require.NoError(t, sync(context.Background(), "testdata/sync/028-consumer-group-consumers-custom_id/kong.yaml"))
reset(t)
testKongState(t, client, true, false, utils.KongRawState{}, nil)
}
Loading