|
| 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 | + // +kubebuilder:validation:MinItems=1 |
| 51 | + // +kubebuilder:validation:MaxItems=16 |
| 52 | + // +kubebuilder:validation:XValidation:message="TargetRef Kind must be: HTTPRoute or GRPCRoute",rule="(self.exists(t, t.kind=='HTTPRoute') || self.exists(t, t.kind=='GRPCRoute'))" |
| 53 | + // +kubebuilder:validation:XValidation:message="TargetRef Group must be gateway.networking.k8s.io.",rule="self.all(t, t.group=='gateway.networking.k8s.io')" |
| 54 | + //nolint:lll |
| 55 | + TargetRefs []gatewayv1alpha2.LocalPolicyTargetReference `json:"targetRefs"` |
| 56 | +} |
| 57 | + |
| 58 | +// Tracing allows for enabling and configuring OpenTelemetry tracing. |
| 59 | +// |
| 60 | +// +kubebuilder:validation:XValidation:message="ratio can only be specified if strategy is of type ratio",rule="!(has(self.ratio) && self.strategy != 'ratio')" |
| 61 | +// |
| 62 | +//nolint:lll |
| 63 | +type Tracing struct { |
| 64 | + // Strategy defines if tracing is ratio-based or parent-based. |
| 65 | + Strategy TraceStrategy `json:"strategy"` |
| 66 | + |
| 67 | + // Ratio is the percentage of traffic that should be sampled. Integer from 0 to 100. |
| 68 | + // By default, 100% of http requests are traced. Not applicable for parent-based tracing. |
| 69 | + // If ratio is set to 0, tracing is disabled. |
| 70 | + // |
| 71 | + // +optional |
| 72 | + // +kubebuilder:validation:Minimum=0 |
| 73 | + // +kubebuilder:validation:Maximum=100 |
| 74 | + Ratio *int32 `json:"ratio,omitempty"` |
| 75 | + |
| 76 | + // Context specifies how to propagate traceparent/tracestate headers. |
| 77 | + // Default: https://nginx.org/en/docs/ngx_otel_module.html#otel_trace_context |
| 78 | + // |
| 79 | + // +optional |
| 80 | + Context *TraceContext `json:"context,omitempty"` |
| 81 | + |
| 82 | + // SpanName defines the name of the Otel span. By default is the name of the location for a request. |
| 83 | + // If specified, applies to all locations that are created for a route. |
| 84 | + // Format: must have all '"' escaped and must not contain any '$' or end with an unescaped '\' |
| 85 | + // Examples of invalid names: some-$value, quoted-"value"-name, unescaped\ |
| 86 | + // |
| 87 | + // +optional |
| 88 | + // +kubebuilder:validation:MinLength=1 |
| 89 | + // +kubebuilder:validation:MaxLength=255 |
| 90 | + // +kubebuilder:validation:Pattern=`^([^"$\\]|\\[^$])*$` |
| 91 | + SpanName *string `json:"spanName,omitempty"` |
| 92 | + |
| 93 | + // SpanAttributes are custom key/value attributes that are added to each span. |
| 94 | + // |
| 95 | + // +optional |
| 96 | + // +listType=map |
| 97 | + // +listMapKey=key |
| 98 | + // +kubebuilder:validation:MaxItems=64 |
| 99 | + SpanAttributes []SpanAttribute `json:"spanAttributes,omitempty"` |
| 100 | +} |
| 101 | + |
| 102 | +// SpanAttribute is a key value pair to be added to a tracing span. |
| 103 | +type SpanAttribute struct { |
| 104 | + // Key is the key for a span attribute. |
| 105 | + // Format: must have all '"' escaped and must not contain any '$' or end with an unescaped '\' |
| 106 | + // |
| 107 | + // +kubebuilder:validation:MinLength=1 |
| 108 | + // +kubebuilder:validation:MaxLength=255 |
| 109 | + // +kubebuilder:validation:Pattern=`^([^"$\\]|\\[^$])*$` |
| 110 | + Key string `json:"key"` |
| 111 | + |
| 112 | + // Value is the value for a span attribute. |
| 113 | + // Format: must have all '"' escaped and must not contain any '$' or end with an unescaped '\' |
| 114 | + // |
| 115 | + // +kubebuilder:validation:MinLength=1 |
| 116 | + // +kubebuilder:validation:MaxLength=255 |
| 117 | + // +kubebuilder:validation:Pattern=`^([^"$\\]|\\[^$])*$` |
| 118 | + Value string `json:"value"` |
| 119 | +} |
| 120 | + |
| 121 | +// TraceStrategy defines the tracing strategy. |
| 122 | +// |
| 123 | +// +kubebuilder:validation:Enum=ratio;parent |
| 124 | +type TraceStrategy string |
| 125 | + |
| 126 | +const ( |
| 127 | + // TraceStrategyRatio enables ratio-based tracing, defaulting to 100% sampling rate. |
| 128 | + TraceStrategyRatio TraceStrategy = "ratio" |
| 129 | + |
| 130 | + // TraceStrategyParent enables tracing and only records spans if the parent span was sampled. |
| 131 | + TraceStrategyParent TraceStrategy = "parent" |
| 132 | +) |
| 133 | + |
| 134 | +// TraceContext specifies how to propagate traceparent/tracestate headers. |
| 135 | +// |
| 136 | +// +kubebuilder:validation:Enum=extract;inject;propagate;ignore |
| 137 | +type TraceContext string |
| 138 | + |
| 139 | +const ( |
| 140 | + // TraceContextExtract uses an existing trace context from the request, so that the identifiers |
| 141 | + // of a trace and the parent span are inherited from the incoming request. |
| 142 | + TraceContextExtract TraceContext = "extract" |
| 143 | + |
| 144 | + // TraceContextInject adds a new context to the request, overwriting existing headers, if any. |
| 145 | + TraceContextInject TraceContext = "inject" |
| 146 | + |
| 147 | + // TraceContextPropagate updates the existing context (combines extract and inject). |
| 148 | + TraceContextPropagate TraceContext = "propagate" |
| 149 | + |
| 150 | + // TraceContextIgnore skips context headers processing. |
| 151 | + TraceContextIgnore TraceContext = "ignore" |
| 152 | +) |
0 commit comments