Skip to content

Commit

Permalink
Add golangci-lint, fix warnings
Browse files Browse the repository at this point in the history
- Simplify the monitor test, it had an unused variable warning from
  `functionReturned`
- The job lister didn't need to add a namespace indexer to the job
  informer, it comes with one by default. When I added error handling to
  the `AddIndexers` call, we were missing an "indexer conflict" error.
  • Loading branch information
benmoss committed Jan 9, 2023
1 parent 84554b5 commit dc691c3
Show file tree
Hide file tree
Showing 15 changed files with 49 additions and 40 deletions.
10 changes: 10 additions & 0 deletions .buildkite/pipeline.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
steps:
- label: ":lint-roller: lint"
agents:
queue: kubernetes
plugins:
- kubernetes:
podSpec:
containers:
- image: golang:latest
command: [.buildkite/steps/lint.sh]

- label: ":buildkite: integration tests"
agents:
queue: kubernetes
Expand Down
11 changes: 11 additions & 0 deletions .buildkite/steps/lint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash
set -euxo pipefail

apt update && apt install -y lsb-release
curl -q 'https://proget.makedeb.org/debian-feeds/prebuilt-mpr.pub' | gpg --dearmor | tee /usr/share/keyrings/prebuilt-mpr-archive-keyring.gpg 1>/dev/null
echo "deb [signed-by=/usr/share/keyrings/prebuilt-mpr-archive-keyring.gpg] https://proget.makedeb.org prebuilt-mpr $(lsb_release -cs)" | tee /etc/apt/sources.list.d/prebuilt-mpr.list
apt update && apt install -y just

curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.50.1

just lint
2 changes: 2 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
run:
timeout: 5m
1 change: 1 addition & 0 deletions Brewfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ brew "go"
brew "ko"
brew "helm"
brew "goreleaser"
brew "golangci-lint"
3 changes: 1 addition & 2 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,5 @@ func (c Config) MarshalLogObject(enc zapcore.ObjectEncoder) error {
enc.AddInt("max-in-flight", c.MaxInFlight)
enc.AddString("namespace", c.Namespace)
enc.AddString("org", c.Org)
enc.AddReflected("tags", c.Tags)
return nil
return enc.AddReflected("tags", c.Tags)
}
1 change: 0 additions & 1 deletion api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ func (t *logTransport) RoundTrip(in *http.Request) (out *http.Response, err erro
// Inspired by: github.com/motemen/go-loghttp
if _, ok := os.LookupEnv("DEBUG"); !ok {
return t.inner.RoundTrip(in)

}

log.Printf("--> %s %s", in.Method, in.URL)
Expand Down
1 change: 0 additions & 1 deletion api/genqlient.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ fragment CommandJob on JobTypeCommand {
scheduledAt
agentQueryRules
command
label
}

# @genqlient(omitempty: true)
Expand Down
7 changes: 5 additions & 2 deletions integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func TestSSHRepoClone(t *testing.T) {
}

func basicTest(t *testing.T, fixture, repo string) {
t.Helper()
// create pipeline
ctx := context.Background()
graphqlClient := api.NewClient(cfg.BuildkiteToken)
Expand Down Expand Up @@ -138,9 +139,11 @@ func basicTest(t *testing.T, fixture, repo string) {
})
require.NoError(t, err)
EnsureCleanup(t, func() {
api.BuildCancel(ctx, graphqlClient, api.BuildCancelInput{
if _, err := api.BuildCancel(ctx, graphqlClient, api.BuildCancelInput{
Id: createBuild.BuildCreate.Build.Id,
})
}); err != nil {
t.Logf("failed to cancel build: %v", err)
}
})
build := createBuild.BuildCreate.Build
require.Len(t, build.Jobs.Edges, 1)
Expand Down
6 changes: 2 additions & 4 deletions justfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
default: lint generate build test

build:
echo Building…
default: lint generate test

run *FLAGS:
go run ./... {{FLAGS}}
Expand All @@ -10,6 +7,7 @@ test *FLAGS:
go test {{FLAGS}} ./...

lint: gomod
golangci-lint run

generate:
go run github.com/Khan/genqlient api/genqlient.yaml
Expand Down
2 changes: 0 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package main
import (
"log"

_ "net/http/pprof"

"github.com/buildkite/agent-stack-k8s/cmd/controller"
"k8s.io/client-go/kubernetes"

Expand Down
8 changes: 3 additions & 5 deletions monitor/lister.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,15 @@ func NewJobLister(ctx context.Context, log *zap.Logger, clientset kubernetes.Int
if err != nil {
return nil, fmt.Errorf("failed to build tag label selector for job manager: %w", err)
}
hasUuid, err := labels.NewRequirement(api.UUIDLabel, selection.Exists, nil)
hasUUID, err := labels.NewRequirement(api.UUIDLabel, selection.Exists, nil)
if err != nil {
return nil, fmt.Errorf("failed to build uuid label selector for job manager: %w", err)
}
factory := informers.NewSharedInformerFactoryWithOptions(clientset, 0, informers.WithTweakListOptions(func(opt *metav1.ListOptions) {
opt.LabelSelector = labels.NewSelector().Add(*hasTag, *hasUuid).String()
opt.LabelSelector = labels.NewSelector().Add(*hasTag, *hasUUID).String()
}))
informer := factory.Batch().V1().Jobs()
jobInformer := informer.Informer()
indexer := cache.NewIndexer(MetaJobLabelKeyFunc, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
jobInformer.AddIndexers(indexer.GetIndexers())

go factory.Start(ctx.Done())

Expand All @@ -49,7 +47,7 @@ func MetaJobLabelKeyFunc(obj interface{}) (string, error) {
}
meta, err := meta.Accessor(obj)
if err != nil {
return "", fmt.Errorf("object has no meta: %v", err)
return "", fmt.Errorf("object has no meta: %w", err)
}
labels := meta.GetLabels()
if v, ok := labels[api.UUIDLabel]; ok {
Expand Down
1 change: 0 additions & 1 deletion monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ func (m *Monitor) start() {
cmdJ := builds[j].Node.(*api.JobJobTypeCommand)

return cmdI.ScheduledAt.Before(cmdJ.ScheduledAt)

})

for _, job := range builds {
Expand Down
28 changes: 10 additions & 18 deletions monitor/monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,38 +89,30 @@ func TestScheduleBuild(t *testing.T) {
shouldSchedule: true,
shouldError: false,
}}
m.jobs = make(chan Job, 1)
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
var errResult error
jobCreated := 0
functionReturned := 0

returned := make(chan error)
go func() {
err := m.scheduleBuild(test.job, test.tag)
returned <- err
}()
err := m.scheduleBuild(test.job, test.tag)
var job *Job
select {
case <-m.jobs:
jobCreated++
case errResult = <-returned:
functionReturned++
case j := <-m.jobs:
job = &j
default:
}
if test.shouldSchedule {
if jobCreated == 0 {
if errResult != nil {
t.Fatalf("Test should schedule job, but error was returned: %s", errResult)
if job == nil {
if err != nil {
t.Fatalf("Test should schedule job, but error was returned: %s", err)
} else {
t.Fatalf("Test should schedule job, but nil error was returned")
}
}
}
if test.shouldError {
if errResult == nil {
if err == nil {
t.Fatalf("Test should error, but no error was returned")
}
}
})
}

}
4 changes: 2 additions & 2 deletions scheduler/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ func (w *worker) k8sify(
return nil, fmt.Errorf("err parsing plugins: %w", err)
}
for _, plugin := range plugins {
asJson, err := json.Marshal(plugin.Configuration)
asJSON, err := json.Marshal(plugin.Configuration)
if err != nil {
return nil, fmt.Errorf("failed to marshal config: %w", err)
}
if err := json.Unmarshal(asJson, &pluginConfig); err != nil {
if err := json.Unmarshal(asJSON, &pluginConfig); err != nil {
return nil, fmt.Errorf("failed to unmarshal config: %w", err)
}
}
Expand Down
4 changes: 2 additions & 2 deletions scheduler/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestJobPluginConversion(t *testing.T) {
},
},
}
pluginsJson, err := json.Marshal([]map[string]interface{}{
pluginsJSON, err := json.Marshal([]map[string]interface{}{
{
"github.com/buildkite-plugins/kubernetes-buildkite-plugin": pluginConfig,
},
Expand All @@ -39,7 +39,7 @@ func TestJobPluginConversion(t *testing.T) {
input := &monitor.Job{
CommandJob: api.CommandJob{
Uuid: "abc",
Env: []string{fmt.Sprintf("BUILDKITE_PLUGINS=%s", string(pluginsJson))},
Env: []string{fmt.Sprintf("BUILDKITE_PLUGINS=%s", string(pluginsJSON))},
},
Tag: "queue=kubernetes",
}
Expand Down

0 comments on commit dc691c3

Please sign in to comment.