Skip to content

Commit 8fbb1e3

Browse files
authored
*: Makefile and other general improvements (#74)
Signed-off-by: Joe Lanford <[email protected]>
1 parent c960930 commit 8fbb1e3

File tree

5 files changed

+108
-56
lines changed

5 files changed

+108
-56
lines changed

.github/workflows/ci.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ jobs:
3333
- name: Get dependencies
3434
run: go mod download
3535

36-
- name: Build
37-
run: make build
36+
- name: check
37+
run: make fix
3838

3939
- name: Test
4040
run: make test
@@ -54,7 +54,7 @@ jobs:
5454
- name: Lint
5555
uses: golangci/golangci-lint-action@v2
5656
with:
57-
version: v1.29
57+
version: v1.35.2
5858

5959
deploy:
6060
name: Deploy

.gitignore

+30-9
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,46 @@
1+
# Binaries for programs and plugins
2+
/bin
3+
/tools/bin
4+
5+
# Other IDE files
6+
.idea
17

8+
# Created by https://www.toptal.com/developers/gitignore/api/go,vim
9+
# Edit at https://www.toptal.com/developers/gitignore?templates=go,vim
10+
11+
### Go ###
212
# Binaries for programs and plugins
313
*.exe
414
*.exe~
515
*.dll
616
*.so
717
*.dylib
8-
bin
9-
testbin
1018

11-
# Test binary, build with `go test -c`
19+
# Test binary, built with `go test -c`
1220
*.test
1321

1422
# Output of the go coverage tool, specifically when used with LiteIDE
1523
*.out
1624

17-
# Kubernetes Generated files - skip generated files, except for vendored files
25+
### Vim ###
26+
# Swap
27+
[._]*.s[a-v][a-z]
28+
!*.svg # comment out if you don't need vector files
29+
[._]*.sw[a-p]
30+
[._]s[a-rt-v][a-z]
31+
[._]ss[a-gi-z]
32+
[._]sw[a-p]
1833

19-
!vendor/**/zz_generated.*
34+
# Session
35+
Session.vim
36+
Sessionx.vim
2037

21-
# editor and IDE paraphernalia
22-
.idea
23-
*.swp
24-
*.swo
38+
# Temporary
39+
.netrwhist
2540
*~
41+
# Auto-generated tag files
42+
tags
43+
# Persistent undo
44+
[._]*.un~
45+
46+
# End of https://www.toptal.com/developers/gitignore/api/go,vim

.golangci.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
run:
2+
timeout: 5m
13
linters:
24
disable-all: true
35
enable:
@@ -13,5 +15,3 @@ linters:
1315
- typecheck
1416
- gofmt
1517
- goconst
16-
run:
17-
deadline: 5m

Makefile

+35-42
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,57 @@
1-
2-
# Image URL to use all building/pushing image targets
3-
IMG ?= quay.io/joelanford/helm-operator
4-
5-
SHELL=/bin/bash
6-
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
7-
ifeq (,$(shell go env GOBIN))
8-
GOBIN=$(shell go env GOPATH)/bin
9-
else
10-
GOBIN=$(shell go env GOBIN)
11-
endif
12-
131
# GO_BUILD_ARGS should be set when running 'go build' or 'go install'.
142
VERSION_PKG = "$(shell go list -m)/internal/version"
153
SCAFFOLD_VERSION = $(shell git describe --abbrev=0)
164
GIT_VERSION = $(shell git describe --dirty --tags --always)
175
GIT_COMMIT = $(shell git rev-parse HEAD)
6+
BUILD_DIR = $(PWD)/bin
187
GO_BUILD_ARGS = \
198
-gcflags "all=-trimpath=$(shell dirname $(shell pwd))" \
209
-asmflags "all=-trimpath=$(shell dirname $(shell pwd))" \
2110
-ldflags " \
11+
-s \
12+
-w \
2213
-X '$(VERSION_PKG).ScaffoldVersion=$(SCAFFOLD_VERSION)' \
2314
-X '$(VERSION_PKG).GitVersion=$(GIT_VERSION)' \
2415
-X '$(VERSION_PKG).GitCommit=$(GIT_COMMIT)' \
2516
" \
2617

18+
# Always use Go modules
2719
export GO111MODULE = on
2820

21+
# Setup project-local paths and build settings
22+
SHELL=/bin/bash
23+
TOOLS_DIR=$(PWD)/tools
24+
TOOLS_BIN_DIR=$(TOOLS_DIR)/bin
25+
SCRIPTS_DIR=$(TOOLS_DIR)/scripts
26+
export PATH := $(BUILD_DIR):$(TOOLS_BIN_DIR):$(SCRIPTS_DIR):$(PATH)
27+
28+
.PHONY: all
29+
all: test lint build
30+
2931
# Run tests
30-
ENVTEST_ASSETS_DIR=$(shell pwd)/testbin
31-
test: fmt vet
32-
mkdir -p ${ENVTEST_ASSETS_DIR}
33-
test -f ${ENVTEST_ASSETS_DIR}/setup-envtest.sh || curl -sSLo ${ENVTEST_ASSETS_DIR}/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/master/hack/setup-envtest.sh
34-
source ${ENVTEST_ASSETS_DIR}/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); go test -race -covermode atomic -coverprofile cover.out ./...
32+
.PHONY: test
33+
export KUBEBUILDER_ASSETS := $(TOOLS_BIN_DIR)
34+
CR_VERSION=$(shell go list -m sigs.k8s.io/controller-runtime | cut -d" " -f2 | sed 's/^v//')
35+
test:
36+
fetch envtest $(CR_VERSION) && go test -race -covermode atomic -coverprofile cover.out ./...
3537

3638
# Build manager binary
37-
build: fmt vet
38-
CGO_ENABLED=0 go build $(GO_BUILD_ARGS) -o bin/helm-operator main.go
39-
40-
# Run go fmt against code
41-
fmt:
39+
.PHONY: build
40+
build:
41+
CGO_ENABLED=0 mkdir -p $(BUILD_DIR) && go build $(GO_BUILD_ARGS) -o $(BUILD_DIR) ./
42+
43+
# Run go fmt and go mod tidy, and check for clean git tree
44+
.PHONY: fix
45+
fix:
46+
go mod tidy
4247
go fmt ./...
48+
git diff --exit-code
4349

44-
# Run go vet against code
45-
vet:
46-
go vet ./...
47-
48-
lint: golangci-lint
49-
$(GOLANGCI_LINT) run
50-
lint-fix: golangci-lint ## Run golangci-lint linter and perform fixes
51-
$(GOLANGCI_LINT) run --fix
50+
# Run various checks against code
51+
.PHONY: lint
52+
lint:
53+
fetch golangci-lint 1.35.2 && golangci-lint run
5254

53-
# find or download controller-gen
54-
# download controller-gen if necessary
55-
golangci-lint:
56-
ifeq (, $(shell which golangci-lint))
57-
@{ \
58-
set -e ;\
59-
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(shell go env GOPATH)/bin v1.27.0 ;\
60-
}
61-
GOLANGCI_LINT=$(shell go env GOPATH)/bin/golangci-lint
62-
else
63-
GOLANGCI_LINT=$(shell which golangci-lint)
64-
endif
55+
.PHONY: clean
56+
clean:
57+
rm -rf $(TOOLS_BIN_DIR) $(BUILD_DIR)

tools/scripts/fetch

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env bash
2+
3+
ROOT="$(git rev-parse --show-toplevel)"
4+
DEST="${ROOT}/tools/bin"
5+
6+
fetch() {
7+
local tool=$1; shift
8+
local ver=$1; shift
9+
10+
local ver_cmd=""
11+
local fetch_cmd=""
12+
case "$tool" in
13+
"golangci-lint")
14+
ver_cmd="${DEST}/golangci-lint --version 2>/dev/null | cut -d\" \" -f4"
15+
fetch_cmd="curl -sSfL \"https://raw.githubusercontent.com/golangci/golangci-lint/v${ver}/install.sh\" | sh -s -- -b \"${DEST}\" \"v${ver}\""
16+
;;
17+
"goreleaser")
18+
ver_cmd="${DEST}/goreleaser --version 2>/dev/null | grep version | cut -d' ' -f3"
19+
fetch_cmd="curl -sSfL https://install.goreleaser.com/github.com/goreleaser/goreleaser.sh | sh -s -- -b \"${DEST}\" -d \"v${ver}\""
20+
;;
21+
"envtest")
22+
ver_cmd="cat ${DEST}/.envtest_version 2>/dev/null"
23+
fetch_cmd="(test -f ${DEST}/setup-envtest.sh || curl -sSLo ${DEST}/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/v${ver}/hack/setup-envtest.sh) && (source ${DEST}/setup-envtest.sh; fetch_envtest_tools ${DEST}/../) && echo ${ver} > ${DEST}/.envtest_version"
24+
;;
25+
*)
26+
echo "unknown tool $tool"
27+
return 1
28+
;;
29+
esac
30+
31+
if [[ "${ver}" != "$(eval ${ver_cmd})" ]]; then
32+
echo "${tool} missing or not version '${ver}', downloading..."
33+
mkdir -p ${DEST}
34+
eval ${fetch_cmd}
35+
fi
36+
}
37+
38+
fetch $@

0 commit comments

Comments
 (0)