Skip to content

Commit b47bc2b

Browse files
kkk777-7jukie
andauthored
api: add cel validation of GrpcStatus (#8803)
* add cel validation of grpcStatus Signed-off-by: kkk777-7 <kota.kimura0725@gmail.com> * fix: gen-check Signed-off-by: kkk777-7 <kota.kimura0725@gmail.com> * add: cel test Signed-off-by: kkk777-7 <kota.kimura0725@gmail.com> --------- Signed-off-by: kkk777-7 <kota.kimura0725@gmail.com> Co-authored-by: Isaac Wilson <isaac.wilson514@gmail.com>
1 parent 874d43f commit b47bc2b

11 files changed

Lines changed: 65 additions & 4 deletions

File tree

api/v1alpha1/fault_injection.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ type FaultInjectionAbort struct {
5353
// GrpcStatus specifies the GRPC status code to be returned
5454
//
5555
// +optional
56+
// +kubebuilder:validation:Minimum=0
57+
// +kubebuilder:validation:Maximum=16
5658
GrpcStatus *int32 `json:"grpcStatus,omitempty"`
5759

5860
// Percentage specifies the percentage of requests to be aborted. Default 100%, if set 0, no requests will be aborted. Accuracy to 0.0001%.

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,8 @@ spec:
349349
description: GrpcStatus specifies the GRPC status code to
350350
be returned
351351
format: int32
352+
maximum: 16
353+
minimum: 0
352354
type: integer
353355
httpStatus:
354356
description: StatusCode specifies the HTTP status code to

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,8 @@ spec:
348348
description: GrpcStatus specifies the GRPC status code to
349349
be returned
350350
format: int32
351+
maximum: 16
352+
minimum: 0
351353
type: integer
352354
httpStatus:
353355
description: StatusCode specifies the HTTP status code to

internal/gatewayapi/backendtrafficpolicy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1695,7 +1695,7 @@ func (t *Translator) buildFaultInjection(policy *egv1a1.BackendTrafficPolicy) *i
16951695
}
16961696

16971697
if policy.Spec.FaultInjection.Abort.GrpcStatus != nil {
1698-
fi.Abort.GrpcStatus = policy.Spec.FaultInjection.Abort.GrpcStatus
1698+
fi.Abort.GrpcStatus = new(uint32(*policy.Spec.FaultInjection.Abort.GrpcStatus))
16991699
}
17001700
if policy.Spec.FaultInjection.Abort.HTTPStatus != nil {
17011701
fi.Abort.HTTPStatus = policy.Spec.FaultInjection.Abort.HTTPStatus

internal/ir/xds.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1689,7 +1689,7 @@ type FaultInjectionAbort struct {
16891689
// HTTPStatus defines the HTTP status code to be returned.
16901690
HTTPStatus *int32 `json:"httpStatus,omitempty" yaml:"httpStatus,omitempty"`
16911691
// GrpcStatus defines the gRPC status code to be returned.
1692-
GrpcStatus *int32 `json:"grpcStatus,omitempty" yaml:"grpcStatus,omitempty"`
1692+
GrpcStatus *uint32 `json:"grpcStatus,omitempty" yaml:"grpcStatus,omitempty"`
16931693
// Percentage defines the percentage of requests to be aborted.
16941694
Percentage *float32 `json:"percentage,omitempty" yaml:"percentage,omitempty"`
16951695
}

internal/ir/zz_generated.deepcopy.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/xds/translator/fault.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func (*fault) patchRoute(route *routev3.Route, irRoute *ir.HTTPRoute, _ *ir.HTTP
152152
}
153153
if abort.GrpcStatus != nil {
154154
routeCfgProto.Abort.ErrorType = &xdshttpfaultv3.FaultAbort_GrpcStatus{
155-
GrpcStatus: uint32(*abort.GrpcStatus),
155+
GrpcStatus: *abort.GrpcStatus,
156156
}
157157
}
158158
}

test/cel-validation/backendtrafficpolicy_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,6 +1081,55 @@ func TestBackendTrafficPolicyTarget(t *testing.T) {
10811081
},
10821082
wantErrors: []string{},
10831083
},
1084+
{
1085+
desc: "grpcStatus 16 (max valid value) is accepted",
1086+
mutate: func(btp *egv1a1.BackendTrafficPolicy) {
1087+
btp.Spec = egv1a1.BackendTrafficPolicySpec{
1088+
PolicyTargetReferences: egv1a1.PolicyTargetReferences{
1089+
TargetRef: &gwapiv1.LocalPolicyTargetReferenceWithSectionName{
1090+
LocalPolicyTargetReference: gwapiv1.LocalPolicyTargetReference{
1091+
Group: gwapiv1.Group("gateway.networking.k8s.io"),
1092+
Kind: gwapiv1.Kind("Gateway"),
1093+
Name: gwapiv1.ObjectName("eg"),
1094+
},
1095+
},
1096+
},
1097+
FaultInjection: &egv1a1.FaultInjection{
1098+
Abort: &egv1a1.FaultInjectionAbort{
1099+
GrpcStatus: new(int32(16)),
1100+
Percentage: new(float32(20)),
1101+
},
1102+
},
1103+
}
1104+
},
1105+
wantErrors: []string{},
1106+
},
1107+
{
1108+
desc: "grpcStatus 17 (exceeds max) is rejected",
1109+
mutate: func(btp *egv1a1.BackendTrafficPolicy) {
1110+
btp.Spec = egv1a1.BackendTrafficPolicySpec{
1111+
PolicyTargetReferences: egv1a1.PolicyTargetReferences{
1112+
TargetRef: &gwapiv1.LocalPolicyTargetReferenceWithSectionName{
1113+
LocalPolicyTargetReference: gwapiv1.LocalPolicyTargetReference{
1114+
Group: gwapiv1.Group("gateway.networking.k8s.io"),
1115+
Kind: gwapiv1.Kind("Gateway"),
1116+
Name: gwapiv1.ObjectName("eg"),
1117+
},
1118+
},
1119+
},
1120+
FaultInjection: &egv1a1.FaultInjection{
1121+
Abort: &egv1a1.FaultInjectionAbort{
1122+
GrpcStatus: new(int32(17)),
1123+
Percentage: new(float32(20)),
1124+
},
1125+
},
1126+
}
1127+
},
1128+
wantErrors: []string{
1129+
"spec.faultInjection.abort.grpcStatus: Invalid value:",
1130+
"should be less than or equal to 16",
1131+
},
1132+
},
10841133
{
10851134
desc: "httpStatus and grpcStatus are set at least one",
10861135
mutate: func(btp *egv1a1.BackendTrafficPolicy) {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22876,6 +22876,8 @@ spec:
2287622876
description: GrpcStatus specifies the GRPC status code to
2287722877
be returned
2287822878
format: int32
22879+
maximum: 16
22880+
minimum: 0
2287922881
type: integer
2288022882
httpStatus:
2288122883
description: StatusCode specifies the HTTP status code to

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,8 @@ spec:
849849
description: GrpcStatus specifies the GRPC status code to
850850
be returned
851851
format: int32
852+
maximum: 16
853+
minimum: 0
852854
type: integer
853855
httpStatus:
854856
description: StatusCode specifies the HTTP status code to

0 commit comments

Comments
 (0)