Skip to content

Commit

Permalink
fix(diff): clear unmatching deprecated fields
Browse files Browse the repository at this point in the history
When user uses decK configuration and they use only
new or the old (deprecated) fields then we don't want to display
incorrect diff. We used to try to "fill" those fields but sometimes
it's impossible to do that. The reason why we need to fill in these
fields is that Kong, in order to be backwards compatible sends, both
new and old fields. This commit removes either the deprecated
or the new fields from the response from Kong before doing the diff
so that there is no false negative difference.

KAG-5577
  • Loading branch information
nowNick committed Oct 28, 2024
1 parent a8c9a93 commit 8568c97
Show file tree
Hide file tree
Showing 19 changed files with 899 additions and 4 deletions.
14 changes: 10 additions & 4 deletions pkg/diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -612,17 +612,15 @@ func (sc *Syncer) Solve(ctx context.Context, parallelism int, dry bool, isJSONOu
pluginCopy := &state.Plugin{Plugin: *plugin.DeepCopy()}
e.Obj = pluginCopy

exists, err := utils.WorkspaceExists(ctx, sc.kongClient)
if err == nil && exists {
if exists, _ := utils.WorkspaceExists(ctx, sc.kongClient); exists {
var schema map[string]interface{}
schema, err = sc.kongClient.Plugins.GetFullSchema(ctx, pluginCopy.Plugin.Name)
if err != nil {
return nil, err
}

// fill defaults and auto fields for the configuration that will be used for the diff
newPlugin := &pluginCopy.Plugin
if err := kong.FillPluginsDefaults(newPlugin, schema); err != nil {
if err := kong.FillPluginsDefaults(&pluginCopy.Plugin, schema); err != nil {
return nil, fmt.Errorf("failed processing auto fields: %w", err)
}

Expand All @@ -635,6 +633,14 @@ func (sc *Syncer) Solve(ctx context.Context, parallelism int, dry bool, isJSONOu
}); err != nil {
return nil, fmt.Errorf("failed processing auto fields: %w", err)
}

if oldPlugin, ok := e.OldObj.(*state.Plugin); ok {
oldPluginCopy := &state.Plugin{Plugin: *oldPlugin.DeepCopy()}
e.OldObj = oldPluginCopy
if err := kong.ClearUnmatchingDeprecations(&pluginCopy.Plugin, &oldPluginCopy.Plugin, schema); err != nil {
return nil, fmt.Errorf("failed processing auto fields: %w", err)
}
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/types/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ func (d *pluginDiffer) createUpdatePlugin(plugin *state.Plugin) (*crud.Event, er
return nil, fmt.Errorf("failed processing auto fields: %w", err)
}

if err := kong.ClearUnmatchingDeprecations(&pluginWithDefaults.Plugin, &currentPlugin.Plugin, schema); err != nil {
return nil, fmt.Errorf("failed clearing unmatching deprecations fields: %w", err)
}

if !currentPlugin.EqualWithOpts(pluginWithDefaults, false, true, false) {
return &crud.Event{
Op: crud.Update,
Expand Down
Loading

0 comments on commit 8568c97

Please sign in to comment.