Prince Pereira | c1c21d6 | 2021-04-22 08:38:15 +0000 | [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 | |
| 17 | # set default shell |
| 18 | SHELL = bash -e -o pipefail |
| 19 | |
| 20 | # Variables |
| 21 | VERSION ?= $(shell cat ./VERSION) |
| 22 | |
| 23 | DOCKER_LABEL_VCS_DIRTY = false |
| 24 | ifneq ($(shell git ls-files --others --modified --exclude-standard 2>/dev/null | wc -l | sed -e 's/ //g'),0) |
| 25 | DOCKER_LABEL_VCS_DIRTY = true |
| 26 | endif |
| 27 | ## Docker related |
| 28 | DOCKER_EXTRA_ARGS ?= |
| 29 | DOCKER_REGISTRY ?= |
| 30 | DOCKER_REPOSITORY ?= |
| 31 | DOCKER_TAG ?= ${VERSION} |
| 32 | OPENDEVMGR_IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}opendevice-manager:${DOCKER_TAG} |
| 33 | TYPE ?= minimal |
| 34 | |
| 35 | ## Docker labels. Only set ref and commit date if committed |
| 36 | DOCKER_LABEL_VCS_URL ?= $(shell git remote get-url $(shell git remote)) |
| 37 | DOCKER_LABEL_VCS_REF = $(shell git rev-parse HEAD) |
| 38 | DOCKER_LABEL_BUILD_DATE ?= $(shell date -u "+%Y-%m-%dT%H:%M:%SZ") |
| 39 | DOCKER_LABEL_COMMIT_DATE = $(shell git show -s --format=%cd --date=iso-strict HEAD) |
| 40 | |
| 41 | DOCKER_BUILD_ARGS ?= \ |
| 42 | ${DOCKER_EXTRA_ARGS} \ |
| 43 | --build-arg org_label_schema_version="${VERSION}" \ |
| 44 | --build-arg org_label_schema_vcs_url="${DOCKER_LABEL_VCS_URL}" \ |
| 45 | --build-arg org_label_schema_vcs_ref="${DOCKER_LABEL_VCS_REF}" \ |
| 46 | --build-arg org_label_schema_build_date="${DOCKER_LABEL_BUILD_DATE}" \ |
| 47 | --build-arg org_opencord_vcs_commit_date="${DOCKER_LABEL_COMMIT_DATE}" \ |
| 48 | --build-arg org_opencord_vcs_dirty="${DOCKER_LABEL_VCS_DIRTY}" |
| 49 | |
| 50 | # tool containers |
| 51 | VOLTHA_TOOLS_VERSION ?= 2.3.1 |
| 52 | |
| 53 | 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 |
| 54 | 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 |
| 55 | GOCOVER_COBERTURA = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app/src/github.com/opencord/opendevice-manager -i voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-gocover-cobertura gocover-cobertura |
| 56 | 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 |
| 57 | 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 |
| 58 | |
| 59 | .PHONY: docker-build local-protos local-lib-go help |
| 60 | .DEFAULT_GOAL := help |
| 61 | |
| 62 | ## Local Development Helpers |
| 63 | local-protos: ## Copies a local version of the dmi-protos dependency into the vendor directory |
| 64 | ifdef LOCAL_PROTOS |
| 65 | rm -rf vendor/github.com/opencord/device-management-interface/go |
| 66 | mkdir -p vendor/github.com/opencord/device-management-interface/go |
| 67 | cp -r ${LOCAL_PROTOS}/go/* vendor/github.com/opencord/device-management-interface/go |
| 68 | rm -rf vendor/github.com/opencord/device-management-interface/go/vendor |
| 69 | endif |
| 70 | |
| 71 | local-lib-go: ## Copies a local version of the voltha-lib-go dependency into the vendor directory |
| 72 | ifdef LOCAL_LIB_GO |
| 73 | mkdir -p vendor/github.com/opencord/voltha-lib-go/v4/pkg |
| 74 | cp -r ${LOCAL_LIB_GO}/pkg/* vendor/github.com/opencord/voltha-lib-go/v4/pkg/ |
| 75 | endif |
| 76 | |
| 77 | ## Docker targets |
| 78 | build: docker-build ## Alias for 'docker build' |
| 79 | |
| 80 | # docker-build: local-protos local-lib-go ## Build opendevice manager docker image (set BUILD_PROFILED=true to also build the profiled image) |
| 81 | docker-build: ## Build opendevice manager docker image (set BUILD_PROFILED=true to also build the profiled image) |
| 82 | docker build $(DOCKER_BUILD_ARGS) -t ${OPENDEVMGR_IMAGENAME} -f docker/Dockerfile.opendevmgr . |
| 83 | ifdef BUILD_PROFILED |
| 84 | docker build $(DOCKER_BUILD_ARGS) --build-arg EXTRA_GO_BUILD_TAGS="-tags profile" -t ${OPENDEVMGR_IMAGENAME}-profile -f docker/Dockerfile.opendevmgr . |
| 85 | endif |
| 86 | |
| 87 | docker-push: ## Push the docker images to an external repository |
| 88 | docker push ${OPENDEVMGR_IMAGENAME} |
| 89 | ifdef BUILD_PROFILED |
| 90 | docker push ${OPENDEVMGR_IMAGENAME}-profile |
| 91 | endif |
| 92 | |
| 93 | docker-kind-load: ## Load docker images into a KinD cluster |
| 94 | @if [ "`kind get clusters | grep opendevmgr-$(TYPE)`" = '' ]; then echo "no opendevmgr-$(TYPE) cluster found" && exit 1; fi |
| 95 | kind load docker-image ${OPENDEVMGR_IMAGENAME} --name=opendevmgr-$(TYPE) --nodes $(shell kubectl get nodes --template='{{range .items}}{{.metadata.name}},{{end}}' | sed 's/,$$//') |
| 96 | |
| 97 | ## lint and unit tests |
| 98 | lint-dockerfile: ## Perform static analysis on Dockerfile |
| 99 | @echo "Running Dockerfile lint check ..." |
| 100 | @${HADOLINT} $$(find . -name "Dockerfile.*") |
| 101 | @echo "Dockerfile lint check OK" |
| 102 | |
| 103 | lint-mod: ## Verify the Go dependencies |
| 104 | @echo "Running dependency check..." |
| 105 | @${GO} mod verify |
| 106 | @echo "Dependency check OK. Running vendor check..." |
| 107 | @git status > /dev/null |
| 108 | @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) |
| 109 | @[[ `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) |
| 110 | ${GO} mod tidy |
| 111 | ${GO} mod vendor |
| 112 | @git status > /dev/null |
| 113 | @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) |
| 114 | @[[ `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) |
| 115 | @echo "Vendor check OK." |
| 116 | |
| 117 | lint: local-lib-go lint-mod lint-dockerfile ## Run all lint targets |
| 118 | |
| 119 | test: ## Run unit tests |
| 120 | @echo "Call unit test case suite" |
| 121 | |
| 122 | sca: ## Runs static code analysis with the golangci-lint tool |
| 123 | @rm -rf ./sca-report |
| 124 | @mkdir -p ./sca-report |
| 125 | @echo "Running static code analysis..." |
| 126 | @${GOLANGCI_LINT} run --deadline=6m --out-format junit-xml ./... | tee ./sca-report/sca-report.xml |
| 127 | @echo "" |
| 128 | @echo "Static code analysis OK" |
| 129 | |
| 130 | clean: distclean ## Removes any local filesystem artifacts generated by a build |
| 131 | |
| 132 | distclean: ## Removes any local filesystem artifacts generated by a build or test run |
| 133 | rm -rf ./sca-report |
| 134 | |
| 135 | mod-update: ## Update go mod files |
| 136 | ${GO} mod tidy |
| 137 | ${GO} mod vendor |
| 138 | |
| 139 | # For each makefile target, add ## <description> on the target line and it will be listed by 'make help' |
| 140 | help: ## Print help for each Makefile target |
| 141 | @echo "Usage: make [<target>]" |
| 142 | @echo "where available targets are:" |
| 143 | @echo |
| 144 | @grep '^[[:alpha:]_-]*:.* ##' $(MAKEFILE_LIST) \ |
| 145 | | sort | awk 'BEGIN {FS=":.* ## "}; {printf "%-25s : %s\n", $$1, $$2};' |