Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use golangci-lint #111

Merged
merged 1 commit into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
run:
deadline: 5m

linters:
disable-all: true
enable:
- bodyclose
- dogsled
- dupl
- errcheck
- exportloopref
- gocritic
- gocyclo
- gofmt
- goimports
- gosec
- goprintffuncname
- gosimple
- govet
- ineffassign
- misspell
- nakedret
- nolintlint
- revive
- staticcheck
- typecheck
- unconvert
- unused
- whitespace

linters-settings:
goimports:
local-prefixes: sigs.k8s.io/custom-metrics-apiserver
misspell:
ignore-words:
- "creater" # Cf. e.g. https://pkg.go.dev/k8s.io/apimachinery@v0.24.3/pkg/runtime#ObjectCreater
44 changes: 37 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@ IMAGE?=k8s-test-metrics-adapter
TEMP_DIR:=$(shell mktemp -d)
ARCH?=amd64
OUT_DIR?=./_output
GOPATH:=$(shell go env GOPATH)

VERSION?=latest

GOLANGCI_VERSION:=1.50.1

.PHONY: all
all: build-test-adapter


# Generate
# --------

generated_openapis := core custommetrics externalmetrics
generated_files := $(generated_openapis:%=pkg/generated/openapi/%/zz_generated.openapi.go)

Expand All @@ -26,26 +33,49 @@ pkg/generated/openapi/%/zz_generated.openapi.go: go.mod go.sum
-o ./ \
-r /dev/null


# Build
# -----

.PHONY: build-test-adapter
build-test-adapter: $(generated_files)
CGO_ENABLED=0 GOOS=linux GOARCH=$(ARCH) go build -o $(OUT_DIR)/$(ARCH)/test-adapter sigs.k8s.io/custom-metrics-apiserver/test-adapter

.PHONY: gofmt
gofmt:
./hack/gofmt-all.sh

# Format and lint
# ---------------

HAS_GOLANGCI_VERSION:=$(shell $(GOPATH)/bin/golangci-lint version --format=short)
.PHONY: golangci
golangci:
ifneq ($(HAS_GOLANGCI_VERSION), $(GOLANGCI_VERSION))
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOPATH)/bin v$(GOLANGCI_VERSION)
endif

.PHONY: verify-lint
verify-lint: golangci
$(GOPATH)/bin/golangci-lint run --modules-download-mode=readonly || (echo 'Run "make update-lint"' && exit 1)

.PHONY: update-lint
update-lint: golangci
$(GOPATH)/bin/golangci-lint run --fix --modules-download-mode=readonly


# Verify
# ------

.PHONY: verify
verify: verify-deps verify-gofmt
verify: verify-deps verify-lint

.PHONY: verify-deps
verify-deps:
go mod verify
go mod tidy
@git diff --exit-code -- go.sum go.mod

.PHONY: verify-gofmt
verify-gofmt:
./hack/gofmt-all.sh -v

# Test
# ----

.PHONY: test
test:
Expand Down
41 changes: 0 additions & 41 deletions hack/gofmt-all.sh

This file was deleted.

13 changes: 7 additions & 6 deletions pkg/apiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/version"
genericapiserver "k8s.io/apiserver/pkg/server"
"k8s.io/client-go/informers"

cminstall "k8s.io/metrics/pkg/apis/custom_metrics/install"
eminstall "k8s.io/metrics/pkg/apis/external_metrics/install"

"sigs.k8s.io/custom-metrics-apiserver/pkg/apiserver/installer"
"sigs.k8s.io/custom-metrics-apiserver/pkg/provider"
)
Expand All @@ -41,7 +42,7 @@ func init() {
eminstall.Install(Scheme)

// we need custom conversion functions to list resources with options
installer.RegisterConversions(Scheme)
utilruntime.Must(installer.RegisterConversions(Scheme))

// we need to add the options to empty v1
// TODO fix the server code to avoid this
Expand Down Expand Up @@ -69,24 +70,24 @@ type CustomMetricsAdapterServer struct {
externalMetricsProvider provider.ExternalMetricsProvider
}

type completedConfig struct {
type CompletedConfig struct {
genericapiserver.CompletedConfig
}

// Complete fills in any fields not set that are required to have valid data. It's mutating the receiver.
func (c *Config) Complete(informers informers.SharedInformerFactory) completedConfig {
func (c *Config) Complete(informers informers.SharedInformerFactory) CompletedConfig {
c.GenericConfig.Version = &version.Info{
Major: "1",
Minor: "0",
}
return completedConfig{c.GenericConfig.Complete(informers)}
return CompletedConfig{c.GenericConfig.Complete(informers)}
}

// New returns a new instance of CustomMetricsAdapterServer from the given config.
// name is used to differentiate for logging.
// Each of the arguments: customMetricsProvider, externalMetricsProvider can be set either to
// a provider implementation, or to nil to disable one of the APIs.
func (c completedConfig) New(name string, customMetricsProvider provider.CustomMetricsProvider, externalMetricsProvider provider.ExternalMetricsProvider) (*CustomMetricsAdapterServer, error) {
func (c CompletedConfig) New(name string, customMetricsProvider provider.CustomMetricsProvider, externalMetricsProvider provider.ExternalMetricsProvider) (*CustomMetricsAdapterServer, error) {
genericServer, err := c.CompletedConfig.New(name, genericapiserver.NewEmptyDelegate()) // completion is done in Complete, no need for a second time
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion pkg/apiserver/cmapis.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import (
genericapi "k8s.io/apiserver/pkg/endpoints"
"k8s.io/apiserver/pkg/endpoints/discovery"
genericapiserver "k8s.io/apiserver/pkg/server"

"k8s.io/metrics/pkg/apis/custom_metrics"

specificapi "sigs.k8s.io/custom-metrics-apiserver/pkg/apiserver/installer"
"sigs.k8s.io/custom-metrics-apiserver/pkg/provider"
metricstorage "sigs.k8s.io/custom-metrics-apiserver/pkg/registry/custom_metrics"
Expand Down
2 changes: 1 addition & 1 deletion pkg/apiserver/emapis.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import (
genericapi "k8s.io/apiserver/pkg/endpoints"
"k8s.io/apiserver/pkg/endpoints/discovery"
genericapiserver "k8s.io/apiserver/pkg/server"

"k8s.io/metrics/pkg/apis/external_metrics"

specificapi "sigs.k8s.io/custom-metrics-apiserver/pkg/apiserver/installer"
"sigs.k8s.io/custom-metrics-apiserver/pkg/provider"
metricstorage "sigs.k8s.io/custom-metrics-apiserver/pkg/registry/external_metrics"
Expand Down
3 changes: 1 addition & 2 deletions pkg/apiserver/endpoints/handlers/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package handlers

import (
"fmt"
"net/http"
"net/url"
"strings"
Expand Down Expand Up @@ -120,7 +119,7 @@ func ListResourceWithOptions(r cm_rest.ListerWithOptions, scope handlers.Request
trace.Step("Listing from storage done")

responsewriters.WriteObjectNegotiated(scope.Serializer, negotiation.DefaultEndpointRestrictions, scope.Kind.GroupVersion(), w, req, http.StatusOK, result)
trace.Step(fmt.Sprintf("Writing http response done"))
dgrisonnet marked this conversation as resolved.
Show resolved Hide resolved
trace.Step("Writing http response done")
}
}

Expand Down
Loading