From 2c4442b17f612c99cc7a8fd5b7d1558f8fbff548 Mon Sep 17 00:00:00 2001 From: Brian Goff Date: Mon, 26 Sep 2022 20:16:30 +0000 Subject: [PATCH] Fix linting issues and update `make lint` target. Signed-off-by: Brian Goff --- .dockerignore | 4 +++- .golangci.yml | 2 ++ Dockerfile | 18 ++++++++++++++++++ Makefile | 8 ++++++-- .../internal/provider/mock/mock.go | 6 +++--- node/nodeutil/tls.go | 4 ++-- 6 files changed, 34 insertions(+), 8 deletions(-) diff --git a/.dockerignore b/.dockerignore index 935cb2a78..8ca5f4eab 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,4 +1,6 @@ .vscode private.env *.private.* -providers/azurebatch/deployment/ \ No newline at end of file +providers/azurebatch/deployment/ +Dockerfile +.dockerignore diff --git a/.golangci.yml b/.golangci.yml index 3768ca0bd..e41a2d064 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -2,6 +2,8 @@ linter-settings: lll: line-length: 200 +timeout: 10m + run: skip-dirs: # This directory contains copy code from upstream kubernetes/kubernetes, skip it. diff --git a/Dockerfile b/Dockerfile index 29c6be42c..70f3de588 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,3 +1,5 @@ +ARG GOLANG_CI_LINT_VERSION + FROM golang:1.18 as builder ENV PATH /go/bin:/usr/local/go/bin:$PATH ENV GOPATH /go @@ -7,6 +9,22 @@ ARG BUILD_TAGS="" RUN make VK_BUILD_TAGS="${BUILD_TAGS}" build RUN cp bin/virtual-kubelet /usr/bin/virtual-kubelet +FROM golangci/golangci-lint:${GOLANG_CI_LINT_VERSION} as lint +WORKDIR /app +COPY go.mod ./ +COPY go.sum ./ +RUN \ + --mount=type=cache,target=/root/.cache/go-build \ + --mount=type=cache,target=/go/pkg/mod \ + go mod download +COPY . . +ARG OUT_FORMAT +RUN \ + --mount=type=cache,target=/root/.cache/go-build \ + --mount=type=cache,target=/go/pkg/mod \ + --mount=type=cache,target=/root/.cache/golangci-lint \ + golangci-lint run -v $(if [ -n "${OUT_FORMAT}" ]; then echo "--out-format=${OUT_FORMAT}"; fi) + FROM scratch COPY --from=builder /usr/bin/virtual-kubelet /usr/bin/virtual-kubelet COPY --from=builder /etc/ssl/certs/ /etc/ssl/certs diff --git a/Makefile b/Makefile index aacabc5ea..d9e491ed7 100644 --- a/Makefile +++ b/Makefile @@ -183,6 +183,10 @@ envtest: kubebuilder_2.3.1_${TEST_OS}_${TEST_ARCH} fmt: goimports -w $(shell go list -f '{{.Dir}}' ./...) + +export GOLANG_CI_LINT_VERSION ?= v1.48.0 +DOCKER_BUILD ?= docker buildx build + .PHONY: lint -lint: $(gobin_tool) - gobin -run github.com/golangci/golangci-lint/cmd/golangci-lint@v1.33.0 run ./... \ No newline at end of file +lint: + $(DOCKER_BUILD) --target=lint --build-arg GOLANG_CI_LINT_VERSION --build-arg OUT_FORMAT . diff --git a/cmd/virtual-kubelet/internal/provider/mock/mock.go b/cmd/virtual-kubelet/internal/provider/mock/mock.go index 987d08752..87834fa36 100644 --- a/cmd/virtual-kubelet/internal/provider/mock/mock.go +++ b/cmd/virtual-kubelet/internal/provider/mock/mock.go @@ -5,8 +5,8 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "math/rand" + "os" "strings" "time" @@ -97,7 +97,7 @@ func NewMockProvider(providerConfig, nodeName, operatingSystem string, internalI // loadConfig loads the given json configuration files. func loadConfig(providerConfig, nodeName string) (config MockConfig, err error) { - data, err := ioutil.ReadFile(providerConfig) + data, err := os.ReadFile(providerConfig) if err != nil { return config, err } @@ -283,7 +283,7 @@ func (p *MockProvider) GetContainerLogs(ctx context.Context, namespace, podName, ctx = addAttributes(ctx, span, namespaceKey, namespace, nameKey, podName, containerNameKey, containerName) log.G(ctx).Infof("receive GetContainerLogs %q", podName) - return ioutil.NopCloser(strings.NewReader("")), nil + return io.NopCloser(strings.NewReader("")), nil } // RunInContainer executes a command in a container in the pod, copying data diff --git a/node/nodeutil/tls.go b/node/nodeutil/tls.go index ac29e3f1b..7c6f3ed45 100644 --- a/node/nodeutil/tls.go +++ b/node/nodeutil/tls.go @@ -4,7 +4,7 @@ import ( "crypto/tls" "crypto/x509" "fmt" - "io/ioutil" + "os" ) // WithTLSConfig returns a NodeOpt which creates a base TLSConfig with the default cipher suites and tls min verions. @@ -31,7 +31,7 @@ func WithTLSConfig(opts ...func(*tls.Config) error) NodeOpt { // WithCAFromPath makes a TLS config option to set up client auth using the path to a PEM encoded CA cert. func WithCAFromPath(p string) func(*tls.Config) error { return func(cfg *tls.Config) error { - pem, err := ioutil.ReadFile(p) + pem, err := os.ReadFile(p) if err != nil { return fmt.Errorf("error reading ca cert pem: %w", err) }