Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion api/v1alpha1/lua_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@

package v1alpha1

import gwapiv1 "sigs.k8s.io/gateway-api/apis/v1"
import (
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
gwapiv1 "sigs.k8s.io/gateway-api/apis/v1"
)

// LuaValueType defines the types of values for Lua supported by Envoy Gateway.
// +kubebuilder:validation:Enum=Inline;ValueRef
Expand Down Expand Up @@ -45,4 +48,11 @@ type Lua struct {
// +optional
// +unionMember
ValueRef *gwapiv1.LocalObjectReference `json:"valueRef,omitempty"`
// FilterContext is the filter context configuration for the Lua script.
// This must be a JSON object (key/value pairs). The values are made available
// to the Lua script via request_handle:filterContext(). This allows a shared
// script to be parameterized differently per EnvoyExtensionPolicy/route.
//
// +optional
FilterContext *apiextensionsv1.JSON `json:"filterContext,omitempty"`
}
Comment thread
norman-zon marked this conversation as resolved.
5 changes: 5 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -1641,6 +1641,13 @@ spec:
Lua defines a Lua extension
Only one of Inline or ValueRef must be set
properties:
filterContext:
description: |-
FilterContext is the filter context configuration for the Lua script.
This must be a JSON object (key/value pairs). The values are made available
to the Lua script via request_handle:filterContext(). This allows a shared
script to be parameterized differently per EnvoyExtensionPolicy/route.
x-kubernetes-preserve-unknown-fields: true
inline:
description: Inline contains the source code as an inline string.
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1640,6 +1640,13 @@ spec:
Lua defines a Lua extension
Only one of Inline or ValueRef must be set
properties:
filterContext:
description: |-
FilterContext is the filter context configuration for the Lua script.
This must be a JSON object (key/value pairs). The values are made available
to the Lua script via request_handle:filterContext(). This allows a shared
script to be parameterized differently per EnvoyExtensionPolicy/route.
x-kubernetes-preserve-unknown-fields: true
inline:
description: Inline contains the source code as an inline string.
type: string
Expand Down
5 changes: 3 additions & 2 deletions internal/gatewayapi/envoyextensionpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -774,8 +774,9 @@ func (t *Translator) buildLua(
return nil, fmt.Errorf("validation failed for lua body in policy with name %v: %w", name, err)
}
return &ir.Lua{
Name: name,
Code: luaCode,
Name: name,
Code: luaCode,
FilterContext: lua.FilterContext,
}, nil
}

Expand Down
13 changes: 13 additions & 0 deletions internal/gatewayapi/luavalidator/lua_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,19 @@ func Test_BasicValidation(t *testing.T) {
},
expectedErrSubstring: "",
},
{
name: "stream:filterContext:get",
code: `function envoy_on_response(response_handle)
local ctx = response_handle:filterContext()
if ctx ~= nil then
local custom_value = ctx:get("custom_value")
if custom_value ~= nil then
response_handle:headers():add("X-Lua-Filter-Context", custom_value)
end
end
end`,
expectedErrSubstring: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions internal/gatewayapi/luavalidator/mocks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,10 @@ function StreamHandle:connectionStreamInfo()
return ConnectionStreamInfo
end

function StreamHandle:filterContext()
return Metadata
end

function StreamHandle:setUpstreamOverrideHost(host, strict)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ xdsIR:
print("Value is greater than 5")
end
end
FilterContext: null
Name: envoyextensionpolicy/default/policy-for-http-route/lua/0
hostname: www.example.com
isHTTP2: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ xdsIR:
end
return envoy.lua.ResponseStatus.Continue
end
FilterContext: null
Name: envoyextensionpolicy/default/policy-for-http-route/lua/0
hostname: www.example.com
isHTTP2: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ xdsIR:
luas:
- Code: function envoy_on_request(request_handle) request_handle:logInfo('Goodbye.')
end
FilterContext: null
Name: envoyextensionpolicy/envoy-gateway/policy-for-gateway/lua/0
hostname: www.example.com
isHTTP2: false
Expand Down Expand Up @@ -327,6 +328,7 @@ xdsIR:
luas:
- Code: function envoy_on_request(request_handle) request_handle:logInfo('Goodbye.')
end
FilterContext: null
Name: envoyextensionpolicy/envoy-gateway/policy-for-gateway/lua/0
hostname: www.example.com
isHTTP2: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ envoyextensionpolicies:
name: cm-policy-for-http-route
kind: ConfigMap
group: v1
filterContext:
token_header: x-session-token
- apiVersion: gateway.envoyproxy.io/v1alpha1
kind: EnvoyExtensionPolicy
metadata:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ envoyExtensionPolicies:
namespace: default
spec:
lua:
- type: ValueRef
- filterContext:
token_header: x-session-token
type: ValueRef
valueRef:
group: v1
kind: ConfigMap
Expand Down Expand Up @@ -464,6 +466,7 @@ xdsIR:
function envoy_on_request(request_handle)
request_handle:logInfo('Goodbye.')
end
FilterContext: null
Name: envoyextensionpolicy/envoy-gateway/policy-for-gateway/lua/0
hostname: www.example.com
isHTTP2: false
Expand Down Expand Up @@ -503,6 +506,7 @@ xdsIR:
function envoy_on_request(request_handle)
request_handle:logInfo('Goodbye.')
end
FilterContext: null
Name: envoyextensionpolicy/envoy-gateway/policy-for-gateway/lua/0
hostname: www.example.com
isHTTP2: false
Expand Down Expand Up @@ -542,6 +546,7 @@ xdsIR:
function envoy_on_request(request_handle)
request_handle:logInfo('Goodbye.')
end
FilterContext: null
Name: envoyextensionpolicy/envoy-gateway/policy-for-gateway/lua/0
hostname: www.example.com
isHTTP2: false
Expand Down Expand Up @@ -579,6 +584,8 @@ xdsIR:
function envoy_on_response(response_handle)
response_handle:logWarn('Goodbye.')
end
FilterContext:
token_header: x-session-token
Name: envoyextensionpolicy/default/policy-for-http-route/lua/0
hostname: www.example.com
isHTTP2: false
Expand Down Expand Up @@ -616,6 +623,7 @@ xdsIR:
function envoy_on_request(request_handle)
request_handle:logInfo('Goodbye.')
end
FilterContext: null
Name: envoyextensionpolicy/envoy-gateway/policy-for-gateway/lua/0
hostname: www.example.com
isHTTP2: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,6 @@ envoyextensionpolicies:
inline: "function envoy_on_response(response_handle)
response_handle:logWarn('Goodbye.')
end"
filterContext:
token_header: x-api-key
mode: strict
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ envoyExtensionPolicies:
namespace: default
spec:
lua:
- inline: function envoy_on_response(response_handle) response_handle:logWarn('Goodbye.')
- filterContext:
mode: strict
token_header: x-api-key
inline: function envoy_on_response(response_handle) response_handle:logWarn('Goodbye.')
end
type: Inline
targetRef:
Expand Down Expand Up @@ -278,6 +281,9 @@ xdsIR:
luas:
- Code: function envoy_on_response(response_handle) response_handle:logWarn('Goodbye.')
end
FilterContext:
mode: strict
token_header: x-api-key
Name: envoyextensionpolicy/default/policy-for-http-route/lua/0
hostname: www.example.com
isHTTP2: false
Expand Down Expand Up @@ -313,6 +319,7 @@ xdsIR:
luas:
- Code: function envoy_on_request(request_handle) request_handle:logInfo('Goodbye.')
end
FilterContext: null
Name: envoyextensionpolicy/envoy-gateway/policy-for-gateway/lua/0
hostname: www.example.com
isHTTP2: false
Expand Down
3 changes: 3 additions & 0 deletions internal/ir/xds.go
Original file line number Diff line number Diff line change
Expand Up @@ -3714,6 +3714,9 @@ type Lua struct {
Name string
// Code is the Lua source code
Code *string
// FilterContext is the filter context configuration for the Lua script.
// This is a JSON object passed to the Lua script via request_handle:filterContext().
FilterContext *apiextensionsv1.JSON
}

// Wasm holds the information associated with the Wasm extensions.
Expand Down
5 changes: 5 additions & 0 deletions internal/ir/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions internal/xds/translator/lua.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import (
routev3 "github.com/envoyproxy/go-control-plane/envoy/config/route/v3"
luafilterv3 "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/http/lua/v3"
hcmv3 "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/network/http_connection_manager/v3"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/types/known/anypb"
"google.golang.org/protobuf/types/known/structpb"

egv1a1 "github.com/envoyproxy/gateway/api/v1alpha1"
"github.com/envoyproxy/gateway/internal/ir"
Expand Down Expand Up @@ -153,6 +155,13 @@ func (*lua) patchRoute(route *routev3.Route, irRoute *ir.HTTPRoute, _ *ir.HTTPLi
},
},
}
if ep.FilterContext != nil && ep.FilterContext.Raw != nil {
filterCtx := &structpb.Struct{}
if err := protojson.Unmarshal(ep.FilterContext.Raw, filterCtx); err != nil {
return err
}
luaPerRoute.FilterContext = filterCtx
}
luaPerRouteAny, err := anypb.New(luaPerRoute)
if err != nil {
return err
Expand Down
3 changes: 3 additions & 0 deletions internal/xds/translator/testdata/in/xds-ir/lua.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ http:
request_handle:logInfo('Goodbye.')
end
name: envoyextensionpolicy/default/policy-for-http-route/lua/0
filterContext:
token_header: x-api-key
mode: strict
- destination:
name: httproute/default/httproute-2/rule/0
settings:
Expand Down
3 changes: 3 additions & 0 deletions internal/xds/translator/testdata/out/xds-ir/lua.routes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
typedPerFilterConfig:
envoy.filters.http.lua/0:
'@type': type.googleapis.com/envoy.extensions.filters.http.lua.v3.LuaPerRoute
filterContext:
mode: strict
token_header: x-api-key
sourceCode:
inlineString: function envoy_on_request(request_handle) request_handle:logInfo('Goodbye.')
end
Expand Down
1 change: 1 addition & 0 deletions release-notes/current.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ new features: |
Added a `requestBody` field to the HTTP active health checker in `BackendTrafficPolicy`, allowing a request body payload to be sent during HTTP health checking. The field requires the health check `method` to be `POST` or `PUT`.
Added a `xdsNACKTotal` metric to track the number of NACKs received from Envoy, labeled by node ID and resource type URL. A NACK is a DiscoveryRequest carrying an ErrorDetail, indicating that Envoy rejected the last config update. This metric can be used to alert on config issues causing xDS rejections.
Added the `autoSNIFromEndpointHostname` TLS setting to Backends, allowing the SNI value sent to the backend to be automatically derived from the backend endpoint hostname instead of using a fixed SNI value.
Added support for `filterContext` field in Lua EnvoyExtensionPolicy, allowing shared Lua scripts to be parameterized per route via `request_handle:filterContext()`.
Added support for `ListenerSet` as a `targetRef` kind in `ClientTrafficPolicy`, allowing client traffic settings to be applied to a named group of listeners without a gateway-wide policy.
Added a `fromMetadata` field to global rate limit `limit` in `BackendTrafficPolicy`, allowing the limit value to be sourced from per-request dynamic metadata (e.g. set by an upstream ext_proc), falling back to the static `requests`/`unit` when the metadata is absent.

Expand Down
1 change: 1 addition & 0 deletions site/content/en/latest/api/extension_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -4108,6 +4108,7 @@ _Appears in:_
| `type` | _[LuaValueType](#luavaluetype)_ | true | Inline | Type is the type of method to use to read the Lua value.<br />Valid values are Inline and ValueRef, default is Inline. |
| `inline` | _string_ | false | | Inline contains the source code as an inline string. |
| `valueRef` | _[LocalObjectReference](#localobjectreference)_ | false | | ValueRef has the source code specified as a local object reference.<br />Only a reference to ConfigMap is supported.<br />The value of key `lua` in the ConfigMap will be used.<br />If the key is not found, the first value in the ConfigMap will be used. |
| `filterContext` | _[JSON](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.32/#json-v1-apiextensions-k8s-io)_ | false | | FilterContext is the filter context configuration for the Lua script.<br />This must be a JSON object (key/value pairs). The values are made available<br />to the Lua script via request_handle:filterContext(). This allows a shared<br />script to be parameterized differently per EnvoyExtensionPolicy/route. |


#### LuaValidation
Expand Down
Loading