From 7c622bd0416e19c7f2416ef0afc4f3bea61b842d Mon Sep 17 00:00:00 2001 From: Samuele Date: Thu, 5 Sep 2024 10:06:05 +0200 Subject: [PATCH] fix(sync): bump go-kong + fill auto fields with nil (#139) * chore(deps): bump go-kong to 0.59 * fix(sync): fill auto fields with nil Fill auto fields with nil in the config sent to the CP. The rest of the default values are still not filled in the configuration that is sent out, as they are meant to be filled by the CP itself, but auto fields should not be generated, to ensure the configuration remains truly "declarative". --------- Co-authored-by: Tao Yi --- go.mod | 2 +- go.sum | 4 ++-- pkg/diff/diff.go | 21 ++++++++++++++------- pkg/types/plugin.go | 2 +- tests/integration/sync_test.go | 29 +++++------------------------ 5 files changed, 23 insertions(+), 35 deletions(-) diff --git a/go.mod b/go.mod index f05697e..e167cbc 100644 --- a/go.mod +++ b/go.mod @@ -21,7 +21,7 @@ require ( github.com/hashicorp/go-retryablehttp v0.7.5 github.com/hexops/gotextdiff v1.0.3 github.com/kong/deck v1.34.0 - github.com/kong/go-kong v0.58.0 + github.com/kong/go-kong v0.59.0 github.com/samber/lo v1.47.0 github.com/shirou/gopsutil/v3 v3.24.5 github.com/ssgelm/cookiejarparser v1.0.1 diff --git a/go.sum b/go.sum index 7a90c0f..3c7c32d 100644 --- a/go.sum +++ b/go.sum @@ -178,8 +178,8 @@ github.com/kong/deck v1.34.0 h1:iKSa5Cq8t7bdCv5R3XRV8UH+7FvnZB/YxhRTuih4rgg= github.com/kong/deck v1.34.0/go.mod h1:IUAixgNa1YSvEphgX9OIHmDyyi1JRaGTB0bYieuD9qo= github.com/kong/go-apiops v0.1.29 h1:c+AB8MmGIr+K01Afm4GB2xaOmJnD/8KWMJQkr9qssnc= github.com/kong/go-apiops v0.1.29/go.mod h1:ZNdiTZyVrAssB4wjEYWV7BfpcV9UME9LxnDDZhMPuNU= -github.com/kong/go-kong v0.58.0 h1:9Y1Hg7ahCS7Ig+iAb9CZyjeT3ARuDe9ohV7mVa8n/eU= -github.com/kong/go-kong v0.58.0/go.mod h1:8Vt6HmtgLNgL/7bSwAlz3DIWqBtzG7qEt9+OnMiQOa0= +github.com/kong/go-kong v0.59.0 h1:U6dE2sqb8E8j0kESW/RCW9TkXH8Y3W0EtNDXJVsDNuM= +github.com/kong/go-kong v0.59.0/go.mod h1:8Vt6HmtgLNgL/7bSwAlz3DIWqBtzG7qEt9+OnMiQOa0= github.com/kong/go-slugify v1.0.0 h1:vCFAyf2sdoSlBtLcrmDWUFn0ohlpKiKvQfXZkO5vSKY= github.com/kong/go-slugify v1.0.0/go.mod h1:dbR2h3J2QKXQ1k0aww6cN7o4cIcwlWflr6RKRdcoaiw= github.com/kong/semver/v4 v4.0.1 h1:DIcNR8W3gfx0KabFBADPalxxsp+q/5COwIFkkhrFQ2Y= diff --git a/pkg/diff/diff.go b/pkg/diff/diff.go index 0fbf690..56ca0bb 100644 --- a/pkg/diff/diff.go +++ b/pkg/diff/diff.go @@ -603,7 +603,7 @@ func (sc *Syncer) Solve(ctx context.Context, parallelism int, dry bool, isJSONOu // Below the configuration in `e` may be modified. This is done solely for // the purpose of displaying a correct diff and should not affect the // configuration that is sent to Kong. - originalE := e + eventForKong := e // If the event is for a plugin, inject defaults in the plugin's config // that will be used for the diff. This is needed to avoid highlighting @@ -620,12 +620,19 @@ func (sc *Syncer) Solve(ctx context.Context, parallelism int, dry bool, isJSONOu return nil, err } - var oldPlugin *kong.Plugin - if kongStatePlugin, ok := e.OldObj.(*state.Plugin); ok { - oldPlugin = &kongStatePlugin.Plugin - } + // fill defaults and auto fields for the configuration that will be used for the diff newPlugin := &pluginCopy.Plugin - if err := kong.FillPluginsDefaultsAutoFields(newPlugin, schema, oldPlugin); err != nil { + if err := kong.FillPluginsDefaults(newPlugin, schema); err != nil { + return nil, fmt.Errorf("failed processing auto fields: %w", err) + } + + // only fill auto fields for the configuration sent to Kong + // this is done because we want to avoid Kong to auto generate fields, which + // would make decK's configuration no longer fully "declarative" + if err := kong.FillPluginsDefaultsWithOpts(&plugin.Plugin, schema, kong.FillRecordOptions{ + FillDefaults: false, + FillAuto: true, + }); err != nil { return nil, fmt.Errorf("failed processing auto fields: %w", err) } } @@ -712,7 +719,7 @@ func (sc *Syncer) Solve(ctx context.Context, parallelism int, dry bool, isJSONOu if !dry { // sync mode // fire the request to Kong - result, err = sc.processor.Do(ctx, originalE.Kind, originalE.Op, originalE) + result, err = sc.processor.Do(ctx, eventForKong.Kind, eventForKong.Op, eventForKong) // TODO https://github.com/Kong/go-database-reconciler/issues/22 this does not print, but is switched on // sc.enableEntityActions because the existing behavior returns a result from the anon Run function. // Refactoring should use only the channel and simplify the return, probably to just an error (all the other diff --git a/pkg/types/plugin.go b/pkg/types/plugin.go index ae09493..b416438 100644 --- a/pkg/types/plugin.go +++ b/pkg/types/plugin.go @@ -181,7 +181,7 @@ func (d *pluginDiffer) createUpdatePlugin(plugin *state.Plugin) (*crud.Event, er return nil, fmt.Errorf("failed getting schema: %w", err) } pluginWithDefaults := &state.Plugin{Plugin: *plugin.DeepCopy()} - err = kong.FillPluginsDefaultsAutoFields(&pluginWithDefaults.Plugin, schema, ¤tPlugin.Plugin) + err = kong.FillPluginsDefaults(&pluginWithDefaults.Plugin, schema) if err != nil { return nil, fmt.Errorf("failed processing auto fields: %w", err) } diff --git a/tests/integration/sync_test.go b/tests/integration/sync_test.go index 3269e2b..e2a65dd 100644 --- a/tests/integration/sync_test.go +++ b/tests/integration/sync_test.go @@ -15,7 +15,6 @@ import ( "net/http/httptest" "net/url" "os" - "regexp" "strings" "testing" "time" @@ -5660,29 +5659,11 @@ func Test_Sync_PluginAutoFields(t *testing.T) { KongClient: c, }) - stats, errs, changes := syncer.Solve(ctx, 1, false, true) - require.Empty(t, errs, "Should have no errors in syncing") - require.NoError(t, err) - - require.Equal(t, int32(1), stats.CreateOps.Count(), "Should create 1 entity") - require.Len(t, changes.Creating, 1, "Should have 1 creating record in changes") + _, errs, _ := syncer.Solve(ctx, 1, false, true) - t.Run("should not override auto values with nils", func(t *testing.T) { - newState, err := fetchCurrentState(ctx, client, deckDump.Config{}) - require.NoError(t, err) - plugins, err := newState.Plugins.GetAll() - require.NoError(t, err) - require.Len(t, plugins, 1) - plugin := plugins[0] - require.Equal(t, "oauth2", *plugin.Name) - provisionKey, ok := plugin.Config["provision_key"] - require.True(t, ok) - - provisionKeyStr, ok := provisionKey.(string) - require.True(t, ok, "provision_key is not a string") - pattern := `^[a-zA-Z0-9]+$` - re := regexp.MustCompile(pattern) - require.True(t, re.MatchString(provisionKeyStr), "provision_key does not match the pattern") - }) + require.NotNil(t, errs) + require.Len(t, errs, 1) + require.Contains(t, errs[0].Error(), "provision_key: required field missing", + "Should error out due to missing provision_key") }) }