Elia Battiston | c8d0d46 | 2022-02-22 16:30:51 +0100 | [diff] [blame^] | 1 | # Copyright 2022-present Open Networking Foundation |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | # set default shell |
| 16 | SHELL = bash -e -o pipefail |
| 17 | |
| 18 | # Variables |
| 19 | VERSION ?= $(shell cat ./VERSION) |
| 20 | |
| 21 | DOCKER_LABEL_VCS_DIRTY = false |
| 22 | ifneq ($(shell git ls-files --others --modified --exclude-standard 2>/dev/null | wc -l | sed -e 's/ //g'),0) |
| 23 | DOCKER_LABEL_VCS_DIRTY = true |
| 24 | endif |
| 25 | ## Docker related |
| 26 | DOCKER_EXTRA_ARGS ?= |
| 27 | DOCKER_REGISTRY ?= |
| 28 | DOCKER_REPOSITORY ?= |
| 29 | DOCKER_TAG ?= ${VERSION}$(shell [[ ${DOCKER_LABEL_VCS_DIRTY} == "true" ]] && echo "-dirty" || true) |
| 30 | ADAPTER_IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-northbound-bbf-adapter:${DOCKER_TAG} |
| 31 | DOCKER_TARGET ?= prod |
| 32 | TYPE ?= minimal |
| 33 | |
| 34 | ## Docker labels. Only set ref and commit date if committed |
| 35 | DOCKER_LABEL_VCS_URL ?= $(shell git remote get-url $(shell git remote)) |
| 36 | DOCKER_LABEL_VCS_REF = $(shell git rev-parse HEAD) |
| 37 | DOCKER_LABEL_BUILD_DATE ?= $(shell date -u "+%Y-%m-%dT%H:%M:%SZ") |
| 38 | DOCKER_LABEL_COMMIT_DATE = $(shell git show -s --format=%cd --date=iso-strict HEAD) |
| 39 | |
| 40 | DOCKER_BUILD_ARGS ?= \ |
| 41 | ${DOCKER_EXTRA_ARGS} \ |
| 42 | --build-arg org_label_schema_version="${VERSION}" \ |
| 43 | --build-arg org_label_schema_vcs_url="${DOCKER_LABEL_VCS_URL}" \ |
| 44 | --build-arg org_label_schema_vcs_ref="${DOCKER_LABEL_VCS_REF}" \ |
| 45 | --build-arg org_label_schema_build_date="${DOCKER_LABEL_BUILD_DATE}" \ |
| 46 | --build-arg org_opencord_vcs_commit_date="${DOCKER_LABEL_COMMIT_DATE}" \ |
| 47 | --build-arg org_opencord_vcs_dirty="${DOCKER_LABEL_VCS_DIRTY}" |
| 48 | |
| 49 | # tool containers |
| 50 | VOLTHA_TOOLS_VERSION ?= 2.5.2 |
| 51 | |
| 52 | 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 |
| 53 | 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 |
| 54 | GOCOVER_COBERTURA = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app/src/github.com/opencord/voltha-northbound-bbf-adapter -i voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-gocover-cobertura gocover-cobertura |
| 55 | 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 |
| 56 | 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 |
| 57 | |
| 58 | .PHONY: docker-build local-protos local-lib-go help |
| 59 | .DEFAULT_GOAL := help |
| 60 | |
| 61 | help: ## Print help for each Makefile target |
| 62 | @echo |
| 63 | @echo Northbound BBF Adapter |
| 64 | @echo |
| 65 | @echo Translates the BBF yang model to VOLTHA Northbound APIs |
| 66 | @echo |
| 67 | @echo "Usage: make [<target>]" |
| 68 | @echo "where available targets are:" |
| 69 | @grep '^[[:alpha:]_-]*:.* ##' $(MAKEFILE_LIST) \ |
| 70 | | sort | awk 'BEGIN {FS=":.* ## "}; {printf "%-25s : %s\n", $$1, $$2};' |
| 71 | |
| 72 | ## Local Development Helpers |
| 73 | local-protos: ## Copies a local version of the voltha-protos dependency into the vendor directory |
| 74 | ifdef LOCAL_PROTOS |
| 75 | rm -rf vendor/github.com/opencord/voltha-protos/v5/go |
| 76 | mkdir -p vendor/github.com/opencord/voltha-protos/v5/go |
| 77 | cp -r ${LOCAL_PROTOS}/go/* vendor/github.com/opencord/voltha-protos/v5/go |
| 78 | rm -rf vendor/github.com/opencord/voltha-protos/v5/go/vendor |
| 79 | endif |
| 80 | |
| 81 | local-lib-go: ## Copies a local version of the voltha-lib-go dependency into the vendor directory |
| 82 | ifdef LOCAL_LIB_GO |
| 83 | mkdir -p vendor/github.com/opencord/voltha-lib-go/v7/pkg |
| 84 | cp -r ${LOCAL_LIB_GO}/pkg/* vendor/github.com/opencord/voltha-lib-go/v7/pkg/ |
| 85 | endif |
| 86 | |
| 87 | ## Docker targets |
| 88 | build: docker-build ## Alias for 'docker build' |
| 89 | |
| 90 | docker-build: local-lib-go ## Build the BBF Adapter docker container |
| 91 | docker build \ |
| 92 | -t ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-northbound-bbf-adapter:${DOCKER_TAG} \ |
| 93 | -f build/package/Dockerfile.bbf-adapter . |
| 94 | |
| 95 | docker-push: ## Push the docker images to an external repository |
| 96 | docker push ${ADAPTER_IMAGENAME} |
| 97 | ifdef BUILD_PROFILED |
| 98 | docker push ${ADAPTER_IMAGENAME}-profile |
| 99 | endif |
| 100 | ifdef BUILD_RACE |
| 101 | docker push ${ADAPTER_IMAGENAME}-rd |
| 102 | endif |
| 103 | |
| 104 | docker-kind-load: ## Load docker images into a KinD cluster |
| 105 | @if [ "`kind get clusters | grep voltha-$(TYPE)`" = '' ]; then echo "no voltha-$(TYPE) cluster found" && exit 1; fi |
| 106 | kind load docker-image ${ADAPTER_IMAGENAME} --name=voltha-$(TYPE) --nodes $(shell kubectl get nodes --template='{{range .items}}{{.metadata.name}},{{end}}' | sed 's/,$$//') |
| 107 | |
| 108 | test: ## Run unit tests |
| 109 | @mkdir -p ./tests/results |
| 110 | @${GO} test -mod=vendor -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\ |
| 111 | RETURN=$$? ;\ |
| 112 | ${GO_JUNIT_REPORT} < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\ |
| 113 | ${GOCOVER_COBERTURA} < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\ |
| 114 | exit $$RETURN |
| 115 | |
| 116 | lint: local-lib-go lint-mod lint-dockerfile ## Run all lint targets |
| 117 | |
| 118 | lint-dockerfile: ## Perform static analysis on Dockerfile |
| 119 | @echo "Running Dockerfile lint check..." |
| 120 | @${HADOLINT} $$(find ./build -name "Dockerfile*") |
| 121 | @echo "Dockerfile lint check OK" |
| 122 | |
| 123 | lint-mod: ## Verify the Go dependencies |
| 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" && git status -- go.mod go.sum vendor && 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" && git status -- go.mod go.sum vendor && 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" && git status -- go.mod go.sum vendor && git checkout -- go.mod go.sum vendor && 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" && git status -- go.mod go.sum vendor && git checkout -- go.mod go.sum vendor && exit 1) |
| 135 | @echo "Vendor check OK." |
| 136 | |
| 137 | sca: ## Runs static code analysis with the golangci-lint tool |
| 138 | @rm -rf ./sca-report |
| 139 | @mkdir -p ./sca-report |
| 140 | @echo "Running static code analysis..." |
| 141 | @${GOLANGCI_LINT} run --deadline=6m --out-format junit-xml ./... | tee ./sca-report/sca-report.xml |
| 142 | @echo "" |
| 143 | @echo "Static code analysis OK" |
| 144 | |
| 145 | clean: distclean ## Removes any local filesystem artifacts generated by a build |
| 146 | |
| 147 | distclean: ## Removes any local filesystem artifacts generated by a build or test run |
| 148 | rm -rf ./sca-report |
| 149 | |
| 150 | mod-update: ## Update go mod files |
| 151 | ${GO} mod tidy |
| 152 | ${GO} mod vendor |