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: deck content to dbless converter #4459

Merged
merged 1 commit into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
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 nil...
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