Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade go to v1.23 and fix linter errors #702

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 34 additions & 73 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
run:
# concurrency: 6
go: "1.20"
go: "1.23"
timeout: 2m
issues-exit-code: 1
tests: true
Expand All @@ -17,27 +17,24 @@ linters-settings:
check-type-assertions: false
check-blank: false
govet:
check-shadowing: true
enable:
- shadow
settings:
printf:
funcs:
- (github.com/sirupsen/logrus.FieldLogger).Infof
- (github.com/sirupsen/logrus.FieldLogger).Warnf
- (github.com/sirupsen/logrus.FieldLogger).Errorf
- (github.com/sirupsen/logrus.FieldLogger).Fatalf
golint:
min-confidence: 0.8
goimports:
local-prefixes: github.com/networkservicemesh
gocyclo:
min-complexity: 15
maligned:
suggest-new: true
dupl:
threshold: 150
funlen:
Lines: 100
Statements: 50
lines: 100
statements: 50
goconst:
min-len: 2
min-occurrences: 2
Expand All @@ -57,106 +54,70 @@ linters-settings:
simple: true
range-loops: true
for-loops: false
varnamelen:
ignore-names:
- cl
- m
- u
- wg
gocritic:
enabled-checks:
- appendAssign
- assignOp
- appendCombine
- argOrder
- badCall
- badCond
- boolExprSimplify
- builtinShadow
- captLocal
- caseOrder
- codegenComment
- commentFormatting
- commentedOutCode
- commentedOutImport
- defaultCaseOrder
- deprecatedComment
- docStub
- dupArg
- dupBranchBody
- dupCase
- dupImport
- dupSubExpr
- elseif
- emptyFallthrough
- emptyStringTest
- equalFold
- evalOrder
- exitAfterDefer
- flagDeref
- flagName
- hexLiteral
- hugeParam
- ifElseChain
- importShadow
- indexAlloc
- initClause
- methodExprCall
- nestingReduce
- newDeref
- nilValReturn
- octalLiteral
- offBy1
- paramTypeCombine
- rangeExprCopy
- rangeValCopy
- regexpMust
- regexpPattern
- singleCaseSwitch
- sloppyLen
- stringXbytes
- switchTrue
- typeAssertChain
- typeSwitchVar
- typeUnparen
- unlabelStmt
- unnamedResult
- unnecessaryBlock
- underef
- unlambda
- unslice
- valSwap
- weakCond
- wrapperFunc
- yodaStyleExpr
linters:
disable-all: true
enable:
- goheader
- bodyclose
- deadcode
- depguard
- dogsled
- dupl
- errcheck
- funlen
- gochecknoinits
- goconst
- gocritic
- gocyclo
- gofmt
- goimports
- golint
- gosec
- gosimple
- govet
- ineffassign
- interfacer
- misspell
- nakedret
- scopelint
- staticcheck
- structcheck
- stylecheck
- typecheck
- unconvert
- unparam
- varcheck
- whitespace
enable-all: true
disable:
- execinquery # The linter 'execinquery' is deprecated (since v1.58.0) due to: The repository of the linter has been archived by the owner.
- exportloopref # The linter 'exportloopref' is deprecated (since v1.60.2) due to: Since Go1.22 (loopvar) this linter is no longer relevant. Replaced by copyloopvar.
- paralleltest # t.Parallel() adds on test run stage if required
- gci # It conflicts with goimports

# New linters, that were not enabled or/either were not created on older linter version.
- testpackage
- thelper
- intrange
- exhaustruct
- nlreturn
- nonamedreturns
- wsl
- varnamelen
- ireturn
- gomnd
- containedctx
- mnd
- lll
- gofumpt
- godox
issues:
exclude-use-default: false
max-issues-per-linter: 0
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.20.12 as go
FROM golang:1.23 as go
ENV GO111MODULE=on
ENV CGO_ENABLED=0
ENV GOBIN=/bin
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/networkservicemesh/cmd-nsmgr

go 1.20
go 1.23

require (
github.com/antonfisher/nested-logrus-formatter v1.3.1
Expand Down
31 changes: 31 additions & 0 deletions go.sum

Large diffs are not rendered by default.

24 changes: 12 additions & 12 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ import (
"time"
)

// Config - configuration for cmd-nsmgr
// Config - configuration for cmd-nsmgr.
type Config struct {
Name string `default:"nmgr" desc:"Name of Network service manager"`
ListenOn []url.URL `default:"unix:///var/lib/networkservicemesh/nsm.io.sock" desc:"url to listen on. tcp:// one will be used a public to register NSM." split_words:"true"`
RegistryURL url.URL `default:"tcp://localhost:5001" desc:"A NSE registry url to use" split_words:"true"`
MaxTokenLifetime time.Duration `default:"10m" desc:"maximum lifetime of tokens" split_words:"true"`
Name string `default:"nmgr" desc:"Name of Network service manager"`
ListenOn []url.URL `default:"unix:///var/lib/networkservicemesh/nsm.io.sock" desc:"url to listen on. tcp:// one will be used a public to register NSM." split_words:"true"`
RegistryURL url.URL `default:"tcp://localhost:5001" desc:"A NSE registry url to use" split_words:"true"`
MaxTokenLifetime time.Duration `default:"10m" desc:"maximum lifetime of tokens" split_words:"true"`
RegistryServerPolicies []string `default:"etc/nsm/opa/common/.*.rego,etc/nsm/opa/registry/.*.rego,etc/nsm/opa/server/.*.rego" desc:"paths to files and directories that contain registry server policies" split_words:"true"`
RegistryClientPolicies []string `default:"etc/nsm/opa/common/.*.rego,etc/nsm/opa/registry/.*.rego,etc/nsm/opa/client/.*.rego" desc:"paths to files and directories that contain registry client policies" split_words:"true"`
LogLevel string `default:"INFO" desc:"Log level" split_words:"true"`
DialTimeout time.Duration `default:"750ms" desc:"Timeout for the dial the next endpoint" split_words:"true"`
ForwarderNetworkServiceName string `default:"forwarder" desc:"the default service name for forwarder discovering" split_words:"true"`
OpenTelemetryEndpoint string `default:"otel-collector.observability.svc.cluster.local:4317" desc:"OpenTelemetry Collector Endpoint" split_words:"true"`
MetricsExportInterval time.Duration `default:"10s" desc:"interval between mertics exports" split_words:"true"`
PprofEnabled bool `default:"false" desc:"is pprof enabled" split_words:"true"`
PprofListenOn string `default:"localhost:6060" desc:"pprof URL to ListenAndServe" split_words:"true"`
LogLevel string `default:"INFO" desc:"Log level" split_words:"true"`
DialTimeout time.Duration `default:"750ms" desc:"Timeout for the dial the next endpoint" split_words:"true"`
ForwarderNetworkServiceName string `default:"forwarder" desc:"the default service name for forwarder discovering" split_words:"true"`
OpenTelemetryEndpoint string `default:"otel-collector.observability.svc.cluster.local:4317" desc:"OpenTelemetry Collector Endpoint" split_words:"true"`
MetricsExportInterval time.Duration `default:"10s" desc:"interval between mertics exports" split_words:"true"`
PprofEnabled bool `default:"false" desc:"is pprof enabled" split_words:"true"`
PprofListenOn string `default:"localhost:6060" desc:"pprof URL to ListenAndServe" split_words:"true"`
}
2 changes: 1 addition & 1 deletion internal/imports/gen.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020-2022 Cisco and/or its affiliates.
// Copyright (c) 2020-2024 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down
5 changes: 4 additions & 1 deletion internal/manager/grpcfd.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) 2020 Doc.ai and/or its affiliates.
//
// Copyright (c) 2022-2024 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -14,7 +16,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//+build !linux
//go:build !linux
// +build !linux

package manager

Expand Down
15 changes: 13 additions & 2 deletions internal/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// Copyright (c) 2022 Nordix and/or its affiliates.
//
// Copyright (c) 2023 Cisco and/or its affiliates.
// Copyright (c) 2023-2024 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -79,14 +79,17 @@ func (m *manager) Stop() {
func (m *manager) initSecurity() (err error) {
// Get a X509Source
logrus.Infof("Obtaining X509 Certificate Source")

m.source, err = workloadapi.NewX509Source(m.ctx)
if err != nil {
logrus.Fatalf("error getting x509 source: %+v", err)
}

m.svid, err = m.source.GetX509SVID()
if err != nil {
logrus.Fatalf("error getting x509 svid: %+v", err)
}

logrus.Infof("SVID: %q", m.svid.ID)
return
}
Expand All @@ -105,6 +108,7 @@ func RunNsmgr(ctx context.Context, configuration *config.Config) error {

if err := m.initSecurity(); err != nil {
m.logger.Errorf("failed to create new spiffe TLS Peer %v", err)

return err
}

Expand Down Expand Up @@ -151,7 +155,7 @@ func RunNsmgr(ctx context.Context, configuration *config.Config) error {
mgrOptions = append(mgrOptions, nsmgr.WithRegistry(&configuration.RegistryURL))
}

m.mgr = nsmgr.NewServer(m.ctx, spiffejwt.TokenGeneratorFunc(m.source, m.configuration.MaxTokenLifetime), mgrOptions...)
m.mgr = nsmgr.NewServer(m.ctx, spiffejwt.TokenGeneratorFunc(m.source, m.configuration.MaxTokenLifetime), mgrOptions...) //nolint: contextcheck

// If we Listen on Unix socket for local connections we need to be sure folder are exist
createListenFolders(configuration)
Expand All @@ -173,6 +177,7 @@ func RunNsmgr(ctx context.Context, configuration *config.Config) error {

m.logger.Infof("Startup completed in %v", time.Since(starttime))
starttime = time.Now()

<-m.ctx.Done()

m.logger.Infof("Exit requested. Uptime: %v", time.Since(starttime))
Expand Down Expand Up @@ -205,6 +210,7 @@ func (m *manager) startServers(server *grpc.Server) {
var wg sync.WaitGroup
for i := 0; i < len(m.configuration.ListenOn); i++ {
listenURL := &m.configuration.ListenOn[i]

wg.Add(1)

go func() {
Expand All @@ -217,24 +223,29 @@ func (m *manager) startServers(server *grpc.Server) {
waitErrChan(m.ctx, errChan, m)
}()
}

wg.Wait()
}

func genPublishableURL(listenOn []url.URL, logger log.Logger) *url.URL {
u := defaultURL(listenOn)
addrs, err := net.InterfaceAddrs()

if err != nil {
logger.Warn(err.Error())
return u
}

return listenonurl.GetPublicURL(addrs, u)
}

func defaultURL(listenOn []url.URL) *url.URL {
for i := 0; i < len(listenOn); i++ {
u := &listenOn[i]
if u.Scheme == tcpSchema {
return u
}
}

return &listenOn[0]
}
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build linux
// +build linux

package main

import (
Expand Down Expand Up @@ -63,6 +66,7 @@ func main() {
if err := envconfig.Usage("nsm", cfg); err != nil {
log.FromContext(ctx).Fatal(err)
}

if err := envconfig.Process("nsm", cfg); err != nil {
log.FromContext(ctx).Fatalf("error processing cfg from env: %+v", err)
}
Expand All @@ -73,6 +77,7 @@ func main() {
if err != nil {
log.FromContext(ctx).Fatalf("invalid log level %s", cfg.LogLevel)
}

logrus.SetLevel(level)
log.EnableTracing(true)

Expand All @@ -82,6 +87,7 @@ func main() {
spanExporter := opentelemetry.InitSpanExporter(ctx, collectorAddress)
metricExporter := opentelemetry.InitOPTLMetricExporter(ctx, collectorAddress, cfg.MetricsExportInterval)
o := opentelemetry.Init(ctx, spanExporter, metricExporter, cfg.Name)

defer func() {
if err = o.Close(); err != nil {
log.FromContext(ctx).Error(err.Error())
Expand Down
Loading
Loading