Skip to content

Commit

Permalink
implment BackendRef in EnvoyProxy
Browse files Browse the repository at this point in the history
  • Loading branch information
zirain committed Apr 8, 2024
1 parent 7912b1f commit 577f8aa
Show file tree
Hide file tree
Showing 22 changed files with 716 additions and 58 deletions.
7 changes: 5 additions & 2 deletions api/v1alpha1/accesslogging_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,14 @@ type FileEnvoyProxyAccessLog struct {

// OpenTelemetryEnvoyProxyAccessLog defines the OpenTelemetry access log sink.
//
// +kubebuilder:validation:XValidation:message="BackendRef only support Service Kind.",rule="!has(self.backendRef) || !has(self.backendRef.kind) || self.backendRef.kind == 'Service'"
// +kubebuilder:validation:XValidation:message="host or backendRef needs to be set",rule="has(self.host) || has(self.backendRef)"
// +kubebuilder:validation:XValidation:message="backendRef only support Service Kind.",rule="!has(self.backendRef) || self.backendRef.kind == 'Service'"
type OpenTelemetryEnvoyProxyAccessLog struct {
// Host define the extension service hostname.
// Deprecated: Use BackendRef instead.
Host string `json:"host"`
//
// +optional
Host *string `json:"host"`
// Port defines the port the extension service is exposed on.
// Deprecated: Use BackendRef instead.
//
Expand Down
7 changes: 5 additions & 2 deletions api/v1alpha1/envoyproxy_metric_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,14 @@ type ProxyMetricSink struct {

// ProxyOpenTelemetrySink defines the configuration for OpenTelemetry sink.
//
// +kubebuilder:validation:XValidation:message="BackendRef only support Service Kind.",rule="!has(self.backendRef) || !has(self.backendRef.kind) || self.backendRef.kind == 'Service'"
// +kubebuilder:validation:XValidation:message="host or backendRef needs to be set",rule="has(self.host) || has(self.backendRef)"
// +kubebuilder:validation:XValidation:message="backendRef only support Service Kind.",rule="!has(self.backendRef) || self.backendRef.kind == 'Service'"
type ProxyOpenTelemetrySink struct {
// Host define the service hostname.
// Deprecated: Use BackendRef instead.
Host string `json:"host"`
//
// +optional
Host *string `json:"host"`
// Port defines the port the service is exposed on.
// Deprecated: Use BackendRef instead.
//
Expand Down
7 changes: 5 additions & 2 deletions api/v1alpha1/tracing_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ const (

// TracingProvider defines the tracing provider configuration.
//
// +kubebuilder:validation:XValidation:message="BackendRef only support Service Kind.",rule="!has(self.backendRef) || !has(self.backendRef.kind) || self.backendRef.kind == 'Service'"
// +kubebuilder:validation:XValidation:message="host or backendRef needs to be set",rule="has(self.host) || has(self.backendRef)"
// +kubebuilder:validation:XValidation:message="backendRef only support Service Kind.",rule="!has(self.backendRef) || self.backendRef.kind == 'Service'"
type TracingProvider struct {
// Type defines the tracing provider type.
// EG currently only supports OpenTelemetry.
Expand All @@ -41,7 +42,9 @@ type TracingProvider struct {
Type TracingProviderType `json:"type"`
// Host define the provider service hostname.
// Deprecated: Use BackendRef instead.
Host string `json:"host"`
//
// +optional
Host *string `json:"host"`
// Port defines the port the provider service is exposed on.
// Deprecated: Use BackendRef instead.
//
Expand Down
2 changes: 1 addition & 1 deletion api/v1alpha1/validation/envoyproxy_validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ func TestValidateEnvoyProxy(t *testing.T) {
{
Type: egv1a1.MetricSinkTypeOpenTelemetry,
OpenTelemetry: &egv1a1.ProxyOpenTelemetrySink{
Host: "0.0.0.0",
Host: ptr.To("0.0.0.0"),
Port: 3217,
},
},
Expand Down
15 changes: 15 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 @@ -5998,13 +5998,13 @@ spec:
Resources is a set of labels that describe the source of a log entry, including envoy node info.
It's recommended to follow [semantic conventions](https://opentelemetry.io/docs/reference/specification/resource/semantic_conventions/).
type: object
required:
- host
type: object
x-kubernetes-validations:
- message: BackendRef only support Service Kind.
rule: '!has(self.backendRef) || !has(self.backendRef.kind)
|| self.backendRef.kind == ''Service'''
- message: host or backendRef needs to be set
rule: has(self.host) || has(self.backendRef)
- message: backendRef only support Service Kind.
rule: '!has(self.backendRef) || self.backendRef.kind
== ''Service'''
type:
description: Type defines the type of accesslog
sink.
Expand Down Expand Up @@ -6185,13 +6185,13 @@ spec:
maximum: 65535
minimum: 0
type: integer
required:
- host
type: object
x-kubernetes-validations:
- message: BackendRef only support Service Kind.
rule: '!has(self.backendRef) || !has(self.backendRef.kind)
|| self.backendRef.kind == ''Service'''
- message: host or backendRef needs to be set
rule: has(self.host) || has(self.backendRef)
- message: backendRef only support Service Kind.
rule: '!has(self.backendRef) || self.backendRef.kind
== ''Service'''
type:
default: OpenTelemetry
description: |-
Expand Down Expand Up @@ -6383,13 +6383,14 @@ spec:
- OpenTelemetry
type: string
required:
- host
- type
type: object
x-kubernetes-validations:
- message: BackendRef only support Service Kind.
rule: '!has(self.backendRef) || !has(self.backendRef.kind)
|| self.backendRef.kind == ''Service'''
- message: host or backendRef needs to be set
rule: has(self.host) || has(self.backendRef)
- message: backendRef only support Service Kind.
rule: '!has(self.backendRef) || self.backendRef.kind ==
''Service'''
samplingRate:
default: 100
description: |-
Expand Down
32 changes: 30 additions & 2 deletions internal/gatewayapi/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/envoyproxy/gateway/internal/ir"
"github.com/envoyproxy/gateway/internal/utils"
"github.com/envoyproxy/gateway/internal/utils/naming"
"github.com/envoyproxy/gateway/internal/utils/net"
)

var _ ListenersTranslator = (*Translator)(nil)
Expand Down Expand Up @@ -216,12 +217,20 @@ func processAccessLog(envoyproxy *egv1a1.EnvoyProxy) *ir.AccessLog {
continue
}

// TODO: remove support for Host/Port in v1.2
al := &ir.OpenTelemetryAccessLog{
Port: uint32(sink.OpenTelemetry.Port),
Host: sink.OpenTelemetry.Host,
Resources: sink.OpenTelemetry.Resources,
}

if sink.OpenTelemetry.Host != nil {
al.Host = *sink.OpenTelemetry.Host
}

if sink.OpenTelemetry.BackendRef != nil {
al.Host, al.Port = net.BackendHostAndPort(*sink.OpenTelemetry.BackendRef, envoyproxy.Namespace)
}

switch accessLog.Format.Type {
case egv1a1.ProxyAccessLogFormatTypeJSON:
al.Attributes = accessLog.Format.JSON
Expand All @@ -243,10 +252,29 @@ func processTracing(gw *gwapiv1.Gateway, envoyproxy *egv1a1.EnvoyProxy) *ir.Trac
envoyproxy.Spec.Telemetry.Tracing == nil {
return nil
}
tracing := envoyproxy.Spec.Telemetry.Tracing

// TODO: remove support for Host/Port in v1.2
var host string
var port uint32
if tracing.Provider.Host != nil {
host, port = *tracing.Provider.Host, uint32(tracing.Provider.Port)
}
if tracing.Provider.BackendRef != nil {
host, port = net.BackendHostAndPort(*tracing.Provider.BackendRef, gw.Namespace)
}

samplingRate := 100.0
if tracing.SamplingRate != nil {
samplingRate = float64(*tracing.SamplingRate)
}

return &ir.Tracing{
ServiceName: naming.ServiceName(utils.NamespacedName(gw)),
ProxyTracing: *envoyproxy.Spec.Telemetry.Tracing,
Host: host,
Port: port,
SamplingRate: samplingRate,
CustomTags: tracing.CustomTags,
}
}

Expand Down
95 changes: 94 additions & 1 deletion internal/gatewayapi/listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"
gwapiv1 "sigs.k8s.io/gateway-api/apis/v1"

egcfgv1a1 "github.com/envoyproxy/gateway/api/v1alpha1"
Expand Down Expand Up @@ -40,7 +41,99 @@ func TestProcessTracing(t *testing.T) {
},
expected: &ir.Tracing{
ServiceName: "fake-gw.fake-ns",
ProxyTracing: egcfgv1a1.ProxyTracing{},
SamplingRate: 100.0,
},
},
{
gw: gwapiv1.Gateway{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-gw",
Namespace: "fake-ns",
},
},
proxy: &egcfgv1a1.EnvoyProxy{
Spec: egcfgv1a1.EnvoyProxySpec{
Telemetry: &egcfgv1a1.ProxyTelemetry{
Tracing: &egcfgv1a1.ProxyTracing{
Provider: egcfgv1a1.TracingProvider{
Host: ptr.To("fake-host"),
Port: 4317,
},
},
},
},
},
expected: &ir.Tracing{
ServiceName: "fake-gw.fake-ns",
SamplingRate: 100.0,
Host: "fake-host",
Port: 4317,
},
},
{
gw: gwapiv1.Gateway{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-gw",
Namespace: "fake-ns",
},
},
proxy: &egcfgv1a1.EnvoyProxy{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-eproxy",
Namespace: "fake-ns",
},
Spec: egcfgv1a1.EnvoyProxySpec{
Telemetry: &egcfgv1a1.ProxyTelemetry{
Tracing: &egcfgv1a1.ProxyTracing{
Provider: egcfgv1a1.TracingProvider{
Host: ptr.To("fake-host"),
Port: 4317,
BackendRef: &gwapiv1.BackendObjectReference{
Name: "fake-name",
Port: PortNumPtr(4317),
},
},
},
},
},
},
expected: &ir.Tracing{
ServiceName: "fake-gw.fake-ns",
SamplingRate: 100.0,
Host: "fake-name.fake-ns.svc",
Port: 4317,
},
},
{
gw: gwapiv1.Gateway{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-gw",
Namespace: "fake-ns",
},
},
proxy: &egcfgv1a1.EnvoyProxy{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-eproxy",
Namespace: "fake-ns",
},
Spec: egcfgv1a1.EnvoyProxySpec{
Telemetry: &egcfgv1a1.ProxyTelemetry{
Tracing: &egcfgv1a1.ProxyTracing{
Provider: egcfgv1a1.TracingProvider{
BackendRef: &gwapiv1.BackendObjectReference{
Name: "fake-name",
Port: PortNumPtr(4317),
},
},
},
},
},
},
expected: &ir.Tracing{
ServiceName: "fake-gw.fake-ns",
SamplingRate: 100.0,
Host: "fake-name.fake-ns.svc",
Port: 4317,
},
},
}
Expand Down
Loading

0 comments on commit 577f8aa

Please sign in to comment.