blob: a213c837f2e135424cc6a179221db25a30ed428c [file] [log] [blame]
khenaidoocfee5f42018-07-19 22:47:38 -04001#
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 Jeanneret2e3051a2019-05-11 15:01:46 -040017# set default shell
18SHELL = bash -e -o pipefail
Kent Hagerman074d0e02019-04-24 17:58:16 -040019
Zack Williams27f59a42019-05-10 09:12:07 -070020# Variables
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040021VERSION ?= $(shell cat ./VERSION)
Zack Williams27f59a42019-05-10 09:12:07 -070022
Maninder9a1bc0d2020-10-26 11:34:02 +053023# Packages
24PACKAGES = $(shell go list ./...)
25
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040026DOCKER_LABEL_VCS_DIRTY = false
27ifneq ($(shell git ls-files --others --modified --exclude-standard 2>/dev/null | wc -l | sed -e 's/ //g'),0)
28 DOCKER_LABEL_VCS_DIRTY = true
29endif
Matt Jeanneret2e3051a2019-05-11 15:01:46 -040030## Docker related
David K. Bainbridgee14914d2019-05-24 13:43:05 -070031DOCKER_EXTRA_ARGS ?=
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040032DOCKER_REGISTRY ?=
33DOCKER_REPOSITORY ?=
34DOCKER_TAG ?= ${VERSION}$(shell [[ ${DOCKER_LABEL_VCS_DIRTY} == "true" ]] && echo "-dirty" || true)
David K. Bainbridge41835142021-04-01 17:26:12 +000035DOCKER_TARGET ?= prod
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040036RWCORE_IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-rw-core
Andrea Campanella7a6cce22019-12-17 14:10:27 -080037TYPE ?= minimal
Matt Jeanneret2e3051a2019-05-11 15:01:46 -040038
39## Docker labels. Only set ref and commit date if committed
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040040DOCKER_LABEL_VCS_URL ?= $(shell git remote get-url $(shell git remote))
41DOCKER_LABEL_VCS_REF = $(shell git rev-parse HEAD)
42DOCKER_LABEL_BUILD_DATE ?= $(shell date -u "+%Y-%m-%dT%H:%M:%SZ")
43DOCKER_LABEL_COMMIT_DATE = $(shell git show -s --format=%cd --date=iso-strict HEAD)
Matt Jeanneret2e3051a2019-05-11 15:01:46 -040044
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040045DOCKER_BUILD_ARGS ?= \
David K. Bainbridgee14914d2019-05-24 13:43:05 -070046 ${DOCKER_EXTRA_ARGS} \
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040047 --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
54DOCKER_BUILD_ARGS_LOCAL ?= ${DOCKER_BUILD_ARGS} \
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040055 --build-arg LOCAL_PROTOS=${LOCAL_PROTOS}
khenaidoocfee5f42018-07-19 22:47:38 -040056
Kent Hagerman812b2572020-01-28 11:57:51 -050057# tool containers
Matteo Scandoloa4c81452020-11-23 14:37:11 -080058VOLTHA_TOOLS_VERSION ?= 2.3.1
Kent Hagerman812b2572020-01-28 11:57:51 -050059
Kent Hagerman1b820752020-02-14 14:45:51 -050060GO = 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 Hagerman812b2572020-01-28 11:57:51 -050061GO_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 Scandoloa4c81452020-11-23 14:37:11 -080062GOCOVER_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 Hagerman1b820752020-02-14 14:45:51 -050063GOLANGCI_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
64HADOLINT = 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 Hagerman812b2572020-01-28 11:57:51 -050065
David K. Bainbridge41835142021-04-01 17:26:12 +000066.PHONY: docker-build local-protos local-lib-go help
67.DEFAULT_GOAL := help
khenaidoocfee5f42018-07-19 22:47:38 -040068
khenaidoocfee5f42018-07-19 22:47:38 -040069
David K. Bainbridge1678e192019-05-17 11:48:29 -070070## Local Development Helpers
David K. Bainbridge41835142021-04-01 17:26:12 +000071local-protos: ## Copies a local version of the voltha-protos dependency into the vendor directory
David K. Bainbridge1678e192019-05-17 11:48:29 -070072ifdef LOCAL_PROTOS
David K. Bainbridge41835142021-04-01 17:26:12 +000073 rm -rf vendor/github.com/opencord/voltha-protos/v4/go
Maninderdfadc982020-10-28 14:04:33 +053074 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. Bainbridge1678e192019-05-17 11:48:29 -070076endif
77
Scott Bakercb7c88a2019-10-16 18:32:48 -070078## Local Development Helpers
David K. Bainbridge41835142021-04-01 17:26:12 +000079local-lib-go: ## Copies a local version of the voltha-lib-go dependency into the vendor directory
Scott Bakercb7c88a2019-10-16 18:32:48 -070080ifdef LOCAL_LIB_GO
David K. Bainbridge41835142021-04-01 17:26:12 +000081 rm -rf vendor/github.com/opencord/voltha-lib-go/v4/pkg
Maninderdfadc982020-10-28 14:04:33 +053082 mkdir -p vendor/github.com/opencord/voltha-lib-go/v4/pkg
83 cp -r ${LOCAL_LIB_GO}/pkg/* vendor/github.com/opencord/voltha-lib-go/v4/pkg/
Scott Bakercb7c88a2019-10-16 18:32:48 -070084endif
85
Matt Jeanneret2e3051a2019-05-11 15:01:46 -040086## Docker targets
David K. Bainbridge41835142021-04-01 17:26:12 +000087build: docker-build ## Alias for 'docker-build'
khenaidoocfee5f42018-07-19 22:47:38 -040088
David K. Bainbridge41835142021-04-01 17:26:12 +000089docker-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 Scandolo450933a2020-04-30 07:47:08 -070091ifdef BUILD_PROFILED
David K. Bainbridge41835142021-04-01 17:26:12 +000092 docker build $(DOCKER_BUILD_ARGS) --target ${DOCKER_TARGET} --build-arg EXTRA_GO_BUILD_TAGS="-tags profile" -t ${RWCORE_IMAGENAME}:${DOCKER_TAG}-profile -f docker/Dockerfile.rw_core .
Matteo Scandolo450933a2020-04-30 07:47:08 -070093endif
David K. Bainbridge3768fde2020-07-29 21:15:35 -070094ifdef BUILD_RACE
David K. Bainbridge41835142021-04-01 17:26:12 +000095 docker build $(DOCKER_BUILD_ARGS) --target ${DOCKER_TARGET} --build-arg GOLANG_IMAGE=golang:1.13.8-buster --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. Bainbridge3768fde2020-07-29 21:15:35 -070096endif
Don Newton8eca4622020-02-10 16:44:48 -050097
David K. Bainbridge41835142021-04-01 17:26:12 +000098docker-push: ## Push the docker images to an external repository
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040099 docker push ${RWCORE_IMAGENAME}:${DOCKER_TAG}
Matteo Scandolo8ed186a2020-05-04 10:52:42 -0700100ifdef BUILD_PROFILED
101 docker push ${RWCORE_IMAGENAME}:${DOCKER_TAG}-profile
102endif
David K. Bainbridge3768fde2020-07-29 21:15:35 -0700103ifdef BUILD_RACE
104 docker push ${RWCORE_IMAGENAME}:${DOCKER_TAG}-rd
105endif
David K. Bainbridge41835142021-04-01 17:26:12 +0000106docker-kind-load: ## Load docker images into a KinD cluster
Andrea Campanella7a6cce22019-12-17 14:10:27 -0800107 @if [ "`kind get clusters | grep voltha-$(TYPE)`" = '' ]; then echo "no voltha-$(TYPE) cluster found" && exit 1; fi
108 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 Campanella7a6cce22019-12-17 14:10:27 -0800109
Matt Jeanneret2e3051a2019-05-11 15:01:46 -0400110## lint and unit tests
khenaidood2b6df92018-12-13 16:37:20 -0500111
David K. Bainbridge41835142021-04-01 17:26:12 +0000112lint-dockerfile: ## Perform static analysis on Dockerfile
Kent Hagermandcd4dcc2020-02-25 17:56:17 -0500113 @echo "Running Dockerfile lint check..."
Kent Hagerman812b2572020-01-28 11:57:51 -0500114 @${HADOLINT} $$(find . -name "Dockerfile.*")
David Bainbridge5f3619c2019-07-10 22:51:09 +0000115 @echo "Dockerfile lint check OK"
116
David K. Bainbridge41835142021-04-01 17:26:12 +0000117lint-mod: ## Verify the Go dependencies
Kent Hagermanca4c51e2019-05-02 12:28:55 -0400118 @echo "Running dependency check..."
Kent Hagerman812b2572020-01-28 11:57:51 -0500119 @${GO} mod verify
Scott Bakere4c2a982019-11-21 16:32:03 -0800120 @echo "Dependency check OK. Running vendor check..."
121 @git status > /dev/null
Kent Hagermandcd4dcc2020-02-25 17:56:17 -0500122 @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)
123 @[[ `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 Hagerman812b2572020-01-28 11:57:51 -0500124 ${GO} mod tidy
125 ${GO} mod vendor
Scott Bakere4c2a982019-11-21 16:32:03 -0800126 @git status > /dev/null
Kent Hagermandcd4dcc2020-02-25 17:56:17 -0500127 @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)
128 @[[ `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 Bakere4c2a982019-11-21 16:32:03 -0800129 @echo "Vendor check OK."
130
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400131
David K. Bainbridge41835142021-04-01 17:26:12 +0000132lint: lint-mod lint-dockerfile ## Run all lint targets
Kent Hagerman074d0e02019-04-24 17:58:16 -0400133
David K. Bainbridge41835142021-04-01 17:26:12 +0000134sca: ## Runs static code analysis with the golangci-lint tool
Kent Hagerman812b2572020-01-28 11:57:51 -0500135 @rm -rf ./sca-report
Scott Baker18ea5a42019-10-15 16:08:42 -0700136 @mkdir -p ./sca-report
Kent Hagerman812b2572020-01-28 11:57:51 -0500137 @echo "Running static code analysis..."
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500138 @${GOLANGCI_LINT} run --deadline=6m --out-format junit-xml ./... | tee ./sca-report/sca-report.xml
Kent Hagerman1b820752020-02-14 14:45:51 -0500139 @echo ""
Kent Hagerman812b2572020-01-28 11:57:51 -0500140 @echo "Static code analysis OK"
Scott Baker18ea5a42019-10-15 16:08:42 -0700141
David K. Bainbridge41835142021-04-01 17:26:12 +0000142test: local-lib-go ## Run unit tests
Kent Hagermanca4c51e2019-05-02 12:28:55 -0400143 @mkdir -p ./tests/results
Kent Hagerman812b2572020-01-28 11:57:51 -0500144 @${GO} test -mod=vendor -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\
Zack Williams7cf78002019-04-30 21:45:35 -0700145 RETURN=$$? ;\
Kent Hagerman812b2572020-01-28 11:57:51 -0500146 ${GO_JUNIT_REPORT} < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\
147 ${GOCOVER_COBERTURA} < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\
Zack Williams7cf78002019-04-30 21:45:35 -0700148 exit $$RETURN
Kent Hagerman074d0e02019-04-24 17:58:16 -0400149
David K. Bainbridge41835142021-04-01 17:26:12 +0000150clean: distclean ## Removes any local filesystem artifacts generated by a build
David K. Bainbridge1678e192019-05-17 11:48:29 -0700151
David K. Bainbridge41835142021-04-01 17:26:12 +0000152distclean: ## Removes any local filesystem artifacts generated by a build or test run
Kent Hagerman812b2572020-01-28 11:57:51 -0500153 rm -rf ./sca-report
David K. Bainbridge1678e192019-05-17 11:48:29 -0700154
David K. Bainbridge41835142021-04-01 17:26:12 +0000155mod-update: ## Update go mod files
Kent Hagerman812b2572020-01-28 11:57:51 -0500156 ${GO} mod tidy
157 ${GO} mod vendor
Maninder9a1bc0d2020-10-26 11:34:02 +0530158
David K. Bainbridge41835142021-04-01 17:26:12 +0000159fmt: ## Formats the soure code to go best practice style
Maninder9a1bc0d2020-10-26 11:34:02 +0530160 @go fmt ${PACKAGES}
David K. Bainbridge41835142021-04-01 17:26:12 +0000161
162# For each makefile target, add ## <description> on the target line and it will be listed by 'make help'
163help: ## 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};'