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: Direct Response #3977

Closed
wants to merge 3 commits into from

Conversation

Demacr
Copy link

@Demacr Demacr commented Jul 31, 2024

What this PR does / why we need it:
I decided to add Direct Response due to making endpoint stubs after deleting services. I found that #2714 issue has information that GW API still in development to add direct response and there is a way to workaround by EnvoyPatchPolicy. So I've implemented new CRD which can be used as backendRef in HTTPRoute.

p.s.: this PR is a draft, I'll add documentation later. Will be glad to discuss about conception of this solution.


Fixed #2714

@Demacr Demacr requested a review from a team as a code owner July 31, 2024 15:30
type VirtualBackendSpec struct {
Body Body `json:"body,omitempty" yaml:"body"`
StatusCode StatusCode `json:"statusCode,omitempty" yaml:"statusCode"`
ResponseHeadersToAdd []ResponseHeader `json:"responseHeadersToAdd,omitempty" yaml:"responseHeadersToAdd,omitempty"`
Copy link
Contributor

Choose a reason for hiding this comment

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

  • Maybe call the field ResponseHeaders?

  • Instead of a string array, maybe make this a map[string]string?

  • You might also consider adding CEL validation for the header names. According to RFC 7230:

Historically, HTTP has allowed field content with text in the
ISO-8859-1 charset [ISO-8859-1], supporting other charsets only
through use of [RFC2047] encoding. In practice, most HTTP header
field values use only a subset of the US-ASCII charset [USASCII].
Newly defined header fields SHOULD limit their field values to
US-ASCII octets. A recipient SHOULD treat other octets in field
content (obs-text) as opaque data.

Copy link
Author

@Demacr Demacr Aug 1, 2024

Choose a reason for hiding this comment

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

Maybe call the field ResponseHeaders?

Agree

Instead of a string array, maybe make this a map[string]string?

Hm, maybe make similar to ir.AddHeader?

type AddHeader struct {
	Name   string `json:"name" yaml:"name"`
	Value  string `json:"value" yaml:"value"`
	Append bool   `json:"append" yaml:"append"`
}

You might also consider adding CEL validation for the header names.

Yeah, I'll add validation when PR will be approved conceptually.

Copy link
Author

Choose a reason for hiding this comment

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

@liorokman done

api/v1alpha1/virtualbackend_types.go Outdated Show resolved Hide resolved
Spec VirtualBackendSpec `json:"spec"`
}

type VirtualBackendSpec struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

Add some CEL validations here, and also document what the defaults are if nothing is provided.

For consistency with other EG APIs, if the value is optional:

  • It should be a pointer
  • The JSON tag should specify "omitempty"
  • There should be some documentation around what is used if the value was not specified.

This is especially important for the StatusCode field - there might not be a body but the direct response should always include a StatusCode - it shouldn't be possible to leave this empty.

Copy link
Author

Choose a reason for hiding this comment

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

@liorokman done.

Copy link
Member

@Xunzhuo Xunzhuo left a comment

Choose a reason for hiding this comment

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

Can this be part of the Backend API ?

@Xunzhuo Xunzhuo requested a review from guydc August 1, 2024 03:38
@zirain
Copy link
Member

zirain commented Aug 1, 2024

Can this be part of the Backend API ?

seems not

@Xunzhuo
Copy link
Member

Xunzhuo commented Aug 1, 2024

Should we add a new CRD to support one feature ? In a long run, will it cause the API maintaince issues ?

@Demacr
Copy link
Author

Demacr commented Aug 1, 2024

Can this be part of the Backend API ?

@Xunzhuo Firstly, I thought about it but it will require so many changes and it won't look good by architect.

Should we add a new CRD to support one feature ? In a long run, will it cause the API maintaince issues ?

@Xunzhuo due to direct response gonna be part of Gateway API it's temporary solution for such functionality. But realization in GW API is unpredictable of time, like one guy took responsibility to add and since April no updates.
After implementation in GW API's side it could be deleted or kept for backward compatibility.

@arkodg
Copy link
Contributor

arkodg commented Aug 1, 2024

we'll have to unfortunately have to wait until the API is available in Gateway API
i suggest using https://gateway.envoyproxy.io/docs/tasks/extensibility/envoy-patch-policy/ as a workaround until then

@zirain
Copy link
Member

zirain commented Aug 2, 2024

we'll have to unfortunately have to wait until the API is available in Gateway API i suggest using https://gateway.envoyproxy.io/docs/tasks/extensibility/envoy-patch-policy/ as a workaround until then

is there an issue on upstream?

@Demacr
Copy link
Author

Demacr commented Aug 2, 2024

@zirain do you mean this one?

Demacr added 3 commits August 6, 2024 16:55
Signed-off-by: Karandashov Daniil <[email protected]>
Signed-off-by: Karandashov Daniil <[email protected]>
@Demacr Demacr force-pushed the feature/add-direct-response branch from 017407e to 564eb48 Compare August 6, 2024 12:55
@@ -1212,6 +1212,8 @@ func (h AddHeader) Validate() error {
// DirectResponse holds the details for returning a body and status code for a route.
// +k8s:deepcopy-gen=true
type DirectResponse struct {
// Body will be used for the direct response's status code.
Body string `json:"body,omitempty" yaml:"body,omitempty"`
Copy link
Contributor

Choose a reason for hiding this comment

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

Body should be a []byte as well. If the direct response is a binary image, converting to a string might mess it up.


if res.Body != "" {
directResponseDataSource = &corev3.DataSource{
Specifier: &corev3.DataSource_InlineString{
Copy link
Contributor

Choose a reason for hiding this comment

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

Use DataSource_InlineBytes instead of DataSource_InlineString.

// +kubebuilder:resource:categories=envoy-gateway,shortName=vb
//
// VirtualBackend defines the configuration for direct response.
type VirtualBackend struct {
Copy link
Member

@zirain zirain Aug 16, 2024

Choose a reason for hiding this comment

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

@guydc do you think it's a good idea to support this in Backend?

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry for the very late response. IMHO, the best direction here is to support this through a custom filter, as outlined here: #4120

@arkodg
Copy link
Contributor

arkodg commented Aug 29, 2024

ive raised #4120 to discuss how we can move forward for Direct Response, hoping to get some consensus on the issue, before we can move forward with the implementation

@zirain
Copy link
Member

zirain commented Sep 17, 2024

@arkodg should we close this, and move to HTTPRouterFilter?

@arkodg
Copy link
Contributor

arkodg commented Sep 17, 2024

@arkodg should we close this, and move to HTTPRouterFilter?

yeah @zirain that makes sense, closing this PR, @Demacr if you're still interested on adding support for Direct Response, can you raise another PR to add the feature as a field to HTTPRouterFilter

@arkodg arkodg closed this Sep 17, 2024
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.

Feature Request: Add support for configuring gateway to direct_response on HTTP routes.
6 participants