Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: fill config defaults #309

Merged
merged 15 commits into from
Apr 7, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions kong/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,14 @@ func fillConfigRecord(schema gjson.Result, config Configuration) Configuration {
// an arbitrary map, field is already set.
return true
}
case []interface{}:
if value.Get(fname).Get("elements.type").String() != "record" &&
config[fname] != nil &&
len(config[fname].([]interface{})) > 0 {
// Non empty array with elements which are not of type record
// this means field is already set
return true
}
default:
// not a map, field is already set.
return true
Expand All @@ -249,6 +257,34 @@ func fillConfigRecord(schema gjson.Result, config Configuration) Configuration {
res[fname] = map[string]interface{}(newSubConfig)
return true
}

// Check if field is of type array of records (in Schema)
// If this array is non-nil and non-empty (in Config), go through all the records in this array and add defaults
// If the array has only primitives like string/number/boolean then the value is already set
// If the array is empty or nil, then no defaults need to be set for its elements
if ftype.String() == "array" {
if value.Get(fname).Get("elements.type").String() == "record" {
if config[fname] != nil {
// Check sub config is of type array and it is non-empty
if subConfigArray, ok := config[fname].([]interface{}); ok && len(subConfigArray) > 0 {
processedSubConfigArray := make([]interface{}, len(subConfigArray))

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)
processedSubConfigArray[i] = processedConfigRecord
continue
}
// Element not of type record, keep the value as is
processedSubConfigArray[i] = configRecord
}
res[fname] = processedSubConfigArray
return true
}
}
}
}
value = value.Get(fname + ".default")
if value.Exists() {
res[fname] = value.Value()
Expand Down
239 changes: 239 additions & 0 deletions kong/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tidwall/gjson"
)

func TestStringArrayToString(t *testing.T) {
Expand Down Expand Up @@ -999,3 +1000,241 @@ func TestFillConsumerGroupPluginDefaults(T *testing.T) {
})
}
}

func Test_fillConfigRecord(T *testing.T) {
nishant95 marked this conversation as resolved.
Show resolved Hide resolved
assert := assert.New(T)

client, err := NewTestClient(nil, nil)
assert.NoError(err)
assert.NotNil(client)
nishant95 marked this conversation as resolved.
Show resolved Hide resolved

tests := []struct {
name string
schema gjson.Result
config Configuration
expected Configuration
}{
{
name: "fills defaults for all missing fields",
schema: gjson.Parse(`{
"fields": {
"config":
{
"type": "record",
"fields":[
{
"enabled":{
"type":"boolean",
"default":true,
"required":true
}
},
{
"mappings":{
"required":false,
"type":"array",
"elements":{
"type":"record",
"fields":[
{"name":{"type":"string","required":false}},
{"nationality":{"type":"string","required":false}}
]
}
}
}
]
}
}
}`),
config: Configuration{
"mappings": []interface{}{
map[string]interface{}{
"nationality": "Ethiopian",
},
},
},
expected: Configuration{
"enabled": true,
"mappings": []any{
Configuration{
"name": nil,
"nationality": "Ethiopian",
},
},
},
},
}

for _, tc := range tests {
T.Run(tc.name, func(t *testing.T) {
configSchema, err := getConfigSchema(tc.schema)
assert.NoError(err)
config := fillConfigRecord(configSchema, tc.config)
assert.NotNil(config)
nishant95 marked this conversation as resolved.
Show resolved Hide resolved
if diff := cmp.Diff(config, tc.expected); diff != "" {
t.Errorf(diff)
}
})
}
}

func Test_FillPluginsDefaults(T *testing.T) {
nishant95 marked this conversation as resolved.
Show resolved Hide resolved
RunWhenKong(T, ">=2.6.0 <2.8.1")
nishant95 marked this conversation as resolved.
Show resolved Hide resolved
assert := assert.New(T)

client, err := NewTestClient(nil, nil)
assert.NoError(err)
assert.NotNil(client)
nishant95 marked this conversation as resolved.
Show resolved Hide resolved

tests := []struct {
name string
plugin *Plugin
expected *Plugin
}{
{
name: "fills defaults for all missing fields",
plugin: &Plugin{
Config: Configuration{
"metrics": []interface{}{
map[string]interface{}{
"name": "response_size",
"stat_type": "histogram",
},
},
},
},
expected: &Plugin{
Config: Configuration{
"host": "localhost",
"port": float64(8125),
"prefix": "kong",
"metrics": []interface{}{
Configuration{
"name": "response_size",
"stat_type": "histogram",
"consumer_identifier": nil,
"sample_rate": nil,
},
},
},
},
},
}

for _, tc := range tests {
T.Run(tc.name, func(t *testing.T) {
plugin := tc.plugin
fullSchema, err := client.Schemas.Get(defaultCtx, "plugins/statsd")
assert.NoError(err)
assert.NotNil(fullSchema)
nishant95 marked this conversation as resolved.
Show resolved Hide resolved
if err := FillPluginsDefaults(plugin, fullSchema); err != nil {
t.Errorf(err.Error())
}
nishant95 marked this conversation as resolved.
Show resolved Hide resolved
opts := cmpopts.IgnoreFields(*plugin,
"Protocols", "Enabled",
)
if diff := cmp.Diff(plugin, tc.expected, opts); diff != "" {
t.Errorf(diff)
}
})
}
}

func Test_FillPluginsDefaults_RequestTransformer(T *testing.T) {
assert := assert.New(T)

client, err := NewTestClient(nil, nil)
assert.NoError(err)
assert.NotNil(client)
nishant95 marked this conversation as resolved.
Show resolved Hide resolved

tests := []struct {
name string
plugin *Plugin
expected *Plugin
}{
{
name: "fills defaults for all missing fields",
plugin: &Plugin{
Config: Configuration{
"add": map[string]interface{}{
"body": []any{},
"headers": []any{
"Knative-Serving-Namespace:e3ffeafd-b5fe-4f34-b2e4-af6f3d9fb417",
"Knative-Serving-Revision:helloworld-go-00001",
},
"querystring": []any{},
},
"append": map[string]interface{}{
"body": []any{},
"headers": []any{},
"querystring": []any{},
},
"http_method": nil,
"enabled": true,
"id": "0beef60e-e7e3-40f8-ac47-f6a10b931cee",
"name": "request-transformer",
"protocols": []any{
"grpc",
"grpcs",
"http",
"https",
},
"service": map[string]interface{}{
"id": "63295454-c41e-447e-bce5-d6b488f3866e",
"name": "30bc1240-ad84-4760-a469-763bce524191.helloworld-go-00001.80",
},
},
},
expected: &Plugin{
Config: Configuration{
"add": map[string]any{
"body": []any{},
"headers": []any{
"Knative-Serving-Namespace:e3ffeafd-b5fe-4f34-b2e4-af6f3d9fb417",
"Knative-Serving-Revision:helloworld-go-00001",
},
"querystring": []any{},
},
"append": map[string]interface{}{
"body": []interface{}{},
"headers": []interface{}{},
"querystring": []interface{}{},
},
"remove": map[string]any{"body": []any{}, "headers": []any{}, "querystring": []any{}},
"rename": map[string]any{"body": []any{}, "headers": []any{}, "querystring": []any{}},
"replace": map[string]any{"body": []any{}, "headers": []any{}, "querystring": []any{}, "uri": nil},
"http_method": nil,
"enabled": true,
"id": "0beef60e-e7e3-40f8-ac47-f6a10b931cee",
"name": "request-transformer",
"protocols": []any{
"grpc",
"grpcs",
"http",
"https",
},
"service": map[string]interface{}{
"id": "63295454-c41e-447e-bce5-d6b488f3866e",
"name": "30bc1240-ad84-4760-a469-763bce524191.helloworld-go-00001.80",
},
},
},
},
}

for _, tc := range tests {
T.Run(tc.name, func(t *testing.T) {
plugin := tc.plugin
fullSchema, err := client.Schemas.Get(defaultCtx, "plugins/request-transformer")
assert.NoError(err)
assert.NotNil(fullSchema)
nishant95 marked this conversation as resolved.
Show resolved Hide resolved
if err := FillPluginsDefaults(plugin, fullSchema); err != nil {
t.Errorf(err.Error())
}
nishant95 marked this conversation as resolved.
Show resolved Hide resolved
opts := cmpopts.IgnoreFields(*plugin, "Enabled", "Protocols")
if diff := cmp.Diff(plugin, tc.expected, opts); diff != "" {
t.Errorf(diff)
}
})
}
}