Skip to content

Commit

Permalink
Add CEL and add some changes
Browse files Browse the repository at this point in the history
Signed-off-by: Karandashov Daniil <[email protected]>
  • Loading branch information
Demacr committed Aug 6, 2024
1 parent 2aeb863 commit 564eb48
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 67 deletions.
34 changes: 27 additions & 7 deletions api/v1alpha1/virtualbackend_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,45 @@ package v1alpha1
import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

const (
// KindSecurityPolicy is the name of the SecurityPolicy kind.
// KindVirtualBackend is the name of the VirtualBackend kind.
KindVirtualBackend = "VirtualBackend"
)

type Body []byte

// +kubebuilder:validation:Minimum=100
// +kubebuilder:validation:Maximum=599
type StatusCode uint32

// +kubebuilder:validation:Pattern=`^([\w-]+)$`
type ResponseHeader string

// +kubebuilder:object:root=true
// +kubebuilder:resource:categories=envoy-gateway,shortName=vb
//
// VirtualBackend defines the configuration for direct response.
type VirtualBackend struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec VirtualBackendSpec `json:"spec"`

// Spec defines desired state of VirtualBackend.
Spec VirtualBackendSpec `json:"spec"`
}

// +kubebuilder:validation:XValidation:rule="has(self.statusCode)"
//
// VirtualBackendSpec defines direct response configuration.
type VirtualBackendSpec struct {
Body Body `json:"body,omitempty" yaml:"body"`
StatusCode StatusCode `json:"statusCode,omitempty" yaml:"statusCode"`
ResponseHeaders []ResponseHeader `json:"responseHeaders,omitempty" yaml:"responseHeaders,omitempty"`
// +optional
//
// Body contains data which gateway returns in direct response.
Body *[]byte `json:"body,omitempty" yaml:"body,omitempty"`

// +kubebuilder:default=200
//
// StatusCode defines HTTP response status code of direct response. Default value is 200.
StatusCode StatusCode `json:"statusCode" yaml:"statusCode"`

// +optional
//
// ResponseHeaders defines Header:Value map of additional headers to response.
ResponseHeaders map[ResponseHeader]string `json:"responseHeaders,omitempty" yaml:"responseHeaders,omitempty"`
}
41 changes: 18 additions & 23 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 @@ -8,9 +8,13 @@ metadata:
spec:
group: gateway.envoyproxy.io
names:
categories:
- envoy-gateway
kind: VirtualBackend
listKind: VirtualBackendList
plural: virtualbackends
shortNames:
- vb
singular: virtualbackend
scope: Namespaced
versions:
Expand All @@ -37,18 +41,31 @@ spec:
metadata:
type: object
spec:
description: Spec defines desired state of VirtualBackend.
properties:
body:
description: Body contains data which gateway returns in direct response.
format: byte
type: string
responseHeaders:
items:
additionalProperties:
type: string
type: array
description: ResponseHeaders defines Header:Value map of additional
headers to response.
type: object
statusCode:
default: 200
description: StatusCode defines HTTP response status code of direct
response. Default value is 200.
format: int32
maximum: 599
minimum: 100
type: integer
required:
- statusCode
type: object
x-kubernetes-validations:
- rule: has(self.statusCode)
required:
- spec
type: object
Expand Down
9 changes: 4 additions & 5 deletions internal/gatewayapi/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,16 +207,15 @@ func (t *Translator) processHTTPRouteRules(httpRoute *HTTPRouteContext, parentRe
vb := resources.GetVirtualBackend(NamespaceDerefOr(backendRef.Namespace, httpRoute.Namespace), string(backendRef.Name))
for _, route := range ruleRoutes {
directResponse := &ir.DirectResponse{
Body: string(vb.Spec.Body),
Body: string(*vb.Spec.Body),
StatusCode: uint32(vb.Spec.StatusCode),
}

route.DirectResponse = directResponse
for _, header := range vb.Spec.ResponseHeaders {
splittedHeader := strings.SplitN(string(header), ": ", 1)
for header, value := range vb.Spec.ResponseHeaders {
responseHeader := ir.AddHeader{
Name: splittedHeader[0],
Value: splittedHeader[1],
Name: string(header),
Value: value,
Append: false,
}
route.AddResponseHeaders = append(route.AddResponseHeaders, responseHeader)
Expand Down
34 changes: 19 additions & 15 deletions site/content/en/latest/api/extension_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ API group.
- [EnvoyProxy](#envoyproxy)
- [SecurityPolicy](#securitypolicy)
- [SecurityPolicyList](#securitypolicylist)
- [VirtualBackend](#virtualbackend)



Expand Down Expand Up @@ -478,17 +479,6 @@ _Appears in:_
| `users` | _[SecretObjectReference](https://gateway-api.sigs.k8s.io/references/spec/#gateway.networking.k8s.io/v1.SecretObjectReference)_ | true | The Kubernetes secret which contains the username-password pairs in<br />htpasswd format, used to verify user credentials in the "Authorization"<br />header.<br /><br />This is an Opaque secret. The username-password pairs should be stored in<br />the key ".htpasswd". As the key name indicates, the value needs to be the<br />htpasswd format, for example: "user1:\{SHA\}hashed_user1_password".<br />Right now, only SHA hash algorithm is supported.<br />Reference to https://httpd.apache.org/docs/2.4/programs/htpasswd.html<br />for more details.<br /><br />Note: The secret must be in the same namespace as the SecurityPolicy. |


#### Body

_Underlying type:_ _integer array_



_Appears in:_
- [VirtualBackendSpec](#virtualbackendspec)



#### BootstrapType

_Underlying type:_ _string_
Expand Down Expand Up @@ -3744,22 +3734,36 @@ _Appears in:_
| `path` | _string_ | true | Path defines the unix domain socket path of the backend endpoint. |


#### VirtualBackend


#### VirtualBackendSpec

VirtualBackend defines the configuration for direct response.



| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `apiVersion` | _string_ | |`gateway.envoyproxy.io/v1alpha1`
| `kind` | _string_ | |`VirtualBackend`
| `metadata` | _[ObjectMeta](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.29/#objectmeta-v1-meta)_ | true | Refer to Kubernetes API documentation for fields of `metadata`. |
| `spec` | _[VirtualBackendSpec](#virtualbackendspec)_ | true | Spec defines desired state of VirtualBackend. |


#### VirtualBackendSpec



VirtualBackendSpec defines direct response configuration.

_Appears in:_
- [VirtualBackend](#virtualbackend)

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `body` | _[Body](#body)_ | true | |
| `statusCode` | _[StatusCode](#statuscode)_ | true | |
| `responseHeaders` | _[ResponseHeader](#responseheader) array_ | true | |
| `body` | _integer_ | false | Body contains data which gateway returns in direct response. |
| `statusCode` | _[StatusCode](#statuscode)_ | true | StatusCode defines HTTP response status code of direct response. Default value is 200. |
| `responseHeaders` | _object (keys:[ResponseHeader](#responseheader), values:string)_ | false | ResponseHeaders defines Header:Value map of additional headers to response. |


#### Wasm
Expand Down
34 changes: 19 additions & 15 deletions site/content/zh/latest/api/extension_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ API group.
- [EnvoyProxy](#envoyproxy)
- [SecurityPolicy](#securitypolicy)
- [SecurityPolicyList](#securitypolicylist)
- [VirtualBackend](#virtualbackend)



Expand Down Expand Up @@ -478,17 +479,6 @@ _Appears in:_
| `users` | _[SecretObjectReference](https://gateway-api.sigs.k8s.io/references/spec/#gateway.networking.k8s.io/v1.SecretObjectReference)_ | true | The Kubernetes secret which contains the username-password pairs in<br />htpasswd format, used to verify user credentials in the "Authorization"<br />header.<br /><br />This is an Opaque secret. The username-password pairs should be stored in<br />the key ".htpasswd". As the key name indicates, the value needs to be the<br />htpasswd format, for example: "user1:\{SHA\}hashed_user1_password".<br />Right now, only SHA hash algorithm is supported.<br />Reference to https://httpd.apache.org/docs/2.4/programs/htpasswd.html<br />for more details.<br /><br />Note: The secret must be in the same namespace as the SecurityPolicy. |


#### Body

_Underlying type:_ _integer array_



_Appears in:_
- [VirtualBackendSpec](#virtualbackendspec)



#### BootstrapType

_Underlying type:_ _string_
Expand Down Expand Up @@ -3744,22 +3734,36 @@ _Appears in:_
| `path` | _string_ | true | Path defines the unix domain socket path of the backend endpoint. |


#### VirtualBackend


#### VirtualBackendSpec

VirtualBackend defines the configuration for direct response.



| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `apiVersion` | _string_ | |`gateway.envoyproxy.io/v1alpha1`
| `kind` | _string_ | |`VirtualBackend`
| `metadata` | _[ObjectMeta](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.29/#objectmeta-v1-meta)_ | true | Refer to Kubernetes API documentation for fields of `metadata`. |
| `spec` | _[VirtualBackendSpec](#virtualbackendspec)_ | true | Spec defines desired state of VirtualBackend. |


#### VirtualBackendSpec



VirtualBackendSpec defines direct response configuration.

_Appears in:_
- [VirtualBackend](#virtualbackend)

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `body` | _[Body](#body)_ | true | |
| `statusCode` | _[StatusCode](#statuscode)_ | true | |
| `responseHeaders` | _[ResponseHeader](#responseheader) array_ | true | |
| `body` | _integer_ | false | Body contains data which gateway returns in direct response. |
| `statusCode` | _[StatusCode](#statuscode)_ | true | StatusCode defines HTTP response status code of direct response. Default value is 200. |
| `responseHeaders` | _object (keys:[ResponseHeader](#responseheader), values:string)_ | false | ResponseHeaders defines Header:Value map of additional headers to response. |


#### Wasm
Expand Down

0 comments on commit 564eb48

Please sign in to comment.