Skip to content

Commit 73dc8b0

Browse files
authored
Remove Gateway API types from Configuration-related types in dataplane package (#976)
Problem: Configuration-related types in the data plane package include Gateway API types. As a result: - We need to make any validation-related assumptions about them (ex. certain fields cannot be not nil) in the config package. - It makes it difficult maintain/extend and potentially allow for supporting more routing resource types (outside of Gateway API) in the future. - It makes unit tests more complicated in the config package Solution: - Remove Gateway API types from Configuration-related types. - Move the types to types.go file. - Move converters to convert.go Testing: Unit testing Closes #660
1 parent aa973b2 commit 73dc8b0

File tree

10 files changed

+913
-1176
lines changed

10 files changed

+913
-1176
lines changed

internal/mode/static/nginx/config/maps_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func TestExecuteMaps(t *testing.T) {
1616
{
1717
MatchRules: []dataplane.MatchRule{
1818
{
19-
Filters: dataplane.Filters{
19+
Filters: dataplane.HTTPFilters{
2020
RequestHeaderModifiers: &dataplane.HTTPHeaderFilter{
2121
Add: []dataplane.HTTPHeader{
2222
{
@@ -28,7 +28,7 @@ func TestExecuteMaps(t *testing.T) {
2828
},
2929
},
3030
{
31-
Filters: dataplane.Filters{
31+
Filters: dataplane.HTTPFilters{
3232
RequestHeaderModifiers: &dataplane.HTTPHeaderFilter{
3333
Add: []dataplane.HTTPHeader{
3434
{
@@ -40,7 +40,7 @@ func TestExecuteMaps(t *testing.T) {
4040
},
4141
},
4242
{
43-
Filters: dataplane.Filters{
43+
Filters: dataplane.HTTPFilters{
4444
RequestHeaderModifiers: &dataplane.HTTPHeaderFilter{
4545
Set: []dataplane.HTTPHeader{
4646
{
@@ -100,7 +100,7 @@ func TestBuildAddHeaderMaps(t *testing.T) {
100100
{
101101
MatchRules: []dataplane.MatchRule{
102102
{
103-
Filters: dataplane.Filters{
103+
Filters: dataplane.HTTPFilters{
104104
RequestHeaderModifiers: &dataplane.HTTPHeaderFilter{
105105
Add: []dataplane.HTTPHeader{
106106
{
@@ -126,7 +126,7 @@ func TestBuildAddHeaderMaps(t *testing.T) {
126126
},
127127
},
128128
{
129-
Filters: dataplane.Filters{
129+
Filters: dataplane.HTTPFilters{
130130
RequestHeaderModifiers: &dataplane.HTTPHeaderFilter{
131131
Set: []dataplane.HTTPHeader{
132132
{

internal/mode/static/nginx/config/servers.go

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import (
66
"strings"
77
gotemplate "text/template"
88

9-
"sigs.k8s.io/gateway-api/apis/v1beta1"
10-
119
"github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/nginx/config/http"
1210
"github.com/nginxinc/nginx-kubernetes-gateway/internal/mode/static/state/dataplane"
1311
)
@@ -89,11 +87,9 @@ func createLocations(pathRules []dataplane.PathRule, listenerPort int32) []http.
8987
extLocations := initializeExternalLocations(rule, pathsAndTypes)
9088

9189
for matchRuleIdx, r := range rule.MatchRules {
92-
m := r.GetMatch()
93-
9490
buildLocations := extLocations
95-
if len(rule.MatchRules) != 1 || !isPathOnlyMatch(m) {
96-
intLocation, match := initializeInternalLocation(rule, matchRuleIdx, m)
91+
if len(rule.MatchRules) != 1 || !isPathOnlyMatch(r.Match) {
92+
intLocation, match := initializeInternalLocation(rule, matchRuleIdx, r.Match)
9793
buildLocations = []http.Location{intLocation}
9894
matches = append(matches, match)
9995
}
@@ -228,20 +224,20 @@ func initializeExternalLocations(
228224
func initializeInternalLocation(
229225
rule dataplane.PathRule,
230226
matchRuleIdx int,
231-
match v1beta1.HTTPRouteMatch,
227+
match dataplane.Match,
232228
) (http.Location, httpMatch) {
233229
path := createPathForMatch(rule.Path, rule.PathType, matchRuleIdx)
234230
return createMatchLocation(path), createHTTPMatch(match, path)
235231
}
236232

237-
func createReturnValForRedirectFilter(filter *v1beta1.HTTPRequestRedirectFilter, listenerPort int32) *http.Return {
233+
func createReturnValForRedirectFilter(filter *dataplane.HTTPRequestRedirectFilter, listenerPort int32) *http.Return {
238234
if filter == nil {
239235
return nil
240236
}
241237

242238
hostname := "$host"
243239
if filter.Hostname != nil {
244-
hostname = string(*filter.Hostname)
240+
hostname = *filter.Hostname
245241
}
246242

247243
code := http.StatusFound
@@ -251,7 +247,7 @@ func createReturnValForRedirectFilter(filter *v1beta1.HTTPRequestRedirectFilter,
251247

252248
port := listenerPort
253249
if filter.Port != nil {
254-
port = int32(*filter.Port)
250+
port = *filter.Port
255251
}
256252

257253
hostnamePort := fmt.Sprintf("%s:%d", hostname, port)
@@ -286,7 +282,7 @@ func createReturnValForRedirectFilter(filter *v1beta1.HTTPRequestRedirectFilter,
286282
// If the request satisfies the httpMatch, NGINX will redirect the request to the location RedirectPath.
287283
type httpMatch struct {
288284
// Method is the HTTPMethod of the HTTPRouteMatch.
289-
Method v1beta1.HTTPMethod `json:"method,omitempty"`
285+
Method string `json:"method,omitempty"`
290286
// RedirectPath is the path to redirect the request to if the request satisfies the match conditions.
291287
RedirectPath string `json:"redirectPath,omitempty"`
292288
// Headers is a list of HTTPHeaders name value pairs with the format "{name}:{value}".
@@ -297,7 +293,7 @@ type httpMatch struct {
297293
Any bool `json:"any,omitempty"`
298294
}
299295

300-
func createHTTPMatch(match v1beta1.HTTPRouteMatch, redirectPath string) httpMatch {
296+
func createHTTPMatch(match dataplane.Match, redirectPath string) httpMatch {
301297
hm := httpMatch{
302298
RedirectPath: redirectPath,
303299
}
@@ -316,14 +312,12 @@ func createHTTPMatch(match v1beta1.HTTPRouteMatch, redirectPath string) httpMatc
316312
headerNames := make(map[string]struct{})
317313

318314
for _, h := range match.Headers {
319-
if *h.Type == v1beta1.HeaderMatchExact {
320-
// duplicate header names are not permitted by the spec
321-
// only configure the first entry for every header name (case-insensitive)
322-
lowerName := strings.ToLower(string(h.Name))
323-
if _, ok := headerNames[lowerName]; !ok {
324-
headers = append(headers, createHeaderKeyValString(h))
325-
headerNames[lowerName] = struct{}{}
326-
}
315+
// duplicate header names are not permitted by the spec
316+
// only configure the first entry for every header name (case-insensitive)
317+
lowerName := strings.ToLower(h.Name)
318+
if _, ok := headerNames[lowerName]; !ok {
319+
headers = append(headers, createHeaderKeyValString(h))
320+
headerNames[lowerName] = struct{}{}
327321
}
328322
}
329323
hm.Headers = headers
@@ -333,9 +327,7 @@ func createHTTPMatch(match v1beta1.HTTPRouteMatch, redirectPath string) httpMatc
333327
params := make([]string, 0, len(match.QueryParams))
334328

335329
for _, p := range match.QueryParams {
336-
if *p.Type == v1beta1.QueryParamMatchExact {
337-
params = append(params, createQueryParamKeyValString(p))
338-
}
330+
params = append(params, createQueryParamKeyValString(p))
339331
}
340332
hm.QueryParams = params
341333
}
@@ -345,7 +337,7 @@ func createHTTPMatch(match v1beta1.HTTPRouteMatch, redirectPath string) httpMatc
345337

346338
// The name and values are delimited by "=". A name and value can always be recovered using strings.SplitN(arg,"=", 2).
347339
// Query Parameters are case-sensitive so case is preserved.
348-
func createQueryParamKeyValString(p v1beta1.HTTPQueryParamMatch) string {
340+
func createQueryParamKeyValString(p dataplane.HTTPQueryParamMatch) string {
349341
return string(p.Name) + "=" + p.Value
350342
}
351343

@@ -354,12 +346,12 @@ func createQueryParamKeyValString(p v1beta1.HTTPQueryParamMatch) string {
354346
// Ex. foo:bar == FOO:bar, but foo:bar != foo:BAR,
355347
// We preserve the case of the name here because NGINX allows us to look up the header names in a case-insensitive
356348
// manner.
357-
func createHeaderKeyValString(h v1beta1.HTTPHeaderMatch) string {
349+
func createHeaderKeyValString(h dataplane.HTTPHeaderMatch) string {
358350
return string(h.Name) + HeaderMatchSeparator + h.Value
359351
}
360352

361-
func isPathOnlyMatch(match v1beta1.HTTPRouteMatch) bool {
362-
return match.Method == nil && match.Headers == nil && match.QueryParams == nil
353+
func isPathOnlyMatch(match dataplane.Match) bool {
354+
return match.Method == nil && len(match.Headers) == 0 && len(match.QueryParams) == 0
363355
}
364356

365357
func createProxyPass(backendGroup dataplane.BackendGroup) string {

0 commit comments

Comments
 (0)