From 12d691e4e297e09c6c9d38142423b7e5367b275d Mon Sep 17 00:00:00 2001 From: "Y.Horie" Date: Thu, 26 Oct 2023 21:27:39 +0900 Subject: [PATCH] ci: add golang lint and fixed. #157 (#320) * ci: add golang lint and fixed. #157 * Fixed linter * use the latest version of golangci-lint in github actions * add the target of lint in Makefile * fix nolint errcheck --- .github/workflows/golangci-lint.yaml | 20 ++++++++++++++++++++ Makefile | 4 ++++ controllers/k6_controller.go | 5 +---- controllers/k6_initialize.go | 2 +- controllers/k6_stopped_jobs.go | 6 ++---- controllers/testrun_controller.go | 9 +++------ main.go | 4 ++-- pkg/resources/jobs/helpers.go | 4 +--- pkg/resources/jobs/runner.go | 2 +- pkg/types/conditions.go | 2 +- pkg/types/k6cli.go | 4 +--- 11 files changed, 37 insertions(+), 25 deletions(-) create mode 100644 .github/workflows/golangci-lint.yaml diff --git a/.github/workflows/golangci-lint.yaml b/.github/workflows/golangci-lint.yaml new file mode 100644 index 00000000..4c8898c5 --- /dev/null +++ b/.github/workflows/golangci-lint.yaml @@ -0,0 +1,20 @@ +--- +name: "Golang Lint" +on: + - push + - pull_request + +jobs: + golangci: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-go@v4 + with: + go-version: '1.19' + cache: false + - name: lint + uses: golangci/golangci-lint-action@v3 + with: + version: v1.55 + args: --timeout=5m diff --git a/Makefile b/Makefile index a1bc94f8..4df439a4 100644 --- a/Makefile +++ b/Makefile @@ -98,6 +98,10 @@ fmt: vet: go vet ./... +# Run golangci-lint +lint: + go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.55.0 + golangci-lint --timeout 5m run ./... # Generate code generate: controller-gen $(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..." diff --git a/controllers/k6_controller.go b/controllers/k6_controller.go index 1cbaa53c..f858108c 100644 --- a/controllers/k6_controller.go +++ b/controllers/k6_controller.go @@ -96,10 +96,7 @@ func (r *K6Reconciler) SetupWithManager(mgr ctrl.Manager) error { func(object client.Object) bool { pod := object.(*v1.Pod) _, ok := pod.GetLabels()[k6CrLabelName] - if !ok { - return false - } - return true + return ok }))). WithOptions(controller.Options{ MaxConcurrentReconciles: 1, diff --git a/controllers/k6_initialize.go b/controllers/k6_initialize.go index ac7c4cad..664aeb05 100644 --- a/controllers/k6_initialize.go +++ b/controllers/k6_initialize.go @@ -133,7 +133,7 @@ func SetupCloudTest(ctx context.Context, log logr.Logger, k6 v1alpha1.TestRunI, // If CloudTestRunCreated has just been updated, wait for a bit before // acting, to avoid race condition between different reconcile loops. t, _ := v1alpha1.LastUpdate(k6, v1alpha1.CloudTestRunCreated) - if time.Now().Sub(t) < 5*time.Second { + if time.Since(t) < 5*time.Second { return ctrl.Result{RequeueAfter: time.Second * 2}, nil } diff --git a/controllers/k6_stopped_jobs.go b/controllers/k6_stopped_jobs.go index 7f95b3e4..da9d73ce 100644 --- a/controllers/k6_stopped_jobs.go +++ b/controllers/k6_stopped_jobs.go @@ -4,7 +4,7 @@ import ( "context" "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "github.com/go-logr/logr" @@ -32,7 +32,7 @@ func isJobRunning(log logr.Logger, service *v1.Service) bool { defer resp.Body.Close() - data, err := ioutil.ReadAll(resp.Body) + data, err := io.ReadAll(resp.Body) if err != nil { log.Error(err, fmt.Sprintf("Error on reading status of the runner job %v", service.ObjectMeta.Name)) return true @@ -63,7 +63,6 @@ func StoppedJobs(ctx context.Context, log logr.Logger, k6 v1alpha1.TestRunI, r * opts := &client.ListOptions{LabelSelector: selector, Namespace: k6.NamespacedName().Namespace} - var hostnames []string sl := &v1.ServiceList{} if err := r.List(ctx, sl, opts); err != nil { @@ -73,7 +72,6 @@ func StoppedJobs(ctx context.Context, log logr.Logger, k6 v1alpha1.TestRunI, r * var count int32 for _, service := range sl.Items { - hostnames = append(hostnames, service.Spec.ClusterIP) if isJobRunning(log, &service) { count++ diff --git a/controllers/testrun_controller.go b/controllers/testrun_controller.go index 6da31aab..5e742ae6 100644 --- a/controllers/testrun_controller.go +++ b/controllers/testrun_controller.go @@ -240,7 +240,7 @@ func (r *TestRunReconciler) reconcile(ctx context.Context, req ctrl.Request, log // If TestRunRunning has just been updated, wait for a bit before // acting, to avoid race condition between different reconcile loops. t, _ := v1alpha1.LastUpdate(k6, v1alpha1.TestRunRunning) - if time.Now().Sub(t) < 5*time.Second { + if time.Since(t) < 5*time.Second { return ctrl.Result{RequeueAfter: time.Second * 2}, nil } @@ -268,7 +268,7 @@ func (r *TestRunReconciler) reconcile(ctx context.Context, req ctrl.Request, log // delete if configured if k6.GetSpec().Cleanup == "post" { log.Info("Cleaning up all resources") - r.Delete(ctx, k6) + _ = r.Delete(ctx, k6) } // notify if configured return ctrl.Result{}, nil @@ -302,10 +302,7 @@ func (r *TestRunReconciler) SetupWithManager(mgr ctrl.Manager) error { func(object client.Object) bool { pod := object.(*v1.Pod) _, ok := pod.GetLabels()[k6CrLabelName] - if !ok { - return false - } - return true + return ok }))). WithOptions(controller.Options{ MaxConcurrentReconciles: 1, diff --git a/main.go b/main.go index 169614b7..d85f8ed5 100644 --- a/main.go +++ b/main.go @@ -89,8 +89,8 @@ func main() { os.Exit(1) } - mgr.AddHealthzCheck("health", healthz.Ping) - mgr.AddReadyzCheck("ready", healthz.Ping) + _ = mgr.AddHealthzCheck("health", healthz.Ping) + _ = mgr.AddReadyzCheck("ready", healthz.Ping) if err = (controllers.NewK6Reconciler(&controllers.TestRunReconciler{ Client: mgr.GetClient(), diff --git a/pkg/resources/jobs/helpers.go b/pkg/resources/jobs/helpers.go index 1c763fbf..df8fd014 100644 --- a/pkg/resources/jobs/helpers.go +++ b/pkg/resources/jobs/helpers.go @@ -28,9 +28,7 @@ func newIstioCommand(istioEnabled string, inheritedCommands []string) ([]string, command = append(command, "scuttle") } - for _, inheritedCommand := range inheritedCommands { - command = append(command, inheritedCommand) - } + command = append(command, inheritedCommands...) return command, istio } diff --git a/pkg/resources/jobs/runner.go b/pkg/resources/jobs/runner.go index 37f47850..d7452773 100644 --- a/pkg/resources/jobs/runner.go +++ b/pkg/resources/jobs/runner.go @@ -55,7 +55,7 @@ func NewRunnerJob(k6 v1alpha1.TestRunI, index int, token string) (*batchv1.Job, command = append( command, - fmt.Sprintf(script.FullName()), + script.FullName(), "--address=0.0.0.0:6565") paused := true diff --git a/pkg/types/conditions.go b/pkg/types/conditions.go index 1cd9045e..b2633d8c 100644 --- a/pkg/types/conditions.go +++ b/pkg/types/conditions.go @@ -56,7 +56,7 @@ func SetIfNewer(cond *[]metav1.Condition, } if callbackF != nil { - if callbackResult := callbackF(proposedCondition); callbackResult == true { + if callbackResult := callbackF(proposedCondition); callbackResult { isNewer = callbackResult } } diff --git a/pkg/types/k6cli.go b/pkg/types/k6cli.go index 7e37987d..3670f161 100644 --- a/pkg/types/k6cli.go +++ b/pkg/types/k6cli.go @@ -14,12 +14,10 @@ type CLI struct { func ParseCLI(arguments string) *CLI { lastArgV := func(start int, args []string) (end int) { - var nextArg bool end = start - for !nextArg && end < len(args) { + for end < len(args) { args[end] = strings.TrimSpace(args[end]) if len(args[end]) > 0 && args[end][0] == '-' { - nextArg = true break } end++