Skip to content

Commit 3327afe

Browse files
committed
Add validation for duplicate targetRefs
1 parent 774a7c2 commit 3327afe

File tree

6 files changed

+36
-3
lines changed

6 files changed

+36
-3
lines changed

apis/v1alpha1/observabilitypolicy_types.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,14 @@ type ObservabilityPolicySpec struct {
4747
// Objects must be in the same namespace as the policy.
4848
// Support: HTTPRoute, GRPCRoute.
4949
//
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+
//
5053
// +kubebuilder:validation:MinItems=1
5154
// +kubebuilder:validation:MaxItems=16
5255
// +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')"
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)))"
5458
//nolint:lll
5559
TargetRefs []gatewayv1alpha2.LocalPolicyTargetReference `json:"targetRefs"`
5660
}

apis/v1alpha1/upstreamsettingspolicy_types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,13 @@ type UpstreamSettingsPolicySpec struct {
5555
// Objects must be in the same namespace as the policy.
5656
// Support: Service
5757
//
58+
// TargetRefs must be _distinct_. The `name` field must be unique for all targetRef entries in the UpstreamSettingsPolicy.
59+
//
5860
// +kubebuilder:validation:MinItems=1
5961
// +kubebuilder:validation:MaxItems=16
6062
// +kubebuilder:validation:XValidation:message="TargetRefs Kind must be: Service",rule="self.all(t, t.kind=='Service')"
6163
// +kubebuilder:validation:XValidation:message="TargetRefs Group must be core",rule="self.exists(t, t.group=='') || self.exists(t, t.group=='core')"
64+
// +kubebuilder:validation:XValidation:message="TargetRef Name must be unique",rule="self.all(p1, self.exists_one(p2, p1.name == p2.name))"
6265
//nolint:lll
6366
TargetRefs []gatewayv1alpha2.LocalPolicyTargetReference `json:"targetRefs"`
6467
}

config/crd/bases/gateway.nginx.org_observabilitypolicies.yaml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ spec:
5555
TargetRefs identifies the API object(s) to apply the policy to.
5656
Objects must be in the same namespace as the policy.
5757
Support: HTTPRoute, GRPCRoute.
58+
59+
TargetRefs must be _distinct_. This means that the multi-part key defined by `kind` and `name` must
60+
be unique across all targetRef entries in the ObservabilityPolicy.
5861
items:
5962
description: |-
6063
LocalPolicyTargetReference identifies an API object to apply a direct or
@@ -90,8 +93,11 @@ spec:
9093
x-kubernetes-validations:
9194
- message: 'TargetRef Kind must be: HTTPRoute or GRPCRoute'
9295
rule: (self.exists(t, t.kind=='HTTPRoute') || self.exists(t, t.kind=='GRPCRoute'))
93-
- message: TargetRef Group must be gateway.networking.k8s.io.
96+
- message: TargetRef Group must be gateway.networking.k8s.io
9497
rule: self.all(t, t.group=='gateway.networking.k8s.io')
98+
- message: TargetRef Kind and Name combination must be unique
99+
rule: self.all(p1, self.exists_one(p2, (p1.name == p2.name) && (p1.kind
100+
== p2.kind)))
95101
tracing:
96102
description: Tracing allows for enabling and configuring tracing.
97103
properties:

config/crd/bases/gateway.nginx.org_upstreamsettingspolicies.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ spec:
9090
TargetRefs identifies API object(s) to apply the policy to.
9191
Objects must be in the same namespace as the policy.
9292
Support: Service
93+
94+
TargetRefs must be _distinct_. The `name` field must be unique for all targetRef entries in the UpstreamSettingsPolicy.
9395
items:
9496
description: |-
9597
LocalPolicyTargetReference identifies an API object to apply a direct or
@@ -127,6 +129,8 @@ spec:
127129
rule: self.all(t, t.kind=='Service')
128130
- message: TargetRefs Group must be core
129131
rule: self.exists(t, t.group=='') || self.exists(t, t.group=='core')
132+
- message: TargetRef Name must be unique
133+
rule: self.all(p1, self.exists_one(p2, p1.name == p2.name))
130134
zoneSize:
131135
description: |-
132136
ZoneSize is the size of the shared memory zone used by the upstream. This memory zone is used to share

deploy/crds.yaml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,9 @@ spec:
874874
TargetRefs identifies the API object(s) to apply the policy to.
875875
Objects must be in the same namespace as the policy.
876876
Support: HTTPRoute, GRPCRoute.
877+
878+
TargetRefs must be _distinct_. This means that the multi-part key defined by `kind` and `name` must
879+
be unique across all targetRef entries in the ObservabilityPolicy.
877880
items:
878881
description: |-
879882
LocalPolicyTargetReference identifies an API object to apply a direct or
@@ -909,8 +912,11 @@ spec:
909912
x-kubernetes-validations:
910913
- message: 'TargetRef Kind must be: HTTPRoute or GRPCRoute'
911914
rule: (self.exists(t, t.kind=='HTTPRoute') || self.exists(t, t.kind=='GRPCRoute'))
912-
- message: TargetRef Group must be gateway.networking.k8s.io.
915+
- message: TargetRef Group must be gateway.networking.k8s.io
913916
rule: self.all(t, t.group=='gateway.networking.k8s.io')
917+
- message: TargetRef Kind and Name combination must be unique
918+
rule: self.all(p1, self.exists_one(p2, (p1.name == p2.name) && (p1.kind
919+
== p2.kind)))
914920
tracing:
915921
description: Tracing allows for enabling and configuring tracing.
916922
properties:
@@ -1574,6 +1580,8 @@ spec:
15741580
TargetRefs identifies API object(s) to apply the policy to.
15751581
Objects must be in the same namespace as the policy.
15761582
Support: Service
1583+
1584+
TargetRefs must be _distinct_. The `name` field must be unique for all targetRef entries in the UpstreamSettingsPolicy.
15771585
items:
15781586
description: |-
15791587
LocalPolicyTargetReference identifies an API object to apply a direct or
@@ -1611,6 +1619,8 @@ spec:
16111619
rule: self.all(t, t.kind=='Service')
16121620
- message: TargetRefs Group must be core
16131621
rule: self.exists(t, t.group=='') || self.exists(t, t.group=='core')
1622+
- message: TargetRef Name must be unique
1623+
rule: self.all(p1, self.exists_one(p2, p1.name == p2.name))
16141624
zoneSize:
16151625
description: |-
16161626
ZoneSize is the size of the shared memory zone used by the upstream. This memory zone is used to share

site/content/reference/api.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,8 @@ Tracing
465465
<p>TargetRefs identifies the API object(s) to apply the policy to.
466466
Objects must be in the same namespace as the policy.
467467
Support: HTTPRoute, GRPCRoute.</p>
468+
<p>TargetRefs must be <em>distinct</em>. This means that the multi-part key defined by <code>kind</code> and <code>name</code> must
469+
be unique across all targetRef entries in the ObservabilityPolicy.</p>
468470
</td>
469471
</tr>
470472
</table>
@@ -683,6 +685,7 @@ UpstreamKeepAlive
683685
<p>TargetRefs identifies API object(s) to apply the policy to.
684686
Objects must be in the same namespace as the policy.
685687
Support: Service</p>
688+
<p>TargetRefs must be <em>distinct</em>. The <code>name</code> field must be unique for all targetRef entries in the UpstreamSettingsPolicy.</p>
686689
</td>
687690
</tr>
688691
</table>
@@ -1530,6 +1533,8 @@ Tracing
15301533
<p>TargetRefs identifies the API object(s) to apply the policy to.
15311534
Objects must be in the same namespace as the policy.
15321535
Support: HTTPRoute, GRPCRoute.</p>
1536+
<p>TargetRefs must be <em>distinct</em>. This means that the multi-part key defined by <code>kind</code> and <code>name</code> must
1537+
be unique across all targetRef entries in the ObservabilityPolicy.</p>
15331538
</td>
15341539
</tr>
15351540
</tbody>
@@ -2290,6 +2295,7 @@ UpstreamKeepAlive
22902295
<p>TargetRefs identifies API object(s) to apply the policy to.
22912296
Objects must be in the same namespace as the policy.
22922297
Support: Service</p>
2298+
<p>TargetRefs must be <em>distinct</em>. The <code>name</code> field must be unique for all targetRef entries in the UpstreamSettingsPolicy.</p>
22932299
</td>
22942300
</tr>
22952301
</tbody>

0 commit comments

Comments
 (0)