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 |
Elia Battiston | be9edc1 | 2022-03-09 11:35:58 +0100 | [diff] [blame] | 50 | VOLTHA_TOOLS_VERSION ?= 2.5.3 |
Elia Battiston | c8d0d46 | 2022-02-22 16:30:51 +0100 | [diff] [blame] | 51 | |
Elia Battiston | ac8d23f | 2022-03-14 17:54:56 +0100 | [diff] [blame^] | 52 | # This container is built to include the necessary sysrepo libraries |
| 53 | # to succesfully build and test the code in this repository |
| 54 | BUILDER_IMAGE_AND_TAG ?= voltha/bbf-adapter-builder:local |
| 55 | build-tools: build/tools/Dockerfile.builder |
| 56 | docker build \ |
| 57 | -t ${BUILDER_IMAGE_AND_TAG} \ |
| 58 | -f build/tools/Dockerfile.builder . |
Elia Battiston | c8d0d46 | 2022-02-22 16:30:51 +0100 | [diff] [blame] | 59 | |
Elia Battiston | ac8d23f | 2022-03-14 17:54:56 +0100 | [diff] [blame^] | 60 | GO_LOCAL_BUILDER = 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 ${BUILDER_IMAGE_AND_TAG} go |
| 61 | 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 |
| 62 | 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 |
| 63 | GOCOVER_COBERTURA = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app/src/github.com/opencord/voltha-northbound-bbf-adapter -v gocache:/.cache -v gocache-${VOLTHA_TOOLS_VERSION}:/go/pkg voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-gocover-cobertura gocover-cobertura |
| 64 | GOLANGCI_LINT_LOCAL_BUILDER = 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 ${BUILDER_IMAGE_AND_TAG} golangci-lint |
| 65 | 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 |
| 66 | |
| 67 | # Default user and password for the netconf user in docker-build |
| 68 | NETCONF_USER ?= voltha |
| 69 | NETCONF_PASSWORD ?= onf |
| 70 | |
| 71 | .PHONY: docker-build local-protos local-lib-go help test sca |
Elia Battiston | c8d0d46 | 2022-02-22 16:30:51 +0100 | [diff] [blame] | 72 | .DEFAULT_GOAL := help |
| 73 | |
| 74 | help: ## Print help for each Makefile target |
| 75 | @echo |
| 76 | @echo Northbound BBF Adapter |
| 77 | @echo |
| 78 | @echo Translates the BBF yang model to VOLTHA Northbound APIs |
| 79 | @echo |
| 80 | @echo "Usage: make [<target>]" |
| 81 | @echo "where available targets are:" |
| 82 | @grep '^[[:alpha:]_-]*:.* ##' $(MAKEFILE_LIST) \ |
| 83 | | sort | awk 'BEGIN {FS=":.* ## "}; {printf "%-25s : %s\n", $$1, $$2};' |
| 84 | |
| 85 | ## Local Development Helpers |
| 86 | local-protos: ## Copies a local version of the voltha-protos dependency into the vendor directory |
| 87 | ifdef LOCAL_PROTOS |
| 88 | rm -rf vendor/github.com/opencord/voltha-protos/v5/go |
| 89 | mkdir -p vendor/github.com/opencord/voltha-protos/v5/go |
| 90 | cp -r ${LOCAL_PROTOS}/go/* vendor/github.com/opencord/voltha-protos/v5/go |
| 91 | rm -rf vendor/github.com/opencord/voltha-protos/v5/go/vendor |
| 92 | endif |
| 93 | |
| 94 | local-lib-go: ## Copies a local version of the voltha-lib-go dependency into the vendor directory |
| 95 | ifdef LOCAL_LIB_GO |
| 96 | mkdir -p vendor/github.com/opencord/voltha-lib-go/v7/pkg |
| 97 | cp -r ${LOCAL_LIB_GO}/pkg/* vendor/github.com/opencord/voltha-lib-go/v7/pkg/ |
| 98 | endif |
| 99 | |
| 100 | ## Docker targets |
| 101 | build: docker-build ## Alias for 'docker build' |
| 102 | |
Elia Battiston | ac8d23f | 2022-03-14 17:54:56 +0100 | [diff] [blame^] | 103 | docker-build: local-lib-go build-tools ## Build the BBF Adapter docker container |
Elia Battiston | c8d0d46 | 2022-02-22 16:30:51 +0100 | [diff] [blame] | 104 | docker build \ |
| 105 | -t ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-northbound-bbf-adapter:${DOCKER_TAG} \ |
Elia Battiston | ac8d23f | 2022-03-14 17:54:56 +0100 | [diff] [blame^] | 106 | -f build/package/Dockerfile.bbf-adapter . \ |
| 107 | --build-arg NETCONF_USER=${NETCONF_USER} \ |
| 108 | --build-arg NETCONF_PASSWORD=${NETCONF_PASSWORD} |
Elia Battiston | c8d0d46 | 2022-02-22 16:30:51 +0100 | [diff] [blame] | 109 | |
| 110 | docker-push: ## Push the docker images to an external repository |
| 111 | docker push ${ADAPTER_IMAGENAME} |
| 112 | ifdef BUILD_PROFILED |
| 113 | docker push ${ADAPTER_IMAGENAME}-profile |
| 114 | endif |
| 115 | ifdef BUILD_RACE |
| 116 | docker push ${ADAPTER_IMAGENAME}-rd |
| 117 | endif |
| 118 | |
| 119 | docker-kind-load: ## Load docker images into a KinD cluster |
| 120 | @if [ "`kind get clusters | grep voltha-$(TYPE)`" = '' ]; then echo "no voltha-$(TYPE) cluster found" && exit 1; fi |
| 121 | kind load docker-image ${ADAPTER_IMAGENAME} --name=voltha-$(TYPE) --nodes $(shell kubectl get nodes --template='{{range .items}}{{.metadata.name}},{{end}}' | sed 's/,$$//') |
| 122 | |
Elia Battiston | ac8d23f | 2022-03-14 17:54:56 +0100 | [diff] [blame^] | 123 | test: build-tools ## Run unit tests |
Elia Battiston | c8d0d46 | 2022-02-22 16:30:51 +0100 | [diff] [blame] | 124 | @mkdir -p ./tests/results |
Elia Battiston | ac8d23f | 2022-03-14 17:54:56 +0100 | [diff] [blame^] | 125 | @${GO_LOCAL_BUILDER} test -mod=vendor -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\ |
Elia Battiston | c8d0d46 | 2022-02-22 16:30:51 +0100 | [diff] [blame] | 126 | RETURN=$$? ;\ |
| 127 | ${GO_JUNIT_REPORT} < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\ |
| 128 | ${GOCOVER_COBERTURA} < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\ |
| 129 | exit $$RETURN |
| 130 | |
| 131 | lint: local-lib-go lint-mod lint-dockerfile ## Run all lint targets |
| 132 | |
| 133 | lint-dockerfile: ## Perform static analysis on Dockerfile |
| 134 | @echo "Running Dockerfile lint check..." |
| 135 | @${HADOLINT} $$(find ./build -name "Dockerfile*") |
| 136 | @echo "Dockerfile lint check OK" |
| 137 | |
| 138 | lint-mod: ## Verify the Go dependencies |
| 139 | @echo "Running dependency check..." |
| 140 | @${GO} mod verify |
| 141 | @echo "Dependency check OK. Running vendor check..." |
| 142 | @git status > /dev/null |
| 143 | @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) |
| 144 | @[[ `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) |
| 145 | ${GO} mod tidy |
| 146 | ${GO} mod vendor |
| 147 | @git status > /dev/null |
| 148 | @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) |
| 149 | @[[ `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) |
| 150 | @echo "Vendor check OK." |
| 151 | |
Elia Battiston | ac8d23f | 2022-03-14 17:54:56 +0100 | [diff] [blame^] | 152 | sca: build-tools ## Runs static code analysis with the golangci-lint tool |
Elia Battiston | c8d0d46 | 2022-02-22 16:30:51 +0100 | [diff] [blame] | 153 | @rm -rf ./sca-report |
| 154 | @mkdir -p ./sca-report |
| 155 | @echo "Running static code analysis..." |
Elia Battiston | ac8d23f | 2022-03-14 17:54:56 +0100 | [diff] [blame^] | 156 | @${GOLANGCI_LINT_LOCAL_BUILDER} run --deadline=6m --out-format junit-xml ./... | tee ./sca-report/sca-report.xml |
Elia Battiston | c8d0d46 | 2022-02-22 16:30:51 +0100 | [diff] [blame] | 157 | @echo "" |
| 158 | @echo "Static code analysis OK" |
| 159 | |
| 160 | clean: distclean ## Removes any local filesystem artifacts generated by a build |
| 161 | |
| 162 | distclean: ## Removes any local filesystem artifacts generated by a build or test run |
| 163 | rm -rf ./sca-report |
| 164 | |
| 165 | mod-update: ## Update go mod files |
| 166 | ${GO} mod tidy |
| 167 | ${GO} mod vendor |