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 25, 2024
1 parent a8c9a93 commit e066228
Show file tree
Hide file tree
Showing 8 changed files with 890 additions and 388 deletions.
47 changes: 27 additions & 20 deletions pkg/diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -612,28 +612,35 @@ 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 {
var schema map[string]interface{}
schema, err = sc.kongClient.Plugins.GetFullSchema(ctx, pluginCopy.Plugin.Name)
if err != nil {
return nil, err
}
if oldPlugin, ok := e.OldObj.(*state.Plugin); ok {
oldPluginCopy := &state.Plugin{Plugin: *oldPlugin.DeepCopy()}
e.OldObj = oldPluginCopy

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 {
return nil, fmt.Errorf("failed processing auto fields: %w", err)
}
// fill defaults and auto fields for the configuration that will be used for the diff
if err := kong.FillPluginsDefaults(&pluginCopy.Plugin, 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)
}

// 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)
if err := kong.ClearUnmatchingDeprecations(&pluginCopy.Plugin, &oldPluginCopy.Plugin, schema); err != nil {

Check failure on line 641 in pkg/diff/diff.go

View workflow job for this annotation

GitHub Actions / test

undefined: kong.ClearUnmatchingDeprecations (typecheck)
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 {

Check failure on line 189 in pkg/types/plugin.go

View workflow job for this annotation

GitHub Actions / test

undefined: kong.ClearUnmatchingDeprecations (typecheck)

Check failure on line 189 in pkg/types/plugin.go

View workflow job for this annotation

GitHub Actions / test

undefined: kong.ClearUnmatchingDeprecations) (typecheck)

Check failure on line 189 in pkg/types/plugin.go

View workflow job for this annotation

GitHub Actions / integration-tests / integration (kong:2.8)

undefined: kong.ClearUnmatchingDeprecations

Check failure on line 189 in pkg/types/plugin.go

View workflow job for this annotation

GitHub Actions / integration-tests / integration (kong:3.4)

undefined: kong.ClearUnmatchingDeprecations

Check failure on line 189 in pkg/types/plugin.go

View workflow job for this annotation

GitHub Actions / integration-tests / integration (kong:3.5)

undefined: kong.ClearUnmatchingDeprecations

Check failure on line 189 in pkg/types/plugin.go

View workflow job for this annotation

GitHub Actions / integration-tests / integration (kong:3.6)

undefined: kong.ClearUnmatchingDeprecations

Check failure on line 189 in pkg/types/plugin.go

View workflow job for this annotation

GitHub Actions / integration-tests / integration (kong:3.7)

undefined: kong.ClearUnmatchingDeprecations

Check failure on line 189 in pkg/types/plugin.go

View workflow job for this annotation

GitHub Actions / integration-tests / integration (kong/kong:master)

undefined: kong.ClearUnmatchingDeprecations

Check failure on line 189 in pkg/types/plugin.go

View workflow job for this annotation

GitHub Actions / enterprise-integration-tests / integration (kong/kong-gateway:2.8)

undefined: kong.ClearUnmatchingDeprecations

Check failure on line 189 in pkg/types/plugin.go

View workflow job for this annotation

GitHub Actions / enterprise-integration-tests / integration (kong/kong-gateway:3.4)

undefined: kong.ClearUnmatchingDeprecations

Check failure on line 189 in pkg/types/plugin.go

View workflow job for this annotation

GitHub Actions / enterprise-integration-tests / integration (kong/kong-gateway:3.5)

undefined: kong.ClearUnmatchingDeprecations

Check failure on line 189 in pkg/types/plugin.go

View workflow job for this annotation

GitHub Actions / enterprise-integration-tests / integration (kong/kong-gateway:3.6)

undefined: kong.ClearUnmatchingDeprecations

Check failure on line 189 in pkg/types/plugin.go

View workflow job for this annotation

GitHub Actions / enterprise-integration-tests / integration (kong/kong-gateway:3.7)

undefined: kong.ClearUnmatchingDeprecations

Check failure on line 189 in pkg/types/plugin.go

View workflow job for this annotation

GitHub Actions / enterprise-integration-tests / integration (kong/kong-gateway-dev:latest)

undefined: kong.ClearUnmatchingDeprecations
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 e066228

Please sign in to comment.