Skip to content

Commit e920dd9

Browse files
chore: update go linting + update dependencies for controller (#137)
* chore: update controller kubebuilder Signed-off-by: Mathew Wicks <[email protected]> * chore: add new controller linting Signed-off-by: Mathew Wicks <[email protected]> * chore: update backend (similar to controller kubebuilder update) Signed-off-by: Mathew Wicks <[email protected]> * chore: add new backend linting Signed-off-by: Mathew Wicks <[email protected]> --------- Signed-off-by: Mathew Wicks <[email protected]>
1 parent 2b747ac commit e920dd9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1396
-997
lines changed

workspaces/backend/.gitignore

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,17 @@
1-
/bin
2-
cover.out
1+
# Binaries for programs and plugins
2+
*.exe
3+
*.exe~
4+
*.dll
5+
*.so
6+
*.dylib
7+
bin/*
8+
Dockerfile.cross
9+
10+
# Test binary, built with `go test -c`
11+
*.test
12+
13+
# Output of the go coverage tool, specifically when used with LiteIDE
14+
*.out
15+
16+
# Go workspace file
17+
go.work

workspaces/backend/.golangci.yml

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,103 @@ run:
33
allow-parallel-runners: true
44

55
issues:
6-
# don't skip warning about doc comments
7-
# don't exclude the default set of lint
6+
# disable the default set of exclude rules
87
exclude-use-default: false
9-
# restore some of the defaults
10-
# (fill in the rest as needed)
8+
119
exclude-rules:
10+
# disable some linters for api packages
1211
- path: "api/*"
1312
linters:
1413
- lll
14+
15+
# disable some linters for internal packages
1516
- path: "internal/*"
1617
linters:
1718
- dupl
1819
- lll
20+
21+
# exclude some linters for the test directory and test files
22+
- path: test/.*|.*_test\.go
23+
linters:
24+
- dupl
25+
- errcheck
26+
- goconst
27+
- gocyclo
28+
- gosec
29+
1930
linters:
2031
disable-all: true
2132
enable:
33+
- asciicheck
34+
- bodyclose
35+
- copyloopvar
36+
- dogsled
2237
- dupl
2338
- errcheck
24-
- exportloopref
39+
- errorlint
40+
- exhaustive
41+
- ginkgolinter
2542
- goconst
43+
- gocritic
2644
- gocyclo
2745
- gofmt
46+
- goheader
2847
- goimports
48+
- goprintffuncname
49+
- gosec
2950
- gosimple
3051
- govet
3152
- ineffassign
3253
- lll
3354
- misspell
3455
- nakedret
56+
- nolintlint
3557
- prealloc
58+
- revive
3659
- staticcheck
3760
- typecheck
3861
- unconvert
3962
- unparam
4063
- unused
64+
65+
linters-settings:
66+
gocritic:
67+
enabled-tags:
68+
- diagnostic
69+
- experimental
70+
- opinionated
71+
- performance
72+
- style
73+
disabled-checks:
74+
- assignOp
75+
- filepathJoin
76+
- paramTypeCombine
77+
- rangeValCopy
78+
- unnamedResult
79+
- whyNoLint
80+
81+
goimports:
82+
local-prefixes: github.com/kubeflow/notebooks/workspaces/backend
83+
84+
goheader:
85+
template: |-
86+
Copyright 2024.
87+
88+
Licensed under the Apache License, Version 2.0 (the "License");
89+
you may not use this file except in compliance with the License.
90+
You may obtain a copy of the License at
91+
92+
http://www.apache.org/licenses/LICENSE-2.0
93+
94+
Unless required by applicable law or agreed to in writing, software
95+
distributed under the License is distributed on an "AS IS" BASIS,
96+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
97+
See the License for the specific language governing permissions and
98+
limitations under the License.
99+
100+
misspell:
101+
locale: US
102+
103+
revive:
104+
rules:
105+
- name: comment-spacings

workspaces/backend/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Use the golang image to build the application
2-
FROM golang:1.22.2 AS builder
2+
FROM golang:1.22 AS builder
33
ARG TARGETOS
44
ARG TARGETARCH
55

workspaces/backend/Makefile

Lines changed: 56 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
1-
# CONTAINER_TOOL defines the container tool to be used for building images.
2-
# Be aware that the target commands are only tested with Docker which is
3-
# scaffolded by default. However, you might want to replace it to use other
4-
# tools. (i.e. podman)
5-
CONTAINER_TOOL ?= docker
61
# Image URL to use all building/pushing image targets
72
IMG ?= nbv2-backend:latest
8-
# Backend default port
9-
PORT ?= 4000
103
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
11-
ENVTEST_K8S_VERSION = 1.29.0
4+
ENVTEST_K8S_VERSION = 1.31.0
125

136
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
147
ifeq (,$(shell go env GOBIN))
@@ -17,6 +10,15 @@ else
1710
GOBIN=$(shell go env GOBIN)
1811
endif
1912

13+
# Backend default port
14+
PORT ?= 4000
15+
16+
# CONTAINER_TOOL defines the container tool to be used for building images.
17+
# Be aware that the target commands are only tested with Docker which is
18+
# scaffolded by default. However, you might want to replace it to use other
19+
# tools. (i.e. podman)
20+
CONTAINER_TOOL ?= docker
21+
2022
# Setting SHELL to bash allows bash commands to be executed by recipes.
2123
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
2224
SHELL = /usr/bin/env bash -o pipefail
@@ -27,6 +29,17 @@ all: build
2729

2830
##@ General
2931

32+
# The help target prints out all targets with their descriptions organized
33+
# beneath their categories. The categories are represented by '##@' and the
34+
# target descriptions by '##'. The awk command is responsible for reading the
35+
# entire set of makefiles included in this invocation, looking for lines of the
36+
# file as xyz: ## something, and then pretty-format the target and help. Then,
37+
# if there's a line with ##@ something, that gets pretty-printed as a category.
38+
# More info on the usage of ANSI control characters for terminal formatting:
39+
# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters
40+
# More info on the awk command:
41+
# http://linuxcommand.org/lc3_adv_awk.php
42+
3043
.PHONY: help
3144
help: ## Display this help.
3245
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
@@ -43,11 +56,10 @@ vet: ## Run go vet against code.
4356

4457
.PHONY: test
4558
test: fmt vet envtest ## Run tests.
46-
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" \
47-
go test ./... -coverprofile cover.out
59+
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... -coverprofile cover.out
4860

4961
.PHONY: lint
50-
lint: golangci-lint ## Run golangci-lint linter & yamllint
62+
lint: golangci-lint ## Run golangci-lint linter
5163
$(GOLANGCI_LINT) run
5264

5365
.PHONY: lint-fix
@@ -57,13 +69,16 @@ lint-fix: golangci-lint ## Run golangci-lint linter and perform fixes
5769
##@ Build
5870

5971
.PHONY: build
60-
build: fmt vet envtest ## Build backend binary.
72+
build: fmt vet ## Build backend binary.
6173
go build -o bin/backend cmd/main.go
6274

6375
.PHONY: run
6476
run: fmt vet ## Run a backend from your host.
65-
go run ./cmd/main.go --port=$(PORT)
77+
go run ./cmd/main.go --port=$(PORT)
6678

79+
# If you wish to build the manager image targeting other platforms you can use the --platform flag.
80+
# (i.e. docker build --platform linux/arm64). However, you must enable docker buildKit for it.
81+
# More info: https://docs.docker.com/develop/develop-images/build_enhancements/
6782
.PHONY: docker-build
6883
docker-build: ## Build docker image with the backend.
6984
$(CONTAINER_TOOL) build -t ${IMG} .
@@ -72,6 +87,23 @@ docker-build: ## Build docker image with the backend.
7287
docker-push: ## Push docker image with the backend.
7388
$(CONTAINER_TOOL) push ${IMG}
7489

90+
# PLATFORMS defines the target platforms for the manager image be built to provide support to multiple
91+
# architectures. (i.e. make docker-buildx IMG=myregistry/mypoperator:0.0.1). To use this option you need to:
92+
# - be able to use docker buildx. More info: https://docs.docker.com/build/buildx/
93+
# - have enabled BuildKit. More info: https://docs.docker.com/develop/develop-images/build_enhancements/
94+
# - be able to push the image to your registry (i.e. if you do not set a valid value via IMG=<myregistry/image:<tag>> then the export will fail)
95+
# To adequately provide solutions that are compatible with multiple platforms, you should consider using this option.
96+
PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
97+
.PHONY: docker-buildx
98+
docker-buildx: ## Build and push docker image for the manager for cross-platform support
99+
# copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile
100+
sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
101+
- $(CONTAINER_TOOL) buildx create --name project-v3-builder
102+
$(CONTAINER_TOOL) buildx use project-v3-builder
103+
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross .
104+
- $(CONTAINER_TOOL) buildx rm project-v3-builder
105+
rm Dockerfile.cross
106+
75107
##@ Dependencies
76108

77109
## Location to install dependencies to
@@ -81,13 +113,12 @@ $(LOCALBIN):
81113

82114
## Tool Binaries
83115
KUBECTL ?= kubectl
84-
ENVTEST ?= $(LOCALBIN)/setup-envtest-$(ENVTEST_VERSION)
85-
GOLANGCI_LINT ?= $(LOCALBIN)/golangci-lint-$(GOLANGCI_LINT_VERSION)
116+
ENVTEST ?= $(LOCALBIN)/setup-envtest
117+
GOLANGCI_LINT = $(LOCALBIN)/golangci-lint
86118

87119
## Tool Versions
88-
KUSTOMIZE_VERSION ?= v5.3.0
89-
ENVTEST_VERSION ?= release-0.17
90-
GOLANGCI_LINT_VERSION ?= v1.57.2
120+
ENVTEST_VERSION ?= release-0.19
121+
GOLANGCI_LINT_VERSION ?= v1.61.0
91122

92123
.PHONY: envtest
93124
envtest: $(ENVTEST) ## Download setup-envtest locally if necessary.
@@ -97,19 +128,20 @@ $(ENVTEST): $(LOCALBIN)
97128
.PHONY: golangci-lint
98129
golangci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary.
99130
$(GOLANGCI_LINT): $(LOCALBIN)
100-
$(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/cmd/golangci-lint,${GOLANGCI_LINT_VERSION})
101-
131+
$(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/cmd/golangci-lint,$(GOLANGCI_LINT_VERSION))
102132

103133
# go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist
104-
# $1 - target path with name of binary (ideally with version)
134+
# $1 - target path with name of binary
105135
# $2 - package url which can be installed
106136
# $3 - specific version of package
107137
define go-install-tool
108-
@[ -f $(1) ] || { \
138+
@[ -f "$(1)-$(3)" ] || { \
109139
set -e; \
110140
package=$(2)@$(3) ;\
111141
echo "Downloading $${package}" ;\
142+
rm -f $(1) || true ;\
112143
GOBIN=$(LOCALBIN) go install $${package} ;\
113-
mv "$$(echo "$(1)" | sed "s/-$(3)$$//")" $(1) ;\
114-
}
144+
mv $(1) $(1)-$(3) ;\
145+
} ;\
146+
ln -sf $(1)-$(3) $(1)
115147
endef

workspaces/backend/api/app.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const (
4747
WorkspaceKindNamePathParam = "name"
4848
WorkspaceKindsByNamePath = AllWorkspaceKindsPath + "/:" + WorkspaceNamePathParam
4949

50-
//namespaces
50+
// namespaces
5151
AllNamespacesPath = PathPrefix + "/namespaces"
5252
)
5353

@@ -59,12 +59,12 @@ type App struct {
5959
}
6060

6161
// NewApp creates a new instance of the app
62-
func NewApp(cfg config.EnvConfig, logger *slog.Logger, client client.Client, scheme *runtime.Scheme) (*App, error) {
62+
func NewApp(cfg config.EnvConfig, logger *slog.Logger, cl client.Client, scheme *runtime.Scheme) (*App, error) {
6363

6464
app := &App{
6565
Config: cfg,
6666
logger: logger,
67-
repositories: repositories.NewRepositories(client),
67+
repositories: repositories.NewRepositories(cl),
6868
Scheme: scheme,
6969
}
7070
return app, nil

workspaces/backend/api/errors.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func (a *App) LogError(r *http.Request, err error) {
4646
a.logger.Error(err.Error(), "method", method, "uri", uri)
4747
}
4848

49-
// nolint:unused
49+
//nolint:unused
5050
func (a *App) badRequestResponse(w http.ResponseWriter, r *http.Request, err error) {
5151
httpError := &HTTPError{
5252
StatusCode: http.StatusBadRequest,
@@ -58,13 +58,13 @@ func (a *App) badRequestResponse(w http.ResponseWriter, r *http.Request, err err
5858
a.errorResponse(w, r, httpError)
5959
}
6060

61-
func (a *App) errorResponse(w http.ResponseWriter, r *http.Request, error *HTTPError) {
62-
env := ErrorEnvelope{Error: error}
61+
func (a *App) errorResponse(w http.ResponseWriter, r *http.Request, httpError *HTTPError) {
62+
env := ErrorEnvelope{Error: httpError}
6363

64-
err := a.WriteJSON(w, error.StatusCode, env, nil)
64+
err := a.WriteJSON(w, httpError.StatusCode, env, nil)
6565
if err != nil {
6666
a.LogError(r, err)
67-
w.WriteHeader(error.StatusCode)
67+
w.WriteHeader(httpError.StatusCode)
6868
}
6969
}
7070

@@ -103,7 +103,7 @@ func (a *App) methodNotAllowedResponse(w http.ResponseWriter, r *http.Request) {
103103
a.errorResponse(w, r, httpError)
104104
}
105105

106-
// nolint:unused
106+
//nolint:unused
107107
func (a *App) failedValidationResponse(w http.ResponseWriter, r *http.Request, errors map[string]string) {
108108
message, err := json.Marshal(errors)
109109
if err != nil {

workspaces/backend/api/healthcheck__handler_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ func TestHealthCheckHandler(t *testing.T) {
3939
}
4040

4141
rr := httptest.NewRecorder()
42-
req, err := http.NewRequest(http.MethodGet, HealthCheckPath, nil)
42+
req, err := http.NewRequest(http.MethodGet, HealthCheckPath, http.NoBody)
4343
if err != nil {
4444
t.Fatal(err)
4545
}
4646

4747
app.HealthcheckHandler(rr, req, nil)
4848
rs := rr.Result()
49-
defer rs.Body.Close() // nolint: errcheck
49+
defer rs.Body.Close()
5050

5151
body, err := io.ReadAll(rs.Body)
5252
if err != nil {

0 commit comments

Comments
 (0)