diff --git a/pkg/diff/diff.go b/pkg/diff/diff.go index 0eb6a0a..39ab040 100644 --- a/pkg/diff/diff.go +++ b/pkg/diff/diff.go @@ -599,6 +599,35 @@ func (sc *Syncer) Solve(ctx context.Context, parallelism int, dry bool, isJSONOu var err error var result crud.Arg + // holds the original event with the unchanged configuration + // below the config may be updated, to render the diff correctly + originalE := e + + // If the event is for a plugin, inject defaults in the plugin's Config + // This is required for computing a diff that takes defaults into account + // Defaults are populated by Kong Gateway when a plugin is created and + // values are not provided; the diff should not display these values as changes + if plugin, ok := e.Obj.(*state.Plugin); ok { + pluginCopy := &state.Plugin{ + Plugin: *plugin.DeepCopy(), + Meta: plugin.Meta, + } + e.Obj = pluginCopy + + exists, err := utils.WorkspaceExists(ctx, sc.kongClient) + if err != nil && exists { + var schema map[string]interface{} + schema, err = sc.kongClient.Plugins.GetFullSchema(ctx, pluginCopy.Plugin.Name) + if err != nil { + return nil, err + } + + if err := kong.FillPluginsDefaults(&pluginCopy.Plugin, schema); err != nil { + return nil, fmt.Errorf("failed filling plugin defaults: %w", err) + } + } + } + c := e.Obj.(state.ConsoleString) objDiff := map[string]interface{}{ "old": e.OldObj, @@ -679,8 +708,8 @@ 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, e.Kind, e.Op, e) + // fire the request to Kong; Pass the original event, with the unchanged configuration. + result, err = sc.processor.Do(ctx, originalE.Kind, originalE.Op, originalE) // 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/file/builder.go b/pkg/file/builder.go index c0bf27f..d24687b 100644 --- a/pkg/file/builder.go +++ b/pkg/file/builder.go @@ -1443,44 +1443,6 @@ func (b *stateBuilder) ingestRoute(r FRoute) error { return nil } -func (b *stateBuilder) getPluginSchema(pluginName string) (map[string]interface{}, error) { - var schema map[string]interface{} - - // lookup in cache - if schema, ok := b.schemasCache[pluginName]; ok { - return schema, nil - } - - exists, err := utils.WorkspaceExists(b.ctx, b.client) - if err != nil { - return nil, fmt.Errorf("ensure workspace exists: %w", err) - } - if !exists { - return schema, ErrWorkspaceNotFound - } - - schema, err = b.client.Plugins.GetFullSchema(b.ctx, &pluginName) - if err != nil { - return schema, err - } - b.schemasCache[pluginName] = schema - return schema, nil -} - -func (b *stateBuilder) addPluginDefaults(plugin *FPlugin) error { - if b.client == nil { - return nil - } - schema, err := b.getPluginSchema(*plugin.Name) - if err != nil { - if errors.Is(err, ErrWorkspaceNotFound) { - return nil - } - return fmt.Errorf("retrieve schema for %v from Kong: %w", *plugin.Name, err) - } - return kong.FillPluginsDefaults(&plugin.Plugin, schema) -} - func (b *stateBuilder) ingestPlugins(plugins []FPlugin) error { for _, p := range plugins { p := p @@ -1504,9 +1466,6 @@ func (b *stateBuilder) ingestPlugins(plugins []FPlugin) error { if err != nil { return err } - if err := b.addPluginDefaults(&p); err != nil { - return fmt.Errorf("add defaults to plugin '%v': %w", *p.Name, err) - } utils.MustMergeTags(&p, b.selectTags) if plugin != nil { p.Plugin.CreatedAt = plugin.CreatedAt