8
8
"strings"
9
9
gotemplate "text/template"
10
10
11
- ngfAPI "github.com/nginxinc/nginx-gateway-fabric/apis/v1alpha1"
12
11
"github.com/nginxinc/nginx-gateway-fabric/internal/framework/helpers"
13
12
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/nginx/config/http"
14
13
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/state/dataplane"
@@ -58,13 +57,23 @@ var grpcBaseHeaders = []http.Header{
58
57
},
59
58
}
60
59
60
+ type serverConfig struct {
61
+ Servers []http.Server
62
+ IPFamily http.IPFamily
63
+ }
64
+
61
65
func executeServers (conf dataplane.Configuration ) []executeResult {
62
66
ipFamily := getIPFamily (conf .BaseHTTPConfig )
63
- servers , httpMatchPairs := createServers (conf .HTTPServers , conf .SSLServers , ipFamily )
67
+ servers , httpMatchPairs := createServers (conf .HTTPServers , conf .SSLServers )
68
+
69
+ serverConfig := serverConfig {
70
+ Servers : servers ,
71
+ IPFamily : ipFamily ,
72
+ }
64
73
65
74
serverResult := executeResult {
66
75
dest : httpConfigFile ,
67
- data : helpers .MustExecuteTemplate (serversTemplate , servers ),
76
+ data : helpers .MustExecuteTemplate (serversTemplate , serverConfig ),
68
77
}
69
78
70
79
// create httpMatchPair conf
@@ -91,9 +100,9 @@ func executeServers(conf dataplane.Configuration) []executeResult {
91
100
// getIPFamily returns whether the server should be configured for IPv4, IPv6, or both.
92
101
func getIPFamily (baseHTTPConfig dataplane.BaseHTTPConfig ) http.IPFamily {
93
102
switch ip := baseHTTPConfig .IPFamily ; ip {
94
- case ngfAPI .IPv4 :
103
+ case dataplane .IPv4 :
95
104
return http.IPFamily {IPv4 : true }
96
- case ngfAPI .IPv6 :
105
+ case dataplane .IPv6 :
97
106
return http.IPFamily {IPv6 : true }
98
107
}
99
108
@@ -155,39 +164,30 @@ func createIncludes(additions []dataplane.Addition) []string {
155
164
return includes
156
165
}
157
166
158
- func createServers (
159
- httpServers ,
160
- sslServers []dataplane.VirtualServer ,
161
- ipFamily http.IPFamily ,
162
- ) ([]http.Server , httpMatchPairs ) {
167
+ func createServers (httpServers , sslServers []dataplane.VirtualServer ) ([]http.Server , httpMatchPairs ) {
163
168
servers := make ([]http.Server , 0 , len (httpServers )+ len (sslServers ))
164
169
finalMatchPairs := make (httpMatchPairs )
165
170
166
171
for serverID , s := range httpServers {
167
- httpServer , matchPairs := createServer (s , serverID , ipFamily )
172
+ httpServer , matchPairs := createServer (s , serverID )
168
173
servers = append (servers , httpServer )
169
174
maps .Copy (finalMatchPairs , matchPairs )
170
175
}
171
176
172
177
for serverID , s := range sslServers {
173
- sslServer , matchPair := createSSLServer (s , serverID , ipFamily )
178
+ sslServer , matchPair := createSSLServer (s , serverID )
174
179
servers = append (servers , sslServer )
175
180
maps .Copy (finalMatchPairs , matchPair )
176
181
}
177
182
178
183
return servers , finalMatchPairs
179
184
}
180
185
181
- func createSSLServer (
182
- virtualServer dataplane.VirtualServer ,
183
- serverID int ,
184
- ipFamily http.IPFamily ,
185
- ) (http.Server , httpMatchPairs ) {
186
+ func createSSLServer (virtualServer dataplane.VirtualServer , serverID int ) (http.Server , httpMatchPairs ) {
186
187
if virtualServer .IsDefault {
187
188
return http.Server {
188
189
IsDefaultSSL : true ,
189
190
Port : virtualServer .Port ,
190
- IPFamily : ipFamily ,
191
191
}, nil
192
192
}
193
193
@@ -203,20 +203,14 @@ func createSSLServer(
203
203
Port : virtualServer .Port ,
204
204
GRPC : grpc ,
205
205
Includes : createIncludes (virtualServer .Additions ),
206
- IPFamily : ipFamily ,
207
206
}, matchPairs
208
207
}
209
208
210
- func createServer (
211
- virtualServer dataplane.VirtualServer ,
212
- serverID int ,
213
- ipFamily http.IPFamily ,
214
- ) (http.Server , httpMatchPairs ) {
209
+ func createServer (virtualServer dataplane.VirtualServer , serverID int ) (http.Server , httpMatchPairs ) {
215
210
if virtualServer .IsDefault {
216
211
return http.Server {
217
212
IsDefaultHTTP : true ,
218
213
Port : virtualServer .Port ,
219
- IPFamily : ipFamily ,
220
214
}, nil
221
215
}
222
216
@@ -228,7 +222,6 @@ func createServer(
228
222
Port : virtualServer .Port ,
229
223
GRPC : grpc ,
230
224
Includes : createIncludes (virtualServer .Additions ),
231
- IPFamily : ipFamily ,
232
225
}, matchPairs
233
226
}
234
227
0 commit comments