forked from insights-onprem/ros-ocp-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
211 lines (177 loc) Β· 8.38 KB
/
Copy pathMakefile
File metadata and controls
211 lines (177 loc) Β· 8.38 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
include scripts/.env
UNAME_S := $(shell uname -s)
identity={"identity": {"org_id": "3340851", "type": "System", "auth_type": "cert-auth", "system": {"cn": "1b36b20f-7fa0-4454-a6d2-008294e06378", "cert_type": "system"}, "internal": {"org_id": "3340851", "auth_time": 6300}}}
ifeq ($(UNAME_S),Darwin)
b64_identity=$(shell echo '${identity}' | base64)
else
b64_identity=$(shell echo '${identity}' | base64 -w 0 -)
endif
ros_ocp_msg='{"request_id": "uuid1234", "b64_identity": "test", "metadata": {"org_id": "3340851", "source_id": "111", "cluster_uuid": "222", "cluster_alias": "name222"}, "files": ["http://localhost:8888/ros-ocp-usage.csv"]}'
ros_ocp_msg_24Hrs='{"request_id": "uuid1234", "b64_identity": "test", "metadata": {"org_id": "3340851", "source_id": "111", "cluster_uuid": "222", "cluster_alias": "name222"}, "files": ["http://localhost:8888/ros-ocp-usage-24Hrs.csv"]}'
file=./scripts/samples/cost-mgmt.tar.gz
CSVfile=./scripts/samples/ros-ocp-usage.csv
CSVfile_name_tuple := $(subst /, ,$(CSVfile:%=%))
CSVfile_name := $(word 4,$(CSVfile_name_tuple))
INGRESS_PORT ?= 3000
GINKGO_ARGS=-mod=mod
ifdef env
short_env=$(shell echo '${env}' | cut -d'-' -f2)
server=$(shell oc get clowdenvironments env-ephemeral-${short_env} -o=jsonpath='{.status.hostname}')
username=$(shell oc get secret env-ephemeral-${short_env}-keycloak -n ephemeral-${short_env} -o=jsonpath='{.data.defaultUsername}' | base64 -d)
password=$(shell oc get secret env-ephemeral-${short_env}-keycloak -n ephemeral-${short_env} -o=jsonpath='{.data.defaultPassword}' | base64 -d)
auth_header=$(shell echo -n '${username}:${password}' | base64)
minio_accessKey=$(shell oc get secret env-ephemeral-${short_env}-minio -o=jsonpath='{.data.accessKey}' | base64 -d)
minio_secretKey=$(shell oc get secret env-ephemeral-${short_env}-minio -o=jsonpath='{.data.secretKey}' | base64 -d)
endif
LOCALBIN ?= $(shell pwd)/bin
$(LOCALBIN):
ifeq (,$(wildcard $(LOCALBIN)))
@echo "π€ Ensuring $(LOCALBIN) is available"
mkdir -p $(LOCALBIN)
@echo "β
Done"
endif
.PHONY: help
help: ## Display this help message
@echo "Available targets:"
@echo " setup-envtest Download setup-envtest tool and Kubernetes test binaries"
@echo " ginkgo Download Ginkgo test framework binary"
@echo " test Run tests with Ginkgo"
@echo " clean-test-binaries Clean up downloaded test binaries"
@echo " clean-build Clean up build artifacts and Podman images"
@echo " lint Run golangci-lint"
@echo " build Build the application binary inside a container"
@echo " build-image Build the container image (use IMAGE_REPO and IMAGE_TAG to customize)"
@echo " db-migrate Run database migrations"
@echo " help Show this help message"
.PHONY: golangci-lint
GOLANGCILINT := $(LOCALBIN)/golangci-lint
GOLANGCI_URL := https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh
start_date := 1970-01-01
GOLANGCI_VERSION := v1.64.4 # TODO: Remove version locking after moving from go1.23 to go1.24
golangci-lint: $(LOCALBIN)
ifeq (,$(wildcard $(GOLANGCILINT)))
@ echo "π₯ Downloading golangci-lint"
curl -sSfL $(GOLANGCI_URL) | sh -s -- -b $(LOCALBIN) $(GOLANGCI_VERSION)
@ echo "β
Done"
endif
.PHONY: setup-envtest
SETUP_ENVTEST := $(LOCALBIN)/setup-envtest
ENVTEST_K8S_VERSION ?= 1.32.0
ENVTEST_BIN_DIR ?= $(LOCALBIN)
setup-envtest: $(LOCALBIN)
ifeq (,$(wildcard $(SETUP_ENVTEST)))
@ echo "π₯ Downloading setup-envtest"
GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@release-0.20
@ echo "β
Done"
endif
$(SETUP_ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(ENVTEST_BIN_DIR) -p path
.PHONY: ginkgo
GINKGO := $(LOCALBIN)/ginkgo
ginkgo: $(LOCALBIN)
ifeq (,$(wildcard $(GINKGO)))
@ echo "π₯ Downloading Ginkgo"
GOBIN=$(LOCALBIN) go install github.com/onsi/ginkgo/v2/ginkgo@latest
@ echo "β
Done"
endif
.PHONY: install-golang-migrate-cli-tool
install-golang-migrate-cli-tool: $(LOCALBIN)
curl -L https://github.com/golang-migrate/migrate/releases/download/v4.15.2/migrate.linux-amd64.tar.gz | tar xvz -C $(LOCALBIN) migrate
.PHONY: db-migrate
db-migrate:
go run rosocp.go db migrate up
.PHONY: run-processor
run-processor:
PROMETHEUS_PORT=5005 go run rosocp.go start processor
.PHONY: run-recommendation-poller
run-recommendation-poller:
PROMETHEUS_PORT=5006 go run rosocp.go start recommendation-poller
.PHONY: run-api-server
run-api-server:
PROMETHEUS_PORT=5007 go run rosocp.go start api
# Image configuration
IMAGE_REPO ?= localhost/ros-ocp-backend
IMAGE_TAG ?= latest
IMAGE_PULLSPEC = $(IMAGE_REPO):$(IMAGE_TAG)
.PHONY: build
build:
@echo "π¨ Building application inside container..."
@mkdir -p bin
podman build --target builder -t ros-ocp-builder .
podman create --name ros-ocp-temp ros-ocp-builder
podman cp ros-ocp-temp:/go/src/app/rosocp ./bin/rosocp
podman rm ros-ocp-temp
@echo "β
Build completed: bin/rosocp"
.PHONY: build-image
build-image:
@echo "π³ Building container image: $(IMAGE_PULLSPEC)"
podman build -t $(IMAGE_PULLSPEC) .
@echo "β
Container image built: $(IMAGE_PULLSPEC)"
.PHONY: lint
lint: golangci-lint
$(GOLANGCILINT) run --timeout=3m ./...
.PHONY: test
test:
@echo "π§ͺ Running tests in isolated container..."
podman build --no-cache -f Dockerfile.tests -t ros-ocp-backend-tests .
.PHONY: clean-test-binaries
clean-test-binaries:
chmod -R +w $(ENVTEST_BIN_DIR)/k8s 2>/dev/null || true
rm -rf $(ENVTEST_BIN_DIR)/k8s
rm -f $(SETUP_ENVTEST)
rm -f $(GINKGO)
.PHONY: clean-build
clean-build:
@echo "π§Ή Cleaning build artifacts..."
rm -f bin/rosocp
-podman rmi ros-ocp-builder 2>/dev/null || true
-podman rmi $(IMAGE_PULLSPEC) 2>/dev/null || true
@echo "β
Build artifacts cleaned"
MCCILINT := $(LOCALBIN)/mc
.PHONY: archive-to-minio
archive-to-minio:
ifdef env
-oc expose svc env-${env}-minio -n ${env}
ifeq (,$(wildcard $(MCCILINT)))
@ echo "π₯ Downloading minio client"
ifeq ($(UNAME_S),Darwin)
curl https://dl.min.io/client/mc/release/darwin-amd64/mc --create-dirs -o $(MCCILINT)
else
curl https://dl.min.io/client/mc/release/linux-amd64/mc --create-dirs -o $(MCCILINT)
endif
chmod +x $(MCCILINT)
@ echo "β
Done"
endif
$(MCCILINT) alias set myminio http://env-${env}-minio-${env}.apps.crc-eph.r9lp.p1.openshiftapps.com ${minio_accessKey} ${minio_secretKey}
$(MCCILINT) cp ${CSVfile} myminio/insights-upload-perma/
sleep 5
$(eval SHAREURL=$(shell $(MCCILINT) share download --json myminio/insights-upload-perma/${CSVfile_name} | jq -r '.share'))
$(eval KAFKAPOD=$(shell oc get pods -o custom-columns=POD:.metadata.name --no-headers -n ${env} | grep kafka))
$(eval ros_ocp_msg_ephemeral = '{\"request_id\": \"uuid1234\", \"b64_identity\": \"test\", \"metadata\": {\"org_id\": \"3340851\", \"source_id\": \"111\", \"cluster_uuid\": \"222\", \"cluster_alias\": \"name222\"}, \"files\": [\"$(SHAREURL)\"]}')
oc exec ${KAFKAPOD} -n ${env} -- /bin/bash -c "echo ${ros_ocp_msg_ephemeral} | /opt/kafka/bin/kafka-console-producer.sh --topic hccm.ros.events --broker-list localhost:9092"
else
@ echo "Env not defined"
endif
.PHONY: upload-msg-to-rosocp
upload-msg-to-rosocp:
echo ${ros_ocp_msg} | podman-compose -f scripts/docker-compose.yml exec -T kafka kafka-console-producer --topic hccm.ros.events --broker-list localhost:29092
.PHONY: upload-msg-to-rosocp-24Hrs
upload-msg-to-rosocp-24Hrs:
echo ${ros_ocp_msg_24Hrs} | podman-compose -f scripts/docker-compose.yml exec -T kafka kafka-console-producer --topic hccm.ros.events --broker-list localhost:29092
.PHONY: get-recommendations
get-recommendations:
ifdef env
$(eval APIPOD=$(shell oc get pods -o custom-columns=POD:.metadata.name --no-headers -n ${env} | grep ros-ocp-backend-api))
oc exec ${APIPOD} -c ros-ocp-backend-api -n ${env} -- /bin/bash -c 'curl -v -H "X-Rh-Identity: ${b64_identity}" -H "x-rh-request_id: testtesttest" http://localhost:8000/api/cost-management/v1/recommendations/openshift?start_date=${start_date}' | python -m json.tool
else
curl -v -H "x-rh-identity: ${b64_identity}" \
-H "x-rh-request_id: testtesttest" \
http://localhost:8000/api/cost-management/v1/recommendations/openshift?start_date=${start_date} | python -m json.tool
endif
.PHONY: oc-deploy-test
oc-deploy-test: ## Deploy ROS test environment with JWT authentication on OpenShift
@echo "π Deploying ROS test environment to OpenShift..."
@echo ""
IMAGE_TAG=$(VERSION) \
IMAGE_REGISTRY=$(REGISTRY) \
IMAGE_REPOSITORY=$(APP_NAME) \
./scripts/ocp/deploy-test-ros.sh