Skip to content

Commit

Permalink
Merge pull request #2423 from threefoldtech/development_fix_liny
Browse files Browse the repository at this point in the history
fix linter
  • Loading branch information
rawdaGastan authored Sep 22, 2024
2 parents a19e11a + e88cc8f commit ec5a85f
Show file tree
Hide file tree
Showing 26 changed files with 74 additions and 112 deletions.
17 changes: 17 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
linters:
enable:
- errcheck
- gofmt
- govet
- ineffassign
- unconvert
- staticcheck
- gocyclo
enable-all: false
linters-settings:
gocyclo:
# Minimal code complexity to report.
# Default: 30 (but we recommend 10-20)
min-complexity: 100
run:
timeout: 20m
44 changes: 7 additions & 37 deletions pkg/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,21 @@ GOPATH := $(shell go env GOPATH)

all: build

getdeps:
@echo "Installing golint" && go install github.com/golangci/golangci-lint/cmd/[email protected]
@echo "Installing gocyclo" && go install github.com/fzipp/gocyclo/cmd/gocyclo@latest
@echo "Installing deadcode" && go install github.com/remyoudompheng/go-misc/deadcode@latest
@echo "Installing misspell" && go install github.com/client9/misspell/cmd/misspell@latest
@echo "Installing ineffassign" && go install github.com/gordonklaus/ineffassign@latest
@echo "Installing staticcheck" && go install honnef.co/go/tools/cmd/staticcheck@latest

verifiers: vet fmt lint cyclo spelling static #deadcode

vet:
@echo "Running $@"
@go vet -atomic -bool -copylocks -nilfunc -printf -rangeloops -unreachable -unsafeptr -unusedresult ./...
all: getdeps test

fmt:
@echo "Running $@"
@gofmt -d .
getdeps:
@echo "Installing golangci-lint" && go get github.com/golangci/golangci-lint/cmd/golangci-lint && go install github.com/golangci/golangci-lint/cmd/golangci-lint
go mod tidy

lint:
@echo "Running $@"
@${GOPATH}/bin/golangci-lint run

ineffassign:
@echo "Running $@"
@${GOPATH}/bin/ineffassign .

cyclo:
@echo "Running $@"
@${GOPATH}/bin/gocyclo -over 100 .

deadcode:
@echo "Running $@"
@${GOPATH}/bin/deadcode -test $(shell go list ./...) || true

spelling:
@${GOPATH}/bin/misspell -i "monitord,forumla,etherent" -error `find .`

static:
@${GOPATH}/bin/staticcheck -- ./...
@${GOPATH}/bin/golangci-lint run -c ../.golangci.yml

check: test
test: verifiers build
test: lint build
go test -vet=off -v $(shell go list ./... | grep -Ev "stubs|network" )

testrace: verifiers build
testrace: lint build
go test -vet=off -v $(shell go list ./... | grep -Ev "stubs|network" )

generate:
Expand Down
4 changes: 2 additions & 2 deletions pkg/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ func (c *Module) start(ns, id string) error {

ctx := namespaces.WithNamespace(context.Background(), ns)

container, err := client.LoadContainer(ctx, string(id))
container, err := client.LoadContainer(ctx, id)
if err != nil {
return err
}
Expand Down Expand Up @@ -615,7 +615,7 @@ func (c *Module) Delete(ns string, id pkg.ContainerID) error {
_ = task.Kill(ctx, syscall.SIGTERM)
select {
case <-exitC:
case <-time.After(time.Duration(shutdownTimeout)):
case <-time.After(shutdownTimeout):
log.Debug().Str("id", string(id)).Int("signal", int(syscall.SIGKILL)).Msg("sending signal")
_ = task.Kill(ctx, syscall.SIGKILL)
select {
Expand Down
2 changes: 1 addition & 1 deletion pkg/container/stats/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func Monitor(addr string, ns string, id string, backend io.WriteCloser) error {

ctx := namespaces.WithNamespace(context.Background(), ns)

container, err := client.LoadContainer(ctx, string(id))
container, err := client.LoadContainer(ctx, id)
if err != nil {
log.Error().Err(err).Msg("metric container")
return err
Expand Down
3 changes: 2 additions & 1 deletion pkg/crypto/crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ func TestPyNACLCompatibilityEncryption(t *testing.T) {
sk := ed25519.NewKeyFromSeed(seed)

chiper := ""
fmt.Sscanf("0bfe9e3b9ce17fe6d570b165ea2a01034326b8c81d5f2c5384c8fe886552f074ec43017465598c4f5a857b495b445be46c3df48d14878bd0b1b907", "%x", &chiper)
_, err := fmt.Sscanf("0bfe9e3b9ce17fe6d570b165ea2a01034326b8c81d5f2c5384c8fe886552f074ec43017465598c4f5a857b495b445be46c3df48d14878bd0b1b907", "%x", &chiper)
require.NoError(t, err)

decrypted, err := Decrypt([]byte(chiper), sk)
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/flist/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (f *flistModule) cleanCache(now time.Time, age time.Duration) error {

if sys, ok := sys.(*syscall.Stat_t); ok {
// int64 cast required for arm32 targets
atime := time.Unix(int64(sys.Atim.Sec), int64(sys.Atim.Nsec))
atime := time.Unix(sys.Atim.Sec, sys.Atim.Nsec)

if now.Sub(atime) > age {
if err := os.Remove(path); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ func (g *gatewayModule) configPath(name string) string {

func (g *gatewayModule) validateNameContract(name string, twinID uint32) error {

contractID, subErr := g.substrateGateway.GetContractIDByNameRegistration(context.Background(), string(name))
contractID, subErr := g.substrateGateway.GetContractIDByNameRegistration(context.Background(), name)
if subErr.IsCode(pkg.CodeNotFound) {
return ErrContractNotReserved
}
Expand Down Expand Up @@ -633,7 +633,7 @@ func (g *gatewayModule) setupRouting(ctx context.Context, wlID string, fqdn stri

backend := config.Backends[0]

if err := zos.Backend(backend).Valid(config.TLSPassthrough); err != nil {
if err := backend.Valid(config.TLSPassthrough); err != nil {
return errors.Wrapf(err, "failed to validate backend '%s'", backend)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/gridtypes/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (i WorkloadID) Unique(n string) string {
b = b[:13]
}

return string(b)
return b
}

// IsValidName validates workload name
Expand Down
2 changes: 1 addition & 1 deletion pkg/gridtypes/workload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestTimestamp(t *testing.T) {
exp, err := json.Marshal(n)
require.NoError(err)

err = json.Unmarshal([]byte(exp), &v)
err = json.Unmarshal(exp, &v)
require.NoError(err)

require.Equal(Timestamp(n.Unix()), v)
Expand Down
2 changes: 1 addition & 1 deletion pkg/gridtypes/zos/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func NetworkID(twin uint32, network gridtypes.Name) NetID {
if len(b) > 13 {
b = b[:13]
}
return NetID(string(b))
return NetID(b)
}

func NetworkIDFromWorkloadID(wl gridtypes.WorkloadID) (NetID, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/network/mycelium/mycelium.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func (s *MyceliumServer) InspectMycelium() (inspection MyceliumInspection, err e

// IP return the address in the 400::/7 subnet allocated by mycelium
func (m *MyceliumInspection) IP() net.IP {
return net.IP(m.Address)
return m.Address
}

// Subnet return the 400::/64 subnet allocated by mycelium
Expand Down
3 changes: 2 additions & 1 deletion pkg/network/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ func TestKeys(t *testing.T) {
strEncrypted := fmt.Sprintf("%x", encrypted)

strDecrypted := ""
fmt.Sscanf(strEncrypted, "%x", &strDecrypted)
_, err = fmt.Sscanf(strEncrypted, "%x", &strDecrypted)
require.NoError(t, err)

decrypted, err := crypto.Decrypt([]byte(strDecrypted), sk)
require.NoError(t, err)
Expand Down
16 changes: 8 additions & 8 deletions pkg/network/networker.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ func (n *networker) SetupPrivTap(networkID pkg.NetID, name string) (ifc string,
}

func (n *networker) TapExists(name string) (bool, error) {
log.Info().Str("tap-name", string(name)).Msg("Checking if tap interface exists")
log.Info().Str("tap-name", name).Msg("Checking if tap interface exists")

tapIface, err := tapName(name)
if err != nil {
Expand All @@ -372,7 +372,7 @@ func (n *networker) TapExists(name string) (bool, error) {

// RemoveTap in the network resource.
func (n *networker) RemoveTap(name string) error {
log.Info().Str("tap-name", string(name)).Msg("Removing tap interface")
log.Info().Str("tap-name", name).Msg("Removing tap interface")

tapIface, err := tapName(name)
if err != nil {
Expand All @@ -390,7 +390,7 @@ func (n *networker) PublicIPv4Support() bool {
// reservation id. It is hooked to the public bridge. The name of the tap
// interface is returned
func (n *networker) SetupPubTap(name string) (string, error) {
log.Info().Str("pubtap-name", string(name)).Msg("Setting up public tap interface")
log.Info().Str("pubtap-name", name).Msg("Setting up public tap interface")

if !n.ndmz.SupportsPubIPv4() {
return "", errors.New("can't create public tap on this node")
Expand All @@ -408,7 +408,7 @@ func (n *networker) SetupPubTap(name string) (string, error) {

// SetupMyceliumTap creates a new mycelium tap device attached to this network resource with deterministic IP address
func (n *networker) SetupMyceliumTap(name string, netID zos.NetID, config zos.MyceliumIP) (tap pkg.PlanetaryTap, err error) {
log.Info().Str("tap-name", string(name)).Msg("Setting up mycelium tap interface")
log.Info().Str("tap-name", name).Msg("Setting up mycelium tap interface")

network, err := n.networkOf(netID)
if err != nil {
Expand Down Expand Up @@ -452,7 +452,7 @@ func (n *networker) SetupMyceliumTap(name string, netID zos.NetID, config zos.My

// SetupYggTap sets up a tap device in the host namespace for the yggdrasil ip
func (n *networker) SetupYggTap(name string) (tap pkg.PlanetaryTap, err error) {
log.Info().Str("tap-name", string(name)).Msg("Setting up yggdrasil tap interface")
log.Info().Str("tap-name", name).Msg("Setting up yggdrasil tap interface")

tapIface, err := tapName(name)
if err != nil {
Expand Down Expand Up @@ -486,7 +486,7 @@ func (n *networker) SetupYggTap(name string) (tap pkg.PlanetaryTap, err error) {

// PubTapExists checks if the tap device for the public network exists already
func (n *networker) PubTapExists(name string) (bool, error) {
log.Info().Str("pubtap-name", string(name)).Msg("Checking if public tap interface exists")
log.Info().Str("pubtap-name", name).Msg("Checking if public tap interface exists")

tapIface, err := pubTapName(name)
if err != nil {
Expand All @@ -499,7 +499,7 @@ func (n *networker) PubTapExists(name string) (bool, error) {
// RemovePubTap removes the public tap device from the host namespace
// of the networkID
func (n *networker) RemovePubTap(name string) error {
log.Info().Str("pubtap-name", string(name)).Msg("Removing public tap interface")
log.Info().Str("pubtap-name", name).Msg("Removing public tap interface")

tapIface, err := pubTapName(name)
if err != nil {
Expand Down Expand Up @@ -660,7 +660,7 @@ func (n *networker) RemovePubIPFilter(filterName string) error {
// DisconnectPubTap disconnects the public tap from the network. The interface
// itself is not removed and will need to be cleaned up later
func (n *networker) DisconnectPubTap(name string) error {
log.Info().Str("pubtap-name", string(name)).Msg("Disconnecting public tap interface")
log.Info().Str("pubtap-name", name).Msg("Disconnecting public tap interface")
tapIfaceName, err := pubTapName(name)
if err != nil {
return errors.Wrap(err, "could not get network namespace tap device name")
Expand Down
2 changes: 1 addition & 1 deletion pkg/network/nr/net_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ func (nr *NetResource) wgPeers() ([]*wireguard.Peer, error) {
}

wgPeer := &wireguard.Peer{
PublicKey: string(peer.WGPublicKey),
PublicKey: peer.WGPublicKey,
AllowedIPs: allowedIPs,
Endpoint: peer.Endpoint,
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/perf/iperf/iperf_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (t *IperfTest) runIperfTest(ctx context.Context, clientIP string, tcp bool)
operation := func() error {
res := runIperfCommand(ctx, opts)
if res.Error == errServerBusy {
return fmt.Errorf(errServerBusy)
return errors.New(errServerBusy)
}

report = res
Expand Down
2 changes: 1 addition & 1 deletion pkg/perf/publicip/publicip_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func isLeastValidNode(ctx context.Context, farmID uint32, substrateGateway *stub
}

for _, node := range nodes {
if node.NodeID >= uint32(nodeID) {
if node.NodeID >= nodeID {
continue
}
n, err := substrateGateway.GetNode(ctx, node.NodeID)
Expand Down
2 changes: 1 addition & 1 deletion pkg/power/ethtool.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func valueOfFlag(output []byte, flag Flag) (string, error) {
if len(parts) != 2 {
return "", fmt.Errorf("invalid ethtool output format (%s)", line)
}
return strings.TrimSpace(string(parts[1])), nil
return strings.TrimSpace(parts[1]), nil
}

return "", ErrFlagNotFound
Expand Down
2 changes: 1 addition & 1 deletion pkg/provision/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (s *substrateTwins) GetKey(id uint32) ([]byte, error) {

key := user.Account.PublicKey()
s.mem.Add(id, key)
return []byte(key), nil
return key, nil
}

type substrateAdmins struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/provision/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ func (e *NativeEngine) validate(ctx context.Context, dl *gridtypes.Deployment, n
return ctx, fmt.Errorf("substrate is not configured in engine")
}

contract, subErr := e.substrateGateway.GetContract(ctx, uint64(dl.ContractID))
contract, subErr := e.substrateGateway.GetContract(ctx, dl.ContractID)
if subErr.IsError() {
return nil, errors.Wrap(subErr.Err, "failed to get deployment contract")
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/provision/storage.fs/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ func (s *Fs) byTwin(twin uint32) ([]uint64, error) {
continue
}

ids = append(ids, uint64(id))
ids = append(ids, id)
}

return ids, nil
Expand Down
8 changes: 4 additions & 4 deletions pkg/qsfsd/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ func (q *QSFS) checkDeadQSFSs(ctx context.Context, state *failedQSFSState) error
}
for _, path := range paths {
wlID := filepath.Base(path)
metrics, err := q.qsfsMetrics(ctx, string(wlID))
metrics, err := q.qsfsMetrics(ctx, wlID)
if err != nil {
log.Err(err).Str("id", string(wlID)).Msg("couldn't get qsfs metrics")
state.metricsFailureCount[string(wlID)] += 1
if state.metricsFailureCount[string(wlID)] >= 10 {
log.Err(err).Str("id", wlID).Msg("couldn't get qsfs metrics")
state.metricsFailureCount[wlID] += 1
if state.metricsFailureCount[wlID] >= 10 {
q.Unmount(wlID)
state.delete(wlID)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/qsfsd/qsfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func (q *QSFS) Mount(wlID string, cfg zos.QuantumSafeFS) (info pkg.QSFSInfo, err
if containerErr != nil {
log.Error().Err(containerErr).Msg("Failed to read container logs")
}
err = errors.Wrapf(lerr, fmt.Sprintf("Container Logs:\n%s", logs))
err = errors.Wrapf(lerr, "Container Logs:\n%s", logs)
return
}
log.Debug().Str("duration", time.Since(t).String()).Msg("waiting for qsfs deployment took")
Expand Down
6 changes: 3 additions & 3 deletions pkg/storage/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ func TestCacheResize(t *testing.T) {
Excl: 1,
},
}
vol.On("Limit", uint64(cacheSize)).Return(nil)
vol.On("Limit", cacheSize).Return(nil)
err := m.checkAndResizeCache(&vol, cacheSize)
require.NoError(t, err)

Expand Down Expand Up @@ -444,7 +444,7 @@ func TestCacheResize(t *testing.T) {
Excl: 0, // no files
},
}
vol.On("Limit", uint64(cacheSize)).Return(nil)
vol.On("Limit", cacheSize).Return(nil)
err = m.checkAndResizeCache(&vol, cacheSize)
require.NoError(t, err)

Expand All @@ -468,7 +468,7 @@ func TestCacheResize(t *testing.T) {
Excl: 91,
},
}
vol.On("Limit", uint64(100+cacheSize)).Return(nil)
vol.On("Limit", 100+cacheSize).Return(nil)
err = m.checkAndResizeCache(&vol, cacheSize)
require.NoError(t, err)

Expand Down
2 changes: 1 addition & 1 deletion pkg/vm/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ func (m *Module) withLogs(path string, err error) error {
return errors.Wrapf(err, "failed to tail machine logs: %s", tailErr)
}

return errors.Wrapf(err, string(logs))
return errors.Wrap(err, logs)
}

func (m *Module) checkDevicesUsed(devices []string) error {
Expand Down
Loading

0 comments on commit ec5a85f

Please sign in to comment.