Skip to content

Commit b2bb2fc

Browse files
committed
Restore RouteType and move capitalizer to helper
1 parent 938b93e commit b2bb2fc

File tree

9 files changed

+80
-31
lines changed

9 files changed

+80
-31
lines changed

internal/controller/state/graph/backend_refs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,7 @@ func validateRouteBackendRefAppProtocol(
689689
) error {
690690
//nolint: staticcheck // used in status condition which is normally capitalized
691691
err := fmt.Errorf(
692-
"The RouteType %s does not support service port appProtocol %s",
692+
"The Route type %s does not support service port appProtocol %s",
693693
routeType,
694694
appProtocol,
695695
)

internal/controller/state/graph/backend_refs_test.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -985,12 +985,12 @@ func TestAddBackendRefsToRules(t *testing.T) {
985985
},
986986
expectedConditions: []conditions.Condition{
987987
conditions.NewRouteBackendRefUnsupportedProtocol(
988-
"The RouteType http does not support service port appProtocol kubernetes.io/h2c;" +
988+
"The Route type http does not support service port appProtocol kubernetes.io/h2c;" +
989989
" nginx does not support proxying to upstreams with http2 or h2c",
990990
),
991991
},
992992
policies: emptyPolicies,
993-
name: "invalid backendRef with service port appProtocol h2c and The RouteType http",
993+
name: "invalid backendRef with service port appProtocol h2c and Route type http",
994994
},
995995
{
996996
route: createRoute("hr1", RouteTypeHTTP, "Service", 1, "svcWS"),
@@ -1005,7 +1005,7 @@ func TestAddBackendRefsToRules(t *testing.T) {
10051005
},
10061006
expectedConditions: nil,
10071007
policies: emptyPolicies,
1008-
name: "valid backendRef with service port appProtocol ws and RouteType http",
1008+
name: "valid backendRef with service port appProtocol ws and Route type http",
10091009
},
10101010
{
10111011
route: createRoute("hr1", RouteTypeHTTP, "Service", 1, "svcWSS"),
@@ -1022,7 +1022,7 @@ func TestAddBackendRefsToRules(t *testing.T) {
10221022
expectedConditions: nil,
10231023
policies: policiesMatching,
10241024
name: "valid backendRef with service port appProtocol wss," +
1025-
" The RouteType http, and corresponding BackendTLSPolicy",
1025+
" The Route type http, and corresponding BackendTLSPolicy",
10261026
},
10271027
{
10281028
route: createRoute("hr1", RouteTypeHTTP, "Service", 1, "svcWSS"),
@@ -1037,12 +1037,12 @@ func TestAddBackendRefsToRules(t *testing.T) {
10371037
},
10381038
expectedConditions: []conditions.Condition{
10391039
conditions.NewRouteBackendRefUnsupportedProtocol(
1040-
"The RouteType http does not support service port appProtocol kubernetes.io/wss;" +
1040+
"The Route type http does not support service port appProtocol kubernetes.io/wss;" +
10411041
" missing corresponding BackendTLSPolicy",
10421042
),
10431043
},
10441044
policies: emptyPolicies,
1045-
name: "invalid backendRef with service port appProtocol wss, RouteType http, but missing BackendTLSPolicy",
1045+
name: "invalid backendRef with service port appProtocol wss, Route type http, but missing BackendTLSPolicy",
10461046
},
10471047
{
10481048
route: createRoute("gr1", RouteTypeGRPC, "Service", 1, "svcH2c"),
@@ -1057,7 +1057,7 @@ func TestAddBackendRefsToRules(t *testing.T) {
10571057
},
10581058
expectedConditions: nil,
10591059
policies: emptyPolicies,
1060-
name: "valid backendRef with service port appProtocol h2c and RouteType grpc",
1060+
name: "valid backendRef with service port appProtocol h2c and Route type grpc",
10611061
},
10621062
{
10631063
route: createRoute("gr1", RouteTypeGRPC, "Service", 1, "svcWS"),
@@ -1072,11 +1072,11 @@ func TestAddBackendRefsToRules(t *testing.T) {
10721072
},
10731073
expectedConditions: []conditions.Condition{
10741074
conditions.NewRouteBackendRefUnsupportedProtocol(
1075-
"The RouteType grpc does not support service port appProtocol kubernetes.io/ws",
1075+
"The Route type grpc does not support service port appProtocol kubernetes.io/ws",
10761076
),
10771077
},
10781078
policies: emptyPolicies,
1079-
name: "invalid backendRef with service port appProtocol ws and RouteType grpc",
1079+
name: "invalid backendRef with service port appProtocol ws and Route type grpc",
10801080
},
10811081
{
10821082
route: createRoute("gr1", RouteTypeGRPC, "Service", 1, "svcWSS"),
@@ -1091,11 +1091,11 @@ func TestAddBackendRefsToRules(t *testing.T) {
10911091
},
10921092
expectedConditions: []conditions.Condition{
10931093
conditions.NewRouteBackendRefUnsupportedProtocol(
1094-
"The RouteType grpc does not support service port appProtocol kubernetes.io/wss",
1094+
"The Route type grpc does not support service port appProtocol kubernetes.io/wss",
10951095
),
10961096
},
10971097
policies: emptyPolicies,
1098-
name: "invalid backendRef with service port appProtocol wss and RouteType grpc",
1098+
name: "invalid backendRef with service port appProtocol wss and Route type grpc",
10991099
},
11001100
{
11011101
route: createRoute("hr1", RouteTypeHTTP, "Service", 1, "svcGRPC"),
@@ -1111,7 +1111,7 @@ func TestAddBackendRefsToRules(t *testing.T) {
11111111
expectedConditions: nil,
11121112
policies: emptyPolicies,
11131113
name: "valid backendRef with non-Kubernetes Standard Application Protocol" +
1114-
" service port appProtocol and RouteType http",
1114+
" service port appProtocol and Route type http",
11151115
},
11161116
{
11171117
route: createRoute("gr1", RouteTypeGRPC, "Service", 1, "svcGRPC"),
@@ -1127,7 +1127,7 @@ func TestAddBackendRefsToRules(t *testing.T) {
11271127
expectedConditions: nil,
11281128
policies: emptyPolicies,
11291129
name: "valid backendRef with non-Kubernetes Standard Application Protocol" +
1130-
" service port appProtocol and RouteType grpc",
1130+
" service port appProtocol and Route type grpc",
11311131
},
11321132
{
11331133
route: modRoute(createRoute("hr1", RouteTypeHTTP, "Service", 1, "svc1"), func(route *L7Route) *L7Route {

internal/controller/state/graph/gateway.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package graph
22

33
import (
4-
"strings"
5-
64
"k8s.io/apimachinery/pkg/types"
75
"k8s.io/apimachinery/pkg/util/validation/field"
86
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -11,6 +9,7 @@ import (
119
"github.com/nginx/nginx-gateway-fabric/v2/internal/controller/config"
1210
"github.com/nginx/nginx-gateway-fabric/v2/internal/controller/state/conditions"
1311
"github.com/nginx/nginx-gateway-fabric/v2/internal/framework/controller"
12+
"github.com/nginx/nginx-gateway-fabric/v2/internal/framework/helpers"
1413
"github.com/nginx/nginx-gateway-fabric/v2/internal/framework/kinds"
1514
)
1615

@@ -136,7 +135,7 @@ func validateGatewayParametersRef(npCfg *NginxProxy, ref v1.LocalParametersRefer
136135

137136
if _, ok := supportedParamKinds[string(ref.Kind)]; !ok {
138137
err := field.NotSupported(path.Child("kind"), string(ref.Kind), []string{kinds.NginxProxy})
139-
condMsg := strings.ToUpper(err.Error()[:1]) + err.Error()[1:] // Capitalize first letter
138+
condMsg := helpers.CapitalizeString(err.Error())
140139
conds = append(
141140
conds,
142141
conditions.NewGatewayRefInvalid(condMsg),
@@ -159,8 +158,7 @@ func validateGatewayParametersRef(npCfg *NginxProxy, ref v1.LocalParametersRefer
159158
}
160159

161160
if !npCfg.Valid {
162-
msg := npCfg.ErrMsgs.ToAggregate().Error()
163-
msg = strings.ToUpper(msg[:1]) + msg[1:]
161+
msg := helpers.CapitalizeString(npCfg.ErrMsgs.ToAggregate().Error())
164162
conds = append(
165163
conds,
166164
conditions.NewGatewayRefInvalid(msg),

internal/controller/state/graph/gatewayclass.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
v1 "sigs.k8s.io/gateway-api/apis/v1"
1111

1212
"github.com/nginx/nginx-gateway-fabric/v2/internal/controller/state/conditions"
13+
"github.com/nginx/nginx-gateway-fabric/v2/internal/framework/helpers"
1314
"github.com/nginx/nginx-gateway-fabric/v2/internal/framework/kinds"
1415
)
1516

@@ -130,8 +131,7 @@ func validateGatewayClassParametersRef(path *field.Path, ref v1.ParametersRefere
130131
}
131132

132133
if len(errs) > 0 {
133-
msg := errs.ToAggregate().Error()
134-
msg = strings.ToUpper(msg[:1]) + msg[1:]
134+
msg := helpers.CapitalizeString(errs.ToAggregate().Error())
135135
return []conditions.Condition{
136136
conditions.NewGatewayClassRefInvalid(msg),
137137
conditions.NewGatewayClassInvalidParameters(msg),
@@ -176,8 +176,7 @@ func validateGatewayClass(
176176
}
177177

178178
if !npCfg.Valid {
179-
msg := npCfg.ErrMsgs.ToAggregate().Error()
180-
msg = strings.ToUpper(msg[:1]) + msg[1:]
179+
msg := helpers.CapitalizeString(npCfg.ErrMsgs.ToAggregate().Error())
181180
conds = append(
182181
conds,
183182
conditions.NewGatewayClassRefInvalid(msg),

internal/controller/state/graph/httproute.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func buildHTTPRoute(
5454
field.NewPath("spec").Child("hostnames"),
5555
); err != nil {
5656
r.Valid = false
57-
condMsg := strings.ToUpper(err.Error()[:1]) + err.Error()[1:]
57+
condMsg := helpers.CapitalizeString(err.Error())
5858
r.Conditions = append(r.Conditions, conditions.NewRouteUnsupportedValue(condMsg))
5959

6060
return r
@@ -338,8 +338,7 @@ func processHTTPRouteRules(
338338

339339
// resolve errors do not invalidate routes
340340
if len(allRulesErrors.resolve) > 0 {
341-
msg := allRulesErrors.resolve.ToAggregate().Error()
342-
msg = strings.ToUpper(msg[:1]) + msg[1:]
341+
msg := helpers.CapitalizeString(allRulesErrors.resolve.ToAggregate().Error())
343342
conds = append(conds, conditions.NewRouteResolvedRefsInvalidFilter(msg))
344343
}
345344

internal/controller/state/graph/tlsroute.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package graph
22

33
import (
4-
"strings"
5-
64
apiv1 "k8s.io/api/core/v1"
75
"k8s.io/apimachinery/pkg/types"
86
"k8s.io/apimachinery/pkg/util/validation/field"
97
"sigs.k8s.io/gateway-api/apis/v1alpha2"
108

119
"github.com/nginx/nginx-gateway-fabric/v2/internal/controller/state/conditions"
10+
"github.com/nginx/nginx-gateway-fabric/v2/internal/framework/helpers"
1211
)
1312

1413
func buildTLSRoute(
@@ -38,7 +37,7 @@ func buildTLSRoute(
3837
field.NewPath("spec").Child("hostnames"),
3938
); err != nil {
4039
r.Valid = false
41-
condMsg := strings.ToUpper(err.Error()[:1]) + err.Error()[1:]
40+
condMsg := helpers.CapitalizeString(err.Error())
4241
r.Conditions = append(r.Conditions, conditions.NewRouteUnsupportedValue(condMsg))
4342
return r
4443
}

internal/controller/state/graph/tlsroute_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ func TestBuildTLSRoute(t *testing.T) {
383383
Attachable: true,
384384
Valid: true,
385385
Conditions: []conditions.Condition{conditions.NewRouteBackendRefUnsupportedProtocol(
386-
"The RouteType tls does not support service port appProtocol kubernetes.io/h2c",
386+
"The Route type tls does not support service port appProtocol kubernetes.io/h2c",
387387
)},
388388
},
389389
gateway: createGateway(),
@@ -412,7 +412,7 @@ func TestBuildTLSRoute(t *testing.T) {
412412
Attachable: true,
413413
Valid: true,
414414
Conditions: []conditions.Condition{conditions.NewRouteBackendRefUnsupportedProtocol(
415-
"The RouteType tls does not support service port appProtocol kubernetes.io/ws",
415+
"The Route type tls does not support service port appProtocol kubernetes.io/ws",
416416
)},
417417
},
418418
gateway: createGateway(),

internal/framework/helpers/helpers.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package helpers
44
import (
55
"bytes"
66
"fmt"
7+
"strings"
78
"text/template"
89

910
"github.com/google/go-cmp/cmp"
@@ -87,3 +88,12 @@ func MustExecuteTemplate(templ *template.Template, data interface{}) []byte {
8788

8889
return buf.Bytes()
8990
}
91+
92+
// CapitalizeString capitalizes the first letter of the string.
93+
func CapitalizeString(s string) string {
94+
if s == "" {
95+
return s
96+
}
97+
98+
return strings.ToUpper(s[:1]) + s[1:]
99+
}

internal/framework/helpers/helpers_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,47 @@ func TestMustExecuteTemplatePanics(t *testing.T) {
108108

109109
g.Expect(execute).To(Panic())
110110
}
111+
112+
func TestCapitalizeString(t *testing.T) {
113+
t.Parallel()
114+
115+
tests := []struct {
116+
name string
117+
in string
118+
out string
119+
}{
120+
{
121+
name: "empty string",
122+
in: "",
123+
out: "",
124+
},
125+
{
126+
name: "single lowercase letter",
127+
in: "a",
128+
out: "A",
129+
},
130+
{
131+
name: "lowercase word",
132+
in: "gateway",
133+
out: "Gateway",
134+
},
135+
{
136+
name: "Phrase with mixed case",
137+
in: "gateway API not found",
138+
out: "Gateway API not found",
139+
},
140+
{
141+
name: "non-letter first char",
142+
in: "1abc",
143+
out: "1abc",
144+
},
145+
}
146+
147+
for _, tc := range tests {
148+
t.Run(tc.name, func(t *testing.T) {
149+
t.Parallel()
150+
g := NewWithT(t)
151+
g.Expect(helpers.CapitalizeString(tc.in)).To(Equal(tc.out))
152+
})
153+
}
154+
}

0 commit comments

Comments
 (0)