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