Skip to content

Commit

Permalink
add CEL for envoy proxy telemetry and test cases
Browse files Browse the repository at this point in the history
Signed-off-by: sh2 <[email protected]>
  • Loading branch information
shawnh2 committed Nov 12, 2023
1 parent 3ed84f3 commit 7162222
Show file tree
Hide file tree
Showing 9 changed files with 269 additions and 103 deletions.
9 changes: 9 additions & 0 deletions api/v1alpha1/accesslogging_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ const (
// ProxyAccessLogFormat defines the format of accesslog.
// By default accesslogs are written to standard output.
// +union
//
// +kubebuilder:validation:XValidation:rule="self.type == 'Text' ? has(self.text) : !has(self.text)",message="If AccessLogFormat type is Text, text field needs to be set."
// +kubebuilder:validation:XValidation:rule="self.type == 'JSON' ? has(self.json) : !has(self.json)",message="If AccessLogFormat type is JSON, json field needs to be set."
type ProxyAccessLogFormat struct {
// Type defines the type of accesslog format.
// +kubebuilder:validation:Enum=Text;JSON
Expand Down Expand Up @@ -65,9 +68,15 @@ const (
ProxyAccessLogSinkTypeOpenTelemetry ProxyAccessLogSinkType = "OpenTelemetry"
)

// ProxyAccessLogSink defines the sink of accesslog.
// +union
//
// +kubebuilder:validation:XValidation:rule="self.type == 'File' ? has(self.file) : !has(self.file)",message="If AccessLogSink type is File, file field needs to be set."
// +kubebuilder:validation:XValidation:rule="self.type == 'OpenTelemetry' ? has(self.openTelemetry) : !has(self.openTelemetry)",message="If AccessLogSink type is OpenTelemetry, openTelemetry field needs to be set."
type ProxyAccessLogSink struct {
// Type defines the type of accesslog sink.
// +kubebuilder:validation:Enum=File;OpenTelemetry
// +unionDiscriminator
Type ProxyAccessLogSinkType `json:"type,omitempty"`
// File defines the file accesslog sink.
// +optional
Expand Down
7 changes: 7 additions & 0 deletions api/v1alpha1/envoyproxy_metric_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,21 @@ type ProxyMetrics struct {
EnableVirtualHostStats bool `json:"enableVirtualHostStats,omitempty"`
}

// ProxyMetricSink defines the sink of metrics.
// Default metrics sink is OpenTelemetry.
// +union
//
// +kubebuilder:validation:XValidation:rule="self.type == 'OpenTelemetry' ? has(self.openTelemetry) : !has(self.openTelemetry)",message="If MetricSink type is OpenTelemetry, openTelemetry field needs to be set."
type ProxyMetricSink struct {
// Type defines the metric sink type.
// EG currently only supports OpenTelemetry.
// +kubebuilder:validation:Enum=OpenTelemetry
// +kubebuilder:default=OpenTelemetry
// +unionDiscriminator
Type MetricSinkType `json:"type"`
// OpenTelemetry defines the configuration for OpenTelemetry sink.
// It's required if the sink type is OpenTelemetry.
// +optional
OpenTelemetry *ProxyOpenTelemetrySink `json:"openTelemetry,omitempty"`
}

Expand Down
8 changes: 4 additions & 4 deletions api/v1alpha1/shared_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,16 +260,16 @@ type StringMatch struct {
type StringMatchType string

const (
// MatchExact :the input string must match exactly the match value.
// StringMatchExact :the input string must match exactly the match value.
StringMatchExact StringMatchType = "Exact"

// MatchPrefix :the input string must start with the match value.
// StringMatchPrefix :the input string must start with the match value.
StringMatchPrefix StringMatchType = "Prefix"

// MatchSuffix :the input string must end with the match value.
// StringMatchSuffix :the input string must end with the match value.
StringMatchSuffix StringMatchType = "Suffix"

// MatchRegularExpression :The input string must match the regular expression
// StringMatchRegularExpression :The input string must match the regular expression
// specified in the match value.
// The regex string must adhere to the syntax documented in
// https://github.com/google/re2/wiki/Syntax.
Expand Down
2 changes: 1 addition & 1 deletion api/v1alpha1/validation/envoygateway_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/envoyproxy/gateway/api/v1alpha1"
)

// ValidateEnvoyGateway validates the provided EnvoyGateway.
// Validate validates the provided EnvoyGateway.
func ValidateEnvoyGateway(eg *v1alpha1.EnvoyGateway) error {
switch {
case eg == nil:
Expand Down
3 changes: 1 addition & 2 deletions api/v1alpha1/validation/envoyproxy_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ func validateService(spec *egv1a1.EnvoyProxySpec) []error {
errs = append(errs, fmt.Errorf("allocateLoadBalancerNodePorts can only be set for %v type", egv1a1.ServiceTypeLoadBalancer))
}
}
if serviceType, serviceLoadBalancerIP :=
spec.Provider.Kubernetes.EnvoyService.Type, spec.Provider.Kubernetes.EnvoyService.LoadBalancerIP; serviceType != nil && serviceLoadBalancerIP != nil {
if serviceType, serviceLoadBalancerIP := spec.Provider.Kubernetes.EnvoyService.Type, spec.Provider.Kubernetes.EnvoyService.LoadBalancerIP; serviceType != nil && serviceLoadBalancerIP != nil {
if *serviceType != egv1a1.ServiceTypeLoadBalancer {
errs = append(errs, fmt.Errorf("loadBalancerIP can only be set for %v type", egv1a1.ServiceTypeLoadBalancer))
}
Expand Down
91 changes: 1 addition & 90 deletions api/v1alpha1/validation/envoyproxy_validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,27 +276,7 @@ func TestValidateEnvoyProxy(t *testing.T) {
},
expected: false,
},
{
name: "envoy service with valid loadBalancerIP but not 'LoadBalancer' type",
proxy: &egv1a1.EnvoyProxy{
ObjectMeta: metav1.ObjectMeta{
Namespace: "test",
Name: "test",
},
Spec: egv1a1.EnvoyProxySpec{
Provider: &egv1a1.EnvoyProxyProvider{
Type: egv1a1.ProviderTypeKubernetes,
Kubernetes: &egv1a1.EnvoyProxyKubernetesProvider{
EnvoyService: &egv1a1.KubernetesServiceSpec{
Type: egv1a1.GetKubernetesServiceType(egv1a1.ServiceTypeClusterIP),
LoadBalancerIP: ptr.To("10.11.12.13"),
},
},
},
},
},
expected: false,
},

{
name: "valid user bootstrap replace type",
proxy: &egv1a1.EnvoyProxy{
Expand Down Expand Up @@ -373,23 +353,6 @@ func TestValidateEnvoyProxy(t *testing.T) {
},
expected: false,
},
{
name: "should valid when accesslog is disabled",
proxy: &egv1a1.EnvoyProxy{
ObjectMeta: metav1.ObjectMeta{
Namespace: "test",
Name: "test",
},
Spec: egv1a1.EnvoyProxySpec{
Telemetry: &egv1a1.ProxyTelemetry{
AccessLog: &egv1a1.ProxyAccessLog{
Disable: true,
},
},
},
},
expected: true,
},
{
name: "should invalid when accesslog enabled using Text format, but `text` field being empty",
proxy: &egv1a1.EnvoyProxy{
Expand All @@ -413,29 +376,6 @@ func TestValidateEnvoyProxy(t *testing.T) {
},
expected: false,
},
{
name: "should invalid when accesslog enabled using JSON format, but `json` field being empty",
proxy: &egv1a1.EnvoyProxy{
ObjectMeta: metav1.ObjectMeta{
Namespace: "test",
Name: "test",
},
Spec: egv1a1.EnvoyProxySpec{
Telemetry: &egv1a1.ProxyTelemetry{
AccessLog: &egv1a1.ProxyAccessLog{
Settings: []egv1a1.ProxyAccessLogSetting{
{
Format: egv1a1.ProxyAccessLogFormat{
Type: egv1a1.ProxyAccessLogFormatTypeJSON,
},
},
},
},
},
},
},
expected: false,
},
{
name: "should invalid when accesslog enabled using File sink, but `file` field being empty",
proxy: &egv1a1.EnvoyProxy{
Expand Down Expand Up @@ -509,35 +449,6 @@ func TestValidateEnvoyProxy(t *testing.T) {
},
expected: true,
},
{
name: "should invalid when accesslog enabled using OpenTelemetry sink, but `openTelemetry` field being empty",
proxy: &egv1a1.EnvoyProxy{
ObjectMeta: metav1.ObjectMeta{
Namespace: "test",
Name: "test",
},
Spec: egv1a1.EnvoyProxySpec{
Telemetry: &egv1a1.ProxyTelemetry{
AccessLog: &egv1a1.ProxyAccessLog{
Settings: []egv1a1.ProxyAccessLogSetting{
{
Format: egv1a1.ProxyAccessLogFormat{
Type: egv1a1.ProxyAccessLogFormatTypeText,
Text: pointer.String("[%START_TIME%]"),
},
Sinks: []egv1a1.ProxyAccessLogSink{
{
Type: egv1a1.ProxyAccessLogSinkTypeOpenTelemetry,
},
},
},
},
},
},
},
},
expected: false,
},
}

for i := range testCases {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5350,9 +5350,18 @@ spec:
- JSON
type: string
type: object
x-kubernetes-validations:
- message: If AccessLogFormat type is Text, text field
needs to be set.
rule: 'self.type == ''Text'' ? has(self.text) : !has(self.text)'
- message: If AccessLogFormat type is JSON, json field
needs to be set.
rule: 'self.type == ''JSON'' ? has(self.json) : !has(self.json)'
sinks:
description: Sinks defines the sinks of accesslog.
items:
description: ProxyAccessLogSink defines the sink of
accesslog.
properties:
file:
description: File defines the file accesslog sink.
Expand Down Expand Up @@ -5397,6 +5406,15 @@ spec:
- OpenTelemetry
type: string
type: object
x-kubernetes-validations:
- message: If AccessLogSink type is File, file field
needs to be set.
rule: 'self.type == ''File'' ? has(self.file) :
!has(self.file)'
- message: If AccessLogSink type is OpenTelemetry,
openTelemetry field needs to be set.
rule: 'self.type == ''OpenTelemetry'' ? has(self.openTelemetry)
: !has(self.openTelemetry)'
minItems: 1
type: array
required:
Expand Down Expand Up @@ -5459,6 +5477,8 @@ spec:
description: Sinks defines the metric sinks where metrics
are sent to.
items:
description: ProxyMetricSink defines the sink of metrics.
Default metrics sink is OpenTelemetry.
properties:
openTelemetry:
description: OpenTelemetry defines the configuration
Expand Down Expand Up @@ -5489,6 +5509,11 @@ spec:
required:
- type
type: object
x-kubernetes-validations:
- message: If MetricSink type is OpenTelemetry, openTelemetry
field needs to be set.
rule: 'self.type == ''OpenTelemetry'' ? has(self.openTelemetry)
: !has(self.openTelemetry)'
type: array
type: object
tracing:
Expand Down
4 changes: 2 additions & 2 deletions site/content/en/latest/api/extension_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -1160,7 +1160,7 @@ _Appears in:_




ProxyAccessLogSink defines the sink of accesslog.

_Appears in:_
- [ProxyAccessLogSetting](#proxyaccesslogsetting)
Expand Down Expand Up @@ -1227,7 +1227,7 @@ _Appears in:_




ProxyMetricSink defines the sink of metrics. Default metrics sink is OpenTelemetry.

_Appears in:_
- [ProxyMetrics](#proxymetrics)
Expand Down
Loading

0 comments on commit 7162222

Please sign in to comment.