-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMakefile
More file actions
239 lines (210 loc) · 7.99 KB
/
Makefile
File metadata and controls
239 lines (210 loc) · 7.99 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# Copyright The Linux Foundation and each contributor to LFX.
# SPDX-License-Identifier: MIT
# Project variables
GO_MODULE=github.com/linuxfoundation/lfx-v2-project-service
CMD_PATH=./cmd/project-api
BINARY_NAME=project-api
BINARY_PATH=bin/$(BINARY_NAME)
# API/Code generation variables
DESIGN_MODULE=$(shell go list -m)/api/project/v1/design
GOA_VERSION=v3.22.6
GO_FILES=$(shell find . -name '*.go' -not -path './api/project/v1/gen/*' -not -path './vendor/*')
# Build variables
BUILD_TIME=$(shell date -u '+%Y-%m-%d_%H:%M:%S')
GIT_COMMIT=$(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown")
VERSION=$(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")
LDFLAGS=-ldflags "-X main.Version=$(VERSION) -X main.BuildTime=$(BUILD_TIME) -X main.GitCommit=$(GIT_COMMIT)"
# Test variables
TEST_FLAGS=-race
TEST_TIMEOUT=5m
# Docker variables
DOCKER_IMAGE=linuxfoundation/lfx-v2-project-service
DOCKER_TAG=latest
# Helm variables
HELM_CHART_PATH=./charts/lfx-v2-project-service
HELM_RELEASE_NAME=lfx-v2-project-service
HELM_NAMESPACE=lfx
HELM_LOCAL_VALUES_FILE=values.local.yaml
# Default target
.PHONY: all
all: clean deps apigen fmt lint test build
# Help target
.PHONY: help
help:
@echo "Available targets:"
@echo " all - Run clean, deps, apigen, fmt, lint, test, and build"
@echo " deps - Install dependencies including goa CLI and git hooks"
@echo " apigen - Generate API code from design files"
@echo " build - Build the binary"
@echo " run - Run the service"
@echo " debug - Run the service with debug logging"
@echo " test - Run unit tests"
@echo " test-verbose - Run tests with verbose output"
@echo " test-coverage - Run tests with coverage report"
@echo " clean - Remove generated files and binaries"
@echo " lint - Run golangci-lint"
@echo " fmt - Format Go code"
@echo " check - Run fmt, lint, and license check without modifying files"
@echo " license-check - Check for license headers (basic validation)"
@echo " verify - Verify API generation is up to date"
@echo " docker-build - Build Docker image"
@echo " helm-install - Install Helm chart"
@echo " helm-install-local - Install Helm chart with local values"
@echo " helm-templates - Print templates for Helm chart"
@echo " helm-templates-local - Print templates for Helm chart with local values"
@echo " helm-uninstall - Uninstall Helm chart"
@echo " helm-restart - Restart the deployment pod in Kubernetes"
# Install dependencies
.PHONY: deps
deps:
@echo "==> Installing dependencies..."
go mod download
go install goa.design/goa/v3/cmd/goa@$(GOA_VERSION)
@command -v golangci-lint >/dev/null 2>&1 || { \
echo "==> Installing golangci-lint..."; \
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest; \
}
@if git rev-parse --is-inside-work-tree >/dev/null 2>&1; then \
git config --local core.hooksPath .githooks; \
echo "==> Git hooks installed from .githooks/"; \
else \
echo "==> Skipping git hooks install (not a git worktree)"; \
fi
# Generate API code from design files
.PHONY: apigen
apigen: deps
@echo "==> Generating API code..."
goa gen $(DESIGN_MODULE) -o api/project/v1
@echo "==> API generation complete"
# Build the binary
.PHONY: build
build: clean
@echo "==> Building $(BINARY_NAME)..."
@mkdir -p bin
go build $(LDFLAGS) -o $(BINARY_PATH) $(CMD_PATH)
@echo "==> Build complete: $(BINARY_PATH)"
# Run the service
.PHONY: run
run: apigen
@echo "==> Running $(BINARY_NAME)..."
go run $(LDFLAGS) $(CMD_PATH)
# Run with debug logging
.PHONY: debug
debug: apigen
@echo "==> Running $(BINARY_NAME) in debug mode..."
go run $(LDFLAGS) $(CMD_PATH) -d
# Run tests
.PHONY: test
test:
@echo "==> Running tests..."
go test $(TEST_FLAGS) -timeout $(TEST_TIMEOUT) ./...
# Run tests with verbose output
.PHONY: test-verbose
test-verbose:
@echo "==> Running tests (verbose)..."
go test $(TEST_FLAGS) -v -timeout $(TEST_TIMEOUT) ./...
# Run tests with coverage
.PHONY: test-coverage
test-coverage:
@echo "==> Running tests with coverage..."
@mkdir -p coverage
go test $(TEST_FLAGS) -cover -timeout $(TEST_TIMEOUT) -coverprofile=coverage/coverage.out ./...
go tool cover -html=coverage/coverage.out -o coverage/coverage.html
@echo "==> Coverage report: coverage/coverage.html"
# Clean build artifacts
.PHONY: clean
clean:
@echo "==> Cleaning build artifacts..."
@rm -rf bin/ coverage/
@go clean -cache
@echo "==> Clean complete"
# Run linter
.PHONY: lint
lint:
@echo "==> Running linter..."
@if command -v golangci-lint >/dev/null 2>&1; then \
golangci-lint run ./...; \
else \
echo "golangci-lint not found. Run 'make deps' to install it."; \
exit 1; \
fi
# Format code
.PHONY: fmt
fmt:
@echo "==> Formatting code..."
@go fmt ./...
@gofmt -s -w $(GO_FILES)
# Check license headers (basic validation - full check runs in CI)
.PHONY: license-check
license-check:
@echo "==> Checking license headers..."
@missing=$$(git ls-files | grep -E '\.(go|html|txt)$$' | grep -v "^api/project/v1/gen/" | grep -v "^internal/service/email/templates/" | while IFS= read -r f; do \
head -4 "$$f" | grep -q "Copyright The Linux Foundation and each contributor to LFX" || echo "Missing copyright: $$f"; \
head -4 "$$f" | grep -q "SPDX-License-Identifier: MIT" || echo "Missing SPDX: $$f"; \
done); \
if [ -n "$$missing" ]; then echo "$$missing"; exit 1; fi
@echo "==> License header check passed"
# Check formatting and linting without modifying files
.PHONY: check
check:
@echo "==> Checking code format..."
@if [ -n "$$(gofmt -l $(GO_FILES))" ]; then \
echo "The following files need formatting:"; \
gofmt -l $(GO_FILES); \
exit 1; \
fi
@echo "==> Code format check passed"
@$(MAKE) lint
@$(MAKE) license-check
# Verify that generated code is up to date
.PHONY: verify
verify: apigen
@echo "==> Verifying generated code is up to date..."
@if [ -n "$$(git status --porcelain api/project/v1/gen/)" ]; then \
echo "Generated code is out of date. Run 'make apigen' and commit the changes."; \
git status --porcelain api/project/v1/gen/; \
exit 1; \
fi
@echo "==> Generated code is up to date"
# Build Docker image
.PHONY: docker-build
docker-build:
@echo "==> Building Docker image..."
docker build -t $(DOCKER_IMAGE):$(DOCKER_TAG) -f Dockerfile .
@echo "==> Docker image built: $(DOCKER_IMAGE):$(DOCKER_TAG)"
# Install Helm chart
.PHONY: helm-install
helm-install:
@echo "==> Installing Helm chart..."
helm upgrade --force --install $(HELM_RELEASE_NAME) $(HELM_CHART_PATH) --namespace $(HELM_NAMESPACE)
@echo "==> Helm chart installed: $(HELM_RELEASE_NAME)"
# Install Helm chart with local values
.PHONY: helm-install-local
helm-install-local:
@echo "==> Installing Helm chart..."
helm upgrade --force --install $(HELM_RELEASE_NAME) $(HELM_CHART_PATH) --namespace $(HELM_NAMESPACE) --values $(HELM_CHART_PATH)/$(HELM_LOCAL_VALUES_FILE)
@echo "==> Helm chart installed: $(HELM_RELEASE_NAME)"
# Print templates for Helm chart
.PHONY: helm-templates
helm-templates:
@echo "==> Printing templates for Helm chart..."
helm template $(HELM_RELEASE_NAME) $(HELM_CHART_PATH) --namespace $(HELM_NAMESPACE)
@echo "==> Templates printed for Helm chart: $(HELM_RELEASE_NAME)"
# Print templates for Helm chart with local values
.PHONY: helm-templates-local
helm-templates-local:
@echo "==> Printing templates for Helm chart..."
helm template $(HELM_RELEASE_NAME) $(HELM_CHART_PATH) --namespace $(HELM_NAMESPACE) --values $(HELM_CHART_PATH)/$(HELM_LOCAL_VALUES_FILE)
@echo "==> Templates printed for Helm chart: $(HELM_RELEASE_NAME)"
# Uninstall Helm chart
.PHONY: helm-uninstall
helm-uninstall:
@echo "==> Uninstalling Helm chart..."
helm uninstall $(HELM_RELEASE_NAME) --namespace $(HELM_NAMESPACE)
@echo "==> Helm chart uninstalled: $(HELM_RELEASE_NAME)"
# Restart the deployment pod
.PHONY: helm-restart
helm-restart:
@echo "==> Restarting deployment pod..."
kubectl rollout restart deployment/$(HELM_RELEASE_NAME) --namespace $(HELM_NAMESPACE)
@echo "==> Deployment restarted: $(HELM_RELEASE_NAME)"