Skip to content

Commit

Permalink
chore: refactor declarative config functionality
Browse files Browse the repository at this point in the history
Remove the config service and wrap its functionality into the Kong
client.
  • Loading branch information
rainest committed Feb 2, 2023
1 parent 09230d0 commit 2f15d71
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 66 deletions.
11 changes: 8 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,14 @@

> Release date: 2023/02/03
- **Breaking change** to `ConfigService.ReloadDeclarativeRawConfig()`. Its
response signature is now `([]byte, error)`. The byte slice is the config
response body. The error is unchanged.
- **Breaking change:** the `ConfigService` is now directly embedded in the
`kong.Client`. Configurations are collections of entities, not entities
themselves, so they do not fit with other go-kong services.
- **Breaking change:** `ReloadDeclarativeRawConfig()` (formerly part of
`ConfigService`, now part of `kong.Client`) now has the response signature
`([]byte, error)` instead of `error`. The byte slice is the config response
body. The error is unchanged.
[#273](https://github.com/Kong/go-kong/pull/273)

## [v0.36.0]

Expand Down
43 changes: 42 additions & 1 deletion kong/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ type Client struct {
workspace string // Do not access directly. Use Workspace()/SetWorkspace().
workspaceLock sync.RWMutex // Synchronizes access to workspace.
common service
Configs AbstractConfigService
ConsumerGroupConsumers AbstractConsumerGroupConsumerService
ConsumerGroups AbstractConsumerGroupService
Consumers AbstractConsumerService
Expand Down Expand Up @@ -378,3 +377,45 @@ func (c *Client) RootJSON(ctx context.Context) ([]byte, error) {
func (c *Client) BaseRootURL() string {
return c.baseRootURL
}

// ReloadDeclarativeRawConfig sends out the specified config to configured Admin
// API endpoint using the provided reader which should contain the JSON
// serialized body that adheres to the configuration format specified at:
// https://docs.konghq.com/gateway/latest/production/deployment-topologies/db-less-and-declarative-config/#declarative-configuration-format
// It returns the response body and an error, if it encounters any.
func (c *Client) ReloadDeclarativeRawConfig(
ctx context.Context,
config io.Reader,
checkHash bool,
) ([]byte, error) {
type sendConfigParams struct {
CheckHash int `url:"check_hash"`
}
var checkHashI int
if checkHash {
checkHashI = 1
}
req, err := c.NewRequest("POST", "/config", sendConfigParams{CheckHash: checkHashI}, config)
if err != nil {
return []byte{}, fmt.Errorf("creating new HTTP request for /config: %w", err)
}

resp, err := c.DoRAW(ctx, req)
if err != nil {
return []byte{}, fmt.Errorf("failed posting new config to /config: %w", err)
}
defer resp.Body.Close()

var b []byte
if resp.StatusCode < 200 || resp.StatusCode >= 400 {
b, err = io.ReadAll(resp.Body)
if err != nil {
return nil,
fmt.Errorf(`failed posting new config to /config: got status code %d
(and failed to read the response body): %w`,
resp.StatusCode, err)
}
}

return b, nil
}
62 changes: 0 additions & 62 deletions kong/config_service.go

This file was deleted.

0 comments on commit 2f15d71

Please sign in to comment.