|  | 
|  | 1 | +package v1alpha2 | 
|  | 2 | + | 
|  | 3 | +import ( | 
|  | 4 | +	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | 
|  | 5 | +	gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2" | 
|  | 6 | +) | 
|  | 7 | + | 
|  | 8 | +// +genclient | 
|  | 9 | +// +kubebuilder:object:root=true | 
|  | 10 | +// +kubebuilder:storageversion | 
|  | 11 | +// +kubebuilder:subresource:status | 
|  | 12 | +// +kubebuilder:resource:categories=nginx-gateway-fabric,scope=Namespaced | 
|  | 13 | +// +kubebuilder:printcolumn:name="Age",type=date,JSONPath=`.metadata.creationTimestamp` | 
|  | 14 | +// +kubebuilder:metadata:labels="gateway.networking.k8s.io/policy=direct" | 
|  | 15 | + | 
|  | 16 | +// ObservabilityPolicy is a Direct Attached Policy. It provides a way to configure observability settings for | 
|  | 17 | +// the NGINX Gateway Fabric data plane. Used in conjunction with the NginxProxy CRD that is attached to the | 
|  | 18 | +// GatewayClass parametersRef. | 
|  | 19 | +type ObservabilityPolicy struct { | 
|  | 20 | +	metav1.TypeMeta   `json:",inline"` | 
|  | 21 | +	metav1.ObjectMeta `json:"metadata,omitempty"` | 
|  | 22 | + | 
|  | 23 | +	// Spec defines the desired state of the ObservabilityPolicy. | 
|  | 24 | +	Spec ObservabilityPolicySpec `json:"spec"` | 
|  | 25 | + | 
|  | 26 | +	// Status defines the state of the ObservabilityPolicy. | 
|  | 27 | +	Status gatewayv1alpha2.PolicyStatus `json:"status,omitempty"` | 
|  | 28 | +} | 
|  | 29 | + | 
|  | 30 | +// +kubebuilder:object:root=true | 
|  | 31 | + | 
|  | 32 | +// ObservabilityPolicyList contains a list of ObservabilityPolicies. | 
|  | 33 | +type ObservabilityPolicyList struct { | 
|  | 34 | +	metav1.TypeMeta `json:",inline"` | 
|  | 35 | +	metav1.ListMeta `json:"metadata,omitempty"` | 
|  | 36 | +	Items           []ObservabilityPolicy `json:"items"` | 
|  | 37 | +} | 
|  | 38 | + | 
|  | 39 | +// ObservabilityPolicySpec defines the desired state of the ObservabilityPolicy. | 
|  | 40 | +type ObservabilityPolicySpec struct { | 
|  | 41 | +	// Tracing allows for enabling and configuring tracing. | 
|  | 42 | +	// | 
|  | 43 | +	// +optional | 
|  | 44 | +	Tracing *Tracing `json:"tracing,omitempty"` | 
|  | 45 | + | 
|  | 46 | +	// TargetRefs identifies the API object(s) to apply the policy to. | 
|  | 47 | +	// Objects must be in the same namespace as the policy. | 
|  | 48 | +	// Support: HTTPRoute, GRPCRoute. | 
|  | 49 | +	// | 
|  | 50 | +	// TargetRefs must be _distinct_. This means that the multi-part key defined by `kind` and `name` must | 
|  | 51 | +	// be unique across all targetRef entries in the ObservabilityPolicy. | 
|  | 52 | +	// | 
|  | 53 | +	// +kubebuilder:validation:MinItems=1 | 
|  | 54 | +	// +kubebuilder:validation:MaxItems=16 | 
|  | 55 | +	// +kubebuilder:validation:XValidation:message="TargetRef Kind must be: HTTPRoute or GRPCRoute",rule="(self.exists(t, t.kind=='HTTPRoute') || self.exists(t, t.kind=='GRPCRoute'))" | 
|  | 56 | +	// +kubebuilder:validation:XValidation:message="TargetRef Group must be gateway.networking.k8s.io",rule="self.all(t, t.group=='gateway.networking.k8s.io')" | 
|  | 57 | +	// +kubebuilder:validation:XValidation:message="TargetRef Kind and Name combination must be unique",rule="self.all(p1, self.exists_one(p2, (p1.name == p2.name) && (p1.kind == p2.kind)))" | 
|  | 58 | +	//nolint:lll | 
|  | 59 | +	TargetRefs []gatewayv1alpha2.LocalPolicyTargetReference `json:"targetRefs"` | 
|  | 60 | +} | 
|  | 61 | + | 
|  | 62 | +// Tracing allows for enabling and configuring OpenTelemetry tracing. | 
|  | 63 | +// | 
|  | 64 | +// +kubebuilder:validation:XValidation:message="ratio can only be specified if strategy is of type ratio",rule="!(has(self.ratio) && self.strategy != 'ratio')" | 
|  | 65 | +// | 
|  | 66 | +//nolint:lll | 
|  | 67 | +type Tracing struct { | 
|  | 68 | +	// Strategy defines if tracing is ratio-based or parent-based. | 
|  | 69 | +	Strategy TraceStrategy `json:"strategy"` | 
|  | 70 | + | 
|  | 71 | +	// Ratio is the percentage of traffic that should be sampled. Integer from 0 to 100. | 
|  | 72 | +	// By default, 100% of http requests are traced. Not applicable for parent-based tracing. | 
|  | 73 | +	// If ratio is set to 0, tracing is disabled. | 
|  | 74 | +	// | 
|  | 75 | +	// +optional | 
|  | 76 | +	// +kubebuilder:validation:Minimum=0 | 
|  | 77 | +	// +kubebuilder:validation:Maximum=100 | 
|  | 78 | +	Ratio *int32 `json:"ratio,omitempty"` | 
|  | 79 | + | 
|  | 80 | +	// Context specifies how to propagate traceparent/tracestate headers. | 
|  | 81 | +	// Default: https://nginx.org/en/docs/ngx_otel_module.html#otel_trace_context | 
|  | 82 | +	// | 
|  | 83 | +	// +optional | 
|  | 84 | +	Context *TraceContext `json:"context,omitempty"` | 
|  | 85 | + | 
|  | 86 | +	// SpanName defines the name of the Otel span. By default is the name of the location for a request. | 
|  | 87 | +	// If specified, applies to all locations that are created for a route. | 
|  | 88 | +	// Format: must have all '"' escaped and must not contain any '$' or end with an unescaped '\' | 
|  | 89 | +	// Examples of invalid names: some-$value, quoted-"value"-name, unescaped\ | 
|  | 90 | +	// | 
|  | 91 | +	// +optional | 
|  | 92 | +	// +kubebuilder:validation:MinLength=1 | 
|  | 93 | +	// +kubebuilder:validation:MaxLength=255 | 
|  | 94 | +	// +kubebuilder:validation:Pattern=`^([^"$\\]|\\[^$])*$` | 
|  | 95 | +	SpanName *string `json:"spanName,omitempty"` | 
|  | 96 | + | 
|  | 97 | +	// SpanAttributes are custom key/value attributes that are added to each span. | 
|  | 98 | +	// | 
|  | 99 | +	// +optional | 
|  | 100 | +	// +listType=map | 
|  | 101 | +	// +listMapKey=key | 
|  | 102 | +	// +kubebuilder:validation:MaxItems=64 | 
|  | 103 | +	SpanAttributes []SpanAttribute `json:"spanAttributes,omitempty"` | 
|  | 104 | +} | 
|  | 105 | + | 
|  | 106 | +// TraceStrategy defines the tracing strategy. | 
|  | 107 | +// | 
|  | 108 | +// +kubebuilder:validation:Enum=ratio;parent | 
|  | 109 | +type TraceStrategy string | 
|  | 110 | + | 
|  | 111 | +const ( | 
|  | 112 | +	// TraceStrategyRatio enables ratio-based tracing, defaulting to 100% sampling rate. | 
|  | 113 | +	TraceStrategyRatio TraceStrategy = "ratio" | 
|  | 114 | + | 
|  | 115 | +	// TraceStrategyParent enables tracing and only records spans if the parent span was sampled. | 
|  | 116 | +	TraceStrategyParent TraceStrategy = "parent" | 
|  | 117 | +) | 
|  | 118 | + | 
|  | 119 | +// TraceContext specifies how to propagate traceparent/tracestate headers. | 
|  | 120 | +// | 
|  | 121 | +// +kubebuilder:validation:Enum=extract;inject;propagate;ignore | 
|  | 122 | +type TraceContext string | 
|  | 123 | + | 
|  | 124 | +const ( | 
|  | 125 | +	// TraceContextExtract uses an existing trace context from the request, so that the identifiers | 
|  | 126 | +	// of a trace and the parent span are inherited from the incoming request. | 
|  | 127 | +	TraceContextExtract TraceContext = "extract" | 
|  | 128 | + | 
|  | 129 | +	// TraceContextInject adds a new context to the request, overwriting existing headers, if any. | 
|  | 130 | +	TraceContextInject TraceContext = "inject" | 
|  | 131 | + | 
|  | 132 | +	// TraceContextPropagate updates the existing context (combines extract and inject). | 
|  | 133 | +	TraceContextPropagate TraceContext = "propagate" | 
|  | 134 | + | 
|  | 135 | +	// TraceContextIgnore skips context headers processing. | 
|  | 136 | +	TraceContextIgnore TraceContext = "ignore" | 
|  | 137 | +) | 
0 commit comments