Skip to content

Commit

Permalink
fix: deck content to dbless converter
Browse files Browse the repository at this point in the history
  • Loading branch information
programmer04 committed Aug 7, 2023
1 parent 9dfe212 commit 07ada11
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 43 deletions.
6 changes: 3 additions & 3 deletions internal/dataplane/kongstate/kongstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,9 +419,9 @@ func (ks *KongState) FillPlugins(
}

// FillIDs iterates over the KongState and fills in the ID field for each entity
// that supports the FillID method (these are Service, Route and Consumer). It makes
// their IDs deterministic, enabling their correct identification in external systems
// (e.g. Konnect Analytics).
// that supports the FillID method (these are Service, Route, Consumer and Consumer
// Group). It makes their IDs deterministic, enabling their correct identification
// in external systems (e.g. Konnect Analytics).
func (ks *KongState) FillIDs(logger logrus.FieldLogger) {
for svcIndex, svc := range ks.Services {
if err := svc.FillID(); err != nil {
Expand Down
30 changes: 4 additions & 26 deletions internal/dataplane/sendconfig/inmemory_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
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.
Expand All @@ -19,12 +18,6 @@ type ConsumerGroupConsumerRelationship struct {
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 {
Expand Down Expand Up @@ -85,41 +78,26 @@ func cleanUpNullsInPluginConfigs(state *file.Content) {
}

// convertConsumerGroups drops consumer groups related fields that are not supported in DBLess schema:
// - Content.Plugins[].ConsumerGroup
// - Content.Consumers[].Group,
// - Content.Consumers[].Groups,
// - 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,
// ... by using FriendlyName() that ensures returning ID if Name is empty...
ConsumerGroup: cg.FriendlyName(),
Consumer: c.FriendlyName(),
})
}
// ... 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 {
Expand Down
78 changes: 64 additions & 14 deletions internal/dataplane/sendconfig/inmemory_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,6 @@ func TestDBLessConfigMarshalToJSON(t *testing.T) {
Consumer: "c1",
},
},
ConsumerGroupPluginRelationships: []sendconfig.ConsumerGroupPluginRelationship{
{
ConsumerGroup: "cg1",
Plugin: "p1",
},
},
}

expected := `{
Expand All @@ -47,12 +41,6 @@ func TestDBLessConfigMarshalToJSON(t *testing.T) {
"consumer_group": "cg1",
"consumer": "c1"
}
],
"consumer_group_plugins": [
{
"consumer_group": "cg1",
"plugin": "p1"
}
]
}`
b, err := json.Marshal(dblessConfig)
Expand Down Expand Up @@ -135,6 +123,10 @@ func TestDefaultContentToDBLessConfigConverter(t *testing.T) {
{
Plugin: kong.Plugin{
Name: kong.String("p1"),
ConsumerGroup: &kong.ConsumerGroup{
Name: kong.String("cg1"),
ID: kong.String("cg1"),
},
},
},
},
Expand All @@ -145,10 +137,68 @@ func TestDefaultContentToDBLessConfigConverter(t *testing.T) {
Consumer: "c1",
},
},
ConsumerGroupPluginRelationships: []sendconfig.ConsumerGroupPluginRelationship{
},
},
{
name: "content with consumer group consumers and plugins (only IDs filled)",
content: &file.Content{
ConsumerGroups: []file.FConsumerGroupObject{
{
ConsumerGroup: kong.ConsumerGroup{
Name: kong.String("cg1"),
},
Consumers: []*kong.Consumer{{ID: kong.String("c1")}},
Plugins: []*kong.ConsumerGroupPlugin{{ID: kong.String("p1")}},
},
},
Consumers: []file.FConsumer{
{
Consumer: kong.Consumer{
ID: kong.String("c1"),
},
Groups: []*kong.ConsumerGroup{{ID: kong.String("cg1")}},
},
},
Plugins: []file.FPlugin{
{
Plugin: kong.Plugin{
Name: kong.String("p1"),
ConsumerGroup: &kong.ConsumerGroup{ID: kong.String("cg1")},
},
},
},
},
expectedDBLessConfig: sendconfig.DBLessConfig{
Content: file.Content{
ConsumerGroups: []file.FConsumerGroupObject{
{
ConsumerGroup: kong.ConsumerGroup{
Name: kong.String("cg1"),
},
},
},
Consumers: []file.FConsumer{
{
Consumer: kong.Consumer{
ID: kong.String("c1"),
},
},
},
Plugins: []file.FPlugin{
{
Plugin: kong.Plugin{
Name: kong.String("p1"),
ConsumerGroup: &kong.ConsumerGroup{
ID: kong.String("cg1"),
},
},
},
},
},
ConsumerGroupConsumerRelationships: []sendconfig.ConsumerGroupConsumerRelationship{
{
ConsumerGroup: "cg1",
Plugin: "p1",
Consumer: "c1",
},
},
},
Expand Down

0 comments on commit 07ada11

Please sign in to comment.