chore(backend): Add golangci-lint and pre-commit config (#9428)

* Add golangci-lint and pre-commit config

* Adopt golanci-lint config from k8s
This commit is contained in:
gkcalat 2023-05-17 11:23:06 -07:00 committed by GitHub
parent 9bce215893
commit c2bc434c11
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 7 deletions

27
.golangci.yaml Normal file
View File

@ -0,0 +1,27 @@
run:
timeout: 30m
skip-files:
- "api\\*.go$"
- "backend\\api\\*.go"
issues:
max-same-issues: 0
linters:
disable-all: true
enable: # please keep this alphabetized
- gocritic
- gosimple
- govet
- ineffassign
- misspell
- staticcheck
- stylecheck
- unused
linters-settings: # please keep this alphabetized
misspell:
locale: US
staticcheck:
checks:
- "all"

View File

@ -46,3 +46,16 @@ repos:
# docformatter messes up certain sphinx link formatting.
# The kubernetes_platform/python docstrings are heavy on links, so ignore.
exclude: (sdk/python/kfp/compiler/compiler_test.py|kubernetes_platform/python/)
# Golang pre-submit hooks
- repo: https://github.com/golangci/golangci-lint
rev: v1.52.2
hooks:
- id: golangci-lint
name: golangci-lint
description: Fast linters runner for Go.
entry: golangci-lint run --new-from-rev HEAD --fix
types: [go]
language: golang
require_serial: true
pass_filenames: false

View File

@ -3,7 +3,7 @@ Pipelines backend.
## Building & Testing
To run all unittests for backend:
To run all unittests for backend:
```
go test -v -cover ./backend/...
@ -17,17 +17,15 @@ go build -o /tmp/apiserver backend/src/apiserver/*.go
## Code Style
Use [gofmt](https://pkg.go.dev/cmd/gofmt) package to format your .go source files. There is no need to format the swagger generated go clients, so only run the following command in `./backend/src` and `./backend/test` folder.
Backend codebase follows the [Google's Go Style Guide](https://google.github.io/styleguide/go/). Please, take time to get familiar with the [best practices](https://google.github.io/styleguide/go/best-practices). It is not intended to be exhaustive, but it often helps minimizing guesswork among developers and keep codebase uniform and consistent.
```
go fmt ./...
```
We use [golangci-lint](https://golangci-lint.run/) tool that can catch common mistakes locally (see detailed configuration [here](https://github.com/kubeflow/pipelines/blob/master/.golangci.yaml)). It can be [conveniently integrated](https://golangci-lint.run/usage/integrations/) with multiple popular IDEs such as VS Code or Vim.
For more information, see [this blog](https://go.dev/blog/gofmt).
Finally, it is advised to install [pre-commit](https://pre-commit.com/) in order to automate linter checks (see configuration [here](https://github.com/kubeflow/pipelines/blob/master/.pre-commit-config.yaml))
## Building APIServer image locally
The API server image can be built from the root folder of the repo using:
The API server image can be built from the root folder of the repo using:
```
export API_SERVER_IMAGE=api_server
docker build -f backend/Dockerfile . --tag $API_SERVER_IMAGE