khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [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 | |
Matt Jeanneret | 2e3051a | 2019-05-11 15:01:46 -0400 | [diff] [blame] | 17 | # set default shell |
| 18 | SHELL = bash -e -o pipefail |
Kent Hagerman | 074d0e0 | 2019-04-24 17:58:16 -0400 | [diff] [blame] | 19 | |
Zack Williams | 27f59a4 | 2019-05-10 09:12:07 -0700 | [diff] [blame] | 20 | # Variables |
Kent Hagerman | f4a3aab | 2019-05-22 14:42:34 -0400 | [diff] [blame] | 21 | VERSION ?= $(shell cat ./VERSION) |
Zack Williams | 27f59a4 | 2019-05-10 09:12:07 -0700 | [diff] [blame] | 22 | |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 23 | # Packages |
| 24 | PACKAGES = $(shell go list ./...) |
| 25 | |
Kent Hagerman | f4a3aab | 2019-05-22 14:42:34 -0400 | [diff] [blame] | 26 | DOCKER_LABEL_VCS_DIRTY = false |
| 27 | ifneq ($(shell git ls-files --others --modified --exclude-standard 2>/dev/null | wc -l | sed -e 's/ //g'),0) |
| 28 | DOCKER_LABEL_VCS_DIRTY = true |
| 29 | endif |
Matt Jeanneret | 2e3051a | 2019-05-11 15:01:46 -0400 | [diff] [blame] | 30 | ## Docker related |
David K. Bainbridge | e14914d | 2019-05-24 13:43:05 -0700 | [diff] [blame] | 31 | DOCKER_EXTRA_ARGS ?= |
Kent Hagerman | f4a3aab | 2019-05-22 14:42:34 -0400 | [diff] [blame] | 32 | DOCKER_REGISTRY ?= |
| 33 | DOCKER_REPOSITORY ?= |
| 34 | DOCKER_TAG ?= ${VERSION}$(shell [[ ${DOCKER_LABEL_VCS_DIRTY} == "true" ]] && echo "-dirty" || true) |
David K. Bainbridge | 4183514 | 2021-04-01 17:26:12 +0000 | [diff] [blame] | 35 | DOCKER_TARGET ?= prod |
Kent Hagerman | f4a3aab | 2019-05-22 14:42:34 -0400 | [diff] [blame] | 36 | RWCORE_IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-rw-core |
Andrea Campanella | 7a6cce2 | 2019-12-17 14:10:27 -0800 | [diff] [blame] | 37 | TYPE ?= minimal |
Matt Jeanneret | 2e3051a | 2019-05-11 15:01:46 -0400 | [diff] [blame] | 38 | |
| 39 | ## Docker labels. Only set ref and commit date if committed |
Kent Hagerman | f4a3aab | 2019-05-22 14:42:34 -0400 | [diff] [blame] | 40 | DOCKER_LABEL_VCS_URL ?= $(shell git remote get-url $(shell git remote)) |
| 41 | DOCKER_LABEL_VCS_REF = $(shell git rev-parse HEAD) |
| 42 | DOCKER_LABEL_BUILD_DATE ?= $(shell date -u "+%Y-%m-%dT%H:%M:%SZ") |
| 43 | DOCKER_LABEL_COMMIT_DATE = $(shell git show -s --format=%cd --date=iso-strict HEAD) |
Matt Jeanneret | 2e3051a | 2019-05-11 15:01:46 -0400 | [diff] [blame] | 44 | |
Kent Hagerman | f4a3aab | 2019-05-22 14:42:34 -0400 | [diff] [blame] | 45 | DOCKER_BUILD_ARGS ?= \ |
David K. Bainbridge | e14914d | 2019-05-24 13:43:05 -0700 | [diff] [blame] | 46 | ${DOCKER_EXTRA_ARGS} \ |
Kent Hagerman | f4a3aab | 2019-05-22 14:42:34 -0400 | [diff] [blame] | 47 | --build-arg org_label_schema_version="${VERSION}" \ |
| 48 | --build-arg org_label_schema_vcs_url="${DOCKER_LABEL_VCS_URL}" \ |
| 49 | --build-arg org_label_schema_vcs_ref="${DOCKER_LABEL_VCS_REF}" \ |
| 50 | --build-arg org_label_schema_build_date="${DOCKER_LABEL_BUILD_DATE}" \ |
| 51 | --build-arg org_opencord_vcs_commit_date="${DOCKER_LABEL_COMMIT_DATE}" \ |
| 52 | --build-arg org_opencord_vcs_dirty="${DOCKER_LABEL_VCS_DIRTY}" |
| 53 | |
| 54 | DOCKER_BUILD_ARGS_LOCAL ?= ${DOCKER_BUILD_ARGS} \ |
Kent Hagerman | f4a3aab | 2019-05-22 14:42:34 -0400 | [diff] [blame] | 55 | --build-arg LOCAL_PROTOS=${LOCAL_PROTOS} |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 56 | |
Kent Hagerman | 812b257 | 2020-01-28 11:57:51 -0500 | [diff] [blame] | 57 | # tool containers |
David K. Bainbridge | fc8e181 | 2021-04-09 16:09:49 +0000 | [diff] [blame] | 58 | VOLTHA_TOOLS_VERSION ?= 2.4.0 |
Kent Hagerman | 812b257 | 2020-01-28 11:57:51 -0500 | [diff] [blame] | 59 | |
Kent Hagerman | 1b82075 | 2020-02-14 14:45:51 -0500 | [diff] [blame] | 60 | GO = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app $(shell test -t 0 && echo "-it") -v gocache:/.cache -v gocache-${VOLTHA_TOOLS_VERSION}:/go/pkg voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-golang go |
Kent Hagerman | 812b257 | 2020-01-28 11:57:51 -0500 | [diff] [blame] | 61 | 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 | a4c8145 | 2020-11-23 14:37:11 -0800 | [diff] [blame] | 62 | GOCOVER_COBERTURA = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app/src/github.com/opencord/voltha-go -i voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-gocover-cobertura gocover-cobertura |
Kent Hagerman | 1b82075 | 2020-02-14 14:45:51 -0500 | [diff] [blame] | 63 | GOLANGCI_LINT = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app $(shell test -t 0 && echo "-it") -v gocache:/.cache -v gocache-${VOLTHA_TOOLS_VERSION}:/go/pkg voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-golangci-lint golangci-lint |
| 64 | HADOLINT = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app $(shell test -t 0 && echo "-it") voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-hadolint hadolint |
Kent Hagerman | 812b257 | 2020-01-28 11:57:51 -0500 | [diff] [blame] | 65 | |
David K. Bainbridge | 4183514 | 2021-04-01 17:26:12 +0000 | [diff] [blame] | 66 | .PHONY: docker-build local-protos local-lib-go help |
| 67 | .DEFAULT_GOAL := help |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 68 | |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 69 | |
David K. Bainbridge | 1678e19 | 2019-05-17 11:48:29 -0700 | [diff] [blame] | 70 | ## Local Development Helpers |
David K. Bainbridge | 4183514 | 2021-04-01 17:26:12 +0000 | [diff] [blame] | 71 | local-protos: ## Copies a local version of the voltha-protos dependency into the vendor directory |
David K. Bainbridge | 1678e19 | 2019-05-17 11:48:29 -0700 | [diff] [blame] | 72 | ifdef LOCAL_PROTOS |
David K. Bainbridge | 4183514 | 2021-04-01 17:26:12 +0000 | [diff] [blame] | 73 | rm -rf vendor/github.com/opencord/voltha-protos/v4/go |
Maninder | dfadc98 | 2020-10-28 14:04:33 +0530 | [diff] [blame] | 74 | mkdir -p vendor/github.com/opencord/voltha-protos/v4/go |
| 75 | cp -r ${LOCAL_PROTOS}/go/* vendor/github.com/opencord/voltha-protos/v4/go |
David K. Bainbridge | 1678e19 | 2019-05-17 11:48:29 -0700 | [diff] [blame] | 76 | endif |
| 77 | |
Scott Baker | cb7c88a | 2019-10-16 18:32:48 -0700 | [diff] [blame] | 78 | ## Local Development Helpers |
David K. Bainbridge | 4183514 | 2021-04-01 17:26:12 +0000 | [diff] [blame] | 79 | local-lib-go: ## Copies a local version of the voltha-lib-go dependency into the vendor directory |
Scott Baker | cb7c88a | 2019-10-16 18:32:48 -0700 | [diff] [blame] | 80 | ifdef LOCAL_LIB_GO |
yasin sapli | 5458a1c | 2021-06-14 22:24:38 +0000 | [diff] [blame] | 81 | rm -rf vendor/github.com/opencord/voltha-lib-go/v5/pkg |
| 82 | mkdir -p vendor/github.com/opencord/voltha-lib-go/v5/pkg |
| 83 | cp -r ${LOCAL_LIB_GO}/pkg/* vendor/github.com/opencord/voltha-lib-go/v5/pkg/ |
Scott Baker | cb7c88a | 2019-10-16 18:32:48 -0700 | [diff] [blame] | 84 | endif |
| 85 | |
Matt Jeanneret | 2e3051a | 2019-05-11 15:01:46 -0400 | [diff] [blame] | 86 | ## Docker targets |
David K. Bainbridge | 4183514 | 2021-04-01 17:26:12 +0000 | [diff] [blame] | 87 | build: docker-build ## Alias for 'docker-build' |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 88 | |
David K. Bainbridge | 4183514 | 2021-04-01 17:26:12 +0000 | [diff] [blame] | 89 | docker-build: local-protos local-lib-go ## Build core docker image (set BUILD_PROFILED=true to also build the profiled image) |
| 90 | docker build $(DOCKER_BUILD_ARGS) -t ${RWCORE_IMAGENAME}:${DOCKER_TAG} --target ${DOCKER_TARGET} -f docker/Dockerfile.rw_core . |
Matteo Scandolo | 450933a | 2020-04-30 07:47:08 -0700 | [diff] [blame] | 91 | ifdef BUILD_PROFILED |
Andrea Campanella | 8d4f3e3 | 2021-04-13 10:02:03 +0200 | [diff] [blame] | 92 | # Force target to dev as profile build must be built with dynamic linking |
| 93 | docker build $(DOCKER_BUILD_ARGS) --target dev --build-arg EXTRA_GO_BUILD_TAGS="-tags profile" --build-arg CGO_PARAMETER="CGO_ENABLED=1" -t ${RWCORE_IMAGENAME}:${DOCKER_TAG}-profile -f docker/Dockerfile.rw_core . |
Matteo Scandolo | 450933a | 2020-04-30 07:47:08 -0700 | [diff] [blame] | 94 | endif |
David K. Bainbridge | 3768fde | 2020-07-29 21:15:35 -0700 | [diff] [blame] | 95 | ifdef BUILD_RACE |
Andrea Campanella | 8d4f3e3 | 2021-04-13 10:02:03 +0200 | [diff] [blame] | 96 | # Force target to dev as race detection build must be built with dynamic linking |
| 97 | docker build $(DOCKER_BUILD_ARGS) --target dev --build-arg GOLANG_IMAGE=golang:1.13.8-buster --build-arg CGO_PARAMETER="CGO_ENABLED=1" --build-arg DEPLOY_IMAGE=debian:buster-slim --build-arg EXTRA_GO_BUILD_TAGS="--race" -t ${RWCORE_IMAGENAME}:${DOCKER_TAG}-rd -f docker/Dockerfile.rw_core . |
David K. Bainbridge | 3768fde | 2020-07-29 21:15:35 -0700 | [diff] [blame] | 98 | endif |
Don Newton | 8eca462 | 2020-02-10 16:44:48 -0500 | [diff] [blame] | 99 | |
David K. Bainbridge | 4183514 | 2021-04-01 17:26:12 +0000 | [diff] [blame] | 100 | docker-push: ## Push the docker images to an external repository |
Kent Hagerman | f4a3aab | 2019-05-22 14:42:34 -0400 | [diff] [blame] | 101 | docker push ${RWCORE_IMAGENAME}:${DOCKER_TAG} |
Matteo Scandolo | 8ed186a | 2020-05-04 10:52:42 -0700 | [diff] [blame] | 102 | ifdef BUILD_PROFILED |
| 103 | docker push ${RWCORE_IMAGENAME}:${DOCKER_TAG}-profile |
| 104 | endif |
David K. Bainbridge | 3768fde | 2020-07-29 21:15:35 -0700 | [diff] [blame] | 105 | ifdef BUILD_RACE |
| 106 | docker push ${RWCORE_IMAGENAME}:${DOCKER_TAG}-rd |
| 107 | endif |
David K. Bainbridge | 4183514 | 2021-04-01 17:26:12 +0000 | [diff] [blame] | 108 | docker-kind-load: ## Load docker images into a KinD cluster |
Andrea Campanella | 7a6cce2 | 2019-12-17 14:10:27 -0800 | [diff] [blame] | 109 | @if [ "`kind get clusters | grep voltha-$(TYPE)`" = '' ]; then echo "no voltha-$(TYPE) cluster found" && exit 1; fi |
| 110 | kind load docker-image ${RWCORE_IMAGENAME}:${DOCKER_TAG} --name=voltha-$(TYPE) --nodes $(shell kubectl get nodes --template='{{range .items}}{{.metadata.name}},{{end}}' | rev | cut -c 2- | rev) |
Andrea Campanella | 7a6cce2 | 2019-12-17 14:10:27 -0800 | [diff] [blame] | 111 | |
Matt Jeanneret | 2e3051a | 2019-05-11 15:01:46 -0400 | [diff] [blame] | 112 | ## lint and unit tests |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 113 | |
David K. Bainbridge | 4183514 | 2021-04-01 17:26:12 +0000 | [diff] [blame] | 114 | lint-dockerfile: ## Perform static analysis on Dockerfile |
Kent Hagerman | dcd4dcc | 2020-02-25 17:56:17 -0500 | [diff] [blame] | 115 | @echo "Running Dockerfile lint check..." |
Kent Hagerman | 812b257 | 2020-01-28 11:57:51 -0500 | [diff] [blame] | 116 | @${HADOLINT} $$(find . -name "Dockerfile.*") |
David Bainbridge | 5f3619c | 2019-07-10 22:51:09 +0000 | [diff] [blame] | 117 | @echo "Dockerfile lint check OK" |
| 118 | |
David K. Bainbridge | 4183514 | 2021-04-01 17:26:12 +0000 | [diff] [blame] | 119 | lint-mod: ## Verify the Go dependencies |
Kent Hagerman | ca4c51e | 2019-05-02 12:28:55 -0400 | [diff] [blame] | 120 | @echo "Running dependency check..." |
Kent Hagerman | 812b257 | 2020-01-28 11:57:51 -0500 | [diff] [blame] | 121 | @${GO} mod verify |
Scott Baker | e4c2a98 | 2019-11-21 16:32:03 -0800 | [diff] [blame] | 122 | @echo "Dependency check OK. Running vendor check..." |
| 123 | @git status > /dev/null |
Kent Hagerman | dcd4dcc | 2020-02-25 17:56:17 -0500 | [diff] [blame] | 124 | @git diff-index --quiet HEAD -- go.mod go.sum vendor || (echo "ERROR: Staged or modified files must be committed before running this test" && git status -- go.mod go.sum vendor && exit 1) |
| 125 | @[[ `git ls-files --exclude-standard --others go.mod go.sum vendor` == "" ]] || (echo "ERROR: Untracked files must be cleaned up before running this test" && git status -- go.mod go.sum vendor && exit 1) |
Kent Hagerman | 812b257 | 2020-01-28 11:57:51 -0500 | [diff] [blame] | 126 | ${GO} mod tidy |
| 127 | ${GO} mod vendor |
Scott Baker | e4c2a98 | 2019-11-21 16:32:03 -0800 | [diff] [blame] | 128 | @git status > /dev/null |
Kent Hagerman | dcd4dcc | 2020-02-25 17:56:17 -0500 | [diff] [blame] | 129 | @git diff-index --quiet HEAD -- go.mod go.sum vendor || (echo "ERROR: Modified files detected after running go mod tidy / go mod vendor" && git status -- go.mod go.sum vendor && git checkout -- go.mod go.sum vendor && exit 1) |
| 130 | @[[ `git ls-files --exclude-standard --others go.mod go.sum vendor` == "" ]] || (echo "ERROR: Untracked files detected after running go mod tidy / go mod vendor" && git status -- go.mod go.sum vendor && git checkout -- go.mod go.sum vendor && exit 1) |
Scott Baker | e4c2a98 | 2019-11-21 16:32:03 -0800 | [diff] [blame] | 131 | @echo "Vendor check OK." |
| 132 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 133 | |
David K. Bainbridge | 4183514 | 2021-04-01 17:26:12 +0000 | [diff] [blame] | 134 | lint: lint-mod lint-dockerfile ## Run all lint targets |
Kent Hagerman | 074d0e0 | 2019-04-24 17:58:16 -0400 | [diff] [blame] | 135 | |
David K. Bainbridge | 4183514 | 2021-04-01 17:26:12 +0000 | [diff] [blame] | 136 | sca: ## Runs static code analysis with the golangci-lint tool |
Kent Hagerman | 812b257 | 2020-01-28 11:57:51 -0500 | [diff] [blame] | 137 | @rm -rf ./sca-report |
Scott Baker | 18ea5a4 | 2019-10-15 16:08:42 -0700 | [diff] [blame] | 138 | @mkdir -p ./sca-report |
Kent Hagerman | 812b257 | 2020-01-28 11:57:51 -0500 | [diff] [blame] | 139 | @echo "Running static code analysis..." |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame] | 140 | @${GOLANGCI_LINT} run --deadline=6m --out-format junit-xml ./... | tee ./sca-report/sca-report.xml |
Kent Hagerman | 1b82075 | 2020-02-14 14:45:51 -0500 | [diff] [blame] | 141 | @echo "" |
Kent Hagerman | 812b257 | 2020-01-28 11:57:51 -0500 | [diff] [blame] | 142 | @echo "Static code analysis OK" |
Scott Baker | 18ea5a4 | 2019-10-15 16:08:42 -0700 | [diff] [blame] | 143 | |
David K. Bainbridge | 4183514 | 2021-04-01 17:26:12 +0000 | [diff] [blame] | 144 | test: local-lib-go ## Run unit tests |
Kent Hagerman | ca4c51e | 2019-05-02 12:28:55 -0400 | [diff] [blame] | 145 | @mkdir -p ./tests/results |
Kent Hagerman | 812b257 | 2020-01-28 11:57:51 -0500 | [diff] [blame] | 146 | @${GO} test -mod=vendor -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\ |
Zack Williams | 7cf7800 | 2019-04-30 21:45:35 -0700 | [diff] [blame] | 147 | RETURN=$$? ;\ |
Kent Hagerman | 812b257 | 2020-01-28 11:57:51 -0500 | [diff] [blame] | 148 | ${GO_JUNIT_REPORT} < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\ |
| 149 | ${GOCOVER_COBERTURA} < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\ |
Zack Williams | 7cf7800 | 2019-04-30 21:45:35 -0700 | [diff] [blame] | 150 | exit $$RETURN |
Kent Hagerman | 074d0e0 | 2019-04-24 17:58:16 -0400 | [diff] [blame] | 151 | |
David K. Bainbridge | 4183514 | 2021-04-01 17:26:12 +0000 | [diff] [blame] | 152 | clean: distclean ## Removes any local filesystem artifacts generated by a build |
David K. Bainbridge | 1678e19 | 2019-05-17 11:48:29 -0700 | [diff] [blame] | 153 | |
David K. Bainbridge | 4183514 | 2021-04-01 17:26:12 +0000 | [diff] [blame] | 154 | distclean: ## Removes any local filesystem artifacts generated by a build or test run |
Kent Hagerman | 812b257 | 2020-01-28 11:57:51 -0500 | [diff] [blame] | 155 | rm -rf ./sca-report |
David K. Bainbridge | 1678e19 | 2019-05-17 11:48:29 -0700 | [diff] [blame] | 156 | |
David K. Bainbridge | 4183514 | 2021-04-01 17:26:12 +0000 | [diff] [blame] | 157 | mod-update: ## Update go mod files |
Kent Hagerman | 812b257 | 2020-01-28 11:57:51 -0500 | [diff] [blame] | 158 | ${GO} mod tidy |
| 159 | ${GO} mod vendor |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 160 | |
David K. Bainbridge | 4183514 | 2021-04-01 17:26:12 +0000 | [diff] [blame] | 161 | fmt: ## Formats the soure code to go best practice style |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 162 | @go fmt ${PACKAGES} |
David K. Bainbridge | 4183514 | 2021-04-01 17:26:12 +0000 | [diff] [blame] | 163 | |
| 164 | # For each makefile target, add ## <description> on the target line and it will be listed by 'make help' |
| 165 | help: ## Print help for each Makefile target |
| 166 | @echo "Usage: make [<target>]" |
| 167 | @echo "where available targets are:" |
| 168 | @echo |
| 169 | @grep '^[[:alpha:]_-]*:.* ##' $(MAKEFILE_LIST) \ |
| 170 | | sort | awk 'BEGIN {FS=":.* ## "}; {printf "%-25s : %s\n", $$1, $$2};' |