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

feat: add GetFullSchema and FillDefaults methods to plugins #114

Merged
merged 8 commits into from
Jan 21, 2022
Merged

Conversation

GGabriele
Copy link
Collaborator

GetFullSchema: returns full schema for plugins (/schemas/plugins/<ID>)
FillDefaults: given a schema and a config object, fills all defaults

Based on the discussion happening in here, this PR is introducing 2 new methods for Plugin (along few other helper functions, mostly taken from KIC plus few changes).
These can be leveraged by deck and KIC to fill defaults values into plugins from their schemas.

GetFullSchema: returns full schema for plugins (/schemas/plugins/<ID>)
FillDefaults: given a schema and a config object, fills all defaults
@GGabriele GGabriele requested a review from a team as a code owner January 18, 2022 10:27
@CLAassistant
Copy link

CLAassistant commented Jan 18, 2022

CLA assistant check
All committers have signed the CLA.

kong/plugin_service.go Show resolved Hide resolved
}

// PluginService handles Plugins in Kong.
type PluginService service

// GetSchema retrieves the schema of a plugin
// GetFullSchema retrieves the full schema of a plugin
func (s *PluginService) GetFullSchema(ctx context.Context,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func (s *PluginService) GetFullSchema(ctx context.Context,
func (s *PluginService) GetPluginSchema(ctx context.Context,

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO this should remain as-is: it's more descriptive of the difference between this and GetSchema, and we already know this is plugin stuff since it's part of the plugin service.

}

// FillDefaults ingests plugin's defaults from its schema
func (s *PluginService) FillDefaults(ctx context.Context,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't actually need to be on the PluginService since this function doesn't interact with the Admin API.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved this under kong/utils.go

Copy link
Contributor

@rainest rainest left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What were the differences between this and the original KIC implementation? It looks like the main thing is that you get back a new plugin instance with the default config (which is fine), rather than getting a config and shoving that into your existing plugin, but I may be missing something since a lot of the code is similar.

@GGabriele
Copy link
Collaborator Author

The main difference with the KIC implementation is that KIC was filling only Config defaults, while here we are filling both Config and Protocols, as well as setting Enabled to true as default. This is done by leveraging the "newer" /schemas/plugins/$ID endpoint, so most of the code differences in the fillConfigRecord function are due to handling a slightly different output from the one used in KIC.

@codecov-commenter
Copy link

codecov-commenter commented Jan 19, 2022

Codecov Report

Merging #114 (36bf996) into main (8d08c5d) will increase coverage by 0.96%.
The diff coverage is 87.95%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #114      +/-   ##
==========================================
+ Coverage   43.49%   44.46%   +0.96%     
==========================================
  Files          42       42              
  Lines        3722     3805      +83     
==========================================
+ Hits         1619     1692      +73     
- Misses       1786     1793       +7     
- Partials      317      320       +3     
Flag Coverage Δ
2.0.5 39.13% <87.95%> (+1.08%) ⬆️
2.1.4 38.94% <87.95%> (+1.09%) ⬆️
2.2.2 38.94% <87.95%> (+1.09%) ⬆️
2.3.3 38.94% <87.95%> (+1.09%) ⬆️
2.4.0 38.94% <87.95%> (+1.09%) ⬆️
community 39.13% <87.95%> (+1.08%) ⬆️
enterprise 44.15% <87.95%> (+0.97%) ⬆️
enterprise-1.5.0.11 44.15% <87.95%> (+0.97%) ⬆️
enterprise-2.1.4.6 43.02% <87.95%> (+1.00%) ⬆️
enterprise-2.2.1.3 43.02% <87.95%> (?)
enterprise-2.3.3.2 43.02% <87.95%> (?)
integration 44.46% <87.95%> (+0.96%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
kong/plugin_service.go 56.79% <60.00%> (+0.32%) ⬆️
kong/utils.go 86.00% <94.11%> (+6.73%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8d08c5d...36bf996. Read the comment docs.

@rainest rainest self-requested a review January 20, 2022 17:59
@@ -29,14 +29,39 @@ type AbstractPluginService interface {
ListAllForRoute(ctx context.Context, routeID *string) ([]*Plugin, error)
// Validate validates a Plugin against its schema
Validate(ctx context.Context, plugin *Plugin) (bool, string, error)
// GetSchema retrieves the schema of a plugin
// GetSchema retrieves the config schema of a plugin
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// GetSchema retrieves the config schema of a plugin
// GetSchema retrieves the config schema of a plugin.

// GetSchema retrieves the schema of a plugin
// GetSchema retrieves the config schema of a plugin
//
// Deprecated: Use GetFullSchema instead
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Deprecated: Use GetFullSchema instead
// Deprecated: Use GetFullSchema instead.

GetSchema(ctx context.Context, pluginName *string) (map[string]interface{}, error)
// GetFullSchema retrieves the full schema of a plugin
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// GetFullSchema retrieves the full schema of a plugin
// GetFullSchema retrieves the full schema of a plugin.
// This makes the use of `/schemas` endpoint in Kong.

}

// PluginService handles Plugins in Kong.
type PluginService service

// GetSchema retrieves the schema of a plugin
// GetFullSchema retrieves the full schema of a plugin
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// GetFullSchema retrieves the full schema of a plugin
// GetFullSchema retrieves the full schema of a plugin.

expected *Plugin
}{
{
name: "no_config_no_protocols",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove all _.

go.sum Outdated
@@ -56,6 +56,8 @@ github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Run go mod tidy

kong/utils.go Outdated
Comment on lines 152 to 155
for key, value := range field.Map() {
if key != "protocols" {
continue
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Map() returns a map[string]Result:
https://pkg.go.dev/github.com/tidwall/gjson?utm_source=godoc#Result.Map

Please remove this loop and instead use field.Map()["protocols"] to do the same thing.
Linear searches are best avoided, especially when an index is already available.

Copy link
Member

@hbagdi hbagdi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add an integration test for the GetFulLSchema function that has been added. The test must ensure that this function works against OSS as well as Enterprise.

@GGabriele
Copy link
Collaborator Author

Add an integration test for the GetFulLSchema function that has been added. The test must ensure that this function works against OSS as well as Enterprise.

isn't it already tested on both as part of this here?

@hbagdi
Copy link
Member

hbagdi commented Jan 20, 2022

isn't it already tested on both as part of this here?

Oh boy.
That test is for another function. If that function is removed, we would be removing the call to the GetFullSchema function.

Please add another test that only tests the GetFullSchema method.
Assert the response code, and check for a few elements in the body to make sure the response is as expected.
Also, add a negative test for the "noexists" plugin.

@GGabriele
Copy link
Collaborator Author

Wanted to add a testcase to check the whole schema response, but couldn't find a plugin whose schema remained fully consistent across the various EE-OSS versions, so I reverted to just test errors and basic response structure. Let me know if that's fine.

@hbagdi hbagdi merged commit bfe90b4 into main Jan 21, 2022
@hbagdi hbagdi deleted the DECK-8 branch January 21, 2022 22:00
}

// FillPluginsDefaults ingests plugin's defaults from its schema
func FillPluginsDefaults(plugin *Plugin, schema map[string]interface{}) (*Plugin, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GGabriele I made a mistake in the review.
This method interface is misleading and wrong.
The code takes in a plugin struct and then mutates it.
But the method signature also returns a plugin resource.

Either of the following are fine (but together not):

  • Return only an error, and document that the plugin is filled and mutated
  • Inside the function, make a copy of the input plugin, mutate the copy and return it

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doh, my bad, sorry :(

#116

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants