Skip to content

Commit b4c40fa

Browse files
committed
update API and code
Signed-off-by: zirain <zirain2009@gmail.com>
1 parent c315935 commit b4c40fa

11 files changed

Lines changed: 62 additions & 30 deletions

File tree

api/v1alpha1/envoypatchpolicy_types.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ const (
8282
type EnvoyJSONPatchConfig struct {
8383
// Type is the typed URL of the Envoy xDS Resource
8484
Type EnvoyResourceType `json:"type"`
85-
// Name is the name of the resource
85+
// Name is the name of the resource.
86+
// When omitted or set to empty string (""), the patch will be applied to all resources of the specified Type.
87+
// When specified, the patch will only be applied to the resource with the matching name.
8688
Name string `json:"name"`
8789
// Patch defines the JSON Patch Operation
8890
Operation JSONPatchOperation `json:"operation"`

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ spec:
6464
using JSONPatch semantic
6565
properties:
6666
name:
67-
description: Name is the name of the resource
67+
description: |-
68+
Name is the name of the resource.
69+
When omitted or set to empty string (""), the patch will be applied to all resources of the specified Type.
70+
When specified, the patch will only be applied to the resource with the matching name.
6871
type: string
6972
operation:
7073
description: Patch defines the JSON Patch Operation

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ spec:
6363
using JSONPatch semantic
6464
properties:
6565
name:
66-
description: Name is the name of the resource
66+
description: |-
67+
Name is the name of the resource.
68+
When omitted or set to empty string (""), the patch will be applied to all resources of the specified Type.
69+
When specified, the patch will only be applied to the resource with the matching name.
6770
type: string
6871
operation:
6972
description: Patch defines the JSON Patch Operation

internal/ir/xds.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2671,7 +2671,8 @@ type EnvoyPatchPolicyStatus struct {
26712671
type JSONPatchConfig struct {
26722672
// Type is the typed URL of the Envoy xDS Resource
26732673
Type string `json:"type" yaml:"type"`
2674-
// Name is the name of the resource
2674+
// Name is the name of the resource.
2675+
// When empty, the patch will be applied to all resources of the specified Type.
26752676
Name string `json:"name" yaml:"name"`
26762677
// Patch defines the JSON Patch Operation
26772678
Operation JSONPatchOperation `json:"operation" yaml:"operation"`

internal/xds/translator/jsonpatch.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func processJSONPatches(tCtx *types.ResourceVersionTable, envoyPatchPolicies []*
9595
continue
9696
}
9797

98-
// find the resources to patch and convert it to JSON
98+
// find the resources to patch and convert them to JSON
9999
dests, err = findXdsResources(tCtx, p)
100100
if err != nil {
101101
tErrs = errors.Join(tErrs, err)
@@ -108,8 +108,8 @@ func processJSONPatches(tCtx *types.ResourceVersionTable, envoyPatchPolicies []*
108108
continue
109109
}
110110

111-
var destsError error
112-
var destsPatched bool
111+
var patchErrors error
112+
var anyPatched bool
113113
for _, dest := range dests {
114114
var (
115115
resourceJSON []byte
@@ -119,13 +119,13 @@ func processJSONPatches(tCtx *types.ResourceVersionTable, envoyPatchPolicies []*
119119
resourceJSON, err = jsonMarshalOpts.Marshal(dest)
120120
if err != nil {
121121
tErr := fmt.Errorf("unable to marshal xds resource %s, err: %w", p.Type, err)
122-
destsError = errors.Join(destsError, tErr)
122+
patchErrors = errors.Join(patchErrors, tErr)
123123
continue
124124
}
125125

126126
modifiedJSON, err = jsonpatch.ApplyJSONPatches(resourceJSON, p.Operation)
127127
if err != nil {
128-
destsError = errors.Join(destsError, err)
128+
patchErrors = errors.Join(patchErrors, err)
129129
continue
130130
}
131131

@@ -134,13 +134,13 @@ func processJSONPatches(tCtx *types.ResourceVersionTable, envoyPatchPolicies []*
134134
// into and validated before saving it into the xds output resource
135135
temp, err := getXdsResourceType(p.Type)
136136
if err != nil {
137-
destsError = errors.Join(destsError, err)
137+
patchErrors = errors.Join(patchErrors, err)
138138
continue
139139
}
140140

141141
if err = protojson.Unmarshal(modifiedJSON, temp); err != nil {
142142
tErr := errors.New(unmarshalErrorMessage(err, string(modifiedJSON)))
143-
destsError = errors.Join(destsError, tErr)
143+
patchErrors = errors.Join(patchErrors, tErr)
144144
continue
145145
}
146146

@@ -149,26 +149,26 @@ func processJSONPatches(tCtx *types.ResourceVersionTable, envoyPatchPolicies []*
149149
if ok {
150150
if err = validator.Validate(); err != nil {
151151
tErr := fmt.Errorf("validation failed for xds resource %s, err:%s", p.Type, err.Error())
152-
destsError = errors.Join(destsError, tErr)
152+
patchErrors = errors.Join(patchErrors, tErr)
153153
continue
154154
}
155155
}
156156

157157
if err = deepCopyPtr(temp, dest); err != nil {
158158
tErr := fmt.Errorf("unable to copy xds resource %s, err: %w", p.Type, err)
159-
destsError = errors.Join(destsError, tErr)
159+
patchErrors = errors.Join(patchErrors, tErr)
160160
continue
161161
}
162162

163163
// Mark that at least one dest has been patched successfully,
164164
// so that we can report partial success if there are multiple dests and some of them fail
165-
destsPatched = true
165+
anyPatched = true
166166
}
167167

168168
// If there are multiple dests and some of them fail,
169169
// consider it as successful and ignore the failures to patch other dests.
170-
if !destsPatched && destsError != nil {
171-
tErrs = errors.Join(tErrs, destsError)
170+
if !anyPatched && patchErrors != nil {
171+
tErrs = errors.Join(tErrs, patchErrors)
172172
}
173173
}
174174

@@ -214,6 +214,9 @@ var jsonMarshalOpts = protojson.MarshalOptions{
214214
UseProtoNames: true,
215215
}
216216

217+
// findXdsResources returns XDS resources to patch based on the patch configuration.
218+
// If p.Name is empty, all resources of the specified type are returned.
219+
// If p.Name is specified, only resources with matching names are returned.
217220
func findXdsResources(tCtx *types.ResourceVersionTable, p *ir.JSONPatchConfig) ([]cachetypes.Resource, error) {
218221
var resources []cachetypes.Resource
219222
switch p.Type {

internal/xds/translator/translator.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -996,7 +996,9 @@ func findXdsListenerByHostPort(tCtx *types.ResourceVersionTable, address string,
996996
return nil
997997
}
998998

999-
// findXdsListeners finds a xds listener with the same name and returns nil if there is no match.
999+
// findXdsListeners finds xds listeners.
1000+
// If name is empty, returns all listeners.
1001+
// If name is specified, returns only listeners with matching names.
10001002
func findXdsListeners(tCtx *types.ResourceVersionTable, name string) []cachetypes.Resource {
10011003
if tCtx == nil || tCtx.XdsResources == nil || tCtx.XdsResources[resourcev3.ListenerType] == nil {
10021004
return nil
@@ -1008,7 +1010,7 @@ func findXdsListeners(tCtx *types.ResourceVersionTable, name string) []cachetype
10081010
if !ok {
10091011
continue
10101012
}
1011-
if name == "" || (listener != nil && listener.Name == name) {
1013+
if name == "" || listener.Name == name {
10121014
result = append(result, r)
10131015
}
10141016
}
@@ -1025,7 +1027,9 @@ func findXdsRouteConfig(tCtx *types.ResourceVersionTable, name string) *routev3.
10251027
return nil
10261028
}
10271029

1028-
// findXdsRouteConfigs finds a xds route with the name and returns nil if there is no match.
1030+
// findXdsRouteConfigs finds xds route configurations.
1031+
// If name is empty, returns all route configurations.
1032+
// If name is specified, returns only route configurations with matching names.
10291033
func findXdsRouteConfigs(tCtx *types.ResourceVersionTable, name string) []cachetypes.Resource {
10301034
if tCtx == nil || tCtx.XdsResources == nil || tCtx.XdsResources[resourcev3.RouteType] == nil {
10311035
return nil
@@ -1037,7 +1041,7 @@ func findXdsRouteConfigs(tCtx *types.ResourceVersionTable, name string) []cachet
10371041
if !ok {
10381042
continue
10391043
}
1040-
if name == "" || (route != nil && route.Name == name) {
1044+
if name == "" || route.Name == name {
10411045
result = append(result, r)
10421046
}
10431047
}
@@ -1054,7 +1058,9 @@ func findXdsCluster(tCtx *types.ResourceVersionTable, name string) *clusterv3.Cl
10541058
return nil
10551059
}
10561060

1057-
// findXdsClusters finds a xds cluster with the same name, and returns nil if there is no match.
1061+
// findXdsClusters finds xds clusters.
1062+
// If name is empty, returns all clusters.
1063+
// If name is specified, returns only clusters with matching names.
10581064
func findXdsClusters(tCtx *types.ResourceVersionTable, name string) []cachetypes.Resource {
10591065
if tCtx == nil || tCtx.XdsResources == nil || tCtx.XdsResources[resourcev3.ClusterType] == nil {
10601066
return nil
@@ -1066,15 +1072,17 @@ func findXdsClusters(tCtx *types.ResourceVersionTable, name string) []cachetypes
10661072
if !ok {
10671073
continue
10681074
}
1069-
if name == "" || (cluster != nil && cluster.Name == name) {
1075+
if name == "" || cluster.Name == name {
10701076
result = append(result, r)
10711077
}
10721078
}
10731079

10741080
return result
10751081
}
10761082

1077-
// findXdsEndpoints finds a xds endpoint with the same cluster name, and returns nil if there is no match.
1083+
// findXdsEndpoints finds xds endpoints.
1084+
// If name is empty, returns all endpoints.
1085+
// If name is specified, returns only endpoints with matching cluster names.
10781086
func findXdsEndpoints(tCtx *types.ResourceVersionTable, name string) []cachetypes.Resource {
10791087
if tCtx == nil || tCtx.XdsResources == nil || tCtx.XdsResources[resourcev3.EndpointType] == nil {
10801088
return nil
@@ -1086,7 +1094,7 @@ func findXdsEndpoints(tCtx *types.ResourceVersionTable, name string) []cachetype
10861094
if !ok {
10871095
continue
10881096
}
1089-
if name == "" || (endpoint != nil && endpoint.ClusterName == name) {
1097+
if name == "" || endpoint.ClusterName == name {
10901098
result = append(result, r)
10911099
}
10921100
}
@@ -1103,7 +1111,9 @@ func findXdsSecret(tCtx *types.ResourceVersionTable, name string) *tlsv3.Secret
11031111
return nil
11041112
}
11051113

1106-
// findXdsSecrets finds a xds secret with the same name, and returns nil if there is no match.
1114+
// findXdsSecrets finds xds secrets.
1115+
// If name is empty, returns all secrets.
1116+
// If name is specified, returns only secrets with matching names.
11071117
func findXdsSecrets(tCtx *types.ResourceVersionTable, name string) []cachetypes.Resource {
11081118
if tCtx == nil || tCtx.XdsResources == nil || tCtx.XdsResources[resourcev3.SecretType] == nil {
11091119
return nil
@@ -1115,7 +1125,7 @@ func findXdsSecrets(tCtx *types.ResourceVersionTable, name string) []cachetypes.
11151125
if !ok {
11161126
continue
11171127
}
1118-
if name == "" || (secret != nil && secret.Name == name) {
1128+
if name == "" || secret.Name == name {
11191129
result = append(result, r)
11201130
}
11211131
}

release-notes/current.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ new features: |
3838
Added support for retry budget in BackendTrafficPolicy.
3939
Added support for BackendUtilization load balancing policy in BackendTrafficPolicy.
4040
Added support for upstrean access log.
41+
Added support for patching multiple resources with empty name in EnvoyExtensionPolicy.
4142
4243
bug fixes: |
4344
Rejected ClientTrafficPolicy if invalid TLS cipher suites are configured.

site/content/en/latest/api/extension_types.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1839,7 +1839,7 @@ _Appears in:_
18391839
| Field | Type | Required | Default | Description |
18401840
| --- | --- | --- | --- | --- |
18411841
| `type` | _[EnvoyResourceType](#envoyresourcetype)_ | true | | Type is the typed URL of the Envoy xDS Resource |
1842-
| `name` | _string_ | true | | Name is the name of the resource |
1842+
| `name` | _string_ | true | | Name is the name of the resource.<br />When omitted or set to empty string (""), the patch will be applied to all resources of the specified Type.<br />When specified, the patch will only be applied to the resource with the matching name. |
18431843
| `operation` | _[JSONPatchOperation](#jsonpatchoperation)_ | true | | Patch defines the JSON Patch Operation |
18441844

18451845

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30251,7 +30251,10 @@ spec:
3025130251
using JSONPatch semantic
3025230252
properties:
3025330253
name:
30254-
description: Name is the name of the resource
30254+
description: |-
30255+
Name is the name of the resource.
30256+
When omitted or set to empty string (""), the patch will be applied to all resources of the specified Type.
30257+
When specified, the patch will only be applied to the resource with the matching name.
3025530258
type: string
3025630259
operation:
3025730260
description: Patch defines the JSON Patch Operation

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8224,7 +8224,10 @@ spec:
82248224
using JSONPatch semantic
82258225
properties:
82268226
name:
8227-
description: Name is the name of the resource
8227+
description: |-
8228+
Name is the name of the resource.
8229+
When omitted or set to empty string (""), the patch will be applied to all resources of the specified Type.
8230+
When specified, the patch will only be applied to the resource with the matching name.
82288231
type: string
82298232
operation:
82308233
description: Patch defines the JSON Patch Operation

0 commit comments

Comments
 (0)