Skip to content

Commit bb37df4

Browse files
committed
fix
Signed-off-by: zirain <zirain2009@gmail.com>
1 parent 119ebe8 commit bb37df4

8 files changed

Lines changed: 48 additions & 3 deletions

File tree

api/v1alpha1/envoypatchpolicy_types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ const (
8181
// using JSONPatch semantics.
8282
//
8383
// +kubebuilder:validation:XValidation:rule="!(has(self.name) && has(self.nameSelector))",message="only one of name and nameSelector can be specified"
84+
// +kubebuilder:validation:XValidation:rule="has(self.name) || has(self.nameSelector)",message="either name or nameSelector must be specified"
8485
type EnvoyJSONPatchConfig struct {
8586
// Type is the typed URL of the Envoy xDS Resource
8687
Type EnvoyResourceType `json:"type"`

charts/gateway-crds-helm/templates/generated/gateway.envoyproxy.io_envoypatchpolicies.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ spec:
145145
x-kubernetes-validations:
146146
- message: only one of name and nameSelector can be specified
147147
rule: '!(has(self.name) && has(self.nameSelector))'
148+
- message: either name or nameSelector must be specified
149+
rule: has(self.name) || has(self.nameSelector)
148150
type: array
149151
priority:
150152
description: |-

charts/gateway-helm/charts/crds/crds/generated/gateway.envoyproxy.io_envoypatchpolicies.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ spec:
144144
x-kubernetes-validations:
145145
- message: only one of name and nameSelector can be specified
146146
rule: '!(has(self.name) && has(self.nameSelector))'
147+
- message: either name or nameSelector must be specified
148+
rule: has(self.name) || has(self.nameSelector)
147149
type: array
148150
priority:
149151
description: |-

internal/gatewayapi/envoypatchpolicy.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ func (t *Translator) ProcessEnvoyPatchPolicies(envoyPatchPolicies []*egv1a1.Envo
129129
}
130130

131131
// Save the patch
132+
policyRejected := false
132133
for _, patch := range policy.Spec.JSONPatches {
133134
irPatch := ir.JSONPatchConfig{}
134135
irPatch.Type = string(patch.Type)
@@ -154,7 +155,8 @@ func (t *Translator) ProcessEnvoyPatchPolicies(envoyPatchPolicies []*egv1a1.Envo
154155
resolveErr,
155156
)
156157

157-
continue
158+
policyRejected = true
159+
break
158160
}
159161
}
160162
irPatch.Operation.Op = ir.JSONPatchOp(patch.Operation.Op)
@@ -166,8 +168,14 @@ func (t *Translator) ProcessEnvoyPatchPolicies(envoyPatchPolicies []*egv1a1.Envo
166168
policyIR.JSONPatches = append(policyIR.JSONPatches, &irPatch)
167169
}
168170

169-
// Set Accepted=True
170-
status.SetAcceptedForPolicyAncestor(&policy.Status, &ancestorRef, t.GatewayControllerName, policy.Generation)
171+
// Only set Accepted=True if policy was not rejected due to validation errors
172+
if !policyRejected {
173+
// Set Accepted=True
174+
status.SetAcceptedForPolicyAncestor(&policy.Status, &ancestorRef, t.GatewayControllerName, policy.Generation)
175+
} else {
176+
// Clear any partial patches that were added before validation failed
177+
policyIR.JSONPatches = nil
178+
}
171179
}
172180

173181
return res

test/cel-validation/envoypatchpolicy_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,32 @@ func TestEnvoyPatchPolicy(t *testing.T) {
125125
},
126126
wantErrors: []string{"only one of name and nameSelector can be specified"},
127127
},
128+
{
129+
desc: "invalid json patch with neither name nor nameSelector",
130+
mutate: func(epp *egv1a1.EnvoyPatchPolicy) {
131+
epp.Spec = egv1a1.EnvoyPatchPolicySpec{
132+
Type: egv1a1.JSONPatchEnvoyPatchType,
133+
JSONPatches: []egv1a1.EnvoyJSONPatchConfig{
134+
{
135+
Type: egv1a1.ListenerEnvoyResourceType,
136+
Operation: egv1a1.JSONPatchOperation{
137+
Op: egv1a1.JSONPatchOperationType("test"),
138+
Path: &patchPath,
139+
Value: &apiextensionsv1.JSON{
140+
Raw: []byte(`"listener-1"`),
141+
},
142+
},
143+
},
144+
},
145+
TargetRef: gwapiv1.LocalPolicyTargetReference{
146+
Group: gwapiv1.Group(gwapiv1.GroupName),
147+
Kind: gwapiv1.Kind("Gateway"),
148+
Name: gwapiv1.ObjectName("eg"),
149+
},
150+
}
151+
},
152+
wantErrors: []string{"either name or nameSelector must be specified"},
153+
},
128154
}
129155

130156
for _, tc := range cases {

test/helm/gateway-crds-helm/all.out.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30517,6 +30517,8 @@ spec:
3051730517
x-kubernetes-validations:
3051830518
- message: only one of name and nameSelector can be specified
3051930519
rule: '!(has(self.name) && has(self.nameSelector))'
30520+
- message: either name or nameSelector must be specified
30521+
rule: has(self.name) || has(self.nameSelector)
3052030522
type: array
3052130523
priority:
3052230524
description: |-

test/helm/gateway-crds-helm/e2e.out.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8490,6 +8490,8 @@ spec:
84908490
x-kubernetes-validations:
84918491
- message: only one of name and nameSelector can be specified
84928492
rule: '!(has(self.name) && has(self.nameSelector))'
8493+
- message: either name or nameSelector must be specified
8494+
rule: has(self.name) || has(self.nameSelector)
84938495
type: array
84948496
priority:
84958497
description: |-

test/helm/gateway-crds-helm/envoy-gateway-crds.out.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8490,6 +8490,8 @@ spec:
84908490
x-kubernetes-validations:
84918491
- message: only one of name and nameSelector can be specified
84928492
rule: '!(has(self.name) && has(self.nameSelector))'
8493+
- message: either name or nameSelector must be specified
8494+
rule: has(self.name) || has(self.nameSelector)
84938495
type: array
84948496
priority:
84958497
description: |-

0 commit comments

Comments
 (0)