Skip to content

Commit

Permalink
ci: add golang lint and fixed. #157 (#320)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
u5surf authored Oct 26, 2023
1 parent 0c3bf71 commit 12d691e
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 25 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/golangci-lint.yaml
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ fmt:
vet:
go vet ./...

# Run golangci-lint
lint:
go install github.com/golangci/golangci-lint/cmd/[email protected]
golangci-lint --timeout 5m run ./...
# Generate code
generate: controller-gen
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
Expand Down
5 changes: 1 addition & 4 deletions controllers/k6_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion controllers/k6_initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
6 changes: 2 additions & 4 deletions controllers/k6_stopped_jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"

"github.com/go-logr/logr"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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++
Expand Down
9 changes: 3 additions & 6 deletions controllers/testrun_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
4 changes: 1 addition & 3 deletions pkg/resources/jobs/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/resources/jobs/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/types/conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
4 changes: 1 addition & 3 deletions pkg/types/k6cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++
Expand Down

0 comments on commit 12d691e

Please sign in to comment.