Skip to content

Commit b5457fd

Browse files
committed
Fix golang-lint findings
1 parent 8eac53c commit b5457fd

File tree

13 files changed

+47
-64
lines changed

13 files changed

+47
-64
lines changed

.golangci.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ linters:
2424
- errname
2525
- ginkgolinter
2626
- gocheckcompilerdirectives
27-
- goconst
2827
- gocritic
2928
- gocyclo
3029
- godox

go.work.sum

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
cel.dev/expr v0.16.0/go.mod h1:TRSuuV7DlVCE/uwv5QbAiW/v8l5O8C4eEPHeu7gf7Sg=
2+
cel.dev/expr v0.16.1/go.mod h1:AsGA5zb3WruAEQeQng1RZdGEXmBj0jvMWh6l5SnNuC8=
23
cloud.google.com/go/compute v1.23.3/go.mod h1:VCgBUoMnIVIR0CscqQiPJLAG25E3ZRZMzcFZeQ+h8CI=
34
cloud.google.com/go/compute/metadata v0.5.0/go.mod h1:aHnloV2TPI38yx4s9+wAZhHykWvVCfu7hQbF+9CWoiY=
45
github.com/Masterminds/semver/v3 v3.2.1/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ=
@@ -20,6 +21,7 @@ github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMn
2021
github.com/cilium/ebpf v0.16.0/go.mod h1:L7u2Blt2jMM/vLAVgjxluxtBKlz3/GWjB0dMOEngfwE=
2122
github.com/cncf/udpa/go v0.0.0-20220112060539-c52dc94e7fbe/go.mod h1:6pvJx4me5XPnfI9Z40ddWsdw2W/uZgQLFXToKeRcDiI=
2223
github.com/cncf/xds/go v0.0.0-20240723142845-024c85f92f20/go.mod h1:W+zGtBO5Y1IgJhy4+A9GOqVhqLpfZi+vwmdNXUehLA8=
24+
github.com/cncf/xds/go v0.0.0-20240905190251-b4127c9b8d78/go.mod h1:W+zGtBO5Y1IgJhy4+A9GOqVhqLpfZi+vwmdNXUehLA8=
2325
github.com/containerd/console v1.0.4/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk=
2426
github.com/coreos/go-oidc v2.2.1+incompatible/go.mod h1:CgnwVTmzoESiwO9qyAFEMiHoZ1nMCKZlZ9V6mm3/LKc=
2527
github.com/coreos/go-semver v0.3.1/go.mod h1:irMmmIw/7yzSRPWryHsK7EYSg09caPQL03VsM8rvUec=
@@ -109,11 +111,18 @@ go.opentelemetry.io/otel/sdk v1.28.0/go.mod h1:oYj7ClPUA7Iw3m+r7GeEjz0qckQRJK2B8
109111
go.opentelemetry.io/otel/trace v1.28.0/go.mod h1:jPyXzNPg6da9+38HEwElrQiHlVMTnVfM3/yv2OlIHaI=
110112
go.opentelemetry.io/proto/otlp v1.3.1/go.mod h1:0X1WI4de4ZsLrrJNLAQbFeLCm3T7yBkR0XqQ7niQU+8=
111113
golang.org/x/arch v0.3.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
114+
golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U=
115+
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
116+
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
117+
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
112118
golang.org/x/telemetry v0.0.0-20240521205824-bda55230c457/go.mod h1:pRgIJT+bRLFKnoM1ldnzKoxTIn14Yxz928LQRYYgIN0=
119+
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
120+
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
113121
google.golang.org/appengine v1.6.8/go.mod h1:1jJ3jBArFh5pcgW8gCtRJnepW8FzD1V44FJffLiz/Ds=
114122
google.golang.org/genproto v0.0.0-20240123012728-ef4313101c80 h1:KAeGQVN3M9nD0/bQXnr/ClcEMJ968gUXJQ9pwfSynuQ=
115123
google.golang.org/genproto v0.0.0-20240123012728-ef4313101c80/go.mod h1:cc8bqMqtv9gMOr0zHg2Vzff5ULhhL2IXP4sbcn32Dro=
116124
google.golang.org/genproto/googleapis/api v0.0.0-20240814211410-ddb44dafa142/go.mod h1:d6be+8HhtEtucleCbxpPW9PA9XwISACu8nvpPqF0BVo=
125+
google.golang.org/genproto/googleapis/api v0.0.0-20240903143218-8af14fe29dc1/go.mod h1:qpvKtACPCQhAdu3PyQgV4l3LMXZEtft7y8QcarRsp9I=
117126
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
118127
gopkg.in/square/go-jose.v2 v2.6.0/go.mod h1:M9dMgbHiYLoDGQrXy7OpJDJWiKiU//h+vD76mk0e1AI=
119128
k8s.io/kms v0.31.2/go.mod h1:OZKwl1fan3n3N5FFxnW5C4V3ygrah/3YXeJWS3O6+94=

internal/ingress/controller/controller_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ func (ntc testNginxTestCommand) Test(cfg string) ([]byte, error) {
157157

158158
type fakeTemplate struct{}
159159

160-
func (fakeTemplate) Validate(filename string) error {
160+
func (fakeTemplate) Validate(_ string) error {
161161
return nil
162162
}
163163

internal/ingress/controller/template/crossplane/cors.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,10 @@ import (
2323
"k8s.io/ingress-nginx/internal/ingress/annotations/cors"
2424
)
2525

26-
func buildCorsDirectives(locationcors cors.Config) ngx_crossplane.Directives {
26+
func buildCorsDirectives(locationcors *cors.Config) ngx_crossplane.Directives {
2727
directives := make(ngx_crossplane.Directives, 0)
2828
if len(locationcors.CorsAllowOrigin) > 0 {
2929
directives = append(directives, buildCorsOriginRegex(locationcors.CorsAllowOrigin)...)
30-
3130
}
3231
directives = append(directives,
3332
buildBlockDirective("if",
@@ -43,7 +42,7 @@ func buildCorsDirectives(locationcors cors.Config) ngx_crossplane.Directives {
4342
}
4443

4544
// commonCorsDirective builds the common cors directives for a location
46-
func commonCorsDirective(cfg cors.Config, options bool) *ngx_crossplane.Directive {
45+
func commonCorsDirective(cfg *cors.Config, options bool) *ngx_crossplane.Directive {
4746
corsDir := "true"
4847
if options {
4948
corsDir = "trueoptions"

internal/ingress/controller/template/crossplane/extramodules/analyze.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ const (
7070

7171
// helpful directive location alias describing "any" context
7272
// doesn't include ngxHTTPSifConf, ngxHTTPLifConf, ngxHTTPLmtConf, or ngxMgmtMainConf.
73+
//
74+
//nolint:unused // This file is generated
7375
const ngxAnyConf = ngxMainConf | ngxEventConf | ngxMailMainConf | ngxMailSrvConf |
7476
ngxStreamMainConf | ngxStreamSrvConf | ngxStreamUpsConf |
7577
ngxHTTPMainConf | ngxHTTPSrvConf | ngxHTTPLocConf | ngxHTTPUpsConf |

internal/ingress/controller/template/crossplane/http.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func (c *Template) initHTTPDirectives() ngx_crossplane.Directives {
8585
return httpBlock
8686
}
8787

88-
//nolint:gocyclo
88+
//nolint:gocyclo // Function is what it is
8989
func (c *Template) buildHTTP() {
9090
cfg := c.tplConfig.Cfg
9191
httpBlock := c.initHTTPDirectives()
@@ -140,10 +140,10 @@ func (c *Template) buildHTTP() {
140140
}
141141

142142
if cfg.EnableBrotli {
143-
httpBlock = append(httpBlock, buildDirective("brotli", "on"))
144-
httpBlock = append(httpBlock, buildDirective("brotli_comp_level", cfg.BrotliLevel))
145-
httpBlock = append(httpBlock, buildDirective("brotli_min_length", cfg.BrotliMinLength))
146-
httpBlock = append(httpBlock, buildDirective("brotli_types", cfg.BrotliTypes))
143+
httpBlock = append(httpBlock, buildDirective("brotli", "on"),
144+
buildDirective("brotli_comp_level", cfg.BrotliLevel),
145+
buildDirective("brotli_min_length", cfg.BrotliMinLength),
146+
buildDirective("brotli_types", cfg.BrotliTypes))
147147
}
148148

149149
if (c.tplConfig.Cfg.EnableOpentelemetry || shouldLoadOpentelemetryModule(c.tplConfig.Servers)) &&
@@ -293,16 +293,17 @@ func (c *Template) buildHTTP() {
293293
httpBlock = append(httpBlock, buildBlockDirective("upstream", []string{"upstream_balancer"}, blockUpstreamDirectives))
294294

295295
// Adding Rate limit
296-
for _, rl := range filterRateLimits(c.tplConfig.Servers) {
297-
id := fmt.Sprintf("$allowlist_%s", rl.ID)
298-
httpBlock = append(httpBlock, buildDirective("#", "Ratelimit", rl.Name))
296+
rl := filterRateLimits(c.tplConfig.Servers)
297+
for i := range rl {
298+
id := fmt.Sprintf("$allowlist_%s", rl[i].ID)
299+
httpBlock = append(httpBlock, buildDirective("#", "Ratelimit", rl[i].Name))
299300
rlDirectives := ngx_crossplane.Directives{
300301
buildDirective("default", 0),
301302
}
302-
for _, ip := range rl.Allowlist {
303+
for _, ip := range rl[i].Allowlist {
303304
rlDirectives = append(rlDirectives, buildDirective(ip, "1"))
304305
}
305-
mapRateLimitDirective := buildMapDirective(id, fmt.Sprintf("$limit_%s", rl.ID), ngx_crossplane.Directives{
306+
mapRateLimitDirective := buildMapDirective(id, fmt.Sprintf("$limit_%s", rl[i].ID), ngx_crossplane.Directives{
306307
buildDirective("0", cfg.LimitConnZoneVariable),
307308
buildDirective("1", ""),
308309
})
@@ -343,10 +344,11 @@ func (c *Template) buildHTTP() {
343344

344345
if redirectServers, ok := c.tplConfig.RedirectServers.([]*utilingress.Redirect); ok {
345346
for _, server := range redirectServers {
346-
httpBlock = append(httpBlock, buildStartServer(server.From))
347-
serverBlock := c.buildRedirectServer(server)
348-
httpBlock = append(httpBlock, serverBlock)
349-
httpBlock = append(httpBlock, buildEndServer(server.From))
347+
httpBlock = append(httpBlock,
348+
buildStartServer(server.From),
349+
c.buildRedirectServer(server),
350+
buildEndServer(server.From),
351+
)
350352
}
351353
}
352354

internal/ingress/controller/template/crossplane/location.go

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ func buildCustomErrorLocationsPerServer(server *ingress.Server, enableMetrics bo
107107
errorLocationsDirectives = append(errorLocationsDirectives, buildCustomErrorLocation(errorLocations[i].UpstreamName, errorLocations[i].Codes, enableMetrics)...)
108108
}
109109
return errorLocationsDirectives
110-
111110
}
112111

113112
func buildCustomErrorLocation(upstreamName string, errorCodes []int, enableMetrics bool) ngx_crossplane.Directives {
@@ -199,7 +198,7 @@ func (c *Template) buildServerLocations(server *ingress.Server, locations []*ing
199198
buildDirective("add_header", "Set-Cookie", "$auth_cookie"),
200199
}
201200
if location.CorsConfig.CorsEnabled {
202-
directives = append(directives, buildCorsDirectives(location.CorsConfig)...)
201+
directives = append(directives, buildCorsDirectives(&location.CorsConfig)...)
203202
}
204203
directives = append(directives,
205204
buildDirective("return",
@@ -211,9 +210,7 @@ func (c *Template) buildServerLocations(server *ingress.Server, locations []*ing
211210

212211
}
213212
serverLocations = append(serverLocations, c.buildLocation(server, location, locationConfig))
214-
215213
}
216-
217214
return serverLocations
218215
}
219216

@@ -294,7 +291,7 @@ func (c *Template) buildAllowedLocation(server *ingress.Server, location *ingres
294291
}
295292

296293
if location.CorsConfig.CorsEnabled {
297-
dir = append(dir, buildCorsDirectives(location.CorsConfig)...)
294+
dir = append(dir, buildCorsDirectives(&location.CorsConfig)...)
298295
}
299296

300297
if !isLocationInLocationList(location, c.tplConfig.Cfg.NoAuthLocations) {
@@ -686,8 +683,8 @@ func buildAuthLocationConfig(location *ingress.Location, locationConfig location
686683
directives := make(ngx_crossplane.Directives, 0)
687684
if locationConfig.authPath != "" {
688685
if locationConfig.applyAuthUpstream && !locationConfig.applyGlobalAuth {
689-
directives = append(directives, buildDirective("set", "$auth_cookie", ""))
690-
directives = append(directives, buildDirective("add_header", "Set-Cookie", "$auth_cookie"))
686+
directives = append(directives, buildDirective("set", "$auth_cookie", ""),
687+
buildDirective("add_header", "Set-Cookie", "$auth_cookie"))
691688
directives = append(directives, buildAuthResponseHeaders(locationConfig.proxySetHeader, locationConfig.externalAuth.ResponseHeaders, true)...)
692689
if len(locationConfig.externalAuth.ResponseHeaders) > 0 {
693690
directives = append(directives, buildDirective("set", "$auth_response_headers", strings.Join(locationConfig.externalAuth.ResponseHeaders, ",")))
@@ -733,24 +730,4 @@ func buildAuthLocationConfig(location *ingress.Location, locationConfig location
733730
}
734731

735732
return directives
736-
/*
737-
Missing this Lua script
738-
# `auth_request` module does not support HTTP keepalives in upstream block:
739-
# https://trac.nginx.org/nginx/ticket/1579
740-
access_by_lua_block {
741-
local res = ngx.location.capture('{{ $authPath }}', { method = ngx.HTTP_GET, body = '', share_all_vars = {{ $externalAuth.KeepaliveShareVars }} })
742-
if res.status == ngx.HTTP_OK then
743-
ngx.var.auth_cookie = res.header['Set-Cookie']
744-
{{- range $line := buildAuthUpstreamLuaHeaders $externalAuth.ResponseHeaders }} # IF 4
745-
{{ $line }}
746-
{{- end }} # END IF 4
747-
return
748-
end
749-
if res.status == ngx.HTTP_UNAUTHORIZED or res.status == ngx.HTTP_FORBIDDEN then
750-
ngx.exit(res.status)
751-
end
752-
ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
753-
}
754-
755-
*/
756733
}

internal/ingress/controller/template/crossplane/server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func (c *Template) buildServerDirective(server *ingress.Server) *ngx_crossplane.
3737
buildDirective("ssl_certificate_by_lua_file", "/etc/nginx/lua/nginx/ngx_conf_certificate.lua"),
3838
}
3939

40-
serverBlock = append(serverBlock, buildListener(*c.tplConfig, server.Hostname)...)
40+
serverBlock = append(serverBlock, buildListener(c.tplConfig, server.Hostname)...)
4141
serverBlock = append(serverBlock, c.buildBlockers()...)
4242

4343
if server.Hostname == "_" {
@@ -167,7 +167,7 @@ func (c *Template) buildRedirectServer(server *utilingress.Redirect) *ngx_crossp
167167
buildDirective("ssl_certificate_by_lua_file", "/etc/nginx/lua/nginx/ngx_conf_certificate.lua"),
168168
buildDirective("set_by_lua_file", "$redirect_to", "/etc/nginx/lua/nginx/ngx_srv_redirect.lua", server.To),
169169
}
170-
serverBlock = append(serverBlock, buildListener(*c.tplConfig, server.From)...)
170+
serverBlock = append(serverBlock, buildListener(c.tplConfig, server.From)...)
171171
serverBlock = append(serverBlock, c.buildBlockers()...)
172172
serverBlock = append(serverBlock, buildDirective("return", c.tplConfig.Cfg.HTTPRedirectCode, "$redirect_to"))
173173

internal/ingress/controller/template/crossplane/utils.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ limitations under the License.
1717
package crossplane
1818

1919
import (
20-
"crypto/sha1"
20+
"crypto/sha1" //nolint: gosec // We cannot move away from sha1
2121
"encoding/base64"
2222
"encoding/hex"
2323
"fmt"
@@ -62,7 +62,7 @@ var (
6262
type seconds int
6363
type minutes int
6464

65-
func buildDirectiveWithComment(directive string, comment string, args ...any) *ngx_crossplane.Directive {
65+
func buildDirectiveWithComment(directive, comment string, args ...any) *ngx_crossplane.Directive {
6666
dir := buildDirective(directive, args...)
6767
dir.Comment = ptr.To(comment)
6868
return dir
@@ -213,25 +213,25 @@ func buildServerName(hostname string) string {
213213
return `~^(?<subdomain>[\w-]+)\.` + strings.Join(parts, "\\.") + `$`
214214
}
215215

216-
func buildListener(tc config.TemplateConfig, hostname string) ngx_crossplane.Directives {
216+
func buildListener(tc *config.TemplateConfig, hostname string) ngx_crossplane.Directives {
217217
listenDirectives := make(ngx_crossplane.Directives, 0)
218218

219-
co := commonListenOptions(&tc, hostname)
219+
co := commonListenOptions(tc, hostname)
220220

221221
addrV4 := []string{""}
222222
if len(tc.Cfg.BindAddressIpv4) > 0 {
223223
addrV4 = tc.Cfg.BindAddressIpv4
224224
}
225-
listenDirectives = append(listenDirectives, httpListener(addrV4, co, &tc, false)...)
226-
listenDirectives = append(listenDirectives, httpListener(addrV4, co, &tc, true)...)
225+
listenDirectives = append(listenDirectives, httpListener(addrV4, co, tc, false)...)
226+
listenDirectives = append(listenDirectives, httpListener(addrV4, co, tc, true)...)
227227

228228
if tc.IsIPV6Enabled {
229229
addrV6 := []string{"[::]"}
230230
if len(tc.Cfg.BindAddressIpv6) > 0 {
231231
addrV6 = tc.Cfg.BindAddressIpv6
232232
}
233-
listenDirectives = append(listenDirectives, httpListener(addrV6, co, &tc, false)...)
234-
listenDirectives = append(listenDirectives, httpListener(addrV6, co, &tc, true)...)
233+
listenDirectives = append(listenDirectives, httpListener(addrV6, co, tc, false)...)
234+
listenDirectives = append(listenDirectives, httpListener(addrV6, co, tc, true)...)
235235
}
236236

237237
return listenDirectives
@@ -258,7 +258,7 @@ func commonListenOptions(template *config.TemplateConfig, hostname string) []str
258258
return out
259259
}
260260

261-
func httpListener(addresses []string, co []string, tc *config.TemplateConfig, ssl bool) ngx_crossplane.Directives {
261+
func httpListener(addresses, co []string, tc *config.TemplateConfig, ssl bool) ngx_crossplane.Directives {
262262
listeners := make(ngx_crossplane.Directives, 0)
263263
port := tc.ListenPorts.HTTP
264264
isTLSProxy := tc.IsSSLPassthroughEnabled
@@ -400,7 +400,7 @@ func changeHostPort(newURL, value string) string {
400400
}
401401

402402
func buildAuthSignURLLocation(location, authSignURL string) string {
403-
hasher := sha1.New() // #nosec
403+
hasher := sha1.New() //nolint: gosec // We cannot move away from sha1
404404
hasher.Write([]byte(location))
405405
hasher.Write([]byte(authSignURL))
406406
return "@" + hex.EncodeToString(hasher.Sum(nil))
@@ -558,7 +558,6 @@ func buildProxyPass(backends []*ingress.Backend, location *ingress.Location) ngx
558558
}
559559

560560
func buildGeoIPDirectives(reloadTime int, files []string) ngx_crossplane.Directives {
561-
562561
directives := make(ngx_crossplane.Directives, 0)
563562
buildGeoIPBlock := func(file string, directives ngx_crossplane.Directives) *ngx_crossplane.Directive {
564563
if reloadTime > 0 && file != "GeoIP2-Connection-Type.mmdb" {

internal/ingress/controller/template/template.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ type LuaListenPorts struct {
234234
}
235235

236236
// Validate is no-op at go-template
237-
func (t *Template) Validate(filename string) error {
237+
func (t *Template) Validate(_ string) error {
238238
return nil
239239
}
240240

0 commit comments

Comments
 (0)