Skip to content

Commit 248e584

Browse files
committed
fix: golangci-lint staticcheck errors (#22642)
* fix: golangci-lint staticcheck errors * fix: missing braces for else in mesh_gateway.go * fix: remove unnecessary type check in otel_sink_test.go
1 parent f5d19c5 commit 248e584

File tree

104 files changed

+414
-449
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

104 files changed

+414
-449
lines changed

agent/agent.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2374,7 +2374,7 @@ func (a *Agent) addServiceLocked(req addServiceLockedRequest) error {
23742374
}
23752375
}
23762376

2377-
req.Service.EnterpriseMeta.Normalize()
2377+
req.Service.Normalize()
23782378

23792379
if err := a.validateService(req.Service, req.chkTypes); err != nil {
23802380
return err
@@ -2855,7 +2855,7 @@ func (a *Agent) AddCheck(check *structs.HealthCheck, chkType *structs.CheckType,
28552855
func (a *Agent) addCheckLocked(check *structs.HealthCheck, chkType *structs.CheckType, persist bool, token string, source configSource) error {
28562856
var service *structs.NodeService
28572857

2858-
check.EnterpriseMeta.Normalize()
2858+
check.Normalize()
28592859

28602860
if check.ServiceID != "" {
28612861
cid := check.CompoundServiceID()
@@ -3630,7 +3630,7 @@ func (a *Agent) storePid() error {
36303630

36313631
// Write out the PID
36323632
pid := os.Getpid()
3633-
_, err = pidFile.WriteString(fmt.Sprintf("%d", pid))
3633+
_, err = fmt.Fprintf(pidFile, "%d", pid)
36343634
if err != nil {
36353635
return fmt.Errorf("Could not write to pid file: %s", err)
36363636
}
@@ -3674,8 +3674,8 @@ func (a *Agent) loadServices(conf *config.RuntimeConfig, snap map[structs.CheckI
36743674
// Register the services from config
36753675
for _, service := range conf.Services {
36763676
// Default service partition to the same as agent
3677-
if service.EnterpriseMeta.PartitionOrEmpty() == "" {
3678-
service.EnterpriseMeta.OverridePartition(a.AgentEnterpriseMeta().PartitionOrDefault())
3677+
if service.PartitionOrEmpty() == "" {
3678+
service.OverridePartition(a.AgentEnterpriseMeta().PartitionOrDefault())
36793679
}
36803680

36813681
ns := service.NodeService()
@@ -3801,7 +3801,7 @@ func (a *Agent) loadServices(conf *config.RuntimeConfig, snap map[structs.CheckI
38013801
} else if !acl.EqualPartitions(a.AgentEnterpriseMeta().PartitionOrDefault(), p.Service.PartitionOrDefault()) {
38023802
a.logger.Info("Purging service file in wrong partition",
38033803
"file", file,
3804-
"partition", p.Service.EnterpriseMeta.PartitionOrDefault(),
3804+
"partition", p.Service.PartitionOrDefault(),
38053805
)
38063806
if err := os.Remove(file); err != nil {
38073807
a.logger.Error("Failed purging service file",

agent/agent_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,7 @@ func test_createAlias(t *testing.T, agent *TestAgent, chk *structs.CheckType, ex
797797
found = true
798798
assert.Equal(t, expectedResult, c.Check.Status, "Check state should be %s, was %s in %#v", expectedResult, c.Check.Status, c.Check)
799799
srvID := structs.NewServiceID(srv.ID, structs.WildcardEnterpriseMetaInDefaultPartition())
800-
if err := agent.Agent.State.RemoveService(srvID); err != nil {
800+
if err := agent.State.RemoveService(srvID); err != nil {
801801
fmt.Println("[DEBUG] Fail to remove service", srvID, ", err:=", err)
802802
}
803803
fmt.Println("[DEBUG] Service Removed", srvID, ", err:=", err)
@@ -2273,7 +2273,7 @@ func TestAgent_HTTPCheck_EnableAgentTLSForChecks(t *testing.T) {
22732273
Status: api.HealthCritical,
22742274
}
22752275

2276-
addr, err := firstAddr(a.Agent.apiServers, "https")
2276+
addr, err := firstAddr(a.apiServers, "https")
22772277
require.NoError(t, err)
22782278
url := fmt.Sprintf("https://%s/v1/agent/self", addr.String())
22792279
chk := &structs.CheckType{
@@ -5378,7 +5378,7 @@ func TestAutoConfig_Integration(t *testing.T) {
53785378
defer client.Shutdown()
53795379

53805380
retry.Run(t, func(r *retry.R) {
5381-
require.NotNil(r, client.Agent.tlsConfigurator.Cert())
5381+
require.NotNil(r, client.tlsConfigurator.Cert())
53825382
})
53835383

53845384
// when this is successful we managed to get the gossip key and serf addresses to bind to
@@ -5390,7 +5390,7 @@ func TestAutoConfig_Integration(t *testing.T) {
53905390
require.NotEmpty(t, client.tokens.AgentToken())
53915391

53925392
// grab the existing cert
5393-
cert1 := client.Agent.tlsConfigurator.Cert()
5393+
cert1 := client.tlsConfigurator.Cert()
53945394
require.NotNil(t, cert1)
53955395

53965396
// force a roots rotation by updating the CA config
@@ -5414,7 +5414,7 @@ func TestAutoConfig_Integration(t *testing.T) {
54145414

54155415
// ensure that a new cert gets generated and pushed into the TLS configurator
54165416
retry.Run(t, func(r *retry.R) {
5417-
require.NotEqual(r, cert1, client.Agent.tlsConfigurator.Cert())
5417+
require.NotEqual(r, cert1, client.tlsConfigurator.Cert())
54185418

54195419
// check that the on disk certs match expectations
54205420
data, err := os.ReadFile(filepath.Join(client.DataDir, "auto-config.json"))
@@ -5428,7 +5428,7 @@ func TestAutoConfig_Integration(t *testing.T) {
54285428

54295429
actual, err := tls.X509KeyPair([]byte(resp.Certificate.CertPEM), []byte(resp.Certificate.PrivateKeyPEM))
54305430
require.NoError(r, err)
5431-
require.Equal(r, client.Agent.tlsConfigurator.Cert(), &actual)
5431+
require.Equal(r, client.tlsConfigurator.Cert(), &actual)
54325432
})
54335433
}
54345434

@@ -5527,7 +5527,7 @@ func TestSharedRPCRouter(t *testing.T) {
55275527

55285528
testrpc.WaitForTestAgent(t, srv.RPC, "dc1")
55295529

5530-
mgr, server := srv.Agent.baseDeps.Router.FindLANRoute()
5530+
mgr, server := srv.baseDeps.Router.FindLANRoute()
55315531
require.NotNil(t, mgr)
55325532
require.NotNil(t, server)
55335533

@@ -5539,7 +5539,7 @@ func TestSharedRPCRouter(t *testing.T) {
55395539

55405540
testrpc.WaitForTestAgent(t, client.RPC, "dc1")
55415541

5542-
mgr, server = client.Agent.baseDeps.Router.FindLANRoute()
5542+
mgr, server = client.baseDeps.Router.FindLANRoute()
55435543
require.NotNil(t, mgr)
55445544
require.NotNil(t, server)
55455545
}

agent/auto-config/auto_config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func New(config Config) (*AutoConfig, error) {
9595
}
9696
}
9797

98-
if err := config.EnterpriseConfig.validateAndFinalize(); err != nil {
98+
if err := config.validateAndFinalize(); err != nil {
9999
return nil, err
100100
}
101101

agent/auto-config/auto_config_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ func TestInitialConfiguration_cancelled(t *testing.T) {
254254
}
255255
verify_outgoing = true
256256
`)
257-
mcfg.Config.Loader = loader.Load
257+
mcfg.Loader = loader.Load
258258

259259
expectedRequest := pbautoconf.AutoConfigRequest{
260260
Datacenter: "dc1",
@@ -290,7 +290,7 @@ func TestInitialConfiguration_restored(t *testing.T) {
290290
verify_outgoing = true
291291
`)
292292

293-
mcfg.Config.Loader = loader.Load
293+
mcfg.Loader = loader.Load
294294

295295
indexedRoots, cert, extraCACerts := mcfg.setupInitialTLS(t, "autoconf", "dc1", "secret")
296296

@@ -344,7 +344,7 @@ func TestInitialConfiguration_success(t *testing.T) {
344344
}
345345
verify_outgoing = true
346346
`)
347-
mcfg.Config.Loader = loader.Load
347+
mcfg.Loader = loader.Load
348348

349349
indexedRoots, cert, extraCerts := mcfg.setupInitialTLS(t, "autoconf", "dc1", "secret")
350350

@@ -423,10 +423,10 @@ func TestInitialConfiguration_retries(t *testing.T) {
423423
}
424424
verify_outgoing = true
425425
`)
426-
mcfg.Config.Loader = loader.Load
426+
mcfg.Loader = loader.Load
427427

428428
// reduce the retry wait times to make this test run faster
429-
mcfg.Config.Waiter = &retry.Waiter{MinFailures: 2, MaxWait: time.Millisecond}
429+
mcfg.Waiter = &retry.Waiter{MinFailures: 2, MaxWait: time.Millisecond}
430430

431431
indexedRoots, cert, extraCerts := mcfg.setupInitialTLS(t, "autoconf", "dc1", "secret")
432432

@@ -535,7 +535,7 @@ func TestGoRoutineManagement(t *testing.T) {
535535
}
536536
verify_outgoing = true
537537
`)
538-
mcfg.Config.Loader = loader.Load
538+
mcfg.Loader = loader.Load
539539

540540
// prepopulation is going to grab the token to populate the correct cache key
541541
mcfg.tokens.On("AgentToken").Return("secret").Times(0)
@@ -604,7 +604,7 @@ func TestGoRoutineManagement(t *testing.T) {
604604
waitForContexts := func() bool {
605605
ctxLock.Lock()
606606
defer ctxLock.Unlock()
607-
return !(rootsCtx == nil || leafCtx == nil)
607+
return rootsCtx != nil && leafCtx != nil
608608
}
609609

610610
// wait for the cache notifications to get started
@@ -676,8 +676,8 @@ func startedAutoConfig(t *testing.T, autoEncrypt bool) testAutoConfig {
676676
verify_outgoing = true
677677
`)
678678
}
679-
mcfg.Config.Loader = loader.Load
680-
mcfg.Config.FallbackLeeway = time.Nanosecond
679+
mcfg.Loader = loader.Load
680+
mcfg.FallbackLeeway = time.Nanosecond
681681

682682
originalToken := "a5deaa25-11ca-48bf-a979-4c3a7aa4b9a9"
683683

agent/auto-config/auto_encrypt_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ func TestAutoEncrypt_InitialCerts(t *testing.T) {
280280
resp.VerifyServerHostname = true
281281
})
282282

283-
mcfg.Config.Waiter = &retry.Waiter{MinFailures: 2, MaxWait: time.Millisecond}
283+
mcfg.Waiter = &retry.Waiter{MinFailures: 2, MaxWait: time.Millisecond}
284284

285285
ac := AutoConfig{
286286
config: &config.RuntimeConfig{
@@ -320,7 +320,7 @@ func TestAutoEncrypt_InitialConfiguration(t *testing.T) {
320320
}
321321
`)
322322
loader.opts.FlagValues.NodeName = &nodeName
323-
mcfg.Config.Loader = loader.Load
323+
mcfg.Loader = loader.Load
324324

325325
indexedRoots, cert, extraCerts := mcfg.setupInitialTLS(t, nodeName, datacenter, token)
326326

agent/auto-config/mock_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,20 @@ func newMockDirectRPC(t *testing.T) *mockDirectRPC {
3535

3636
func (m *mockDirectRPC) RPC(dc string, node string, addr net.Addr, method string, args interface{}, reply interface{}) error {
3737
var retValues mock.Arguments
38-
if method == "AutoConfig.InitialConfiguration" {
38+
switch method {
39+
case "AutoConfig.InitialConfiguration":
3940
req := args.(*pbautoconf.AutoConfigRequest)
4041
csr := req.CSR
4142
req.CSR = ""
4243
retValues = m.Called(dc, node, addr, method, args, reply)
4344
req.CSR = csr
44-
} else if method == "AutoEncrypt.Sign" {
45+
case "AutoEncrypt.Sign":
4546
req := args.(*structs.CASignRequest)
4647
csr := req.CSR
4748
req.CSR = ""
4849
retValues = m.Called(dc, node, addr, method, args, reply)
4950
req.CSR = csr
50-
} else {
51+
default:
5152
retValues = m.Called(dc, node, addr, method, args, reply)
5253
}
5354

@@ -383,7 +384,7 @@ func (m *mockedConfig) expectInitialTLS(t *testing.T, agentName, datacenter, tok
383384
true,
384385
).Return(nil).Once()
385386

386-
rootRes := cache.FetchResult{Value: indexedRoots, Index: indexedRoots.QueryMeta.Index}
387+
rootRes := cache.FetchResult{Value: indexedRoots, Index: indexedRoots.Index}
387388
rootsReq := structs.DCSpecificRequest{Datacenter: datacenter}
388389

389390
// we should prepopulate the cache with the CA roots

agent/auto-config/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func (ac *AutoConfig) run(ctx context.Context, exit chan struct{}) {
129129
return -1
130130
}
131131
expiry := cert.NotAfter.Add(ac.acConfig.FallbackLeeway)
132-
return expiry.Sub(time.Now())
132+
return time.Until(expiry)
133133
}
134134
fallbackTimer := time.NewTimer(calcFallbackInterval())
135135

agent/auto-config/tls.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func (ac *AutoConfig) populateCertificateCache(certs *structs.SignedResponse) er
9797
}
9898

9999
// prepolutate roots cache
100-
rootRes := cache.FetchResult{Value: &certs.ConnectCARoots, Index: certs.ConnectCARoots.QueryMeta.Index}
100+
rootRes := cache.FetchResult{Value: &certs.ConnectCARoots, Index: certs.ConnectCARoots.Index}
101101
rootsReq := ac.caRootsRequest()
102102
// getting the roots doesn't require a token so in order to potentially share the cache with another
103103
if err := ac.acConfig.Cache.Prepopulate(cachetype.ConnectCARootName, rootRes, ac.config.Datacenter, structs.DefaultPeerKeyword, "", rootsReq.CacheInfo().Key); err != nil {
@@ -110,7 +110,7 @@ func (ac *AutoConfig) populateCertificateCache(certs *structs.SignedResponse) er
110110
err = ac.acConfig.LeafCertManager.Prepopulate(
111111
context.Background(),
112112
leafReq.Key(),
113-
certs.IssuedCert.RaftIndex.ModifyIndex,
113+
certs.IssuedCert.ModifyIndex,
114114
&certs.IssuedCert,
115115
connect.EncodeSigningKeyID(cert.AuthorityKeyId),
116116
)

agent/catalog_endpoint.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ func (s *HTTPHandlers) CatalogDatacenters(resp http.ResponseWriter, req *http.Re
201201
parseCacheControl(resp, req, &args.QueryOptions)
202202
var out []string
203203

204-
if args.QueryOptions.UseCache {
204+
if args.UseCache {
205205
raw, m, err := s.agent.cache.Get(req.Context(), cachetype.CatalogDatacentersName, &args)
206206
if err != nil {
207207
metrics.IncrCounterWithLabels([]string{"client", "rpc", "error", "catalog_datacenters"}, 1,
@@ -251,12 +251,12 @@ RETRY_ONCE:
251251
if err := s.agent.RPC(req.Context(), "Catalog.ListNodes", &args, &out); err != nil {
252252
return nil, err
253253
}
254-
if args.QueryOptions.AllowStale && args.MaxStaleDuration > 0 && args.MaxStaleDuration < out.LastContact {
254+
if args.AllowStale && args.MaxStaleDuration > 0 && args.MaxStaleDuration < out.LastContact {
255255
args.AllowStale = false
256256
args.MaxStaleDuration = 0
257257
goto RETRY_ONCE
258258
}
259-
out.ConsistencyLevel = args.QueryOptions.ConsistencyLevel()
259+
out.ConsistencyLevel = args.ConsistencyLevel()
260260

261261
s.agent.TranslateAddresses(args.Datacenter, out.Nodes, dnsutil.TranslateAddressAcceptAny)
262262

@@ -285,7 +285,7 @@ func (s *HTTPHandlers) CatalogServices(resp http.ResponseWriter, req *http.Reque
285285
var out structs.IndexedServices
286286
defer setMeta(resp, &out.QueryMeta)
287287

288-
if args.QueryOptions.UseCache {
288+
if args.UseCache {
289289
raw, m, err := s.agent.cache.Get(req.Context(), cachetype.CatalogListServicesName, &args)
290290
if err != nil {
291291
metrics.IncrCounterWithLabels([]string{"client", "rpc", "error", "catalog_services"}, 1,
@@ -306,14 +306,14 @@ func (s *HTTPHandlers) CatalogServices(resp http.ResponseWriter, req *http.Reque
306306
s.nodeMetricsLabels())
307307
return nil, err
308308
}
309-
if args.QueryOptions.AllowStale && args.MaxStaleDuration > 0 && args.MaxStaleDuration < out.LastContact {
309+
if args.AllowStale && args.MaxStaleDuration > 0 && args.MaxStaleDuration < out.LastContact {
310310
args.AllowStale = false
311311
args.MaxStaleDuration = 0
312312
goto RETRY_ONCE
313313
}
314314
}
315315

316-
out.ConsistencyLevel = args.QueryOptions.ConsistencyLevel()
316+
out.ConsistencyLevel = args.ConsistencyLevel()
317317

318318
// Use empty map instead of nil
319319
if out.Services == nil {
@@ -377,7 +377,7 @@ func (s *HTTPHandlers) catalogServiceNodes(resp http.ResponseWriter, req *http.R
377377
var out structs.IndexedServiceNodes
378378
defer setMeta(resp, &out.QueryMeta)
379379

380-
if args.QueryOptions.UseCache {
380+
if args.UseCache {
381381
raw, m, err := s.agent.cache.Get(req.Context(), cachetype.CatalogServicesName, &args)
382382
if err != nil {
383383
metrics.IncrCounterWithLabels([]string{"client", "rpc", "error", "catalog_service_nodes"}, 1,
@@ -398,14 +398,14 @@ func (s *HTTPHandlers) catalogServiceNodes(resp http.ResponseWriter, req *http.R
398398
s.nodeMetricsLabels())
399399
return nil, err
400400
}
401-
if args.QueryOptions.AllowStale && args.MaxStaleDuration > 0 && args.MaxStaleDuration < out.LastContact {
401+
if args.AllowStale && args.MaxStaleDuration > 0 && args.MaxStaleDuration < out.LastContact {
402402
args.AllowStale = false
403403
args.MaxStaleDuration = 0
404404
goto RETRY_ONCE
405405
}
406406
}
407407

408-
out.ConsistencyLevel = args.QueryOptions.ConsistencyLevel()
408+
out.ConsistencyLevel = args.ConsistencyLevel()
409409
s.agent.TranslateAddresses(args.Datacenter, out.ServiceNodes, dnsutil.TranslateAddressAcceptAny)
410410

411411
// Use empty list instead of nil
@@ -453,12 +453,12 @@ RETRY_ONCE:
453453
s.nodeMetricsLabels())
454454
return nil, err
455455
}
456-
if args.QueryOptions.AllowStale && args.MaxStaleDuration > 0 && args.MaxStaleDuration < out.LastContact {
456+
if args.AllowStale && args.MaxStaleDuration > 0 && args.MaxStaleDuration < out.LastContact {
457457
args.AllowStale = false
458458
args.MaxStaleDuration = 0
459459
goto RETRY_ONCE
460460
}
461-
out.ConsistencyLevel = args.QueryOptions.ConsistencyLevel()
461+
out.ConsistencyLevel = args.ConsistencyLevel()
462462
if out.NodeServices != nil {
463463
s.agent.TranslateAddresses(args.Datacenter, out.NodeServices, dnsutil.TranslateAddressAcceptAny)
464464
}
@@ -518,12 +518,12 @@ RETRY_ONCE:
518518
s.nodeMetricsLabels())
519519
return nil, err
520520
}
521-
if args.QueryOptions.AllowStale && args.MaxStaleDuration > 0 && args.MaxStaleDuration < out.LastContact {
521+
if args.AllowStale && args.MaxStaleDuration > 0 && args.MaxStaleDuration < out.LastContact {
522522
args.AllowStale = false
523523
args.MaxStaleDuration = 0
524524
goto RETRY_ONCE
525525
}
526-
out.ConsistencyLevel = args.QueryOptions.ConsistencyLevel()
526+
out.ConsistencyLevel = args.ConsistencyLevel()
527527
s.agent.TranslateAddresses(args.Datacenter, &out.NodeServices, dnsutil.TranslateAddressAcceptAny)
528528

529529
// Use empty list instead of nil
@@ -565,12 +565,12 @@ RETRY_ONCE:
565565
s.nodeMetricsLabels())
566566
return nil, err
567567
}
568-
if args.QueryOptions.AllowStale && args.MaxStaleDuration > 0 && args.MaxStaleDuration < out.LastContact {
568+
if args.AllowStale && args.MaxStaleDuration > 0 && args.MaxStaleDuration < out.LastContact {
569569
args.AllowStale = false
570570
args.MaxStaleDuration = 0
571571
goto RETRY_ONCE
572572
}
573-
out.ConsistencyLevel = args.QueryOptions.ConsistencyLevel()
573+
out.ConsistencyLevel = args.ConsistencyLevel()
574574

575575
metrics.IncrCounterWithLabels([]string{"client", "api", "success", "catalog_gateway_services"}, 1,
576576
s.nodeMetricsLabels())

0 commit comments

Comments
 (0)