Skip to content

Commit 66cc0da

Browse files
committed
chore: improve validateClusterStatName
Signed-off-by: zirain <[email protected]>
1 parent 146e7f4 commit 66cc0da

File tree

2 files changed

+59
-12
lines changed

2 files changed

+59
-12
lines changed

api/v1alpha1/validation/envoyproxy_validate.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"net"
1212
"net/netip"
1313
"regexp"
14+
"strings"
1415

1516
"github.com/dominikbraun/graph"
1617
utilerrors "k8s.io/apimachinery/pkg/util/errors"
@@ -296,32 +297,34 @@ func validateFilterOrder(filterOrder []egv1a1.FilterPosition) error {
296297
return nil
297298
}
298299

299-
func ValidateRouteStatName(routeStatName string) error {
300-
supportedOperators := map[string]bool{
300+
var (
301+
routeStatSupportedOperators = map[string]bool{
301302
egv1a1.StatFormatterRouteName: true,
302303
egv1a1.StatFormatterRouteNamespace: true,
303304
egv1a1.StatFormatterRouteKind: true,
304305
egv1a1.StatFormatterRouteRuleName: true,
305306
}
306307

307-
if err := validateStatName(routeStatName, supportedOperators); err != nil {
308-
return fmt.Errorf("unable to configure Route Stat Name: %w", err)
309-
}
310-
311-
return nil
312-
}
313-
314-
func ValidateClusterStatName(clusterStatName string) error {
315-
supportedOperators := map[string]bool{
308+
clusterStatSupportedOperators = map[string]bool{
316309
egv1a1.StatFormatterRouteName: true,
317310
egv1a1.StatFormatterRouteNamespace: true,
318311
egv1a1.StatFormatterRouteKind: true,
319312
egv1a1.StatFormatterRouteRuleName: true,
320313
egv1a1.StatFormatterRouteRuleNumber: true,
321314
egv1a1.StatFormatterBackendRefs: true,
322315
}
316+
)
317+
318+
func ValidateRouteStatName(routeStatName string) error {
319+
if err := validateStatName(routeStatName, routeStatSupportedOperators); err != nil {
320+
return fmt.Errorf("unable to configure Route Stat Name: %w", err)
321+
}
323322

324-
if err := validateStatName(clusterStatName, supportedOperators); err != nil {
323+
return nil
324+
}
325+
326+
func ValidateClusterStatName(clusterStatName string) error {
327+
if err := validateStatName(clusterStatName, clusterStatSupportedOperators); err != nil {
325328
return fmt.Errorf("unable to configure Cluster Stat Name: %w", err)
326329
}
327330

@@ -331,6 +334,9 @@ func ValidateClusterStatName(clusterStatName string) error {
331334
func validateStatName(statName string, supportedOperators map[string]bool) error {
332335
var unsupportedOperators []string
333336
matches := statNameRegex.FindAllString(statName, -1)
337+
if len(matches) == 0 && strings.Contains(statName, "%") {
338+
return fmt.Errorf("unable to configure Cluster Stat Name with invalid operator")
339+
}
334340
for _, operator := range matches {
335341
if _, ok := supportedOperators[operator]; !ok {
336342
unsupportedOperators = append(unsupportedOperators, operator)

api/v1alpha1/validation/envoyproxy_validate_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -964,3 +964,44 @@ func TestGetEnvoyProxyComponentLevelArgs(t *testing.T) {
964964
})
965965
}
966966
}
967+
968+
func TestValidateClusterStatName(t *testing.T) {
969+
testCases := []struct {
970+
name string
971+
statName string
972+
expected bool
973+
}{
974+
{
975+
name: "valid cluster stat name with supported operators",
976+
statName: "%ROUTE_NAME%/%ROUTE_NAMESPACE%/%BACKEND_REFS%",
977+
expected: true,
978+
},
979+
{
980+
name: "invalid cluster stat name with unsupported operators",
981+
statName: "%ROUTE_NAME%/%FOO%/%BAR%",
982+
expected: false,
983+
},
984+
{
985+
name: "valid cluster stat name",
986+
statName: "any_custom_name",
987+
expected: true,
988+
},
989+
{
990+
name: "invalid cluster stat name",
991+
statName: "%ROUTE_NAME",
992+
expected: false,
993+
},
994+
}
995+
996+
for i := range testCases {
997+
tc := testCases[i]
998+
t.Run(tc.name, func(t *testing.T) {
999+
errs := ValidateClusterStatName(tc.statName)
1000+
if tc.expected {
1001+
require.NoError(t, errs)
1002+
} else {
1003+
require.Error(t, errs)
1004+
}
1005+
})
1006+
}
1007+
}

0 commit comments

Comments
 (0)