Skip to content

Commit

Permalink
Makefile refactor
Browse files Browse the repository at this point in the history
- cleanup makefile
- update buildtest workflow
- ran go mod tidy, update go.mod

Signed-off-by: adrianc <[email protected]>
  • Loading branch information
adrianchiris committed Jul 28, 2024
1 parent 3478b67 commit c1c00ab
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 111 deletions.
12 changes: 5 additions & 7 deletions .github/workflows/buildtest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ jobs:
goos: [linux]
goarch: [amd64, arm64, ppc64le]
runs-on: ${{ matrix.os }}
env:
GO111MODULE: on
steps:
- name: Set up Go
uses: actions/setup-go@v4
Expand All @@ -29,11 +27,11 @@ jobs:
env:
GOARCH: ${{ matrix.goarch }}
GOOS: ${{ matrix.goos }}
run: GOARCH="${TARGET}" go build ./cmd/sriov/...
run: GOARCH="${TARGET}" make build

- name: Go test
if: ${{ matrix.goarch }} == "amd64"
run: sudo go test -race ./... # sudo needed for netns change in test
run: sudo make test-race # sudo needed for netns change in test

coverage:
runs-on: ubuntu-latest
Expand All @@ -45,8 +43,8 @@ jobs:
with:
go-version: 1.22.x

- name: Check out code into the Go module directory
uses: actions/checkout@v2
- name: Check out code
uses: actions/checkout@v3

- name: Go test with coverage
run: sudo make test-coverage # sudo needed for netns change in test
Expand All @@ -55,7 +53,7 @@ jobs:
uses: coverallsapp/github-action@v2
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
file: test/coverage/lcov.info
file: test/coverage/cover.out

sriov-operator-e2e-test:
name: SR-IOV operator e2e tests
Expand Down
155 changes: 52 additions & 103 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,21 @@
# Package related
BINARY_NAME=sriov
PACKAGE=sriov-cni
ORG_PATH=github.com/k8snetworkplumbingwg
REPO_PATH=$(ORG_PATH)/$(PACKAGE)
GOPATH=$(CURDIR)/.gopath
GOBIN=$(CURDIR)/bin
BINDIR=$(CURDIR)/bin
BUILDDIR=$(CURDIR)/build
BASE=$(GOPATH)/src/$(REPO_PATH)
GOFILES = $(shell find . -name *.go | grep -vE "(\/vendor\/)|(_test.go)")
PKGS = $(or $(PKG),$(shell cd $(BASE) && env GOPATH=$(GOPATH) $(GO) list ./... | grep -v "^$(PACKAGE)/vendor/"))
TESTPKGS = $(shell env GOPATH=$(GOPATH) $(GO) list -f '{{ if or .TestGoFiles .XTestGoFiles }}{{ .ImportPath }}{{ end }}' $(PKGS))
PKGS = $(or $(PKG),$(shell go list ./... | grep -v ".*/mocks"))
IMAGE_BUILDER ?= docker

export GOPATH
export GOBIN
export GO111MODULE=on
# Test settings
TIMEOUT = 30
COVERAGE_DIR = $(CURDIR)/test/coverage
COVERAGE_MODE = atomic
COVERAGE_PROFILE = $(COVERAGE_DIR)/cover.out

# Docker
IMAGEDIR=$(BASE)/images
DOCKERFILE=$(CURDIR)/Dockerfile
TAG=ghcr.io/k8snetworkplumbingwg/sriov-cni
IMAGEDIR=$(CURDIR)/images
DOCKERFILE?=$(CURDIR)/Dockerfile
TAG?=ghcr.io/k8snetworkplumbingwg/sriov-cni
# Accept proxy settings for docker
DOCKERARGS=
ifdef HTTP_PROXY
Expand All @@ -32,129 +29,81 @@ ifdef HTTPS_PROXY
DOCKERARGS += --build-arg https_proxy=$(HTTPS_PROXY)
endif

# Go tools
GO = go
GOFMT = gofmt
TIMEOUT = 15
# Go settings
GO = go
GO_BUILD_OPTS ?=
GO_LDFLAGS ?=
GO_FLAGS ?=
GO_TAGS ?=-tags no_openssl
export GOPATH?=$(shell go env GOPATH)

# debug
V ?= 0
Q = $(if $(filter 1,$V),,@)

.PHONY: all
all: fmt lint build

$(BASE): ; $(info Setting GOPATH...)
@mkdir -p $(dir $@)
@ln -sf $(CURDIR) $@
all: fmt lint build test

$(GOBIN):
$(BINDIR) $(BUILDDIR) $(COVERAGE_DIR): ; $(info Creating directory $@...)
@mkdir -p $@

$(BUILDDIR): | $(BASE) ; $(info Creating build directory...)
@cd $(BASE) && mkdir -p $@

build: $(BUILDDIR)/$(BINARY_NAME) ; $(info Building $(BINARY_NAME)...) @ ## Build SR-IOV CNI plugin
.PHONY: build
build: | $(BUILDDIR) ; $(info Building $(BINARY_NAME)...) @ ## Build SR-IOV CNI plugin
$Q cd $(CURDIR)/cmd/$(BINARY_NAME) && $(GO_BUILD_OPTS) go build -ldflags '$(GO_LDFLAGS)' $(GO_FLAGS) -o $(BUILDDIR)/$(BINARY_NAME) $(GO_TAGS) -v
$(info Done!)

$(BUILDDIR)/$(BINARY_NAME): $(GOFILES) | $(BUILDDIR)
@cd $(BASE)/cmd/$(BINARY_NAME) && CGO_ENABLED=0 $(GO) build -o $(BUILDDIR)/$(BINARY_NAME) -tags no_openssl -v


# Tools

GOLANGCILINT = $(GOBIN)/golangci-lint
$(GOLANGCILINT): | $(BASE) ; $(info Installing golangci-lint...)
$Q go install github.com/golangci/golangci-lint/cmd/[email protected]

GOCOVMERGE = $(GOBIN)/gocovmerge
$(GOCOVMERGE): | $(BASE) ; $(info Building gocovmerge...)
$Q go install github.com/wadey/gocovmerge@latest

GOCOV = $(GOBIN)/gocov
$(GOCOV): | $(BASE) ; $(info Building gocov...)
$Q go install github.com/axw/gocov/[email protected]

GOCOVXML = $(GOBIN)/gocov-xml
$(GOCOVXML): | $(BASE) ; $(info Building gocov-xml...)
$Q go install github.com/AlekSi/gocov-xml@latest

GCOV2LCOV = $(GOBIN)/gcov2lcov
$(GCOV2LCOV): | $(BASE) ; $(info building gcov2lcov...)
$Q go install github.com/jandelgado/gcov2lcov@latest

GO2XUNIT = $(GOBIN)/go2xunit
$(GO2XUNIT): | $(BASE) ; $(info Building go2xunit...)
$Q go install github.com/tebeka/go2xunit@latest

GOLANGCI_LINT = $(BINDIR)/golangci-lint
GOLANGCI_LINT_VERSION = v1.52.2
$(GOLANGCI_LINT): | $(BINDIR) ; $(info Installing golangci-lint...)
$Q GOBIN=$(BINDIR) $(GO) install github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLANGCI_LINT_VERSION)

# Tests

TEST_TARGETS := test-default test-bench test-short test-verbose test-race
.PHONY: $(TEST_TARGETS) test-xml check test tests
test-bench: ARGS=-run=__absolutelynothing__ -bench=. ## Run benchmarks
test-short: ARGS=-short ## Run only short tests
TEST_TARGETS := test-default test-verbose test-race
.PHONY: $(TEST_TARGETS) test
test-verbose: ARGS=-v ## Run tests in verbose mode with coverage reporting
test-race: ARGS=-race ## Run tests with race detector
$(TEST_TARGETS): NAME=$(MAKECMDGOALS:test-%=%)
$(TEST_TARGETS): test
check test tests: fmt lint | $(BASE) ; $(info Running $(NAME:%=% )tests...) @ ## Run tests
$Q cd $(BASE) && $(GO) test -timeout $(TIMEOUT)s $(ARGS) $(TESTPKGS)
test: ; $(info running $(NAME:%=% )tests...) @ ## Run tests
$Q $(GO) test -timeout $(TIMEOUT)s $(ARGS) $(PKGS)

test-xml: fmt lint | $(BASE) $(GO2XUNIT) ; $(info Running $(NAME:%=% )tests...) @ ## Run tests with xUnit output
$Q cd $(BASE) && 2>&1 $(GO) test -timeout 20s -v $(TESTPKGS) | tee test/tests.output
$(GO2XUNIT) -fail -input test/tests.output -output test/tests.xml

COVERAGE_DIR = $(CURDIR)/test/coverage
COVERAGE_MODE = atomic
COVERAGE_PROFILE = $(COVERAGE_DIR)/profile.out
COVERAGE_XML = $(COVERAGE_DIR)/coverage.xml
COVERAGE_HTML = $(COVERAGE_DIR)/index.html
.PHONY: test-coverage test-coverage-tools
test-coverage-tools: | $(GOCOVMERGE) $(GOCOV) $(GOCOVXML) $(GCOV2LCOV)
test-coverage: fmt test-coverage-tools | $(BASE) ; $(info Running coverage tests...) @ ## Run coverage tests
$Q mkdir -p $(COVERAGE_DIR)/pkgs
$Q cd $(BASE) && for pkg in $(TESTPKGS); do \
$(GO) test \
-coverpkg=$(REPO_PATH)/... \
-covermode=$(COVERAGE_MODE) \
-coverprofile="$(COVERAGE_DIR)/pkgs/`echo $$pkg | tr "/" "-"`.cover" $$pkg ;\
done
$Q $(GOCOVMERGE) $(COVERAGE_DIR)/pkgs/*.cover > $(COVERAGE_PROFILE)
$Q $(GO) tool cover -html=$(COVERAGE_PROFILE) -o $(COVERAGE_HTML)
$Q $(GOCOV) convert $(COVERAGE_PROFILE) | $(GOCOVXML) > $(COVERAGE_XML)
$Q $(GCOV2LCOV) -infile $(COVERAGE_PROFILE) -outfile $(COVERAGE_DIR)/lcov.info
.PHONY: test-coverage
test-coverage: | $(COVERAGE_DIR) ; $(info Running coverage tests...) @ ## Run coverage tests
$Q $(GO) test -timeout $(TIMEOUT)s -cover -covermode=$(COVERAGE_MODE) -coverprofile=$(COVERAGE_PROFILE) $(PKGS)

.PHONY: lint
lint: | $(BASE) $(GOLANGCILINT) ; $(info Running golangci-lint...) @ ## Run golint on all source files
$Q $(GOLANGCILINT) run ./...
lint: $(GOLANGCI_LINT) ; $(info Running golangci-lint linter...) @ ## Run golangci-lint linter
$Q $(GOLANGCI_LINT) run

.PHONY: fmt
fmt: ; $(info Running gofmt...) @ ## Run gofmt on all source files
@ret=0 && for d in $$($(GO) list -f '{{.Dir}}' ./... | grep -v /vendor/); do \
$(GOFMT) -l -w $$d/*.go || ret=$$? ; \
done ; exit $$ret
fmt: ; $(info Running go fmt...) @ ## Run go fmt on all source files
@ $(GO) fmt ./...

.PHONY: vet
vet: ; $(info Running go vet...) @ ## Run go vet on all source files
@ $(GO) vet ./...

# Docker image
# To pass proxy for Docker invoke it as 'make image HTTP_POXY=http://192.168.0.1:8080'
.PHONY: image
image: | $(BASE) ; $(info Building Docker image...) @ ## Build SR-IOV CNI docker image
image: ; $(info Building Docker image...) @ ## Build SR-IOV CNI docker image
@$(IMAGE_BUILDER) build -t $(TAG) -f $(DOCKERFILE) $(CURDIR) $(DOCKERARGS)

test-image: image
$Q $(BASE)/images/image_test.sh $(IMAGE_BUILDER) $(TAG)
$Q $(IMAGEDIR)/image_test.sh $(IMAGE_BUILDER) $(TAG)

# Misc

.PHONY: deps-update
deps-update: ; $(info Updating dependencies...) @ ## Update dependencies
@go mod tidy && go mod vendor
@ $(GO) mod tidy

.PHONY: clean
clean: | $(BASE) ; $(info Cleaning...) @ ## Cleanup everything
@cd $(BASE) && $(GO) clean --modcache --cache --testcache
@rm -rf $(GOPATH)
@rm -rf $(BUILDDIR)
@rm -rf $(GOBIN)
@rm -rf test/
clean: ; $(info Cleaning...) @ ## Cleanup everything
@ $(GO) clean --modcache --cache --testcache
@ rm -rf $(BUILDDIR)
@ rm -rf $(BINDIR)
@ rm -rf test/

.PHONY: help
help: ; @ ## Display this help message
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/stretchr/testify v1.8.2
github.com/vishvananda/netlink v1.2.1-beta.2.0.20240221172127-ec7bcb248e94
golang.org/x/net v0.23.0
golang.org/x/sys v0.18.0
)

require (
Expand All @@ -24,7 +25,6 @@ require (
github.com/safchain/ethtool v0.3.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/vishvananda/netns v0.0.4 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.17.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
Expand Down

0 comments on commit c1c00ab

Please sign in to comment.