Skip to content

Commit

Permalink
feat: support scoping plugins to consumer-groups
Browse files Browse the repository at this point in the history
  • Loading branch information
GGabriele committed Jul 11, 2023
1 parent 4959e75 commit cf552c4
Show file tree
Hide file tree
Showing 16 changed files with 622 additions and 64 deletions.
27 changes: 24 additions & 3 deletions file/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ func (b *stateBuilder) consumerGroups() {
ConsumerGroup: &cg.ConsumerGroup,
}

err := b.intermediate.ConsumerGroups.Add(state.ConsumerGroup{ConsumerGroup: cg.ConsumerGroup})
if err != nil {
b.err = err
return
}

for _, plugin := range cg.Plugins {
if utils.Empty(plugin.ID) {
current, err := b.currentState.ConsumerGroupPlugins.Get(
Expand Down Expand Up @@ -882,6 +888,18 @@ func (b *stateBuilder) plugins() {
}
p.Route = utils.GetRouteReference(r.Route)
}
if p.ConsumerGroup != nil && !utils.Empty(p.ConsumerGroup.ID) {
cg, err := b.intermediate.ConsumerGroups.Get(*p.ConsumerGroup.ID)
if errors.Is(err, state.ErrNotFound) {
b.err = fmt.Errorf("consumer-group %v for plugin %v: %w",
p.ConsumerGroup.FriendlyName(), *p.Name, err)
return
} else if err != nil {
b.err = err
return
}
p.ConsumerGroup = utils.GetConsumerGroupReference(cg.ConsumerGroup)
}
plugins = append(plugins, p)
}
if err := b.ingestPlugins(plugins); err != nil {
Expand Down Expand Up @@ -997,9 +1015,9 @@ func (b *stateBuilder) ingestPlugins(plugins []FPlugin) error {
for _, p := range plugins {
p := p
if utils.Empty(p.ID) {
cID, rID, sID := pluginRelations(&p.Plugin)
cID, rID, sID, cgID := pluginRelations(&p.Plugin)
plugin, err := b.currentState.Plugins.GetByProp(*p.Name,
sID, rID, cID)
sID, rID, cID, cgID)
if errors.Is(err, state.ErrNotFound) {
p.ID = uuid()
} else if err != nil {
Expand Down Expand Up @@ -1044,7 +1062,7 @@ func (b *stateBuilder) fillPluginConfig(plugin *FPlugin) error {
return nil
}

func pluginRelations(plugin *kong.Plugin) (cID, rID, sID string) {
func pluginRelations(plugin *kong.Plugin) (cID, rID, sID, cgID string) {
if plugin.Consumer != nil && !utils.Empty(plugin.Consumer.ID) {
cID = *plugin.Consumer.ID
}
Expand All @@ -1054,6 +1072,9 @@ func pluginRelations(plugin *kong.Plugin) (cID, rID, sID string) {
if plugin.Service != nil && !utils.Empty(plugin.Service.ID) {
sID = *plugin.Service.ID
}
if plugin.ConsumerGroup != nil && !utils.Empty(plugin.ConsumerGroup.ID) {
cgID = *plugin.ConsumerGroup.ID
}
return
}

Expand Down
42 changes: 30 additions & 12 deletions file/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,9 @@ func existingPluginState() *state.KongState {
Route: &kong.Route{
ID: kong.String("700bc504-b2b1-4abd-bd38-cec92779659e"),
},
ConsumerGroup: &kong.ConsumerGroup{
ID: kong.String("69ed4618-a653-4b54-8bb6-dc33bd6fe048"),
},
},
})
return s
Expand Down Expand Up @@ -751,6 +754,9 @@ func Test_stateBuilder_ingestPlugins(t *testing.T) {
Route: &kong.Route{
ID: kong.String("700bc504-b2b1-4abd-bd38-cec92779659e"),
},
ConsumerGroup: &kong.ConsumerGroup{
ID: kong.String("69ed4618-a653-4b54-8bb6-dc33bd6fe048"),
},
},
},
},
Expand Down Expand Up @@ -780,6 +786,9 @@ func Test_stateBuilder_ingestPlugins(t *testing.T) {
Route: &kong.Route{
ID: kong.String("700bc504-b2b1-4abd-bd38-cec92779659e"),
},
ConsumerGroup: &kong.ConsumerGroup{
ID: kong.String("69ed4618-a653-4b54-8bb6-dc33bd6fe048"),
},
Config: kong.Configuration{},
},
},
Expand All @@ -805,21 +814,23 @@ func Test_pluginRelations(t *testing.T) {
plugin *kong.Plugin
}
tests := []struct {
name string
args args
wantCID string
wantRID string
wantSID string
name string
args args
wantCID string
wantRID string
wantSID string
wantCGID string
}{
{
args: args{
plugin: &kong.Plugin{
Name: kong.String("foo"),
},
},
wantCID: "",
wantRID: "",
wantSID: "",
wantCID: "",
wantRID: "",
wantSID: "",
wantCGID: "",
},
{
args: args{
Expand All @@ -834,16 +845,20 @@ func Test_pluginRelations(t *testing.T) {
Service: &kong.Service{
ID: kong.String("sID"),
},
ConsumerGroup: &kong.ConsumerGroup{
ID: kong.String("cgID"),
},
},
},
wantCID: "cID",
wantRID: "rID",
wantSID: "sID",
wantCID: "cID",
wantRID: "rID",
wantSID: "sID",
wantCGID: "cgID",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotCID, gotRID, gotSID := pluginRelations(tt.args.plugin)
gotCID, gotRID, gotSID, gotCGID := pluginRelations(tt.args.plugin)
if gotCID != tt.wantCID {
t.Errorf("pluginRelations() gotCID = %v, want %v", gotCID, tt.wantCID)
}
Expand All @@ -853,6 +868,9 @@ func Test_pluginRelations(t *testing.T) {
if gotSID != tt.wantSID {
t.Errorf("pluginRelations() gotSID = %v, want %v", gotSID, tt.wantSID)
}
if gotCGID != tt.wantCGID {
t.Errorf("pluginRelations() gotCGID = %v, want %v", gotCGID, tt.wantCGID)
}
})
}
}
Expand Down
1 change: 1 addition & 0 deletions file/codegen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ func main() {
schema.Definitions["FPlugin"].Properties["consumer"] = stringType
schema.Definitions["FPlugin"].Properties["service"] = stringType
schema.Definitions["FPlugin"].Properties["route"] = stringType
schema.Definitions["FPlugin"].Properties["consumer_group"] = stringType

schema.Definitions["FService"].Properties["client_certificate"] = stringType

Expand Down
4 changes: 3 additions & 1 deletion file/kong_json_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,6 @@
},
"groups": {
"items": {
"$schema": "http://json-schema.org/draft-04/schema#",
"$ref": "#/definitions/ConsumerGroup"
},
"type": "array"
Expand Down Expand Up @@ -600,6 +599,9 @@
"consumer": {
"type": "string"
},
"consumer_group": {
"type": "string"
},
"created_at": {
"type": "integer"
},
Expand Down
38 changes: 25 additions & 13 deletions file/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,19 +321,20 @@ type FPlugin struct {
// foo is a shadow type of Plugin.
// It is used for custom marshalling of plugin.
type foo struct {
CreatedAt *int `json:"created_at,omitempty" yaml:"created_at,omitempty"`
ID *string `json:"id,omitempty" yaml:"id,omitempty"`
Name *string `json:"name,omitempty" yaml:"name,omitempty"`
InstanceName *string `json:"instance_name,omitempty" yaml:"instance_name,omitempty"`
Config kong.Configuration `json:"config,omitempty" yaml:"config,omitempty"`
Service string `json:"service,omitempty" yaml:",omitempty"`
Consumer string `json:"consumer,omitempty" yaml:",omitempty"`
Route string `json:"route,omitempty" yaml:",omitempty"`
Enabled *bool `json:"enabled,omitempty" yaml:"enabled,omitempty"`
RunOn *string `json:"run_on,omitempty" yaml:"run_on,omitempty"`
Ordering *kong.PluginOrdering `json:"ordering,omitempty" yaml:"ordering,omitempty"`
Protocols []*string `json:"protocols,omitempty" yaml:"protocols,omitempty"`
Tags []*string `json:"tags,omitempty" yaml:"tags,omitempty"`
CreatedAt *int `json:"created_at,omitempty" yaml:"created_at,omitempty"`
ID *string `json:"id,omitempty" yaml:"id,omitempty"`
Name *string `json:"name,omitempty" yaml:"name,omitempty"`
InstanceName *string `json:"instance_name,omitempty" yaml:"instance_name,omitempty"`
Config kong.Configuration `json:"config,omitempty" yaml:"config,omitempty"`
Service string `json:"service,omitempty" yaml:",omitempty"`
Consumer string `json:"consumer,omitempty" yaml:",omitempty"`
ConsumerGroup string `json:"consumer_group,omitempty" yaml:",omitempty"`
Route string `json:"route,omitempty" yaml:",omitempty"`
Enabled *bool `json:"enabled,omitempty" yaml:"enabled,omitempty"`
RunOn *string `json:"run_on,omitempty" yaml:"run_on,omitempty"`
Ordering *kong.PluginOrdering `json:"ordering,omitempty" yaml:"ordering,omitempty"`
Protocols []*string `json:"protocols,omitempty" yaml:"protocols,omitempty"`
Tags []*string `json:"tags,omitempty" yaml:"tags,omitempty"`

ConfigSource *string `json:"_config,omitempty" yaml:"_config,omitempty"`
}
Expand Down Expand Up @@ -379,6 +380,9 @@ func copyToFoo(p FPlugin) foo {
if p.Plugin.Service != nil {
f.Service = *p.Plugin.Service.ID
}
if p.Plugin.ConsumerGroup != nil {
f.ConsumerGroup = *p.Plugin.ConsumerGroup.ID
}
return f
}

Expand Down Expand Up @@ -428,6 +432,11 @@ func copyFromFoo(f foo, p *FPlugin) {
ID: kong.String(f.Service),
}
}
if f.ConsumerGroup != "" {
p.ConsumerGroup = &kong.ConsumerGroup{
ID: kong.String(f.ConsumerGroup),
}
}
}

// MarshalYAML is a custom marshal method to handle
Expand Down Expand Up @@ -480,6 +489,9 @@ func (p FPlugin) sortKey() string {
if p.Service != nil {
key += *p.Service.ID
}
if p.ConsumerGroup != nil {
key += *p.ConsumerGroup.ID
}
return key
}
if p.ID != nil {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require (
github.com/hashicorp/go-retryablehttp v0.7.4
github.com/hexops/gotextdiff v1.0.3
github.com/imdario/mergo v0.3.16
github.com/kong/go-kong v0.44.0
github.com/kong/go-kong v0.45.1-0.20230707124609-5236d86ec5d6
github.com/mitchellh/go-homedir v1.1.0
github.com/shirou/gopsutil/v3 v3.23.6
github.com/spf13/cobra v1.7.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHm
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kong/go-kong v0.44.0 h1:1x3w/TYdJjIZ6c1j9HiYP8755c923XN2O6j3kEaUkTA=
github.com/kong/go-kong v0.44.0/go.mod h1:41Sot1N/n8UHBp+gE/6nOw3vuzoHbhMSyU/zOS7VzPE=
github.com/kong/go-kong v0.45.1-0.20230707124609-5236d86ec5d6 h1:VFPUX0r0dW+lEPJ0ytTQcY4WD2d+HMcW/lPXFFWyo9w=
github.com/kong/go-kong v0.45.1-0.20230707124609-5236d86ec5d6/go.mod h1:41Sot1N/n8UHBp+gE/6nOw3vuzoHbhMSyU/zOS7VzPE=
github.com/kong/semver/v4 v4.0.1 h1:DIcNR8W3gfx0KabFBADPalxxsp+q/5COwIFkkhrFQ2Y=
github.com/kong/semver/v4 v4.0.1/go.mod h1:LImQ0oT15pJvSns/hs2laLca2zcYoHu5EsSNY0J6/QA=
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
Expand Down
21 changes: 21 additions & 0 deletions state/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ func ensureConsumer(kongState *KongState, consumerID string) (bool, *kong.Consum
return true, utils.GetConsumerReference(c.Consumer), nil
}

func ensureConsumerGroup(kongState *KongState, consumerGroupID string) (bool, *kong.ConsumerGroup, error) {
c, err := kongState.ConsumerGroups.Get(consumerGroupID)
if err != nil {
if errors.Is(err, ErrNotFound) {
return false, nil, nil
}
return false, nil, fmt.Errorf("looking up consumer-group %q: %w", consumerGroupID, err)

}
return true, utils.GetConsumerGroupReference(c.ConsumerGroup), nil
}

func buildKong(kongState *KongState, raw *utils.KongRawState) error {
for _, s := range raw.Services {
err := kongState.Services.Add(Service{Service: *s})
Expand Down Expand Up @@ -282,6 +294,15 @@ func buildKong(kongState *KongState, raw *utils.KongRawState) error {
p.Consumer = c
}
}
if p.ConsumerGroup != nil && !utils.Empty(p.ConsumerGroup.ID) {
ok, cg, err := ensureConsumerGroup(kongState, *p.ConsumerGroup.ID)
if err != nil {
return err
}
if ok {
p.ConsumerGroup = cg
}
}
err := kongState.Plugins.Add(Plugin{Plugin: *p})
if err != nil {
return fmt.Errorf("inserting plugins into state: %w", err)
Expand Down
Loading

0 comments on commit cf552c4

Please sign in to comment.