Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 1 | # |
| 2 | # Copyright 2016 the original author or authors. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | # |
| 16 | |
| 17 | # set default shell |
| 18 | SHELL = bash -e -o pipefail |
| 19 | |
| 20 | # Variables |
| 21 | VERSION ?= $(shell cat ./VERSION) |
| 22 | |
| 23 | DOCKER_LABEL_VCS_DIRTY = false |
| 24 | ifneq ($(shell git ls-files --others --modified --exclude-standard 2>/dev/null | wc -l | sed -e 's/ //g'),0) |
| 25 | DOCKER_LABEL_VCS_DIRTY = true |
| 26 | endif |
| 27 | ## Docker related |
| 28 | DOCKER_EXTRA_ARGS ?= |
| 29 | DOCKER_REGISTRY ?= |
| 30 | DOCKER_REPOSITORY ?= |
David K. Bainbridge | ee01bc4 | 2021-04-06 17:38:20 +0000 | [diff] [blame] | 31 | DOCKER_TAG ?= ${VERSION}$(shell [[ ${DOCKER_LABEL_VCS_DIRTY} == "true" ]] && echo "-dirty" || true) |
| 32 | DOCKER_TARGET ?= prod |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 33 | ADAPTER_IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-openonu-adapter-go:${DOCKER_TAG} |
| 34 | TYPE ?= minimal |
| 35 | |
| 36 | ## Docker labels. Only set ref and commit date if committed |
| 37 | DOCKER_LABEL_VCS_URL ?= $(shell git remote get-url $(shell git remote)) |
| 38 | DOCKER_LABEL_VCS_REF = $(shell git rev-parse HEAD) |
| 39 | DOCKER_LABEL_BUILD_DATE ?= $(shell date -u "+%Y-%m-%dT%H:%M:%SZ") |
| 40 | DOCKER_LABEL_COMMIT_DATE = $(shell git show -s --format=%cd --date=iso-strict HEAD) |
| 41 | |
| 42 | DOCKER_BUILD_ARGS ?= \ |
| 43 | ${DOCKER_EXTRA_ARGS} \ |
| 44 | --build-arg org_label_schema_version="${VERSION}" \ |
| 45 | --build-arg org_label_schema_vcs_url="${DOCKER_LABEL_VCS_URL}" \ |
| 46 | --build-arg org_label_schema_vcs_ref="${DOCKER_LABEL_VCS_REF}" \ |
| 47 | --build-arg org_label_schema_build_date="${DOCKER_LABEL_BUILD_DATE}" \ |
| 48 | --build-arg org_opencord_vcs_commit_date="${DOCKER_LABEL_COMMIT_DATE}" \ |
| 49 | --build-arg org_opencord_vcs_dirty="${DOCKER_LABEL_VCS_DIRTY}" |
| 50 | |
| 51 | # tool containers |
David K. Bainbridge | d80007b | 2021-04-12 12:22:29 +0000 | [diff] [blame^] | 52 | VOLTHA_TOOLS_VERSION ?= 2.4.0 |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 53 | |
| 54 | GO = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app -v gocache:/.cache -v gocache-${VOLTHA_TOOLS_VERSION}:/go/pkg voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-golang go |
| 55 | GO_JUNIT_REPORT = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app -i voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-go-junit-report go-junit-report |
Matteo Scandolo | 761f751 | 2020-11-23 15:52:40 -0800 | [diff] [blame] | 56 | GOCOVER_COBERTURA = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app/src/github.com/opencord/voltha-openonu-adapter-go -i voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-gocover-cobertura gocover-cobertura |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 57 | GOFMT = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-golang gofmt |
| 58 | GOLANGCI_LINT = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app -v gocache:/.cache -v gocache-${VOLTHA_TOOLS_VERSION}:/go/pkg voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-golangci-lint golangci-lint |
| 59 | HADOLINT = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-hadolint hadolint |
| 60 | |
David K. Bainbridge | ee01bc4 | 2021-04-06 17:38:20 +0000 | [diff] [blame] | 61 | .PHONY: docker-build local-protos local-lib-go help |
| 62 | .DEFAULT_GOAL := help |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 63 | |
| 64 | ## Local Development Helpers |
David K. Bainbridge | ee01bc4 | 2021-04-06 17:38:20 +0000 | [diff] [blame] | 65 | local-protos: ## Copies a local version of the voltha-protos dependency into the vendor directory |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 66 | ifdef LOCAL_PROTOS |
Holger Hildebrandt | 6e846d1 | 2020-12-11 14:35:55 +0000 | [diff] [blame] | 67 | rm -rf vendor/github.com/opencord/voltha-protos/v4/go |
| 68 | mkdir -p vendor/github.com/opencord/voltha-protos/v4/go |
| 69 | cp -r ${LOCAL_PROTOS}/go/* vendor/github.com/opencord/voltha-protos/v4/go |
| 70 | rm -rf vendor/github.com/opencord/voltha-protos/v4/go/vendor |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 71 | endif |
| 72 | |
| 73 | ## Local Development Helpers |
David K. Bainbridge | ee01bc4 | 2021-04-06 17:38:20 +0000 | [diff] [blame] | 74 | local-lib-go: ## Copies a local version of the voltha-lib-go dependency into the vendor directory |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 75 | ifdef LOCAL_LIB_GO |
David K. Bainbridge | ee01bc4 | 2021-04-06 17:38:20 +0000 | [diff] [blame] | 76 | rm -rf vendor/github.com/opencord/voltha-lib-go/v4/pkg |
Holger Hildebrandt | 6e846d1 | 2020-12-11 14:35:55 +0000 | [diff] [blame] | 77 | mkdir -p vendor/github.com/opencord/voltha-lib-go/v4/pkg |
| 78 | cp -r ${LOCAL_LIB_GO}/pkg/* vendor/github.com/opencord/voltha-lib-go/v4/pkg/ |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 79 | endif |
| 80 | |
David K. Bainbridge | ee01bc4 | 2021-04-06 17:38:20 +0000 | [diff] [blame] | 81 | build: docker-build ## Alias for 'docker build' |
| 82 | |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 83 | ## Docker targets |
Holger Hildebrandt | 6999fe5 | 2021-02-11 11:49:05 +0000 | [diff] [blame] | 84 | docker-build: local-protos local-lib-go ## Build openonu adapter docker image (set BUILD_PROFILED=true to also build the profiled image) |
David K. Bainbridge | ee01bc4 | 2021-04-06 17:38:20 +0000 | [diff] [blame] | 85 | docker build $(DOCKER_BUILD_ARGS) --target=${DOCKER_TARGET} -t ${ADAPTER_IMAGENAME} -f docker/Dockerfile.openonu . |
Holger Hildebrandt | 6999fe5 | 2021-02-11 11:49:05 +0000 | [diff] [blame] | 86 | ifdef BUILD_PROFILED |
David K. Bainbridge | ee01bc4 | 2021-04-06 17:38:20 +0000 | [diff] [blame] | 87 | docker build $(DOCKER_BUILD_ARGS) --target=${DOCKER_TARGET} --build-arg EXTRA_GO_BUILD_TAGS="-tags profile" -t ${ADAPTER_IMAGENAME}-profile -f docker/Dockerfile.openonu . |
Holger Hildebrandt | 6999fe5 | 2021-02-11 11:49:05 +0000 | [diff] [blame] | 88 | endif |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 89 | |
David K. Bainbridge | ee01bc4 | 2021-04-06 17:38:20 +0000 | [diff] [blame] | 90 | docker-push: ## Push the docker images to an external repository |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 91 | docker push ${ADAPTER_IMAGENAME} |
Holger Hildebrandt | 6999fe5 | 2021-02-11 11:49:05 +0000 | [diff] [blame] | 92 | ifdef BUILD_PROFILED |
| 93 | docker push ${ADAPTER_IMAGENAME}-profile |
| 94 | endif |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 95 | |
David K. Bainbridge | ee01bc4 | 2021-04-06 17:38:20 +0000 | [diff] [blame] | 96 | docker-kind-load: ## Load docker images into a KinD cluster |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 97 | @if [ "`kind get clusters | grep voltha-$(TYPE)`" = '' ]; then echo "no voltha-$(TYPE) cluster found" && exit 1; fi |
| 98 | kind load docker-image ${ADAPTER_IMAGENAME} --name=voltha-$(TYPE) --nodes $(shell kubectl get nodes --template='{{range .items}}{{.metadata.name}},{{end}}' | sed 's/,$$//') |
| 99 | |
| 100 | |
| 101 | ## lint and unit tests |
| 102 | |
David K. Bainbridge | ee01bc4 | 2021-04-06 17:38:20 +0000 | [diff] [blame] | 103 | lint-dockerfile: ## Perform static analysis on Dockerfile |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 104 | @echo "Running Dockerfile lint check ..." |
| 105 | @${HADOLINT} $$(find . -name "Dockerfile.*") |
| 106 | @echo "Dockerfile lint check OK" |
| 107 | |
David K. Bainbridge | ee01bc4 | 2021-04-06 17:38:20 +0000 | [diff] [blame] | 108 | lint-style: ## Perform lint style checks on source code |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 109 | @echo "Running style check..." |
| 110 | @gofmt_out="$$(${GOFMT} -l $$(find . -name '*.go' -not -path './vendor/*'))" ;\ |
| 111 | if [ ! -z "$$gofmt_out" ]; then \ |
| 112 | echo "$$gofmt_out" ;\ |
| 113 | echo "Style check failed on one or more files ^, run 'go fmt' to fix." ;\ |
| 114 | exit 1 ;\ |
| 115 | fi |
| 116 | @echo "Style check OK" |
| 117 | |
David K. Bainbridge | ee01bc4 | 2021-04-06 17:38:20 +0000 | [diff] [blame] | 118 | lint-sanity: ## Perform basic code checks on source |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 119 | @echo "Running sanity check..." |
| 120 | @${GO} vet -mod=vendor ./... |
| 121 | @echo "Sanity check OK" |
| 122 | |
David K. Bainbridge | ee01bc4 | 2021-04-06 17:38:20 +0000 | [diff] [blame] | 123 | lint-mod: ## Verify the Go dependencies |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 124 | @echo "Running dependency check..." |
| 125 | @${GO} mod verify |
| 126 | @echo "Dependency check OK. Running vendor check..." |
| 127 | @git status > /dev/null |
| 128 | @git diff-index --quiet HEAD -- go.mod go.sum vendor || (echo "ERROR: Staged or modified files must be committed before running this test" && echo "`git status`" && exit 1) |
| 129 | @[[ `git ls-files --exclude-standard --others go.mod go.sum vendor` == "" ]] || (echo "ERROR: Untracked files must be cleaned up before running this test" && echo "`git status`" && exit 1) |
| 130 | ${GO} mod tidy |
| 131 | ${GO} mod vendor |
| 132 | @git status > /dev/null |
| 133 | @git diff-index --quiet HEAD -- go.mod go.sum vendor || (echo "ERROR: Modified files detected after running go mod tidy / go mod vendor" && echo "`git status`" && exit 1) |
| 134 | @[[ `git ls-files --exclude-standard --others go.mod go.sum vendor` == "" ]] || (echo "ERROR: Untracked files detected after running go mod tidy / go mod vendor" && echo "`git status`" && exit 1) |
| 135 | @echo "Vendor check OK." |
| 136 | |
David K. Bainbridge | ee01bc4 | 2021-04-06 17:38:20 +0000 | [diff] [blame] | 137 | lint: local-lib-go lint-style lint-sanity lint-mod lint-dockerfile ## Run all lint targets |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 138 | |
David K. Bainbridge | ee01bc4 | 2021-04-06 17:38:20 +0000 | [diff] [blame] | 139 | test: lint ## Run unit tests |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 140 | @mkdir -p ./tests/results |
| 141 | @${GO} test -mod=vendor -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\ |
| 142 | RETURN=$$? ;\ |
| 143 | ${GO_JUNIT_REPORT} < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\ |
| 144 | ${GOCOVER_COBERTURA} < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\ |
| 145 | exit $$RETURN |
| 146 | |
David K. Bainbridge | ee01bc4 | 2021-04-06 17:38:20 +0000 | [diff] [blame] | 147 | sca: ## Runs static code analysis with the golangci-lint tool |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 148 | @mkdir -p ./sca-report |
| 149 | @echo "Running static code analysis..." |
ozgecanetsia | b644196 | 2021-03-10 10:58:48 +0300 | [diff] [blame] | 150 | @${GOLANGCI_LINT} run --deadline=6m --out-format junit-xml ./... | tee ./sca-report/sca-report.xml |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 151 | @echo "Static code analysis OK" |
| 152 | |
David K. Bainbridge | ee01bc4 | 2021-04-06 17:38:20 +0000 | [diff] [blame] | 153 | clean: distclean ## Removes any local filesystem artifacts generated by a build |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 154 | |
David K. Bainbridge | ee01bc4 | 2021-04-06 17:38:20 +0000 | [diff] [blame] | 155 | distclean: ## Removes any local filesystem artifacts generated by a build or test run |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 156 | rm -rf ./sca-report |
| 157 | |
David K. Bainbridge | ee01bc4 | 2021-04-06 17:38:20 +0000 | [diff] [blame] | 158 | mod-update: ## Update go mod files |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 159 | ${GO} mod tidy |
| 160 | ${GO} mod vendor |
David K. Bainbridge | ee01bc4 | 2021-04-06 17:38:20 +0000 | [diff] [blame] | 161 | |
| 162 | # For each makefile target, add ## <description> on the target line and it will be listed by 'make help' |
| 163 | help: ## Print help for each Makefile target |
| 164 | @echo "Usage: make [<target>]" |
| 165 | @echo "where available targets are:" |
| 166 | @echo |
| 167 | @grep '^[[:alpha:]_-]*:.* ##' $(MAKEFILE_LIST) \ |
| 168 | | sort | awk 'BEGIN {FS=":.* ## "}; {printf "%-25s : %s\n", $$1, $$2};' |