-
Notifications
You must be signed in to change notification settings - Fork 593
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: introduce deck content to dbless config converter (#4453)
Introduces `ContentToDBLessConfigConverter` used by `UpdateStrategyInMemory` to adapt decK's `file.Content` to match DBLess schema constraints. It extracts the already existing conversions from `UpdateStrategyInMemory` (dropping `Info` field, cleaning up nulls in plugins' configs), and also adds cleanups for `ConsumerGroups` related fields (dropping `Plugins[i].ConsumerGroup`, `Consumers[i].Group`, `ConsumerGroups[i].Plugins`, `ConsumerGroups[i].Consumers`, filling relationships). Also adds an example config for `ConsumerGroup`.
- Loading branch information
Showing
7 changed files
with
517 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
apiVersion: configuration.konghq.com/v1beta1 | ||
kind: KongConsumerGroup | ||
metadata: | ||
name: cg1 | ||
annotations: | ||
kubernetes.io/ingress.class: kong | ||
--- | ||
apiVersion: configuration.konghq.com/v1 | ||
kind: KongConsumer | ||
metadata: | ||
name: c1 | ||
annotations: | ||
kubernetes.io/ingress.class: kong | ||
username: c1 | ||
consumerGroups: | ||
- cg1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package sendconfig | ||
|
||
import ( | ||
"github.com/kong/deck/file" | ||
) | ||
|
||
// DBLessConfig is the configuration that is sent to Kong's data-plane via its `POST /config` endpoint after being | ||
// marshalled to JSON. | ||
// It uses file.Content as its base schema, but it also includes additional fields that are not part of decK's schema. | ||
type DBLessConfig struct { | ||
file.Content | ||
ConsumerGroupConsumerRelationships []ConsumerGroupConsumerRelationship `json:"consumer_group_consumers,omitempty"` | ||
ConsumerGroupPluginRelationships []ConsumerGroupPluginRelationship `json:"consumer_group_plugins,omitempty"` | ||
} | ||
|
||
// ConsumerGroupConsumerRelationship is a relationship between a ConsumerGroup and a Consumer. | ||
type ConsumerGroupConsumerRelationship struct { | ||
ConsumerGroup string `json:"consumer_group"` | ||
Consumer string `json:"consumer"` | ||
} | ||
|
||
// ConsumerGroupPluginRelationship is a relationship between a ConsumerGroup and a Plugin. | ||
type ConsumerGroupPluginRelationship struct { | ||
ConsumerGroup string `json:"consumer_group"` | ||
Plugin string `json:"plugin"` | ||
} | ||
|
||
type DefaultContentToDBLessConfigConverter struct{} | ||
|
||
func (DefaultContentToDBLessConfigConverter) Convert(content *file.Content) DBLessConfig { | ||
dblessConfig := DBLessConfig{ | ||
Content: *content, | ||
} | ||
|
||
// DBLess schema does not support decK's Info section. | ||
dblessConfig.Content.Info = nil | ||
|
||
// DBLess schema does not support nulls in plugin configs. | ||
cleanUpNullsInPluginConfigs(&dblessConfig.Content) | ||
|
||
// DBLess schema does not 1-1 match decK's schema for ConsumerGroups. | ||
convertConsumerGroups(&dblessConfig) | ||
|
||
return dblessConfig | ||
} | ||
|
||
// cleanUpNullsInPluginConfigs removes null values from plugins' configs. | ||
func cleanUpNullsInPluginConfigs(state *file.Content) { | ||
for _, s := range state.Services { | ||
for _, p := range s.Plugins { | ||
for k, v := range p.Config { | ||
if v == nil { | ||
delete(p.Config, k) | ||
} | ||
} | ||
} | ||
for _, r := range state.Routes { | ||
for _, p := range r.Plugins { | ||
for k, v := range p.Config { | ||
if v == nil { | ||
delete(p.Config, k) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
for _, c := range state.Consumers { | ||
for _, p := range c.Plugins { | ||
for k, v := range p.Config { | ||
if v == nil { | ||
delete(p.Config, k) | ||
} | ||
} | ||
} | ||
} | ||
|
||
for _, p := range state.Plugins { | ||
for k, v := range p.Config { | ||
if v == nil { | ||
delete(p.Config, k) | ||
} | ||
} | ||
} | ||
} | ||
|
||
// convertConsumerGroups drops consumer groups related fields that are not supported in DBLess schema: | ||
// - Content.Plugins[].ConsumerGroup | ||
// - Content.Consumers[].Group, | ||
// - Content.ConsumerGroups[].Plugins | ||
// - Content.ConsumerGroups[].Consumers | ||
// | ||
// In their place it creates relationships slices: | ||
// - ConsumerGroupConsumerRelationships | ||
// - ConsumerGroupPluginRelationships | ||
func convertConsumerGroups(dblessConfig *DBLessConfig) { | ||
// DBLess schema does not support Consumer.Groups field... | ||
for i, c := range dblessConfig.Content.Consumers { | ||
// ... therefore we need to convert them to relationships... | ||
for _, cg := range dblessConfig.Content.Consumers[i].Groups { | ||
dblessConfig.ConsumerGroupConsumerRelationships = append(dblessConfig.ConsumerGroupConsumerRelationships, ConsumerGroupConsumerRelationship{ | ||
ConsumerGroup: *cg.Name, | ||
Consumer: *c.Username, | ||
}) | ||
} | ||
// ... and remove them from the Consumer struct. | ||
dblessConfig.Content.Consumers[i].Groups = nil | ||
} | ||
|
||
// DBLess schema does not support Consumer.ConsumerGroup field... | ||
for i, p := range dblessConfig.Content.Plugins { | ||
// ... therefore we need to convert it to relationships... | ||
if p.ConsumerGroup != nil { | ||
dblessConfig.ConsumerGroupPluginRelationships = append(dblessConfig.ConsumerGroupPluginRelationships, ConsumerGroupPluginRelationship{ | ||
ConsumerGroup: *p.ConsumerGroup.Name, | ||
Plugin: *p.Name, | ||
}) | ||
} | ||
// ... and remove it from the Plugin struct. | ||
dblessConfig.Content.Plugins[i].ConsumerGroup = nil | ||
} | ||
|
||
// DBLess schema does not support ConsumerGroups.Consumers and ConsumerGroups.Plugins fields so we need to remove | ||
// them. | ||
for i := range dblessConfig.Content.ConsumerGroups { | ||
dblessConfig.Content.ConsumerGroups[i].Consumers = nil | ||
dblessConfig.Content.ConsumerGroups[i].Plugins = nil | ||
} | ||
} |
Oops, something went wrong.