Skip to content

Commit

Permalink
chore: remove run-in-docker logic from Makefile (#946)
Browse files Browse the repository at this point in the history
* chore: remove run-in-docker logic from Makefile

The same benefits can be achieved by simply launching an
interactive docker session inside a supported image and running
commands from there. This approach has some additional benefits
- Easier to maintain
- Debloats Makefile
- Less hidden magic
- Same code for local and containerised execution

* chore: suggested changes

---------

Co-authored-by: George Blue <gblue@pivotal.io>
  • Loading branch information
fnaranjo-vmw and blgm authored Feb 9, 2024
1 parent 35fc6a0 commit dd99af1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 30 deletions.
48 changes: 19 additions & 29 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,6 @@ else
OSFAMILY=linux
endif

ifeq ($(USE_GO_CONTAINERS),)
GO=go
GOFMT=gofmt
else
UID:=$(shell id -u)
DOCKER_OPTS=--rm -u $(UID) -v $(HOME):$(HOME) -e HOME -e USER=$(USER) -e USERNAME=$(USER) -w $(PWD)
GO=docker run $(DOCKER_OPTS) -e GOARCH -e GOOS -e CGO_ENABLED golang:$(GO-VERSION) go
GOFMT=docker run $(DOCKER_OPTS) -e GOARCH -e GOOS -e CGO_ENABLED golang:$(GO-VERSION) gofmt
endif

SRC = $(shell find . -name "*.go" | grep -v "_test\." )

ifeq ($(VERSION),)
Expand All @@ -42,8 +32,8 @@ PKG="github.com/cloudfoundry/cloud-service-broker"
.PHONY: deps-go-binary
deps-go-binary:
ifeq ($(SKIP_GO_VERSION_CHECK),)
@@if [ "$$($(GO) version | awk '{print $$3}')" != "${GO-VER}" ]; then \
echo "Go version does not match: expected: ${GO-VER}, got $$($(GO) version | awk '{print $$3}')"; \
@@if [ "$$(go version | awk '{print $$3}')" != "${GO-VER}" ]; then \
echo "Go version does not match: expected: ${GO-VER}, got $$(go version | awk '{print $$3}')"; \
exit 1; \
fi
endif
Expand All @@ -64,53 +54,53 @@ test: download lint test-units test-integration ## run lint and unit tests

.PHONY: test-units
test-units: deps-go-binary ## run unit tests
$(GO) test $(PKG)/brokerapi/... $(PKG)/cmd/... $(PKG)/dbservice/... $(PKG)/internal/... $(PKG)/pkg/... $(PKG)/utils/... -tags=service_broker
go test $(PKG)/brokerapi/... $(PKG)/cmd/... $(PKG)/dbservice/... $(PKG)/internal/... $(PKG)/pkg/... $(PKG)/utils/... -tags=service_broker

# Integration tests are relatively resource-hungry, so we tune down the number of processes that run in parallel
.PHONY: test-integration
test-integration: deps-go-binary .pak-cache ## run integration tests
PAK_BUILD_CACHE_PATH=$(PAK_CACHE) $(GO) run github.com/onsi/ginkgo/v2/ginkgo --procs 4 integrationtest/...
PAK_BUILD_CACHE_PATH=$(PAK_CACHE) go run github.com/onsi/ginkgo/v2/ginkgo --procs 4 integrationtest/...

.pak-cache:
mkdir -p $(PAK_CACHE)

.PHONY: test-units-coverage
test-units-coverage: ## test-units coverage score
$(GO) list ./... | grep -v fake > /tmp/csb-non-fake.txt
go list ./... | grep -v fake > /tmp/csb-non-fake.txt
paste -sd "," /tmp/csb-non-fake.txt > /tmp/csb-coverage-pkgs.txt
$(GO) test -coverpkg=`cat /tmp/csb-coverage-pkgs.txt` -coverprofile=/tmp/csb-coverage.out `$(GO) list ./... | grep -v integrationtest`
$(GO) tool cover -func /tmp/csb-coverage.out | grep total
go test -coverpkg=`cat /tmp/csb-coverage-pkgs.txt` -coverprofile=/tmp/csb-coverage.out `go list ./... | grep -v integrationtest`
go tool cover -func /tmp/csb-coverage.out | grep total

###### Build ##################################################################

./build/cloud-service-broker.linux: $(SRC)
CGO_ENABLED=0 GOARCH=amd64 GOOS=linux $(GO) build -o ./build/cloud-service-broker.linux -ldflags ${LDFLAGS}
CGO_ENABLED=0 GOARCH=amd64 GOOS=linux go build -o ./build/cloud-service-broker.linux -ldflags ${LDFLAGS}

./build/cloud-service-broker.darwin: $(SRC)
GOARCH=amd64 GOOS=darwin $(GO) build -o ./build/cloud-service-broker.darwin -ldflags ${LDFLAGS}
GOARCH=amd64 GOOS=darwin go build -o ./build/cloud-service-broker.darwin -ldflags ${LDFLAGS}

.PHONY: build
build: deps-go-binary ./build/cloud-service-broker.linux ./build/cloud-service-broker.darwin ## build binary

.PHONY: install
install: deps-go-binary ## install as /usr/local/bin/csb
$(GO) build -o csb -ldflags ${LDFLAGS}
go build -o csb -ldflags ${LDFLAGS}
mv csb /usr/local/bin/csb

.PHONY: generate
generate: ## generate test fakes
${GO} generate ./...
go generate ./...
make format

.PHONY: download
download: ## download go module dependencies
${GO} mod download
go mod download

###### Clean ##################################################################

.PHONY: clean
clean: deps-go-binary ## clean up from previous builds
-$(GO) clean --modcache
-go clean --modcache
-rm -rf ./build
-rm -rf /tmp/csb-non-fake.txt
-rm -rf /tmp/csb-coverage.out
Expand All @@ -122,29 +112,29 @@ clean: deps-go-binary ## clean up from previous builds
lint: checkformat checkimports vet staticcheck ## lint the source

checkformat: ## Checks that the code is formatted correctly
@@if [ -n "$$(${GOFMT} -s -e -l -d .)" ]; then \
@@if [ -n "$$(gofmt -s -e -l -d .)" ]; then \
echo "gofmt check failed: run 'make format'"; \
exit 1; \
fi

checkimports: ## Checks that imports are formatted correctly
@@if [ -n "$$(${GO} run golang.org/x/tools/cmd/goimports -l -d .)" ]; then \
@@if [ -n "$$(go run golang.org/x/tools/cmd/goimports -l -d .)" ]; then \
echo "goimports check failed: run 'make format'"; \
exit 1; \
fi

vet: ## Runs go vet
${GO} vet ./...
go vet ./...

staticcheck: ## Runs staticcheck
${GO} list ./... | grep -v 'fakes$$' | xargs ${GO} run honnef.co/go/tools/cmd/staticcheck
go list ./... | grep -v 'fakes$$' | xargs go run honnef.co/go/tools/cmd/staticcheck

###### Format #################################################################

.PHONY: format
format: ## format the source
${GOFMT} -s -e -l -w .
${GO} run golang.org/x/tools/cmd/goimports -l -w .
gofmt -s -e -l -w .
go run golang.org/x/tools/cmd/goimports -l -w .

###### Image ##################################################################

Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,13 @@ It supports the following sub-commands:
## Development

`make` is used to orchestrate most development tasks.
`go` is required to build the broker. If you don't have `go` installed, it is possible to use a `docker` image to build and unit test the broker. If the environment variable `USE_GO_CONTAINERS` exists, `make` will use `docker` versions of the tools so you don't need to have them installed locally.
`go` is required to build the broker.
If you don't have `go` installed, it is possible to use `docker` to launch an interactive shell into some supported image containing all necessary tools. For example:
```
# From the root of this repo run:
docker run -it --rm -v "${PWD}:/repo" --workdir "/repo" --entrypoint "/bin/bash" golang:latest
make
```

There are make targets for most common dev tasks. Running make without a target will list the possible targets.

Expand Down

0 comments on commit dd99af1

Please sign in to comment.