Skip to content

Commit

Permalink
fix(utils): support auto fields
Browse files Browse the repository at this point in the history
When fields marked as `auto` are passed to kong without a value
they are auto generated by the gateway. In that case, comparing with
"defaults" doesn't work, so in order to compute a correct diff they must
be copied from Kong's configuration.

This commit addS a new FillPluginsDefaultsAutoFields utils function that
in addition to default fields, adds auto fields to the configuration.
It takes an additional parameter: oldConfig, which represents the config
from Kong: auto fields are just copied from it so that they don't show
up as differences in the diff.
  • Loading branch information
samugi committed Aug 28, 2024
1 parent f96e181 commit e3567c9
Showing 1 changed file with 36 additions and 8 deletions.
44 changes: 36 additions & 8 deletions kong/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ func backfillResultConfigMap(res Configuration, path []string, configValue inter
return nil
}

func fillConfigRecord(schema gjson.Result, config Configuration) Configuration {
func fillConfigRecord(schema gjson.Result, config Configuration, kongConfig Configuration) Configuration {
res := config.DeepCopy()
configFields := schema.Get("fields")
// Fetch deprecated fields
Expand All @@ -295,7 +295,7 @@ func fillConfigRecord(schema gjson.Result, config Configuration) Configuration {
}

if fname == "config" {
newConfig := fillConfigRecord(value.Get(fname), config)
newConfig := fillConfigRecord(value.Get(fname), config, kongConfig)
res = newConfig
return true
}
Expand Down Expand Up @@ -348,7 +348,7 @@ func fillConfigRecord(schema gjson.Result, config Configuration) Configuration {
default:
fieldConfig = subConfig.(map[string]interface{})
}
newSubConfig := fillConfigRecord(value.Get(fname), fieldConfig)
newSubConfig := fillConfigRecord(value.Get(fname), fieldConfig, kongConfig)
res[fname] = map[string]interface{}(newSubConfig)
return true
}
Expand All @@ -368,7 +368,7 @@ func fillConfigRecord(schema gjson.Result, config Configuration) Configuration {
for i, configRecord := range subConfigArray {
// Check if element is of type record, if it is, set default values by recursively calling `fillConfigRecord`
if configRecordMap, ok := configRecord.(map[string]interface{}); ok {
processedConfigRecord := fillConfigRecord(value.Get(fname).Get("elements"), configRecordMap)
processedConfigRecord := fillConfigRecord(value.Get(fname).Get("elements"), configRecordMap, kongConfig)
processedSubConfigArray[i] = processedConfigRecord
continue
}
Expand All @@ -382,6 +382,19 @@ func fillConfigRecord(schema gjson.Result, config Configuration) Configuration {
}
}

// handle `auto` fields by copying them from kongConfig if they don't have
// a value. When fields marked as `auto` are passed to kong without a value
// they are auto generated by the gateway. In that case, comparing with
// "defaults" doesn't work, so in order to compute a correct diff they must
// be copied from the kongConfig.
auto := value.Get(fname + ".auto")
if auto.Exists() && auto.Bool() && kongConfig != nil {
if v, ok := kongConfig[fname]; ok {
res[fname] = v
return true
}
}

// Check if the record has a default value for the specified field.
// If so, use it. If not, fall back to the default value of the field itself.
if defaultRecordValue.Exists() && defaultRecordValue.Get(fname).Exists() {
Expand Down Expand Up @@ -665,9 +678,7 @@ func FillEntityDefaults(entity interface{}, schema Schema) error {
return nil
}

// FillPluginsDefaults ingests plugin's defaults from its schema.
// Takes in a plugin struct and mutate it in place.
func FillPluginsDefaults(plugin *Plugin, schema Schema) error {
func fillConfigRecordDefaultsAutoFields(plugin *Plugin, schema map[string]interface{}, oldPlugin *Plugin) error {
jsonb, err := json.Marshal(&schema)
if err != nil {
return err
Expand All @@ -680,7 +691,12 @@ func FillPluginsDefaults(plugin *Plugin, schema Schema) error {
if plugin.Config == nil {
plugin.Config = make(Configuration)
}
plugin.Config = fillConfigRecord(configSchema, plugin.Config)

var oldConfig Configuration
if oldPlugin != nil {
oldConfig = oldPlugin.Config
}
plugin.Config = fillConfigRecord(configSchema, plugin.Config, oldConfig)
if plugin.Protocols == nil {
plugin.Protocols = getDefaultProtocols(gjsonSchema)
}
Expand All @@ -689,3 +705,15 @@ func FillPluginsDefaults(plugin *Plugin, schema Schema) error {
}
return nil
}

// FillPluginsDefaults ingests plugin's defaults from its schema.
// Takes in a plugin struct and mutate it in place.
func FillPluginsDefaults(plugin *Plugin, schema Schema) error {
return fillConfigRecordDefaultsAutoFields(plugin, schema, nil)
}

// same as FillPluginsDefaults but also fills auto fields
// keeping both for compatibility: these utils are public
func FillPluginsDefaultsAutoFields(plugin *Plugin, schema map[string]interface{}, oldPlugin *Plugin) error {
return fillConfigRecordDefaultsAutoFields(plugin, schema, oldPlugin)
}

0 comments on commit e3567c9

Please sign in to comment.