Backport of Default `ProxyType` for builtin extensions into release/1.16.x (#17667)

* backport of commit 131d234bda

* backport of commit 1adc48734d

---------

Co-authored-by: Chris Thain <chris.m.thain@gmail.com>
pull/17669/head
hc-github-team-consul-core 2023-06-12 14:13:29 -04:00 committed by GitHub
parent 89c1d39c0c
commit 593403fa3c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 45 additions and 3 deletions

View File

@ -60,6 +60,9 @@ func (r *ratelimit) fromArguments(args map[string]interface{}) error {
if err := mapstructure.Decode(args, r); err != nil { if err := mapstructure.Decode(args, r); err != nil {
return fmt.Errorf("error decoding extension arguments: %v", err) return fmt.Errorf("error decoding extension arguments: %v", err)
} }
if r.ProxyType == "" {
r.ProxyType = string(api.ServiceKindConnectProxy)
}
return r.validate() return r.validate()
} }
@ -188,7 +191,7 @@ func (r ratelimit) PatchFilter(p extensioncommon.FilterPayload) (*envoy_listener
} }
func validateProxyType(t string) error { func validateProxyType(t string) error {
if t != "connect-proxy" { if t != string(api.ServiceKindConnectProxy) {
return fmt.Errorf("unexpected ProxyType %q", t) return fmt.Errorf("unexpected ProxyType %q", t)
} }

View File

@ -111,6 +111,30 @@ func TestConstructor(t *testing.T) {
expectedErrMsg: "cannot parse 'FilterEnforced', -1 overflows uint", expectedErrMsg: "cannot parse 'FilterEnforced', -1 overflows uint",
ok: false, ok: false,
}, },
"invalid proxy type": {
arguments: makeArguments(map[string]interface{}{
"ProxyType": "invalid",
"FillInterval": 30,
"MaxTokens": 20,
"TokensPerFill": 5,
}),
expectedErrMsg: `unexpected ProxyType "invalid"`,
ok: false,
},
"default proxy type": {
arguments: makeArguments(map[string]interface{}{
"FillInterval": 30,
"MaxTokens": 20,
"TokensPerFill": 5,
}),
expected: ratelimit{
ProxyType: "connect-proxy",
MaxTokens: intPointer(20),
FillInterval: intPointer(30),
TokensPerFill: intPointer(5),
},
ok: true,
},
"valid everything": { "valid everything": {
arguments: makeArguments(map[string]interface{}{ arguments: makeArguments(map[string]interface{}{
"ProxyType": "connect-proxy", "ProxyType": "connect-proxy",

View File

@ -45,6 +45,9 @@ func (l *lua) fromArguments(args map[string]interface{}) error {
if err := mapstructure.Decode(args, l); err != nil { if err := mapstructure.Decode(args, l); err != nil {
return fmt.Errorf("error decoding extension arguments: %v", err) return fmt.Errorf("error decoding extension arguments: %v", err)
} }
if l.ProxyType == "" {
l.ProxyType = string(api.ServiceKindConnectProxy)
}
return l.validate() return l.validate()
} }
@ -53,7 +56,7 @@ func (l *lua) validate() error {
if l.Script == "" { if l.Script == "" {
resultErr = multierror.Append(resultErr, fmt.Errorf("missing Script value")) resultErr = multierror.Append(resultErr, fmt.Errorf("missing Script value"))
} }
if l.ProxyType != "connect-proxy" { if l.ProxyType != string(api.ServiceKindConnectProxy) {
resultErr = multierror.Append(resultErr, fmt.Errorf("unexpected ProxyType %q", l.ProxyType)) resultErr = multierror.Append(resultErr, fmt.Errorf("unexpected ProxyType %q", l.ProxyType))
} }
if l.Listener != "inbound" && l.Listener != "outbound" { if l.Listener != "inbound" && l.Listener != "outbound" {

View File

@ -54,6 +54,15 @@ func TestConstructor(t *testing.T) {
arguments: makeArguments(map[string]interface{}{"Listener": "invalid"}), arguments: makeArguments(map[string]interface{}{"Listener": "invalid"}),
ok: false, ok: false,
}, },
"default proxy type": {
arguments: makeArguments(map[string]interface{}{"ProxyType": ""}),
expected: lua{
ProxyType: "connect-proxy",
Listener: "inbound",
Script: "lua-script",
},
ok: true,
},
"valid everything": { "valid everything": {
arguments: makeArguments(map[string]interface{}{}), arguments: makeArguments(map[string]interface{}{}),
expected: lua{ expected: lua{

View File

@ -261,6 +261,9 @@ func (p *propertyOverride) validate() error {
} }
} }
if p.ProxyType == "" {
p.ProxyType = api.ServiceKindConnectProxy
}
if err := validProxyTypes.CheckRequired(string(p.ProxyType), "ProxyType"); err != nil { if err := validProxyTypes.CheckRequired(string(p.ProxyType), "ProxyType"); err != nil {
resultErr = multierror.Append(resultErr, err) resultErr = multierror.Append(resultErr, err)
} }

View File

@ -236,7 +236,7 @@ func TestConstructor(t *testing.T) {
// enforces expected behavior until we do. Multi-member slices should be unaffected // enforces expected behavior until we do. Multi-member slices should be unaffected
// by WeakDecode as it is a more-permissive version of the default behavior. // by WeakDecode as it is a more-permissive version of the default behavior.
"single value Patches decoded as map construction succeeds": { "single value Patches decoded as map construction succeeds": {
arguments: makeArguments(map[string]any{"Patches": makePatch(map[string]any{})}), arguments: makeArguments(map[string]any{"Patches": makePatch(map[string]any{}), "ProxyType": nil}),
expected: validTestCase(OpAdd, extensioncommon.TrafficDirectionOutbound, ResourceTypeRoute).expected, expected: validTestCase(OpAdd, extensioncommon.TrafficDirectionOutbound, ResourceTypeRoute).expected,
ok: true, ok: true,
}, },