Skip to content

Commit

Permalink
Update linter configuration and fix errors (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
phillebaba committed Jun 9, 2024
1 parent a7a27af commit 43bf0e8
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 13 deletions.
35 changes: 33 additions & 2 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
run:
timeout: 3m
linters:
disable-all: true
enable:
Expand All @@ -9,7 +11,36 @@ linters:
- unused
- misspell
- testifylint
- nolintlint
- bodyclose
- goimports
- importas
- ireturn
- perfsprint
- gocritic
- paralleltest
linters-settings:
errcheck:
check-type-assertions: true
check-blank: true
disable-default-exclusions: true
govet:
enable:
- fieldalignment
enable-all: true
disable:
- shadow
testifylint:
enable-all: true
nolintlint:
require-explanation: true
require-specific: true
perfsprint:
strconcat: false
gocritic:
enable-all: true
disabled-checks:
- importShadow
- hugeParam
- rangeValCopy
- whyNoLint
- unnamedResult
- httpNoBody
2 changes: 2 additions & 0 deletions internal/analyze/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ func createPlot(result measure.Result, path string) error {
return a.Start.Compare(b.Start)
})
zeroTime := bench.Measurements[0].Start
//nolint:gocritic // False positive
max := int64(0)
//nolint:gocritic // False positive
min := int64(0)
sum := int64(0)
for i, result := range bench.Measurements {
Expand Down
4 changes: 2 additions & 2 deletions internal/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package generate

import (
"context"
"fmt"
"errors"
"time"

"github.com/c2h5oh/datasize"
Expand Down Expand Up @@ -60,7 +60,7 @@ func Generate(ctx context.Context, imgName string, layerCount int, imageSize dat
func layerSize(layerCount int, imageSize datasize.ByteSize) (int64, error) {
layerSize := imageSize.Bytes() / uint64(layerCount)
if layerSize*uint64(layerCount) != imageSize.Bytes() {
return 0, fmt.Errorf("cannot evenly divide image size into layers")
return 0, errors.New("cannot evenly divide image size into layers")
}
return int64(layerSize), nil
}
2 changes: 2 additions & 0 deletions internal/generate/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
)

func TestLayerSize(t *testing.T) {
t.Parallel()

layerSize, err := layerSize(4, 1*datasize.GB)
require.NoError(t, err)
require.Equal(t, int64(268435456), layerSize)
Expand Down
18 changes: 10 additions & 8 deletions internal/measure/measure.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package measure
import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
Expand All @@ -13,7 +14,7 @@ import (
"github.com/go-logr/logr"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/wait"
Expand Down Expand Up @@ -55,10 +56,10 @@ func Measure(ctx context.Context, kubeconfigPath, namespace, resultDir string, i
ts := time.Now().Unix()
runName := fmt.Sprintf("spegel-benchmark-%d", ts)
_, err = cs.CoreV1().Namespaces().Get(ctx, namespace, metav1.GetOptions{})
if err != nil && !errors.IsNotFound(err) {
if err != nil && !kerrors.IsNotFound(err) {
return err
}
if errors.IsNotFound(err) {
if kerrors.IsNotFound(err) {
ns := corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: namespace,
Expand Down Expand Up @@ -158,7 +159,7 @@ func clearImages(ctx context.Context, cs kubernetes.Interface, dc dynamic.Interf
},
}
_, err := cs.AppsV1().DaemonSets(namespace).Create(ctx, ds, metav1.CreateOptions{})
if err != nil && !errors.IsNotFound(err) {
if err != nil && !kerrors.IsNotFound(err) {
return err
}
defer func() {
Expand Down Expand Up @@ -194,10 +195,10 @@ func measureImagePull(ctx context.Context, cs kubernetes.Interface, dc dynamic.I
log := logr.FromContextOrDiscard(ctx).WithValues("image", image)
log.Info("measuring pull performance")
ds, err := cs.AppsV1().DaemonSets(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil && !errors.IsNotFound(err) {
if err != nil && !kerrors.IsNotFound(err) {
return Benchmark{}, err
}
if errors.IsNotFound(err) {
if kerrors.IsNotFound(err) {
ds := &appsv1.DaemonSet{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Expand Down Expand Up @@ -269,7 +270,7 @@ func measureImagePull(ctx context.Context, cs kubernetes.Interface, dc dynamic.I
return Benchmark{}, err
}
if len(podList.Items) == 0 {
return Benchmark{}, fmt.Errorf("received empty benchmark pod list")
return Benchmark{}, errors.New("received empty benchmark pod list")
}
bench := Benchmark{
Image: image,
Expand Down Expand Up @@ -307,13 +308,14 @@ func getEvent(events []corev1.Event, reason string) (corev1.Event, error) {
}

func parsePullMessage(msg string) (time.Duration, error) {
//nolint: gocritic // We should never panic and always check errors.
r, err := regexp.Compile(`\" in (.*) \(`)
if err != nil {
return 0, err
}
match := r.FindStringSubmatch(msg)
if len(match) < 2 {
return 0, fmt.Errorf("could not find image pull duration")
return 0, errors.New("could not find image pull duration")
}
s := match[1]
d, err := time.ParseDuration(s)
Expand Down
2 changes: 2 additions & 0 deletions internal/measure/measure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
)

func TestParsePullMessage(t *testing.T) {
t.Parallel()

s := "Successfully pulled image \"docker.io/library/nginx:mainline-alpine\" in 873.420598ms (873.428863ms including waiting)"
d, err := parsePullMessage(s)
require.NoError(t, err)
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"errors"
"fmt"
"log/slog"
"os"
Expand Down Expand Up @@ -70,6 +71,6 @@ func run(args Arguments) error {
case args.Analyze != nil:
return analyze.Analyze(ctx, args.Analyze.Path)
default:
return fmt.Errorf("unknown command")
return errors.New("unknown command")
}
}

0 comments on commit 43bf0e8

Please sign in to comment.