From 4614a54806b6c3649b7aa8a788de672a1b871fb3 Mon Sep 17 00:00:00 2001 From: Antoine Jacquemin Date: Mon, 6 Nov 2023 20:23:11 +0100 Subject: [PATCH 01/15] add share tag feature --- file/builder.go | 13 ++++++ file/kong_json_schema.json | 3 ++ file/types.go | 8 ++++ tests/integration/sync_test.go | 80 ++++++++++++++++++++++++++++++++++ 4 files changed, 104 insertions(+) diff --git a/file/builder.go b/file/builder.go index f70cc7956..732f5d9c5 100644 --- a/file/builder.go +++ b/file/builder.go @@ -10,6 +10,7 @@ import ( "github.com/kong/deck/state" "github.com/kong/deck/utils" "github.com/kong/go-kong/kong" + "github.com/kong/deck/dump" ) const ratelimitingAdvancedPluginName = "rate-limiting-advanced" @@ -946,6 +947,18 @@ func (b *stateBuilder) plugins() { var plugins []FPlugin for _, p := range b.targetContent.Plugins { p := p + var sharedEntities bool + if p.SharedTag != nil && sharedEntities != true { + consumersGlobal, err := dump.GetAllConsumers(b.ctx, b.client, []string{*p.SharedTag}) + for _, c := range consumersGlobal { + b.intermediate.Consumers.Add(state.Consumer{Consumer: *c}) + } + if err != nil { + fmt.Printf("Error retrieving global consumers: %w", err) + } + // add future logic for global entities + sharedEntities = true + } if p.Consumer != nil && !utils.Empty(p.Consumer.ID) { c, err := b.intermediate.Consumers.GetByIDOrUsername(*p.Consumer.ID) if errors.Is(err, state.ErrNotFound) { diff --git a/file/kong_json_schema.json b/file/kong_json_schema.json index 5f9d7b818..a9109a2b0 100644 --- a/file/kong_json_schema.json +++ b/file/kong_json_schema.json @@ -616,6 +616,9 @@ "instance_name": { "type": "string" }, + "shared_tag": { + "type": "string" + }, "name": { "type": "string" }, diff --git a/file/types.go b/file/types.go index d70b60e2a..0c18c9ba2 100644 --- a/file/types.go +++ b/file/types.go @@ -316,6 +316,7 @@ type FPlugin struct { kong.Plugin `yaml:",inline,omitempty"` ConfigSource *string `json:"_config,omitempty" yaml:"_config,omitempty"` + SharedTag *string `json:"shared_tag,omitempty" yaml:"shared_tag,omitempty"` } // foo is a shadow type of Plugin. @@ -325,6 +326,7 @@ type foo struct { 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"` + SharedTag *string `json:"shared_tag,omitempty" yaml:"shared_tag,omitempty"` Config kong.Configuration `json:"config,omitempty" yaml:"config,omitempty"` Service string `json:"service,omitempty" yaml:",omitempty"` Consumer string `json:"consumer,omitempty" yaml:",omitempty"` @@ -350,6 +352,9 @@ func copyToFoo(p FPlugin) foo { if p.InstanceName != nil { f.InstanceName = p.InstanceName } + if p.SharedTag != nil { + f.SharedTag = p.SharedTag + } if p.Enabled != nil { f.Enabled = p.Enabled } @@ -396,6 +401,9 @@ func copyFromFoo(f foo, p *FPlugin) { if f.InstanceName != nil { p.InstanceName = f.InstanceName } + if f.SharedTag != nil { + p.SharedTag = f.SharedTag + } if f.Enabled != nil { p.Enabled = f.Enabled } diff --git a/tests/integration/sync_test.go b/tests/integration/sync_test.go index 5a143ead7..9bb7df5ae 100644 --- a/tests/integration/sync_test.go +++ b/tests/integration/sync_test.go @@ -3477,6 +3477,86 @@ func Test_Sync_PluginInstanceName(t *testing.T) { } } +// test scope: +// - 3.2.0+ +func Test_Sync_PluginSharedTag(t *testing.T) { + // setup stage + client, err := getTestClient() + if err != nil { + t.Fatalf(err.Error()) + } + + tests := []struct { + name string + kongFile string + initialKongFile string + expectedState utils.KongRawState + }{ + { + name: "create a plugin with shared_tag", + kongFile: "testdata/sync/018-plugin-shared_tag/kong-with-shared_tag.yaml", + expectedState: utils.KongRawState{ + Plugins: []*kong.Plugin{ + { + Name: kong.String("request-termination"), + SharedTag: kong.String("share-tag"), + Protocols: []*string{ + kong.String("grpc"), + kong.String("grpcs"), + kong.String("http"), + kong.String("https"), + }, + Enabled: kong.Bool(true), + Config: kong.Configuration{ + "status_code": float64(200), + "echo": false, + "content_type": nil, + "body": nil, + "message": nil, + "trigger": nil, + }, + }, + }, + }, + }, + { + name: "create a plugin without shared_tag", + kongFile: "testdata/sync/018-plugin-shared_tag/kong-without-shared_tag.yaml", + expectedState: utils.KongRawState{ + Plugins: []*kong.Plugin{ + { + Name: kong.String("request-termination"), + Protocols: []*string{ + kong.String("grpc"), + kong.String("grpcs"), + kong.String("http"), + kong.String("https"), + }, + Enabled: kong.Bool(true), + Config: kong.Configuration{ + "status_code": float64(200), + "echo": false, + "content_type": nil, + "body": nil, + "message": nil, + "trigger": nil, + }, + }, + }, + }, + }, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + runWhenKongOrKonnect(t, ">=3.2.0") + setup(t) + + sync(tc.kongFile) + testKongState(t, client, false, tc.expectedState, nil) + }) + } +} + // test scope: // - 3.2.x // - 3.3.x From ccf45227556be0cbf7a13e40e68d5bd99c6f25bc Mon Sep 17 00:00:00 2001 From: Antoine Jacquemin Date: Mon, 6 Nov 2023 22:14:46 +0100 Subject: [PATCH 02/15] edit fix and test --- file/builder.go | 10 +++++++--- tests/integration/sync_test.go | 6 +++--- .../kong-with-instance_name.yaml | 0 .../kong-without-instance_name.yaml | 0 .../028-plugin-shared_tag/kong-with-shared_tag.yaml | 13 +++++++++++++ .../kong-without-shared_tag.yaml | 12 ++++++++++++ 6 files changed, 35 insertions(+), 6 deletions(-) rename tests/integration/testdata/sync/{018-plugin-instance_name => 018-plugin-instance_name copy}/kong-with-instance_name.yaml (100%) rename tests/integration/testdata/sync/{018-plugin-instance_name => 018-plugin-instance_name copy}/kong-without-instance_name.yaml (100%) create mode 100644 tests/integration/testdata/sync/028-plugin-shared_tag/kong-with-shared_tag.yaml create mode 100644 tests/integration/testdata/sync/028-plugin-shared_tag/kong-without-shared_tag.yaml diff --git a/file/builder.go b/file/builder.go index 732f5d9c5..7e3f4a82b 100644 --- a/file/builder.go +++ b/file/builder.go @@ -948,13 +948,17 @@ func (b *stateBuilder) plugins() { for _, p := range b.targetContent.Plugins { p := p var sharedEntities bool - if p.SharedTag != nil && sharedEntities != true { + if p.SharedTag != nil && !sharedEntities { consumersGlobal, err := dump.GetAllConsumers(b.ctx, b.client, []string{*p.SharedTag}) for _, c := range consumersGlobal { - b.intermediate.Consumers.Add(state.Consumer{Consumer: *c}) + err = b.intermediate.Consumers.Add(state.Consumer{Consumer: *c}) + if err != nil { + fmt.Errorf("Error adding global consumer %v: %w", + *c.Username, err) + } } if err != nil { - fmt.Printf("Error retrieving global consumers: %w", err) + fmt.Errorf("Error retrieving global consumers: %w", err) } // add future logic for global entities sharedEntities = true diff --git a/tests/integration/sync_test.go b/tests/integration/sync_test.go index 9bb7df5ae..1577818c2 100644 --- a/tests/integration/sync_test.go +++ b/tests/integration/sync_test.go @@ -3494,12 +3494,12 @@ func Test_Sync_PluginSharedTag(t *testing.T) { }{ { name: "create a plugin with shared_tag", - kongFile: "testdata/sync/018-plugin-shared_tag/kong-with-shared_tag.yaml", + kongFile: "testdata/sync/028-plugin-shared_tag/kong-with-shared_tag.yaml", expectedState: utils.KongRawState{ Plugins: []*kong.Plugin{ { Name: kong.String("request-termination"), - SharedTag: kong.String("share-tag"), + SharedTag: kong.String("shared-tag-example"), Protocols: []*string{ kong.String("grpc"), kong.String("grpcs"), @@ -3521,7 +3521,7 @@ func Test_Sync_PluginSharedTag(t *testing.T) { }, { name: "create a plugin without shared_tag", - kongFile: "testdata/sync/018-plugin-shared_tag/kong-without-shared_tag.yaml", + kongFile: "testdata/sync/028-plugin-shared_tag/kong-without-shared_tag.yaml", expectedState: utils.KongRawState{ Plugins: []*kong.Plugin{ { diff --git a/tests/integration/testdata/sync/018-plugin-instance_name/kong-with-instance_name.yaml b/tests/integration/testdata/sync/018-plugin-instance_name copy/kong-with-instance_name.yaml similarity index 100% rename from tests/integration/testdata/sync/018-plugin-instance_name/kong-with-instance_name.yaml rename to tests/integration/testdata/sync/018-plugin-instance_name copy/kong-with-instance_name.yaml diff --git a/tests/integration/testdata/sync/018-plugin-instance_name/kong-without-instance_name.yaml b/tests/integration/testdata/sync/018-plugin-instance_name copy/kong-without-instance_name.yaml similarity index 100% rename from tests/integration/testdata/sync/018-plugin-instance_name/kong-without-instance_name.yaml rename to tests/integration/testdata/sync/018-plugin-instance_name copy/kong-without-instance_name.yaml diff --git a/tests/integration/testdata/sync/028-plugin-shared_tag/kong-with-shared_tag.yaml b/tests/integration/testdata/sync/028-plugin-shared_tag/kong-with-shared_tag.yaml new file mode 100644 index 000000000..ea6e1bfaf --- /dev/null +++ b/tests/integration/testdata/sync/028-plugin-shared_tag/kong-with-shared_tag.yaml @@ -0,0 +1,13 @@ +_format_version: "3.0" +plugins: +- id: efead952-0a1d-43ec-9794-0ac6abdc7f55 + name: request-termination + shared_tag: shared-tag-example + config: + status_code: 200 + enabled: true + protocols: + - grpc + - grpcs + - http + - https diff --git a/tests/integration/testdata/sync/028-plugin-shared_tag/kong-without-shared_tag.yaml b/tests/integration/testdata/sync/028-plugin-shared_tag/kong-without-shared_tag.yaml new file mode 100644 index 000000000..ca22fb425 --- /dev/null +++ b/tests/integration/testdata/sync/028-plugin-shared_tag/kong-without-shared_tag.yaml @@ -0,0 +1,12 @@ +_format_version: "3.0" +plugins: +- id: efead952-0a1d-43ec-9794-0ac6abdc7f55 + name: request-termination + config: + status_code: 200 + enabled: true + protocols: + - grpc + - grpcs + - http + - https From f4d0404da122419c41e2a8c051a99401a5b67361 Mon Sep 17 00:00:00 2001 From: Antoine Jacquemin Date: Mon, 6 Nov 2023 22:29:55 +0100 Subject: [PATCH 03/15] chage type --- tests/integration/sync_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/sync_test.go b/tests/integration/sync_test.go index 1577818c2..c932e8c6c 100644 --- a/tests/integration/sync_test.go +++ b/tests/integration/sync_test.go @@ -3496,7 +3496,7 @@ func Test_Sync_PluginSharedTag(t *testing.T) { name: "create a plugin with shared_tag", kongFile: "testdata/sync/028-plugin-shared_tag/kong-with-shared_tag.yaml", expectedState: utils.KongRawState{ - Plugins: []*kong.Plugin{ + Plugins: []FPlugin{ { Name: kong.String("request-termination"), SharedTag: kong.String("shared-tag-example"), From a4f5d45d4a4ea24f7b02914ae233b3e41a28d4cd Mon Sep 17 00:00:00 2001 From: Antoine Jacquemin Date: Mon, 6 Nov 2023 22:42:06 +0100 Subject: [PATCH 04/15] chage type --- tests/integration/sync_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/sync_test.go b/tests/integration/sync_test.go index c932e8c6c..1577818c2 100644 --- a/tests/integration/sync_test.go +++ b/tests/integration/sync_test.go @@ -3496,7 +3496,7 @@ func Test_Sync_PluginSharedTag(t *testing.T) { name: "create a plugin with shared_tag", kongFile: "testdata/sync/028-plugin-shared_tag/kong-with-shared_tag.yaml", expectedState: utils.KongRawState{ - Plugins: []FPlugin{ + Plugins: []*kong.Plugin{ { Name: kong.String("request-termination"), SharedTag: kong.String("shared-tag-example"), From 0bf3ed4b5c8da3b06610f2271e8f24ccd6541374 Mon Sep 17 00:00:00 2001 From: Antoine Jacquemin Date: Tue, 7 Nov 2023 03:31:33 +0100 Subject: [PATCH 05/15] fix bool --- file/builder.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/file/builder.go b/file/builder.go index 7e3f4a82b..a1e9d97a3 100644 --- a/file/builder.go +++ b/file/builder.go @@ -945,20 +945,20 @@ func (b *stateBuilder) plugins() { } var plugins []FPlugin + var sharedEntities bool for _, p := range b.targetContent.Plugins { p := p - var sharedEntities bool if p.SharedTag != nil && !sharedEntities { consumersGlobal, err := dump.GetAllConsumers(b.ctx, b.client, []string{*p.SharedTag}) for _, c := range consumersGlobal { err = b.intermediate.Consumers.Add(state.Consumer{Consumer: *c}) if err != nil { - fmt.Errorf("Error adding global consumer %v: %w", + fmt.Println("Error adding global consumer %v: %w", *c.Username, err) } } if err != nil { - fmt.Errorf("Error retrieving global consumers: %w", err) + fmt.Println("Error retrieving global consumers: %w", err) } // add future logic for global entities sharedEntities = true From fe9bf79d7981925294a7512327053b9c96d0593f Mon Sep 17 00:00:00 2001 From: Antoine Jacquemin Date: Tue, 7 Nov 2023 03:32:37 +0100 Subject: [PATCH 06/15] fix bool --- file/builder.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/file/builder.go b/file/builder.go index a1e9d97a3..5cbacd94e 100644 --- a/file/builder.go +++ b/file/builder.go @@ -945,9 +945,9 @@ func (b *stateBuilder) plugins() { } var plugins []FPlugin - var sharedEntities bool for _, p := range b.targetContent.Plugins { p := p + var sharedEntities bool if p.SharedTag != nil && !sharedEntities { consumersGlobal, err := dump.GetAllConsumers(b.ctx, b.client, []string{*p.SharedTag}) for _, c := range consumersGlobal { From e25717d28a29ce545989b5df2c8fad4c6d0b7e2d Mon Sep 17 00:00:00 2001 From: Antoine Jacquemin Date: Tue, 7 Nov 2023 12:13:15 +0100 Subject: [PATCH 07/15] better factoring --- file/builder.go | 11 ++- file/types.go | 7 -- tests/integration/sync_test.go | 80 ------------------- .../kong-with-shared_tag.yaml | 13 --- .../kong-without-shared_tag.yaml | 12 --- 5 files changed, 5 insertions(+), 118 deletions(-) delete mode 100644 tests/integration/testdata/sync/028-plugin-shared_tag/kong-with-shared_tag.yaml delete mode 100644 tests/integration/testdata/sync/028-plugin-shared_tag/kong-without-shared_tag.yaml diff --git a/file/builder.go b/file/builder.go index 5cbacd94e..41cfa341c 100644 --- a/file/builder.go +++ b/file/builder.go @@ -947,21 +947,20 @@ func (b *stateBuilder) plugins() { var plugins []FPlugin for _, p := range b.targetContent.Plugins { p := p - var sharedEntities bool - if p.SharedTag != nil && !sharedEntities { + sharedEntities := make(map[string]bool) + if p.SharedTag != nil && !sharedEntities[*p.SharedTag] { consumersGlobal, err := dump.GetAllConsumers(b.ctx, b.client, []string{*p.SharedTag}) for _, c := range consumersGlobal { err = b.intermediate.Consumers.Add(state.Consumer{Consumer: *c}) if err != nil { - fmt.Println("Error adding global consumer %v: %w", - *c.Username, err) + fmt.Printf("Error adding global consumer %v: %v\n", *c.Username, err) } } if err != nil { - fmt.Println("Error retrieving global consumers: %w", err) + fmt.Printf("Error retrieving global consumers: %v\n", err) } // add future logic for global entities - sharedEntities = true + sharedEntities[*p.SharedTag] = true } if p.Consumer != nil && !utils.Empty(p.Consumer.ID) { c, err := b.intermediate.Consumers.GetByIDOrUsername(*p.Consumer.ID) diff --git a/file/types.go b/file/types.go index 0c18c9ba2..23ec4da04 100644 --- a/file/types.go +++ b/file/types.go @@ -326,7 +326,6 @@ type foo struct { 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"` - SharedTag *string `json:"shared_tag,omitempty" yaml:"shared_tag,omitempty"` Config kong.Configuration `json:"config,omitempty" yaml:"config,omitempty"` Service string `json:"service,omitempty" yaml:",omitempty"` Consumer string `json:"consumer,omitempty" yaml:",omitempty"` @@ -352,9 +351,6 @@ func copyToFoo(p FPlugin) foo { if p.InstanceName != nil { f.InstanceName = p.InstanceName } - if p.SharedTag != nil { - f.SharedTag = p.SharedTag - } if p.Enabled != nil { f.Enabled = p.Enabled } @@ -401,9 +397,6 @@ func copyFromFoo(f foo, p *FPlugin) { if f.InstanceName != nil { p.InstanceName = f.InstanceName } - if f.SharedTag != nil { - p.SharedTag = f.SharedTag - } if f.Enabled != nil { p.Enabled = f.Enabled } diff --git a/tests/integration/sync_test.go b/tests/integration/sync_test.go index 1577818c2..5a143ead7 100644 --- a/tests/integration/sync_test.go +++ b/tests/integration/sync_test.go @@ -3477,86 +3477,6 @@ func Test_Sync_PluginInstanceName(t *testing.T) { } } -// test scope: -// - 3.2.0+ -func Test_Sync_PluginSharedTag(t *testing.T) { - // setup stage - client, err := getTestClient() - if err != nil { - t.Fatalf(err.Error()) - } - - tests := []struct { - name string - kongFile string - initialKongFile string - expectedState utils.KongRawState - }{ - { - name: "create a plugin with shared_tag", - kongFile: "testdata/sync/028-plugin-shared_tag/kong-with-shared_tag.yaml", - expectedState: utils.KongRawState{ - Plugins: []*kong.Plugin{ - { - Name: kong.String("request-termination"), - SharedTag: kong.String("shared-tag-example"), - Protocols: []*string{ - kong.String("grpc"), - kong.String("grpcs"), - kong.String("http"), - kong.String("https"), - }, - Enabled: kong.Bool(true), - Config: kong.Configuration{ - "status_code": float64(200), - "echo": false, - "content_type": nil, - "body": nil, - "message": nil, - "trigger": nil, - }, - }, - }, - }, - }, - { - name: "create a plugin without shared_tag", - kongFile: "testdata/sync/028-plugin-shared_tag/kong-without-shared_tag.yaml", - expectedState: utils.KongRawState{ - Plugins: []*kong.Plugin{ - { - Name: kong.String("request-termination"), - Protocols: []*string{ - kong.String("grpc"), - kong.String("grpcs"), - kong.String("http"), - kong.String("https"), - }, - Enabled: kong.Bool(true), - Config: kong.Configuration{ - "status_code": float64(200), - "echo": false, - "content_type": nil, - "body": nil, - "message": nil, - "trigger": nil, - }, - }, - }, - }, - }, - } - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - runWhenKongOrKonnect(t, ">=3.2.0") - setup(t) - - sync(tc.kongFile) - testKongState(t, client, false, tc.expectedState, nil) - }) - } -} - // test scope: // - 3.2.x // - 3.3.x diff --git a/tests/integration/testdata/sync/028-plugin-shared_tag/kong-with-shared_tag.yaml b/tests/integration/testdata/sync/028-plugin-shared_tag/kong-with-shared_tag.yaml deleted file mode 100644 index ea6e1bfaf..000000000 --- a/tests/integration/testdata/sync/028-plugin-shared_tag/kong-with-shared_tag.yaml +++ /dev/null @@ -1,13 +0,0 @@ -_format_version: "3.0" -plugins: -- id: efead952-0a1d-43ec-9794-0ac6abdc7f55 - name: request-termination - shared_tag: shared-tag-example - config: - status_code: 200 - enabled: true - protocols: - - grpc - - grpcs - - http - - https diff --git a/tests/integration/testdata/sync/028-plugin-shared_tag/kong-without-shared_tag.yaml b/tests/integration/testdata/sync/028-plugin-shared_tag/kong-without-shared_tag.yaml deleted file mode 100644 index ca22fb425..000000000 --- a/tests/integration/testdata/sync/028-plugin-shared_tag/kong-without-shared_tag.yaml +++ /dev/null @@ -1,12 +0,0 @@ -_format_version: "3.0" -plugins: -- id: efead952-0a1d-43ec-9794-0ac6abdc7f55 - name: request-termination - config: - status_code: 200 - enabled: true - protocols: - - grpc - - grpcs - - http - - https From 62dbd66e8d9bfda6891f209122b9ecd22b2e00ca Mon Sep 17 00:00:00 2001 From: Antoine Jacquemin Date: Tue, 7 Nov 2023 12:13:56 +0100 Subject: [PATCH 08/15] better factoring --- .../kong-with-instance_name.yaml | 0 .../kong-without-instance_name.yaml | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename tests/integration/testdata/sync/{018-plugin-instance_name copy => 018-plugin-instance_name}/kong-with-instance_name.yaml (100%) rename tests/integration/testdata/sync/{018-plugin-instance_name copy => 018-plugin-instance_name}/kong-without-instance_name.yaml (100%) diff --git a/tests/integration/testdata/sync/018-plugin-instance_name copy/kong-with-instance_name.yaml b/tests/integration/testdata/sync/018-plugin-instance_name/kong-with-instance_name.yaml similarity index 100% rename from tests/integration/testdata/sync/018-plugin-instance_name copy/kong-with-instance_name.yaml rename to tests/integration/testdata/sync/018-plugin-instance_name/kong-with-instance_name.yaml diff --git a/tests/integration/testdata/sync/018-plugin-instance_name copy/kong-without-instance_name.yaml b/tests/integration/testdata/sync/018-plugin-instance_name/kong-without-instance_name.yaml similarity index 100% rename from tests/integration/testdata/sync/018-plugin-instance_name copy/kong-without-instance_name.yaml rename to tests/integration/testdata/sync/018-plugin-instance_name/kong-without-instance_name.yaml From 4866ff4e3ecdf5528fdfaf232f9386a715404a63 Mon Sep 17 00:00:00 2001 From: Antoine Jacquemin Date: Tue, 7 Nov 2023 12:29:46 +0100 Subject: [PATCH 09/15] better factoring --- file/types.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/file/types.go b/file/types.go index 23ec4da04..512fe0e45 100644 --- a/file/types.go +++ b/file/types.go @@ -338,6 +338,7 @@ type foo struct { Tags []*string `json:"tags,omitempty" yaml:"tags,omitempty"` ConfigSource *string `json:"_config,omitempty" yaml:"_config,omitempty"` + SharedTag *string `json:"shared_tag,omitempty" yaml:"shared_tag,omitempty"` } func copyToFoo(p FPlugin) foo { @@ -351,6 +352,9 @@ func copyToFoo(p FPlugin) foo { if p.InstanceName != nil { f.InstanceName = p.InstanceName } + if p.SharedTag != nil { + f.SharedTag = p.SharedTag + } if p.Enabled != nil { f.Enabled = p.Enabled } @@ -397,6 +401,9 @@ func copyFromFoo(f foo, p *FPlugin) { if f.InstanceName != nil { p.InstanceName = f.InstanceName } + if f.SharedTag != nil { + p.SharedTag = f.SharedTag + } if f.Enabled != nil { p.Enabled = f.Enabled } From 6bc437faa3baa67f4179f8cbefc6c77932d88bac Mon Sep 17 00:00:00 2001 From: Antoine Jacquemin Date: Tue, 7 Nov 2023 12:32:14 +0100 Subject: [PATCH 10/15] better factoring --- file/builder.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/file/builder.go b/file/builder.go index 41cfa341c..b54f1f1c8 100644 --- a/file/builder.go +++ b/file/builder.go @@ -945,9 +945,9 @@ func (b *stateBuilder) plugins() { } var plugins []FPlugin + sharedEntities := make(map[string]bool) for _, p := range b.targetContent.Plugins { p := p - sharedEntities := make(map[string]bool) if p.SharedTag != nil && !sharedEntities[*p.SharedTag] { consumersGlobal, err := dump.GetAllConsumers(b.ctx, b.client, []string{*p.SharedTag}) for _, c := range consumersGlobal { From 4bfd236e4307a7adf9ed18d56a22443f335f45e1 Mon Sep 17 00:00:00 2001 From: Antoine Jacquemin Date: Tue, 7 Nov 2023 17:11:12 +0100 Subject: [PATCH 11/15] f --- file/builder.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/file/builder.go b/file/builder.go index b54f1f1c8..b538770fe 100644 --- a/file/builder.go +++ b/file/builder.go @@ -6,11 +6,11 @@ import ( "fmt" "github.com/blang/semver/v4" + "github.com/kong/deck/dump" "github.com/kong/deck/konnect" "github.com/kong/deck/state" "github.com/kong/deck/utils" "github.com/kong/go-kong/kong" - "github.com/kong/deck/dump" ) const ratelimitingAdvancedPluginName = "rate-limiting-advanced" From 90be46b02683d568085d8cae4547fd3008be0f3e Mon Sep 17 00:00:00 2001 From: Antoine Jacquemin Date: Tue, 7 Nov 2023 17:15:37 +0100 Subject: [PATCH 12/15] f --- file/kong_json_schema.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/file/kong_json_schema.json b/file/kong_json_schema.json index a9109a2b0..6505890e7 100644 --- a/file/kong_json_schema.json +++ b/file/kong_json_schema.json @@ -616,9 +616,6 @@ "instance_name": { "type": "string" }, - "shared_tag": { - "type": "string" - }, "name": { "type": "string" }, @@ -641,6 +638,9 @@ "service": { "type": "string" }, + "shared_tag": { + "type": "string" + }, "tags": { "items": { "type": "string" From 242fb2aba1ca77fdd8866984609061cd83622372 Mon Sep 17 00:00:00 2001 From: Antoine Jacquemin Date: Wed, 8 Nov 2023 13:13:32 +0100 Subject: [PATCH 13/15] refactor with consumer name --- file/builder.go | 32 ++++++++++++++++++++------------ file/kong_json_schema.json | 3 --- file/types.go | 8 -------- 3 files changed, 20 insertions(+), 23 deletions(-) diff --git a/file/builder.go b/file/builder.go index b538770fe..a5a228739 100644 --- a/file/builder.go +++ b/file/builder.go @@ -3,6 +3,7 @@ package file import ( "context" "errors" + "regexp" "fmt" "github.com/blang/semver/v4" @@ -945,24 +946,31 @@ func (b *stateBuilder) plugins() { } var plugins []FPlugin + var SharedUser, SharedTag string sharedEntities := make(map[string]bool) for _, p := range b.targetContent.Plugins { p := p - if p.SharedTag != nil && !sharedEntities[*p.SharedTag] { - consumersGlobal, err := dump.GetAllConsumers(b.ctx, b.client, []string{*p.SharedTag}) - for _, c := range consumersGlobal { - err = b.intermediate.Consumers.Add(state.Consumer{Consumer: *c}) + if p.Consumer != nil && !utils.Empty(p.Consumer.ID) { + shareTagRegex := regexp.MustCompile(`\/\/([^/]+)/(.+)$`) + matches := shareTagRegex.FindStringSubmatch(*p.Consumer.ID) + if len(matches) >= 2 { + SharedTag, SharedUser = matches[1], matches[2] + *p.Consumer.ID = SharedUser + } + if SharedTag != "" && !sharedEntities[SharedTag] { + consumersGlobal, err := dump.GetAllConsumers(b.ctx, b.client, []string{SharedTag}) if err != nil { - fmt.Printf("Error adding global consumer %v: %v\n", *c.Username, err) + fmt.Printf("Error retrieving global consumers: %v\n", err) } + for _, c := range consumersGlobal { + err = b.intermediate.Consumers.Add(state.Consumer{Consumer: *c}) + if err != nil { + fmt.Printf("Error adding global consumer %v: %v\n", *c.Username, err) + } + } + // add future logic for global entities + sharedEntities[SharedTag] = true } - if err != nil { - fmt.Printf("Error retrieving global consumers: %v\n", err) - } - // add future logic for global entities - sharedEntities[*p.SharedTag] = true - } - if p.Consumer != nil && !utils.Empty(p.Consumer.ID) { c, err := b.intermediate.Consumers.GetByIDOrUsername(*p.Consumer.ID) if errors.Is(err, state.ErrNotFound) { b.err = fmt.Errorf("consumer %v for plugin %v: %w", diff --git a/file/kong_json_schema.json b/file/kong_json_schema.json index 6505890e7..5f9d7b818 100644 --- a/file/kong_json_schema.json +++ b/file/kong_json_schema.json @@ -638,9 +638,6 @@ "service": { "type": "string" }, - "shared_tag": { - "type": "string" - }, "tags": { "items": { "type": "string" diff --git a/file/types.go b/file/types.go index 512fe0e45..d70b60e2a 100644 --- a/file/types.go +++ b/file/types.go @@ -316,7 +316,6 @@ type FPlugin struct { kong.Plugin `yaml:",inline,omitempty"` ConfigSource *string `json:"_config,omitempty" yaml:"_config,omitempty"` - SharedTag *string `json:"shared_tag,omitempty" yaml:"shared_tag,omitempty"` } // foo is a shadow type of Plugin. @@ -338,7 +337,6 @@ type foo struct { Tags []*string `json:"tags,omitempty" yaml:"tags,omitempty"` ConfigSource *string `json:"_config,omitempty" yaml:"_config,omitempty"` - SharedTag *string `json:"shared_tag,omitempty" yaml:"shared_tag,omitempty"` } func copyToFoo(p FPlugin) foo { @@ -352,9 +350,6 @@ func copyToFoo(p FPlugin) foo { if p.InstanceName != nil { f.InstanceName = p.InstanceName } - if p.SharedTag != nil { - f.SharedTag = p.SharedTag - } if p.Enabled != nil { f.Enabled = p.Enabled } @@ -401,9 +396,6 @@ func copyFromFoo(f foo, p *FPlugin) { if f.InstanceName != nil { p.InstanceName = f.InstanceName } - if f.SharedTag != nil { - p.SharedTag = f.SharedTag - } if f.Enabled != nil { p.Enabled = f.Enabled } From e81ed7ffe6b1226a35638769d39d2711479c0b45 Mon Sep 17 00:00:00 2001 From: Antoine Jacquemin Date: Wed, 8 Nov 2023 13:20:38 +0100 Subject: [PATCH 14/15] fix lint --- file/builder.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/file/builder.go b/file/builder.go index a5a228739..d1ebe5bc0 100644 --- a/file/builder.go +++ b/file/builder.go @@ -3,8 +3,9 @@ package file import ( "context" "errors" - "regexp" "fmt" + "fmt" + "regexp" "github.com/blang/semver/v4" "github.com/kong/deck/dump" From a338b0612b582296e5df36b960ecc0779ec20579 Mon Sep 17 00:00:00 2001 From: Antoine Jacquemin Date: Wed, 8 Nov 2023 13:21:22 +0100 Subject: [PATCH 15/15] fix lint --- file/builder.go | 1 - 1 file changed, 1 deletion(-) diff --git a/file/builder.go b/file/builder.go index d1ebe5bc0..9940f17d0 100644 --- a/file/builder.go +++ b/file/builder.go @@ -4,7 +4,6 @@ import ( "context" "errors" "fmt" - "fmt" "regexp" "github.com/blang/semver/v4"