Skip to content

Commit 30df2ca

Browse files
authored
Eliminate dead code (#3104)
1 parent 1bc5233 commit 30df2ca

File tree

4 files changed

+40
-111
lines changed

4 files changed

+40
-111
lines changed

internal/configs/annotations.go

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -148,20 +148,12 @@ func parseAnnotations(ingEx *IngressEx, baseCfgParams *ConfigParams, isPlus bool
148148
}
149149
}
150150

151-
if serverSnippets, exists, err := GetMapKeyAsStringSlice(ingEx.Ingress.Annotations, "nginx.org/server-snippets", ingEx.Ingress, "\n"); exists {
152-
if err != nil {
153-
glog.Error(err)
154-
} else {
155-
cfgParams.ServerSnippets = serverSnippets
156-
}
151+
if serverSnippets, exists := GetMapKeyAsStringSlice(ingEx.Ingress.Annotations, "nginx.org/server-snippets", ingEx.Ingress, "\n"); exists {
152+
cfgParams.ServerSnippets = serverSnippets
157153
}
158154

159-
if locationSnippets, exists, err := GetMapKeyAsStringSlice(ingEx.Ingress.Annotations, "nginx.org/location-snippets", ingEx.Ingress, "\n"); exists {
160-
if err != nil {
161-
glog.Error(err)
162-
} else {
163-
cfgParams.LocationSnippets = locationSnippets
164-
}
155+
if locationSnippets, exists := GetMapKeyAsStringSlice(ingEx.Ingress.Annotations, "nginx.org/location-snippets", ingEx.Ingress, "\n"); exists {
156+
cfgParams.LocationSnippets = locationSnippets
165157
}
166158

167159
if proxyConnectTimeout, exists := ingEx.Ingress.Annotations["nginx.org/proxy-connect-timeout"]; exists {
@@ -188,20 +180,12 @@ func parseAnnotations(ingEx *IngressEx, baseCfgParams *ConfigParams, isPlus bool
188180
}
189181
}
190182

191-
if proxyHideHeaders, exists, err := GetMapKeyAsStringSlice(ingEx.Ingress.Annotations, "nginx.org/proxy-hide-headers", ingEx.Ingress, ","); exists {
192-
if err != nil {
193-
glog.Error(err)
194-
} else {
195-
cfgParams.ProxyHideHeaders = proxyHideHeaders
196-
}
183+
if proxyHideHeaders, exists := GetMapKeyAsStringSlice(ingEx.Ingress.Annotations, "nginx.org/proxy-hide-headers", ingEx.Ingress, ","); exists {
184+
cfgParams.ProxyHideHeaders = proxyHideHeaders
197185
}
198186

199-
if proxyPassHeaders, exists, err := GetMapKeyAsStringSlice(ingEx.Ingress.Annotations, "nginx.org/proxy-pass-headers", ingEx.Ingress, ","); exists {
200-
if err != nil {
201-
glog.Error(err)
202-
} else {
203-
cfgParams.ProxyPassHeaders = proxyPassHeaders
204-
}
187+
if proxyPassHeaders, exists := GetMapKeyAsStringSlice(ingEx.Ingress.Annotations, "nginx.org/proxy-pass-headers", ingEx.Ingress, ","); exists {
188+
cfgParams.ProxyPassHeaders = proxyPassHeaders
205189
}
206190

207191
if clientMaxBodySize, exists := ingEx.Ingress.Annotations["nginx.org/client-max-body-size"]; exists {

internal/configs/configmaps.go

Lines changed: 26 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,12 @@ func ParseConfigMap(cfgm *v1.ConfigMap, nginxPlus bool, hasAppProtect bool, hasA
5858
cfgParams.ProxySendTimeout = proxySendTimeout
5959
}
6060

61-
if proxyHideHeaders, exists, err := GetMapKeyAsStringSlice(cfgm.Data, "proxy-hide-headers", cfgm, ","); exists {
62-
if err != nil {
63-
glog.Error(err)
64-
} else {
65-
cfgParams.ProxyHideHeaders = proxyHideHeaders
66-
}
61+
if proxyHideHeaders, exists := GetMapKeyAsStringSlice(cfgm.Data, "proxy-hide-headers", cfgm, ","); exists {
62+
cfgParams.ProxyHideHeaders = proxyHideHeaders
6763
}
6864

69-
if proxyPassHeaders, exists, err := GetMapKeyAsStringSlice(cfgm.Data, "proxy-pass-headers", cfgm, ","); exists {
70-
if err != nil {
71-
glog.Error(err)
72-
} else {
73-
cfgParams.ProxyPassHeaders = proxyPassHeaders
74-
}
65+
if proxyPassHeaders, exists := GetMapKeyAsStringSlice(cfgm.Data, "proxy-pass-headers", cfgm, ","); exists {
66+
cfgParams.ProxyPassHeaders = proxyPassHeaders
7567
}
7668

7769
if clientMaxBodySize, exists := cfgm.Data["client-max-body-size"]; exists {
@@ -170,12 +162,8 @@ func ParseConfigMap(cfgm *v1.ConfigMap, nginxPlus bool, hasAppProtect bool, hasA
170162
}
171163
}
172164

173-
if setRealIPFrom, exists, err := GetMapKeyAsStringSlice(cfgm.Data, "set-real-ip-from", cfgm, ","); exists {
174-
if err != nil {
175-
glog.Error(err)
176-
} else {
177-
cfgParams.SetRealIPFrom = setRealIPFrom
178-
}
165+
if setRealIPFrom, exists := GetMapKeyAsStringSlice(cfgm.Data, "set-real-ip-from", cfgm, ","); exists {
166+
cfgParams.SetRealIPFrom = setRealIPFrom
179167
}
180168

181169
if realIPRecursive, exists, err := GetMapKeyAsBool(cfgm.Data, "real-ip-recursive", cfgm); exists {
@@ -219,12 +207,8 @@ func ParseConfigMap(cfgm *v1.ConfigMap, nginxPlus bool, hasAppProtect bool, hasA
219207
}
220208
}
221209

222-
if logFormat, exists, err := GetMapKeyAsStringSlice(cfgm.Data, "log-format", cfgm, "\n"); exists {
223-
if err != nil {
224-
glog.Error(err)
225-
} else {
226-
cfgParams.MainLogFormat = logFormat
227-
}
210+
if logFormat, exists := GetMapKeyAsStringSlice(cfgm.Data, "log-format", cfgm, "\n"); exists {
211+
cfgParams.MainLogFormat = logFormat
228212
}
229213

230214
if logFormatEscaping, exists := cfgm.Data["log-format-escaping"]; exists {
@@ -234,12 +218,8 @@ func ParseConfigMap(cfgm *v1.ConfigMap, nginxPlus bool, hasAppProtect bool, hasA
234218
}
235219
}
236220

237-
if streamLogFormat, exists, err := GetMapKeyAsStringSlice(cfgm.Data, "stream-log-format", cfgm, "\n"); exists {
238-
if err != nil {
239-
glog.Error(err)
240-
} else {
241-
cfgParams.MainStreamLogFormat = streamLogFormat
242-
}
221+
if streamLogFormat, exists := GetMapKeyAsStringSlice(cfgm.Data, "stream-log-format", cfgm, "\n"); exists {
222+
cfgParams.MainStreamLogFormat = streamLogFormat
243223
}
244224

245225
if streamLogFormatEscaping, exists := cfgm.Data["stream-log-format-escaping"]; exists {
@@ -281,36 +261,20 @@ func ParseConfigMap(cfgm *v1.ConfigMap, nginxPlus bool, hasAppProtect bool, hasA
281261
cfgParams.ProxyMaxTempFileSize = proxyMaxTempFileSize
282262
}
283263

284-
if mainMainSnippets, exists, err := GetMapKeyAsStringSlice(cfgm.Data, "main-snippets", cfgm, "\n"); exists {
285-
if err != nil {
286-
glog.Error(err)
287-
} else {
288-
cfgParams.MainMainSnippets = mainMainSnippets
289-
}
264+
if mainMainSnippets, exists := GetMapKeyAsStringSlice(cfgm.Data, "main-snippets", cfgm, "\n"); exists {
265+
cfgParams.MainMainSnippets = mainMainSnippets
290266
}
291267

292-
if mainHTTPSnippets, exists, err := GetMapKeyAsStringSlice(cfgm.Data, "http-snippets", cfgm, "\n"); exists {
293-
if err != nil {
294-
glog.Error(err)
295-
} else {
296-
cfgParams.MainHTTPSnippets = mainHTTPSnippets
297-
}
268+
if mainHTTPSnippets, exists := GetMapKeyAsStringSlice(cfgm.Data, "http-snippets", cfgm, "\n"); exists {
269+
cfgParams.MainHTTPSnippets = mainHTTPSnippets
298270
}
299271

300-
if locationSnippets, exists, err := GetMapKeyAsStringSlice(cfgm.Data, "location-snippets", cfgm, "\n"); exists {
301-
if err != nil {
302-
glog.Error(err)
303-
} else {
304-
cfgParams.LocationSnippets = locationSnippets
305-
}
272+
if locationSnippets, exists := GetMapKeyAsStringSlice(cfgm.Data, "location-snippets", cfgm, "\n"); exists {
273+
cfgParams.LocationSnippets = locationSnippets
306274
}
307275

308-
if serverSnippets, exists, err := GetMapKeyAsStringSlice(cfgm.Data, "server-snippets", cfgm, "\n"); exists {
309-
if err != nil {
310-
glog.Error(err)
311-
} else {
312-
cfgParams.ServerSnippets = serverSnippets
313-
}
276+
if serverSnippets, exists := GetMapKeyAsStringSlice(cfgm.Data, "server-snippets", cfgm, "\n"); exists {
277+
cfgParams.ServerSnippets = serverSnippets
314278
}
315279

316280
if _, exists, err := GetMapKeyAsInt(cfgm.Data, "worker-processes", cfgm); exists {
@@ -373,23 +337,15 @@ func ParseConfigMap(cfgm *v1.ConfigMap, nginxPlus bool, hasAppProtect bool, hasA
373337
cfgParams.VirtualServerTemplate = &virtualServerTemplate
374338
}
375339

376-
if mainStreamSnippets, exists, err := GetMapKeyAsStringSlice(cfgm.Data, "stream-snippets", cfgm, "\n"); exists {
377-
if err != nil {
378-
glog.Error(err)
379-
} else {
380-
cfgParams.MainStreamSnippets = mainStreamSnippets
381-
}
340+
if mainStreamSnippets, exists := GetMapKeyAsStringSlice(cfgm.Data, "stream-snippets", cfgm, "\n"); exists {
341+
cfgParams.MainStreamSnippets = mainStreamSnippets
382342
}
383343

384-
if resolverAddresses, exists, err := GetMapKeyAsStringSlice(cfgm.Data, "resolver-addresses", cfgm, ","); exists {
385-
if err != nil {
386-
glog.Error(err)
344+
if resolverAddresses, exists := GetMapKeyAsStringSlice(cfgm.Data, "resolver-addresses", cfgm, ","); exists {
345+
if nginxPlus {
346+
cfgParams.ResolverAddresses = resolverAddresses
387347
} else {
388-
if nginxPlus {
389-
cfgParams.ResolverAddresses = resolverAddresses
390-
} else {
391-
glog.Warning("ConfigMap key 'resolver-addresses' requires NGINX Plus")
392-
}
348+
glog.Warning("ConfigMap key 'resolver-addresses' requires NGINX Plus")
393349
}
394350
}
395351

@@ -521,12 +477,8 @@ func ParseConfigMap(cfgm *v1.ConfigMap, nginxPlus bool, hasAppProtect bool, hasA
521477
}
522478

523479
if hasAppProtectDos {
524-
if appProtectDosLogFormat, exists, err := GetMapKeyAsStringSlice(cfgm.Data, "app-protect-dos-log-format", cfgm, "\n"); exists {
525-
if err != nil {
526-
glog.Error(err)
527-
} else {
528-
cfgParams.MainAppProtectDosLogFormat = appProtectDosLogFormat
529-
}
480+
if appProtectDosLogFormat, exists := GetMapKeyAsStringSlice(cfgm.Data, "app-protect-dos-log-format", cfgm, "\n"); exists {
481+
cfgParams.MainAppProtectDosLogFormat = appProtectDosLogFormat
530482
}
531483

532484
if appProtectDosLogFormatEscaping, exists := cfgm.Data["app-protect-dos-log-format-escaping"]; exists {

internal/configs/parsing_helpers.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,12 @@ func GetMapKeyAsUint64(m map[string]string, key string, context apiObject, nonZe
7979
}
8080

8181
// GetMapKeyAsStringSlice tries to find and parse a key in the map as string slice splitting it on delimiter.
82-
func GetMapKeyAsStringSlice(m map[string]string, key string, _ apiObject, delimiter string) ([]string, bool, error) {
82+
func GetMapKeyAsStringSlice(m map[string]string, key string, _ apiObject, delimiter string) ([]string, bool) {
8383
if str, exists := m[key]; exists {
8484
slice := strings.Split(str, delimiter)
85-
return slice, exists, nil
85+
return slice, exists
8686
}
87-
88-
return nil, false, nil
87+
return nil, false
8988
}
9089

9190
// ParseLBMethod parses method and matches it to a corresponding load balancing method in NGINX. An error is returned if method is not valid.

internal/configs/parsing_helpers_test.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,7 @@ func TestGetMapKeyAsStringSlice(t *testing.T) {
229229
"key": "1.String,2.String,3.String",
230230
}
231231

232-
slice, exists, err := GetMapKeyAsStringSlice(configMap.Data, "key", &configMap, ",")
233-
if err != nil {
234-
t.Errorf("Unexpected error: %v", err)
235-
}
232+
slice, exists := GetMapKeyAsStringSlice(configMap.Data, "key", &configMap, ",")
236233
if !exists {
237234
t.Errorf("The key 'key' must exist in the configMap")
238235
}
@@ -253,10 +250,7 @@ func TestGetMapKeyAsStringSliceMultilineSnippets(t *testing.T) {
253250
}`,
254251
}
255252

256-
slice, exists, err := GetMapKeyAsStringSlice(configMap.Data, "server-snippets", &configMap, "\n")
257-
if err != nil {
258-
t.Errorf("Unexpected error: %v", err)
259-
}
253+
slice, exists := GetMapKeyAsStringSlice(configMap.Data, "server-snippets", &configMap, "\n")
260254
if !exists {
261255
t.Errorf("The key 'server-snippets' must exist in the configMap")
262256
}
@@ -272,7 +266,7 @@ func TestGetMapKeyAsStringSliceNotFound(t *testing.T) {
272266
configMap := configMap
273267
configMap.Data = map[string]string{}
274268

275-
_, exists, _ := GetMapKeyAsStringSlice(configMap.Data, "key", &configMap, ",")
269+
_, exists := GetMapKeyAsStringSlice(configMap.Data, "key", &configMap, ",")
276270
if exists {
277271
t.Errorf("The key 'key' must not exist in the configMap")
278272
}

0 commit comments

Comments
 (0)