Skip to content

Commit

Permalink
Enable error linting (#733)
Browse files Browse the repository at this point in the history
  • Loading branch information
bschimke95 authored Oct 18, 2024
1 parent db9fdd6 commit 2ee8e70
Show file tree
Hide file tree
Showing 14 changed files with 35 additions and 32 deletions.
14 changes: 8 additions & 6 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ linters:
- decorder
- dogsled
- dupword
- durationcheck
# - err113 - TODO(ben): maybe consider later
# - errcheck
# - errchkjson
- errname
- errorlint


# TODO(ben): Enable those linters step by step and fix existing issues.
# - durationcheck
# - err113
# - errcheck
# - errchkjson
# - errname
# - errorlint
# - exhaustive
# - exhaustruct
# - fatcontext
Expand Down Expand Up @@ -327,3 +328,4 @@ issues:
- path: "_test\\.go"
linters:
- dupword
- err113
3 changes: 2 additions & 1 deletion src/k8s/pkg/client/helm/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"path/filepath"

Expand Down Expand Up @@ -57,7 +58,7 @@ func (h *client) Apply(ctx context.Context, c InstallableChart, desired State, v
get := action.NewGet(cfg)
release, err := get.Run(c.Name)
if err != nil {
if err != driver.ErrReleaseNotFound {
if !errors.Is(err, driver.ErrReleaseNotFound) {
return false, fmt.Errorf("failed to get status of release %s: %w", c.Name, err)
}
isInstalled = false
Expand Down
2 changes: 1 addition & 1 deletion src/k8s/pkg/client/kubernetes/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (c *Client) HasReadyNodes(ctx context.Context) (bool, error) {
nodes, err := c.CoreV1().Nodes().List(ctx, metav1.ListOptions{})

if err != nil {
return false, fmt.Errorf("failed to list nodes: %v", err)
return false, fmt.Errorf("failed to list nodes: %w", err)
}

for _, node := range nodes.Items {
Expand Down
4 changes: 2 additions & 2 deletions src/k8s/pkg/client/snapd/refresh_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ func (c *Client) GetRefreshStatus(changeID string) (*types.RefreshStatus, error)

resBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("client: could not read response body: %s", err)
return nil, fmt.Errorf("client: could not read response body: %w", err)
}

var changeResponse snapdChangeResponse
if err := json.Unmarshal(resBody, &changeResponse); err != nil {
return nil, fmt.Errorf("client: could not unmarshal response body: %s", err)
return nil, fmt.Errorf("client: could not unmarshal response body: %w", err)
}

return &changeResponse.Result, nil
Expand Down
2 changes: 1 addition & 1 deletion src/k8s/pkg/docgen/godoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func getPackageDoc(packagePath string, projectDir string) (*doc.Package, error)

packageDir, err := getGoPackageDir(packagePath, projectDir)
if err != nil {
return nil, fmt.Errorf("couldn't retrieve package dir, error: %v", err)
return nil, fmt.Errorf("couldn't retrieve package dir, error: %w", err)
}

pkg, err := parsePackageDir(packageDir)
Expand Down
15 changes: 8 additions & 7 deletions src/k8s/pkg/docgen/gomod.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package docgen

import (
"fmt"
"golang.org/x/mod/modfile"
"golang.org/x/mod/module"
"os"
"path"
"strings"

"golang.org/x/mod/modfile"
"golang.org/x/mod/module"
)

func getGoDepModulePath(name string, version string) (string, error) {
Expand All @@ -22,21 +23,21 @@ func getGoDepModulePath(name string, version string) (string, error) {
escapedPath, err := module.EscapePath(name)
if err != nil {
return "", fmt.Errorf(
"couldn't escape module path: %s %v", name, err)
"couldn't escape module path %s: %w", name, err)
}

escapedVersion, err := module.EscapeVersion(version)
if err != nil {
return "", fmt.Errorf(
"couldn't escape module version: %s %v", version, err)
"couldn't escape module version %s: %w", version, err)
}

path := path.Join(cachePath, escapedPath+"@"+escapedVersion)

// Validate the path.
if _, err := os.Stat(path); err != nil {
return "", fmt.Errorf(
"Go module path not accessible: %s %s %s, error: %v.",
"go module path not accessible %s %s %s: %w",
name, version, path, err)
}

Expand All @@ -46,11 +47,11 @@ func getGoDepModulePath(name string, version string) (string, error) {
func getDependencyVersionFromGoMod(goModPath string, packageName string, directOnly bool) (string, string, error) {
goModContents, err := os.ReadFile(goModPath)
if err != nil {
return "", "", fmt.Errorf("could not read go.mod file %s, error: %v", goModPath, err)
return "", "", fmt.Errorf("could not read go.mod file %s: %w", goModPath, err)
}
goModFile, err := modfile.ParseLax(goModPath, goModContents, nil)
if err != nil {
return "", "", fmt.Errorf("could not parse go.mod file %s, error: %v", goModPath, err)
return "", "", fmt.Errorf("could not parse go.mod file %s: %w", goModPath, err)
}

for _, dep := range goModFile.Require {
Expand Down
5 changes: 2 additions & 3 deletions src/k8s/pkg/docgen/json_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ func MarkdownFromJsonStructToFile(i any, outFilePath string, projectDir string)

err = os.WriteFile(outFilePath, []byte(content), 0644)
if err != nil {
return fmt.Errorf("failed to write markdown documentation to %s, error: %v.",
outFilePath, err)
return fmt.Errorf("failed to write markdown documentation to %s: %w", outFilePath, err)
}
return nil
}
Expand Down Expand Up @@ -105,7 +104,7 @@ func ParseStruct(i any, projectDir string) ([]Field, error) {
fieldIface := reflect.ValueOf(i).FieldByName(field.Name).Interface()
nestedFields, err := ParseStruct(fieldIface, projectDir)
if err != nil {
return nil, fmt.Errorf("couldn't parse %s.%s, error: %v", inType, field.Name, err)
return nil, fmt.Errorf("couldn't parse %s.%s: %w", inType, field.Name, err)
}

outField := Field{
Expand Down
4 changes: 2 additions & 2 deletions src/k8s/pkg/k8sd/database/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func schemaApplyMigration(migrationPath ...string) schema.Update {
path := filepath.Join(append([]string{"sql", "migrations"}, migrationPath...)...)
b, err := sqlMigrations.ReadFile(path)
if err != nil {
panic(fmt.Errorf("invalid migration file %s: %s", path, err))
panic(fmt.Errorf("invalid migration file %s: %w", path, err))
}
return func(ctx context.Context, tx *sql.Tx) error {
if _, err := tx.ExecContext(ctx, string(b)); err != nil {
Expand All @@ -51,7 +51,7 @@ func MustPrepareStatement(queryPath ...string) int {
path := filepath.Join(append([]string{"sql", "queries"}, queryPath...)...)
b, err := sqlQueries.ReadFile(path)
if err != nil {
panic(fmt.Errorf("invalid query file %s: %s", path, err))
panic(fmt.Errorf("invalid query file %s: %w", path, err))
}
return cluster.RegisterStmt(string(b))
}
2 changes: 1 addition & 1 deletion src/k8s/pkg/k8sd/features/calico/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func ApplyNetwork(ctx context.Context, snap snap.Snap, apiserver types.APIServer
serviceCIDRs := []string{}
ipv4ServiceCIDR, ipv6ServiceCIDR, err := utils.SplitCIDRStrings(network.GetServiceCIDR())
if err != nil {
err = fmt.Errorf("invalid service cidr: %v", err)
err = fmt.Errorf("invalid service cidr: %w", err)
return types.FeatureStatus{
Enabled: false,
Version: CalicoTag,
Expand Down
2 changes: 1 addition & 1 deletion src/k8s/pkg/k8sd/features/cilium/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func ApplyNetwork(ctx context.Context, snap snap.Snap, apiserver types.APIServer

ipv4CIDR, ipv6CIDR, err := utils.SplitCIDRStrings(network.GetPodCIDR())
if err != nil {
err = fmt.Errorf("invalid kube-proxy --cluster-cidr value: %v", err)
err = fmt.Errorf("invalid kube-proxy --cluster-cidr value: %w", err)
return types.FeatureStatus{
Enabled: false,
Version: CiliumAgentImageTag,
Expand Down
2 changes: 1 addition & 1 deletion src/k8s/pkg/k8sd/setup/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func mustTemplate(parts ...string) *template.Template {
path := filepath.Join(append([]string{"embed"}, parts...)...)
b, err := templates.ReadFile(path)
if err != nil {
panic(fmt.Errorf("invalid template %s: %s", path, err))
panic(fmt.Errorf("invalid template %s: %w", path, err))
}
return template.Must(template.New(path).Parse(string(b)))
}
2 changes: 1 addition & 1 deletion src/k8s/pkg/snap/util/arguments.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func UpdateServiceArguments(snap snap.Snap, serviceName string, updateMap map[st
sort.Strings(newArguments)

if err := os.WriteFile(argumentsFile, []byte(strings.Join(newArguments, "\n")+"\n"), 0600); err != nil {
return false, fmt.Errorf("failed to write arguments for service %s: %q", serviceName, err)
return false, fmt.Errorf("failed to write arguments for service %s: %w", serviceName, err)
}
return changed, nil
}
2 changes: 1 addition & 1 deletion src/k8s/pkg/utils/control/retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestRetryFor(t *testing.T) {
return errors.New("failed")
})

if err != context.Canceled {
if !errors.Is(err, context.Canceled) {
t.Errorf("Expected context.Canceled error, got: %v", err)
}
})
Expand Down
8 changes: 4 additions & 4 deletions src/k8s/pkg/utils/control/wait_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ func mockCheckFunc() (bool, error) {
return true, nil
}

var testError = errors.New("test error")
var errTest = errors.New("test error")

// Mock check function that returns an error.
func mockErrorCheckFunc() (bool, error) {
return false, testError
return false, errTest
}

func TestWaitUntilReady(t *testing.T) {
Expand All @@ -34,7 +34,7 @@ func TestWaitUntilReady(t *testing.T) {
cancel2() // Cancel the context immediately

err2 := WaitUntilReady(ctx2, mockCheckFunc)
if err2 == nil || err2 != context.Canceled {
if err2 == nil || !errors.Is(err2, context.Canceled) {
t.Errorf("Expected context.Canceled error, got: %v", err2)
}

Expand All @@ -52,7 +52,7 @@ func TestWaitUntilReady(t *testing.T) {
defer cancel4()

err4 := WaitUntilReady(ctx4, mockErrorCheckFunc)
if err4 == nil || !errors.Is(err4, testError) {
if err4 == nil || !errors.Is(err4, errTest) {
t.Errorf("Expected test error, got: %v", err4)
}
}

0 comments on commit 2ee8e70

Please sign in to comment.