-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathMakefile
More file actions
299 lines (255 loc) · 11.1 KB
/
Copy pathMakefile
File metadata and controls
299 lines (255 loc) · 11.1 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# Makefile for kubechecks
# Replaces Earthly build system with Docker Buildx + native Go tooling
.PHONY: help build build-debug build-multiarch push-multiarch test lint fmt validate ci clean setup-buildx
# Default target
.DEFAULT_GOAL := help
# ============================================================================
# Version Management - Read from .tool-versions
# ============================================================================
# Parse .tool-versions file for tool versions
GOLANG_VERSION := $(shell grep '^golang ' .tool-versions | awk '{print $$2}')
GOLANGCI_LINT_VERSION := $(shell grep '^golangci-lint ' .tool-versions | awk '{print $$2}')
HELM_VERSION := $(shell grep '^helm ' .tool-versions | awk '{print $$2}')
ALPINE_VERSION := 3.21
# ============================================================================
# Git Information
# ============================================================================
GIT_COMMIT := $(shell git rev-parse --short HEAD)
GIT_TAG := $(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")
GIT_BRANCH := $(shell git rev-parse --abbrev-ref HEAD)
# ============================================================================
# Build Configuration
# ============================================================================
# Docker image configuration
IMAGE_NAME ?= kubechecks
IMAGE_TAG ?= $(GIT_COMMIT)
IMAGE_REGISTRY ?= ghcr.io/zapier
# Full image reference
IMAGE_REF := $(IMAGE_REGISTRY)/$(IMAGE_NAME):$(IMAGE_TAG)
IMAGE_LATEST := $(IMAGE_REGISTRY)/$(IMAGE_NAME):latest
# Build platforms for multi-arch
PLATFORMS := linux/amd64,linux/arm64
# Docker Buildx builder name
BUILDER_NAME := kubechecks-builder
# Cache configuration for GitHub Actions
CACHE_FROM ?= type=registry,ref=$(IMAGE_REGISTRY)/$(IMAGE_NAME):buildcache
CACHE_TO ?= type=registry,ref=$(IMAGE_REGISTRY)/$(IMAGE_NAME):buildcache,mode=max
# For GitHub Actions, use GHA cache
ifdef GITHUB_ACTIONS
CACHE_FROM = type=gha
CACHE_TO = type=gha,mode=max
endif
# ============================================================================
# Help Target
# ============================================================================
help: ## Show this help message
@echo "kubechecks Makefile - Docker Buildx Build System"
@echo ""
@echo "Configuration:"
@echo " GOLANG_VERSION: $(GOLANG_VERSION)"
@echo " GOLANGCI_LINT_VERSION: $(GOLANGCI_LINT_VERSION)"
@echo " GIT_COMMIT: $(GIT_COMMIT)"
@echo " GIT_TAG: $(GIT_TAG)"
@echo " IMAGE_REF: $(IMAGE_REF)"
@echo ""
@echo "Usage:"
@echo " make <target>"
@echo ""
@echo "Targets:"
@awk 'BEGIN {FS = ":.*##"; printf ""} /^[a-zA-Z_-]+:.*?##/ { printf " \033[36m%-20s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
# ============================================================================
# Docker Buildx Setup
# ============================================================================
setup-buildx: ## Setup Docker Buildx builder
@echo "==> Setting up Docker Buildx builder..."
@docker buildx inspect $(BUILDER_NAME) >/dev/null 2>&1 || \
docker buildx create --name $(BUILDER_NAME) --driver docker-container --bootstrap --use
@docker buildx inspect --bootstrap
# ============================================================================
# Build Targets
# ============================================================================
build: setup-buildx ## Build single-platform production image (local architecture)
@echo "==> Building production image for local platform..."
docker buildx build \
--builder $(BUILDER_NAME) \
--target production \
--build-arg GOLANG_VERSION=$(GOLANG_VERSION) \
--build-arg ALPINE_VERSION=$(ALPINE_VERSION) \
--build-arg GIT_COMMIT=$(GIT_COMMIT) \
--build-arg GIT_TAG=$(GIT_TAG) \
--tag $(IMAGE_NAME):$(IMAGE_TAG) \
--tag $(IMAGE_NAME):latest \
--load \
.
@echo ""
@echo "==> Build complete!"
@echo " Image: $(IMAGE_NAME):$(IMAGE_TAG)"
@docker images $(IMAGE_NAME):$(IMAGE_TAG) --format " Size: {{.Size}}"
build-debug: setup-buildx ## Build debug image with delve for local development
@echo "==> Building debug image with delve..."
docker buildx build \
--builder $(BUILDER_NAME) \
--target debug \
--build-arg GOLANG_VERSION=$(GOLANG_VERSION) \
--build-arg ALPINE_VERSION=$(ALPINE_VERSION) \
--build-arg GIT_COMMIT=$(GIT_COMMIT) \
--build-arg GIT_TAG=$(GIT_TAG) \
--tag $(IMAGE_NAME):debug \
--tag $(IMAGE_NAME):$(IMAGE_TAG) \
--load \
.
@echo ""
@echo "==> Debug build complete!"
@echo " Image: $(IMAGE_NAME):debug"
@echo " Image: $(IMAGE_NAME):$(IMAGE_TAG)"
@echo " Debug port: 2345"
build-multiarch: setup-buildx ## Build multi-architecture images (amd64 + arm64)
@echo "==> Building multi-arch images for $(PLATFORMS)..."
docker buildx build \
--builder $(BUILDER_NAME) \
--platform $(PLATFORMS) \
--target production \
--build-arg GOLANG_VERSION=$(GOLANG_VERSION) \
--build-arg ALPINE_VERSION=$(ALPINE_VERSION) \
--build-arg GIT_COMMIT=$(GIT_COMMIT) \
--build-arg GIT_TAG=$(GIT_TAG) \
--tag $(IMAGE_REF) \
--cache-from=$(CACHE_FROM) \
--cache-to=$(CACHE_TO) \
.
@echo ""
@echo "==> Multi-arch build complete (not loaded locally)"
@echo " Platforms: $(PLATFORMS)"
@echo " Use 'make push-multiarch' to build and push"
push-multiarch: setup-buildx ## Build and push multi-architecture images to registry
@echo "==> Building and pushing multi-arch images..."
docker buildx build \
--builder $(BUILDER_NAME) \
--platform $(PLATFORMS) \
--target production \
--build-arg GOLANG_VERSION=$(GOLANG_VERSION) \
--build-arg ALPINE_VERSION=$(ALPINE_VERSION) \
--build-arg GIT_COMMIT=$(GIT_COMMIT) \
--build-arg GIT_TAG=$(GIT_TAG) \
--tag $(IMAGE_REF) \
--tag $(IMAGE_LATEST) \
--cache-from=$(CACHE_FROM) \
--cache-to=$(CACHE_TO) \
--push \
.
@echo ""
@echo "==> Images pushed successfully!"
@echo " $(IMAGE_REF)"
@echo " $(IMAGE_LATEST)"
# ============================================================================
# Go Targets
# ============================================================================
test: ## Run Go tests
@echo "==> Running Go tests..."
# Note: -race flag temporarily removed due to existing concurrency bug in appset_directory.go
# See: data race between Count() and RemoveAppSet() in TestApplicationSetWatcher_OnApplicationDEleted
# TODO: Add proper mutex synchronization and re-enable -race detection
go test -v -timeout 60s ./...
test-coverage: ## Run Go tests with coverage
@echo "==> Running Go tests with coverage..."
# Note: -race flag removed (see comment in test target above)
go test -v -coverprofile=coverage.out -covermode=atomic ./...
@echo ""
@echo "==> Coverage summary:"
@go tool cover -func=coverage.out | tail -1
lint: ## Run golangci-lint
@echo "==> Running golangci-lint..."
@which golangci-lint >/dev/null 2>&1 || \
(echo "Error: golangci-lint not found. Install with: go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest" && exit 1)
golangci-lint run --timeout 15m --verbose
fmt: ## Format Go code
@echo "==> Formatting Go code..."
go fmt ./...
@echo "==> Checking for Go file formatting changes..."
@git diff --exit-code -- '*.go' ':!vendor/' || (echo "Error: Code formatting changed files. Please commit the changes." && exit 1)
validate: ## Validate Go code with go vet
@echo "==> Running go vet..."
go vet ./...
tidy: ## Run go mod tidy
@echo "==> Running go mod tidy..."
go mod tidy
@git diff --exit-code go.mod go.sum || \
(echo "Warning: go.mod or go.sum changed. Please commit the changes." && exit 1)
rebuild-docs: ## Rebuild documentation from code
@echo "==> Rebuilding documentation..."
go run hacks/env-to-docs.go
@echo "==> Documentation regenerated: docs/usage.md"
# ============================================================================
# CI Target
# ============================================================================
ci: fmt lint test ## Run full CI pipeline (fmt, lint, test)
@echo ""
@echo "==> ✅ All CI checks passed!"
# ============================================================================
# Helm Targets
# ============================================================================
test-helm: ## Run helm chart tests (requires helm, chart-testing)
@echo "==> Testing Helm charts..."
@which ct >/dev/null 2>&1 || \
(echo "Error: chart-testing (ct) not found. Install from: https://github.com/helm/chart-testing" && exit 1)
ct lint --config ./.github/ct.yaml ./charts
# ============================================================================
# Utility Targets
# ============================================================================
clean: ## Clean up build artifacts and Docker images
@echo "==> Cleaning up..."
rm -f coverage.out
docker rmi $(IMAGE_NAME):$(IMAGE_TAG) 2>/dev/null || true
docker rmi $(IMAGE_NAME):latest 2>/dev/null || true
docker rmi $(IMAGE_NAME):debug 2>/dev/null || true
@echo "==> Clean complete"
clean-cache: ## Clean Docker buildx cache
@echo "==> Cleaning Docker buildx cache..."
docker buildx prune -f
@echo "==> Cache cleaned"
inspect: build ## Inspect the built image
@echo "==> Image details:"
@docker images $(IMAGE_NAME):$(IMAGE_TAG) --format "table {{.Repository}}\t{{.Tag}}\t{{.Size}}"
@echo ""
@echo "==> Image layers:"
@docker history $(IMAGE_NAME):$(IMAGE_TAG) --no-trunc --format "table {{.Size}}\t{{.CreatedBy}}" | head -20
run: build ## Build and run the image locally
@echo "==> Running $(IMAGE_NAME):$(IMAGE_TAG)..."
docker run --rm -it $(IMAGE_NAME):$(IMAGE_TAG) help
version: ## Show version information
@echo "Git Commit: $(GIT_COMMIT)"
@echo "Git Tag: $(GIT_TAG)"
@echo "Git Branch: $(GIT_BRANCH)"
@echo "Golang: $(GOLANG_VERSION)"
@echo "Alpine: $(ALPINE_VERSION)"
# ============================================================================
# Development Targets
# ============================================================================
dev-setup: ## Setup development environment
@echo "==> Setting up development environment..."
@echo "Checking required tools..."
@which go >/dev/null 2>&1 || (echo "Error: Go not installed" && exit 1)
@which docker >/dev/null 2>&1 || (echo "Error: Docker not installed" && exit 1)
@echo "✅ Go version: $$(go version)"
@echo "✅ Docker version: $$(docker --version)"
@echo ""
@echo "Installing Go tools..."
@if which golangci-lint >/dev/null 2>&1; then \
echo "✅ golangci-lint already installed ($$(golangci-lint version --format short 2>/dev/null || golangci-lint --version | head -1))"; \
else \
echo "Installing golangci-lint@v$(GOLANGCI_LINT_VERSION)..."; \
go install github.com/golangci/golangci-lint/cmd/golangci-lint@v$(GOLANGCI_LINT_VERSION); \
echo "✅ golangci-lint installed"; \
fi
@echo ""
@echo "==> Development environment ready!"
# ============================================================================
# Quick Reference
# ============================================================================
## Common workflows:
## Local build: make build
## Run tests: make test
## Run CI checks: make ci
## Debug build: make build-debug
## Multi-arch: make build-multiarch
## Push to registry: make push-multiarch