-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ReloadDeclarativeRawConfig() for sending declarative config (…
- Loading branch information
Showing
7 changed files
with
300 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
//nolint:lll | ||
package kong | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
) | ||
|
||
// AbstractConfigService handles Config in Kong. | ||
type AbstractConfigService interface { | ||
// 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 | ||
ReloadDeclarativeRawConfig(ctx context.Context, config io.Reader, checkHash bool) error | ||
} | ||
|
||
// ConfigService handles Config in Kong. | ||
type ConfigService service | ||
|
||
// 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 | ||
func (c *ConfigService) ReloadDeclarativeRawConfig( | ||
ctx context.Context, | ||
config io.Reader, | ||
checkHash bool, | ||
) error { | ||
type sendConfigParams struct { | ||
CheckHash int `url:"check_hash"` | ||
} | ||
var checkHashI int | ||
if checkHash { | ||
checkHashI = 1 | ||
} | ||
req, err := c.client.NewRequest("POST", "/config", sendConfigParams{CheckHash: checkHashI}, config) | ||
if err != nil { | ||
return fmt.Errorf("creating new HTTP request for /config: %w", err) | ||
} | ||
|
||
resp, err := c.client.DoRAW(ctx, req) | ||
if err != nil { | ||
return fmt.Errorf("failed posting new config to /config: %w", err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode < 200 || resp.StatusCode >= 400 { | ||
b, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return fmt.Errorf( | ||
"failed posting new config to /config: got status code %d (and failed to read the response body): %w", | ||
resp.StatusCode, err, | ||
) | ||
} | ||
|
||
return fmt.Errorf( | ||
"failed posting new config to /config: got status code %d, body: %s", | ||
resp.StatusCode, b, | ||
) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package kong | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestConfigService(t *testing.T) { | ||
RunWhenDBMode(t, "off") | ||
|
||
tests := []struct { | ||
name string | ||
config Configuration | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "basic config works", | ||
config: Configuration{ | ||
"_format_version": "1.1", | ||
"services": []Configuration{ | ||
{ | ||
"host": "mockbin.com", | ||
"port": 443, | ||
"protocol": "https", | ||
"routes": []Configuration{ | ||
{"paths": []string{"/"}}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "missing _format_version fails", | ||
config: Configuration{ | ||
"services": []Configuration{ | ||
{ | ||
"host": "mockbin.com", | ||
"port": 443, | ||
"protocol": "https", | ||
"routes": []Configuration{ | ||
{"paths": []string{"/"}}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "invalid config fails", | ||
config: Configuration{ | ||
"dummy_key": []Configuration{ | ||
{ | ||
"host": "mockbin.com", | ||
"port": 443, | ||
"protocol": "https", | ||
}, | ||
}, | ||
}, | ||
wantErr: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
client, err := NewTestClient(nil, nil) | ||
require.NoError(t, err) | ||
require.NotNil(t, client) | ||
|
||
tt := tt | ||
t.Run("with_schema/"+tt.name, func(t *testing.T) { | ||
ctx := context.Background() | ||
b, err := json.Marshal(tt.config) | ||
require.NoError(t, err) | ||
|
||
if err := client.Configs.ReloadDeclarativeRawConfig(ctx, bytes.NewBuffer(b), true); (err != nil) != tt.wantErr { | ||
t.Errorf("Client.SendConfig() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package kong | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNewRequestBody(t *testing.T) { | ||
t.Run("body can be string", func(t *testing.T) { | ||
cl, err := NewClient(nil, nil) | ||
require.NoError(t, err) | ||
|
||
body := `{"_format_version":"1.1","services":[{"host":"example.com","name":"foo"}]}` | ||
|
||
req, err := cl.NewRequest("POST", "/", nil, body) | ||
require.NoError(t, err) | ||
|
||
b, err := io.ReadAll(req.Body) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, | ||
`{"_format_version":"1.1","services":[{"host":"example.com","name":"foo"}]}`, | ||
string(b), | ||
) | ||
}) | ||
|
||
t.Run("body can be []byte", func(t *testing.T) { | ||
cl, err := NewClient(nil, nil) | ||
require.NoError(t, err) | ||
|
||
body := []byte(`{"_format_version":"1.1","services":[{"host":"example.com","name":"foo"}]}`) | ||
|
||
req, err := cl.NewRequest("POST", "/", nil, body) | ||
require.NoError(t, err) | ||
|
||
b, err := io.ReadAll(req.Body) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, | ||
`{"_format_version":"1.1","services":[{"host":"example.com","name":"foo"}]}`, | ||
string(b), | ||
) | ||
}) | ||
|
||
t.Run("body can be a bytes.Buffer", func(t *testing.T) { | ||
cl, err := NewClient(nil, nil) | ||
require.NoError(t, err) | ||
|
||
body := bytes.NewBufferString(`{"_format_version":"1.1","services":[{"host":"example.com","name":"foo"}]}`) | ||
|
||
req, err := cl.NewRequest("POST", "/", nil, body) | ||
require.NoError(t, err) | ||
|
||
b, err := io.ReadAll(req.Body) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, | ||
`{"_format_version":"1.1","services":[{"host":"example.com","name":"foo"}]}`, | ||
string(b), | ||
) | ||
}) | ||
|
||
t.Run("body can be a map", func(t *testing.T) { | ||
cl, err := NewClient(nil, nil) | ||
require.NoError(t, err) | ||
|
||
body := map[string]any{ | ||
"_format_version": "1.1", | ||
"services": []map[string]any{ | ||
{ | ||
"host": "example.com", | ||
"name": "foo", | ||
}, | ||
}, | ||
} | ||
|
||
req, err := cl.NewRequest("POST", "/", nil, body) | ||
require.NoError(t, err) | ||
|
||
b, err := io.ReadAll(req.Body) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, | ||
`{"_format_version":"1.1","services":[{"host":"example.com","name":"foo"}]}`, | ||
string(b), | ||
) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters