-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
113 lines (92 loc) · 2.95 KB
/
Makefile
File metadata and controls
113 lines (92 loc) · 2.95 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
SHELL := bash
export MSYS_NO_PATHCONV := 1
IMAGE_NAME := devops-test
IMAGE_TAG := latest
IMAGE := $(IMAGE_NAME):$(IMAGE_TAG)
CI_IMAGE := devops-test-ci:latest
CLUSTER_NAME := devops-test
KIND_CONFIG := kind/cluster.yaml
K8S_OVERLAY := k8s/overlays/dev
NAMESPACE := devops-test
GO_CACHE_VOL := devops-test-gomod
KUBE_VOL := devops-test-kube
DOCKER_RUN := docker run --rm \
-v "$(CURDIR):/src" \
-v $(GO_CACHE_VOL):/go/pkg/mod \
$(CI_IMAGE)
DOCKER_RUN_DOCKER := docker run --rm \
-v "$(CURDIR):/src" \
-v /var/run/docker.sock:/var/run/docker.sock \
$(CI_IMAGE)
DOCKER_RUN_K8S := docker run --rm \
-v "$(CURDIR):/src" \
-v /var/run/docker.sock:/var/run/docker.sock \
-v $(KUBE_VOL):/root/.kube \
--network host \
$(CI_IMAGE)
.PHONY: all
all: ci-image lint test build scan cluster-up deploy
.PHONY: lint
lint:
$(DOCKER_RUN) golangci-lint run ./...
.PHONY: test
test:
$(DOCKER_RUN) go test -v ./...
.PHONY: vet
vet:
$(DOCKER_RUN) go vet ./...
.PHONY: build
build:
docker build --pull --no-cache -t $(IMAGE) .
@echo "Image built: $(IMAGE)"
@docker image inspect $(IMAGE) --format 'Size: {{.Size}} bytes'
.PHONY: scan
scan:
$(DOCKER_RUN_DOCKER) trivy image --severity HIGH,CRITICAL --exit-code 1 $(IMAGE)
.PHONY: ci-image
ci-image:
docker build --target ci -t $(CI_IMAGE) .
.PHONY: cluster-up
cluster-up:
$(DOCKER_RUN_K8S) sh -c '\
kind get clusters 2>/dev/null | grep -q "^$(CLUSTER_NAME)$$" \
&& echo "Cluster '"'"'$(CLUSTER_NAME)'"'"' already exists, skipping." \
|| kind create cluster --config=$(KIND_CONFIG) --name=$(CLUSTER_NAME); \
kind get kubeconfig --name=$(CLUSTER_NAME) > /root/.kube/config'
.PHONY: cluster-down
cluster-down:
$(DOCKER_RUN_K8S) kind delete cluster --name=$(CLUSTER_NAME)
.PHONY: load-image
load-image:
$(DOCKER_RUN_K8S) kind load docker-image $(IMAGE) --name=$(CLUSTER_NAME)
.PHONY: deploy
deploy: load-image
$(DOCKER_RUN_K8S) sh -c '\
kubectl apply -k $(K8S_OVERLAY) \
&& echo "" \
&& echo "Waiting for deployment rollout..." \
&& kubectl rollout status deployment/$(IMAGE_NAME) -n $(NAMESPACE) --timeout=90s \
&& echo "" \
&& echo "Service available at: http://localhost:30080"'
.PHONY: undeploy
undeploy:
$(DOCKER_RUN_K8S) kubectl delete -k $(K8S_OVERLAY) --ignore-not-found
.PHONY: run
run:
APP_PORT=8080 go run ./cmd/server
.PHONY: port-forward
port-forward:
kubectl port-forward service/$(IMAGE_NAME) 8080:8080 -n $(NAMESPACE)
.PHONY: smoke-test
smoke-test:
@echo "--- GET /health ---"
curl -sf http://localhost:30080/health | python3 -m json.tool || curl -sf http://localhost:30080/health
@echo ""
@echo "--- GET /api/time ---"
curl -sf http://localhost:30080/api/time | python3 -m json.tool || curl -sf http://localhost:30080/api/time
@echo ""
@echo "--- GET /health X-Debug: fail (expect 500) ---"
curl -sf -H "X-Debug: fail" http://localhost:30080/health || echo "Got non-2xx (expected)"
.PHONY: clean
clean: cluster-down
-docker volume rm $(KUBE_VOL) $(GO_CACHE_VOL) 2>/dev/null || true