-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMakefile
133 lines (103 loc) · 4.7 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#
# ClusterIQ Makefile
################################################################################
# Global Vars
SHORT_COMMIT_HASH := $(shell git rev-parse --short=7 HEAD)
# Binary vars
CONTAINER_ENGINE ?= $(shell which podman >/dev/null 2>&1 && echo podman || echo docker)
K8S_CLI ?= $(shell which oc >/dev/null 2>&1 && echo oc || echo kubectl)
# Required binaries
REQUIRED_BINS := $(CONTAINER_ENGINE) $(CONTAINER_ENGINE)-compose $(K8S_CLI) swag
# Container image registy vars
REGISTRY ?= quay.io
PROJECT_NAME ?= cluster-iq
REGISTRY_REPO ?= ecosystem-appeng
COMPOSE_NETWORK ?= compose_cluster_iq
# Building vars
LDFLAGS := -ldflags "-X main.version=$(VERSION) -X main.commit=$(SHORT_COMMIT_HASH)"
# Project directories
TEST_DIR ?= ./test
BUILD_DIR ?= ./build
BIN_DIR ?= $(BUILD_DIR)/bin
CMD_DIR ?= ./cmd
PKG_DIR ?= ./internal
DEPLOYMENTS_DIR ?= ./deployments
# Images
API_IMG_NAME ?= $(PROJECT_NAME)-api
API_IMAGE ?= $(REGISTRY)/$(REGISTRY_REPO)/$(API_IMG_NAME)
API_CONTAINERFILE ?= ./$(DEPLOYMENTS_DIR)/containerfiles/Containerfile-api
SCANNER_IMG_NAME ?= $(PROJECT_NAME)-scanner
SCANNER_IMAGE ?= $(REGISTRY)/$(REGISTRY_REPO)/$(SCANNER_IMG_NAME)
SCANNER_CONTAINERFILE ?= ./$(DEPLOYMENTS_DIR)/containerfiles/Containerfile-scanner
# Standard targets
all: ## Stops, build and starts the development environment based on containers
all: stop-dev build start-dev
.PHONY: check-dependencies
check-dependencies:
@$(foreach bin,$(REQUIRED_BINS),\
$(if $(shell command -v $(bin) 2> /dev/null),,\
$(error ✗ $(bin) is required but not installed)))
# Building in local environment
local-clean:
@echo "### [Cleanning local building] ###"
@rm -Rf $(BIN_DIR)
local-build: local-build-scanner local-build-api
local-build-api: swagger-doc
@echo "### [Building API] ###"
@go build -o $(BIN_DIR)/api/api $(LDFLAGS) ./cmd/api/
local-build-scanner:
@echo "### [Building Scanner] ###"
@go build -o $(BIN_DIR)/scanners/scanner $(LDFLAGS) ./cmd/scanner
# Container based working targets
clean: ## Removes the container images for the API and the Scanner
@echo "### [Cleanning Container images] ###"
@-$(CONTAINER_ENGINE) images | grep -e $(SCANNER_IMAGE) -e $(API_IMAGE) | awk '{print $$3}' | xargs $(CONTAINER_ENGINE) rmi -f
build: ## Builds the container images for the API and the Scanner
build: build-api build-scanner
build-api: ## Builds the API Container image
@echo "### [Building API container image] ###"
@$(CONTAINER_ENGINE) build -t $(API_IMAGE):latest -f $(API_CONTAINERFILE) .
@$(CONTAINER_ENGINE) tag $(API_IMAGE):latest $(API_IMAGE):$(SHORT_COMMIT_HASH)
@echo "Build Successful"
build-scanner: ## Builds the Scanner Container image
@echo "### [Building Scanner container image] ###"
@$(CONTAINER_ENGINE) build -t $(SCANNER_IMAGE):latest -f $(SCANNER_CONTAINERFILE) .
@$(CONTAINER_ENGINE) tag $(SCANNER_IMAGE):latest $(SCANNER_IMAGE):$(SHORT_COMMIT_HASH)
@echo "Build Successful"
# Development targets
start-dev: ## Development env based on Docker/Podman Compose tool
@echo "### [Starting dev environment] ###"
@$(CONTAINER_ENGINE)-compose -f $(DEPLOYMENTS_DIR)/compose/compose-devel.yaml up -d
@echo "### [Dev environment running] ###"
@echo "### [API: http://localhost:8081/api/v1/healthcheck ] ###"
stop-dev: ## Stops the container based development env
@echo "### [Stopping dev environment] ###"
@$(CONTAINER_ENGINE)-compose -f $(DEPLOYMENTS_DIR)/compose/compose-devel.yaml down
@# If there are no containers attached to the network, remove it
@[ "$(shell $(CONTAINER_ENGINE) ps --all --filter network=$(COMPOSE_NETWORK) | tail -n +2 | wc -l)" -eq "0" ] && { $(CONTAINER_ENGINE) network rm $(COMPOSE_NETWORK); } || { exit 0; }
restart-dev: ## Restarts the container based development env
restart-dev: stop-dev start-dev
# Tests targets
test:
@[[ -d $(TEST_DIR) ]] || mkdir $(TEST_DIR)
@go test -race ./... -coverprofile $(TEST_DIR)/cover.out
cover: test
@go tool cover -func $(TEST_DIR)/cover.out
# Documentation targets
swagger-editor: ## Opens web editor for modifying Swagger docs
@echo "### [Launching Swagger editor] ###"
@$(CONTAINER_ENGINE) run --rm -p 127.0.0.1:8082:8080 \
-e SWAGGER_FILE=/tmp/swagger.yaml \
-v ./cmd/api/docs/swagger.yaml:/tmp/swagger.yaml:Z \
swaggerapi/swagger-editor
@echo "Open your browser at http://127.0.0.1:8082"
swagger-doc: # Generates Swagger documentation for ClusterIQ API
@echo "### [Generating Swagger Docs] ###"
@swag fmt
@swag init --generalInfo ./cmd/api/api_server.go --parseDependency --output ./cmd/api/docs
# Set the default target to "help"
.DEFAULT_GOAL := help
# Help
help: ## Display this help message
@echo "### [Makefile Help] ###"
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\t\033[1;36m%-30s\033[0m %s\n", $$1, $$2}'