blob: 08d9cfe36ad061aa372fd8774dcc7c89e7eed759 [file] [log] [blame]
Holger Hildebrandtda7758b2020-03-16 11:30:03 +00001#
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
18SHELL = bash -e -o pipefail
19
20# Variables
21VERSION ?= $(shell cat ./VERSION)
22
23DOCKER_LABEL_VCS_DIRTY = false
24ifneq ($(shell git ls-files --others --modified --exclude-standard 2>/dev/null | wc -l | sed -e 's/ //g'),0)
25 DOCKER_LABEL_VCS_DIRTY = true
26endif
27## Docker related
28DOCKER_EXTRA_ARGS ?=
29DOCKER_REGISTRY ?=
30DOCKER_REPOSITORY ?=
31DOCKER_TAG ?= ${VERSION}
32ADAPTER_IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-openonu-adapter-go:${DOCKER_TAG}
33TYPE ?= minimal
34
35## Docker labels. Only set ref and commit date if committed
36DOCKER_LABEL_VCS_URL ?= $(shell git remote get-url $(shell git remote))
37DOCKER_LABEL_VCS_REF = $(shell git rev-parse HEAD)
38DOCKER_LABEL_BUILD_DATE ?= $(shell date -u "+%Y-%m-%dT%H:%M:%SZ")
39DOCKER_LABEL_COMMIT_DATE = $(shell git show -s --format=%cd --date=iso-strict HEAD)
40
41DOCKER_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
Matteo Scandolo761f7512020-11-23 15:52:40 -080051VOLTHA_TOOLS_VERSION ?= 2.3.1
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000052
53GO = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app -v gocache:/.cache -v gocache-${VOLTHA_TOOLS_VERSION}:/go/pkg voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-golang go
54GO_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 Scandolo761f7512020-11-23 15:52:40 -080055GOCOVER_COBERTURA = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app/src/github.com/opencord/voltha-openonu-adapter-go -i voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-gocover-cobertura gocover-cobertura
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000056GOFMT = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-golang gofmt
57GOLANGCI_LINT = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app -v gocache:/.cache -v gocache-${VOLTHA_TOOLS_VERSION}:/go/pkg voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-golangci-lint golangci-lint
58HADOLINT = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-hadolint hadolint
59
60.PHONY: docker-build local-protos local-lib-go
61
62# This should to be the first and default target in this Makefile
63help:
64 @echo "Usage: make [<target>]"
65 @echo "where available targets are:"
66 @echo ""
67 @echo "clean : Removes any local filesystem artifacts generated by a build"
68 @echo "distclean : Removes any local filesystem artifacts generated by a build or test run"
69 @echo "build : Build all openonu adapter artifacts"
70 @echo "docker-build : Build openonu adapter docker image"
71 @echo "docker-push : Push the docker images to an external repository"
72 @echo "help : Print this help"
73 @echo "lint : Run all lint targets"
74 @echo "lint-dockerfile : Perform static analysis on Dockerfiles"
75 @echo "lint-mod : Verify the Go dependencies"
76 @echo "lint-sanity : Run the Go language sanity tests (vet)"
77 @echo "lint-style : Verify the Go standard format of the source"
78 @echo "local-protos : Copies a local verison of the VOLTHA protos into the vendor directory"
79 @echo "local-lib-go : Copies a local version of the VOTLHA dependencies into the vendor directory"
80 @echo "sca : Runs various SCA through golangci-lint tool"
81 @echo "test : Run unit tests, if any"
82 @echo
83
84## Local Development Helpers
85
86local-protos:
87ifdef LOCAL_PROTOS
Holger Hildebrandt6e846d12020-12-11 14:35:55 +000088 rm -rf vendor/github.com/opencord/voltha-protos/v4/go
89 mkdir -p vendor/github.com/opencord/voltha-protos/v4/go
90 cp -r ${LOCAL_PROTOS}/go/* vendor/github.com/opencord/voltha-protos/v4/go
91 rm -rf vendor/github.com/opencord/voltha-protos/v4/go/vendor
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000092endif
93
94## Local Development Helpers
95local-lib-go:
96ifdef LOCAL_LIB_GO
Holger Hildebrandt6e846d12020-12-11 14:35:55 +000097 mkdir -p vendor/github.com/opencord/voltha-lib-go/v4/pkg
98 cp -r ${LOCAL_LIB_GO}/pkg/* vendor/github.com/opencord/voltha-lib-go/v4/pkg/
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000099endif
100
101## Docker targets
102
103build: docker-build
104
Holger Hildebrandt6999fe52021-02-11 11:49:05 +0000105docker-build: local-protos local-lib-go ## Build openonu adapter docker image (set BUILD_PROFILED=true to also build the profiled image)
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000106 docker build $(DOCKER_BUILD_ARGS) -t ${ADAPTER_IMAGENAME} -f docker/Dockerfile.openonu .
Holger Hildebrandt6999fe52021-02-11 11:49:05 +0000107ifdef BUILD_PROFILED
108 docker build $(DOCKER_BUILD_ARGS) --build-arg EXTRA_GO_BUILD_TAGS="-tags profile" -t ${ADAPTER_IMAGENAME}-profile -f docker/Dockerfile.openonu .
109endif
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000110
111docker-push:
112 docker push ${ADAPTER_IMAGENAME}
Holger Hildebrandt6999fe52021-02-11 11:49:05 +0000113ifdef BUILD_PROFILED
114 docker push ${ADAPTER_IMAGENAME}-profile
115endif
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000116
117docker-kind-load:
118 @if [ "`kind get clusters | grep voltha-$(TYPE)`" = '' ]; then echo "no voltha-$(TYPE) cluster found" && exit 1; fi
119 kind load docker-image ${ADAPTER_IMAGENAME} --name=voltha-$(TYPE) --nodes $(shell kubectl get nodes --template='{{range .items}}{{.metadata.name}},{{end}}' | sed 's/,$$//')
120
121
122## lint and unit tests
123
124lint-dockerfile:
125 @echo "Running Dockerfile lint check ..."
126 @${HADOLINT} $$(find . -name "Dockerfile.*")
127 @echo "Dockerfile lint check OK"
128
129lint-style:
130 @echo "Running style check..."
131 @gofmt_out="$$(${GOFMT} -l $$(find . -name '*.go' -not -path './vendor/*'))" ;\
132 if [ ! -z "$$gofmt_out" ]; then \
133 echo "$$gofmt_out" ;\
134 echo "Style check failed on one or more files ^, run 'go fmt' to fix." ;\
135 exit 1 ;\
136 fi
137 @echo "Style check OK"
138
139lint-sanity:
140 @echo "Running sanity check..."
141 @${GO} vet -mod=vendor ./...
142 @echo "Sanity check OK"
143
144lint-mod:
145 @echo "Running dependency check..."
146 @${GO} mod verify
147 @echo "Dependency check OK. Running vendor check..."
148 @git status > /dev/null
149 @git diff-index --quiet HEAD -- go.mod go.sum vendor || (echo "ERROR: Staged or modified files must be committed before running this test" && echo "`git status`" && exit 1)
150 @[[ `git ls-files --exclude-standard --others go.mod go.sum vendor` == "" ]] || (echo "ERROR: Untracked files must be cleaned up before running this test" && echo "`git status`" && exit 1)
151 ${GO} mod tidy
152 ${GO} mod vendor
153 @git status > /dev/null
154 @git diff-index --quiet HEAD -- go.mod go.sum vendor || (echo "ERROR: Modified files detected after running go mod tidy / go mod vendor" && echo "`git status`" && exit 1)
155 @[[ `git ls-files --exclude-standard --others go.mod go.sum vendor` == "" ]] || (echo "ERROR: Untracked files detected after running go mod tidy / go mod vendor" && echo "`git status`" && exit 1)
156 @echo "Vendor check OK."
157
158lint: local-lib-go lint-style lint-sanity lint-mod lint-dockerfile
159
Matteo Scandolod132c0e2020-04-24 17:06:25 -0700160test: lint
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000161 @mkdir -p ./tests/results
162 @${GO} test -mod=vendor -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\
163 RETURN=$$? ;\
164 ${GO_JUNIT_REPORT} < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\
165 ${GOCOVER_COBERTURA} < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\
166 exit $$RETURN
167
168sca:
169 @mkdir -p ./sca-report
170 @echo "Running static code analysis..."
ozgecanetsiab6441962021-03-10 10:58:48 +0300171 @${GOLANGCI_LINT} run --deadline=6m --out-format junit-xml ./... | tee ./sca-report/sca-report.xml
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000172 @echo "Static code analysis OK"
173
174clean: distclean
175
176distclean:
177 rm -rf ./sca-report
178
179mod-update:
180 ${GO} mod tidy
181 ${GO} mod vendor