Skip to content

Commit

Permalink
Enable gocritic linter
Browse files Browse the repository at this point in the history
... and fix lints on the way.

Signed-off-by: Tom Wieczorek <[email protected]>
  • Loading branch information
twz123 committed Dec 13, 2024
1 parent e338b28 commit 3ed8e41
Show file tree
Hide file tree
Showing 23 changed files with 34 additions and 37 deletions.
9 changes: 9 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ linters:
- depguard # Checks if package imports are in a list of acceptable packages
- dupword # Finds word repetitions
- errorlint # Find code that will cause problems with Go's error wrapping scheme
- gocritic # another style/metalinter (dynamic rules supported)
- gofmt # Checks whether code was gofmt-ed
- goheader # Checks is file headers matche a given pattern
- intrange # Checking for loops that could use an integer range
Expand Down Expand Up @@ -67,6 +68,14 @@ linters-settings:
deny:
- pkg: github.com/k0sproject/k0s/inttest
desc: Usage of integration test code outside of integration tests.

gocritic:
disabled-checks:
- singleCaseSwitch
settings:
ifElseChain:
minThreshold: 5

goheader:
template-path: .go-header.txt
values:
Expand Down
3 changes: 1 addition & 2 deletions cmd/token/invalidate.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package token
import (
"errors"
"fmt"
"path/filepath"

"github.com/k0sproject/k0s/pkg/config"
"github.com/k0sproject/k0s/pkg/token"
Expand All @@ -40,7 +39,7 @@ func tokenInvalidateCmd() *cobra.Command {
if len(args) < 1 {
return errors.New("invalidate requires at least one token ID to invalidate")
}
manager, err := token.NewManager(filepath.Join(opts.K0sVars.AdminKubeConfigPath))
manager, err := token.NewManager(opts.K0sVars.AdminKubeConfigPath)
if err != nil {
return err
}
Expand Down
4 changes: 1 addition & 3 deletions cmd/token/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package token

import (
"fmt"
"path/filepath"

"github.com/k0sproject/k0s/pkg/config"
"github.com/k0sproject/k0s/pkg/token"
Expand All @@ -42,7 +41,7 @@ func tokenListCmd() *cobra.Command {
if err != nil {
return err
}
manager, err := token.NewManager(filepath.Join(opts.K0sVars.AdminKubeConfigPath))
manager, err := token.NewManager(opts.K0sVars.AdminKubeConfigPath)
if err != nil {
return err
}
Expand All @@ -56,7 +55,6 @@ func tokenListCmd() *cobra.Command {
return nil
}

//fmt.Fprintf(cmd.OutOrStdout(), "Tokens: %v \n", tokens)
table := tablewriter.NewWriter(cmd.OutOrStdout())
table.SetHeader([]string{"ID", "Role", "Expires at"})
table.SetAutoWrapText(false)
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/sysinfo/probes/linux/cgroup_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (g *cgroupV1) loadControllers(seen func(name, msg string)) error {
var lineNo uint
lines := bufio.NewScanner(f)
for lines.Scan() {
lineNo = lineNo + 1
lineNo++
if err := lines.Err(); err != nil {
return fmt.Errorf("failed to parse /proc/cgroups at line %d: %w ", lineNo, err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/sysinfo/probes/probes.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func (p *probes) Get(id string) Probe {
}

func (p *probes) Set(id string, setter func(ProbePath, Probe) Probe) {
path := append(p.path, id)
path := append(p.path, id) //nolint:gocritic // not assigning to p.path is intended
for _, probe := range p.probes {
if probe.id == id {
probe.probe = ensureSet(setter(path, probe.probe))
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/sysinfo/probes/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (b iecBytes) String() string {
const prefixes = "KMGTPE"
const unit = 1 << 10

for i := 0; ; i = i + 1 {
for i := 0; ; i++ {
x := float32(b) / float32((uint64(1) << (i * 10)))
if x < unit {
if i == 0 {
Expand Down
4 changes: 2 additions & 2 deletions internal/pkg/sysinfo/sysinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ func (s *K0sSysinfoSpec) NewSysinfoProbes() probes.Probes {
// https://docs.k0sproject.io/main/system-requirements/#storage
var minFreeDiskSpace uint64
if s.ControllerRoleEnabled {
minFreeDiskSpace = minFreeDiskSpace + 500*probes.Mi
minFreeDiskSpace += 500 * probes.Mi
}
if s.WorkerRoleEnabled {
minFreeDiskSpace = minFreeDiskSpace + 1300*probes.Mi
minFreeDiskSpace += 1300 * probes.Mi
}
probes.AssertFileSystem(p, s.DataDir)
probes.AssertFreeDiskSpace(p, s.DataDir, minFreeDiskSpace)
Expand Down
2 changes: 1 addition & 1 deletion internal/testutil/fakeclient/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ func fromUnstructured(scheme *runtime.Scheme, obj runtime.Object, gvk schema.Gro
}

listGVK := gvk
listGVK.Kind = listGVK.Kind + "List"
listGVK.Kind += "List"

list, err := scheme.New(listGVK)
if err != nil {
Expand Down
1 change: 0 additions & 1 deletion inttest/kubeletcertrotate/kubeletcertrotate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ spec:

s.Equal(appc.PlanCompleted, cmd.State)
s.NotNil(cmd.K0sUpdate)
//s.Nil(cmd.K0sUpdate.Controllers)
s.NotNil(cmd.K0sUpdate.Workers)

for _, group := range [][]apv1beta2.PlanCommandTargetStatus{cmd.K0sUpdate.Controllers, cmd.K0sUpdate.Workers} {
Expand Down
10 changes: 4 additions & 6 deletions pkg/apis/k0s/v1beta1/clusterconfig_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,10 @@ func (c *ClusterConfig) StripDefaults() *ClusterConfig {
}
if reflect.DeepEqual(copy.Spec.Network, DefaultNetwork()) {
copy.Spec.Network = nil
} else {
if copy.Spec.Network.NodeLocalLoadBalancing != nil &&
copy.Spec.Network.NodeLocalLoadBalancing.EnvoyProxy != nil &&
reflect.DeepEqual(copy.Spec.Network.NodeLocalLoadBalancing.EnvoyProxy.Image, DefaultEnvoyProxyImage()) {
copy.Spec.Network.NodeLocalLoadBalancing.EnvoyProxy.Image = nil
}
} else if copy.Spec.Network.NodeLocalLoadBalancing != nil &&
copy.Spec.Network.NodeLocalLoadBalancing.EnvoyProxy != nil &&
reflect.DeepEqual(copy.Spec.Network.NodeLocalLoadBalancing.EnvoyProxy.Image, DefaultEnvoyProxyImage()) {
copy.Spec.Network.NodeLocalLoadBalancing.EnvoyProxy.Image = nil
}
if reflect.DeepEqual(copy.Spec.Telemetry, DefaultClusterTelemetry()) {
copy.Spec.Telemetry = nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/apis/k0s/v1beta1/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,9 @@ func (n *Network) DNSAddress() (string, error) {

prefixlen, _ := ipnet.Mask.Size()
if prefixlen < 29 {
address[3] = address[3] + 10
address[3] += 10
} else {
address[3] = address[3] + 2
address[3] += 2
}

if !ipnet.Contains(address) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestFindNextPendingRandom(t *testing.T) {
assert.NoError(t, err)
assert.NotNil(t, node)

countMap[node.Name] = countMap[node.Name] + 1
countMap[node.Name] += 1
}

assert.Contains(t, countMap, "bbb")
Expand Down
6 changes: 1 addition & 5 deletions pkg/autopilot/controller/readyprober.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,7 @@ func (p readyProber) Probe() error {
g := errgroup.Group{}

for _, target := range p.targets {
g.Go(func() error {
return func(target apv1beta2.PlanCommandTargetStatus) error {
return p.probeOne(target)
}(target)
})
g.Go(func() error { return p.probeOne(target) })
}

return g.Wait()
Expand Down
2 changes: 0 additions & 2 deletions pkg/autopilot/controller/root_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,7 @@ func (c *rootController) startSubControllers(ctx context.Context, event LeaseEve

ctx, cancel := context.WithCancel(ctx)

//wg := sync.WaitGroup{}
g, ctx := errgroup.WithContext(ctx)
//wg.Add(1)

g.Go(func() error {
logger.Info("Starting controller-runtime subhandlers")
Expand Down
2 changes: 1 addition & 1 deletion pkg/autopilot/controller/signal/k0s/cordon.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func (r *cordoning) drainNode(ctx context.Context, signalNode crcli.Object) erro
}
}

//otherwise get node from client
// otherwise get node from client
if err := r.client.Get(ctx, crcli.ObjectKey{Name: nodeName}, node); err != nil {
return fmt.Errorf("failed to get node: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/autopilot/controller/signal/k0s/uncordon.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func (r *uncordoning) unCordonNode(ctx context.Context, signalNode crcli.Object)
}
}

//otherwise get node from client
// otherwise get node from client
if err := r.client.Get(ctx, crcli.ObjectKey{Name: nodeName}, node); err != nil {
return fmt.Errorf("failed to get node: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/backup/filesystem_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func TestFileSystemStepRestore(t *testing.T) {
step := NewFileSystemStep(src)
require.NoError(t, step.Restore(src, dst), "Unable to copy empty dir")

//rmdir will fail if the directory has anything at all
// rmdir will fail if the directory has anything at all
require.NoError(t, os.Remove(dst), "Unable to remove supposedly empty dir")
})

Expand Down
2 changes: 1 addition & 1 deletion pkg/component/worker/nllb/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func NewReconciler(
runtimeDir := os.Getenv("XDG_RUNTIME_DIR")
if runtimeDir == "" {
if runtime.GOOS == "windows" {
runtimeDir = filepath.Join(k0sVars.DataDir)
runtimeDir = k0sVars.DataDir
} else {
runtimeDir = "/run/k0s"
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/k0scloudprovider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (p *provider) InstancesV2() (cloudprovider.InstancesV2, bool) {
}

// Zones returns a zones interface. Also returns true if the interface is supported, false otherwise.
// DEPRECATED: Zones is deprecated in favor of retrieving zone/region information from InstancesV2.
// Deprecated: Zones is deprecated in favor of retrieving zone/region information from InstancesV2.
// This interface will not be called if InstancesV2 is enabled.
func (p *provider) Zones() (cloudprovider.Zones, bool) {
// Not supported
Expand Down
2 changes: 1 addition & 1 deletion pkg/kubernetes/watch/watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ func TestWatcher(t *testing.T) {
switch callsToCondition {
case 0:
return false, nil
default:
default: //nolint:gocritic // needed for fallthrough
require.Fail(t, "Unexpected call to condition")
fallthrough
case 1:
Expand Down
2 changes: 1 addition & 1 deletion pkg/supervisor/logwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (w *logWriter) writeBytes(in []byte) {
len := w.len
for i := 0; i < utf8.MaxRune && i < w.len; i++ {
if r, _ := utf8.DecodeLastRune(w.buf[:w.len-i]); r != utf8.RuneError {
len = len - i
len -= i
break
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/supervisor/supervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ func (s *Supervisor) shouldKillProcess(ph procHandle) (bool, error) {
return false, nil
}

//only kill process if it has the _KOS_MANAGED env set
// only kill process if it has the _KOS_MANAGED env set
if env, err := ph.environ(); err != nil {
if errors.Is(err, syscall.ESRCH) {
return false, nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/token/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func loadCACert(k0sVars *config.CfgVars) ([]byte, error) {
}

func loadToken(ctx context.Context, k0sVars *config.CfgVars, role string, expiry time.Duration) (string, error) {
manager, err := NewManager(filepath.Join(k0sVars.AdminKubeConfigPath))
manager, err := NewManager(k0sVars.AdminKubeConfigPath)
if err != nil {
return "", err
}
Expand Down

0 comments on commit 3ed8e41

Please sign in to comment.