Repository navigation
Expand file tree
/
Copy pathMakefile
More file actions
259 lines (222 loc) · 11.4 KB
/
Copy pathMakefile
File metadata and controls
259 lines (222 loc) · 11.4 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
# RHDH Must-Gather Tool Makefile
# Variables
VERSION ?= 2.2.0
GIT_SHA := $(shell git describe --no-match --always --abbrev=9 --dirty --broken 2>/dev/null || echo unknown)
RHDH_MUST_GATHER_VERSION := $(VERSION)-$(GIT_SHA)
REGISTRY ?= quay.io
IMAGE_NAME ?= rhdh-community/rhdh-must-gather
IMAGE_TAG ?= latest
FULL_IMAGE_NAME ?= $(REGISTRY)/$(IMAGE_NAME):$(IMAGE_TAG)
LOG_LEVEL ?= info
OPTS ?= ## Additional options to pass to must-gather (e.g., --with-heap-dumps --with-secrets)
NAMESPACE ?= ## Namespace for deploy-k8s/deploy-openshift (default: random for k8s, auto for openshift)
HELM_SET ?= ## Additional Helm --set flags for deploy-k8s (e.g., "gather.logLevel=debug")
HELM_SET_STRING ?= ## Additional Helm --set-string flags for deploy-k8s (string values)
OUTPUT_FILE ?= ## Output file for deploy-k8s (default: rhdh-must-gather-output.k8s.<timestamp>.tar.gz)
HELM_TIMEOUT ?= ## Timeout for Helm install/upgrade in deploy-k8s (default: 60m)
CONTAINER_TOOL ?= podman
# renovate: datasource=docker depName=quay.io/konflux-ci/hermeto
HERMETO_IMAGE ?= quay.io/konflux-ci/hermeto:0.65.1
BUILD_ARGS ?=
LABELS ?=
RPM_LOCKFILE_CONTAINERFILE_PATH ?= .rhdh/docker/Containerfile
BASE_COLLECTION_PATH ?= ./out
# Tool Binaries
LOCALBIN ?= $(shell pwd)/bin
GOSEC ?= $(LOCALBIN)/gosec
RPM_LOCKFILE_PROTOTYPE ?= $(LOCALBIN)/rpm-lockfile-prototype
# Tool Versions
GOSEC_VERSION ?= v2.29.0
# renovate: datasource=github-releases depName=konflux-ci/rpm-lockfile-prototype
RPM_LOCKFILE_PROTOTYPE_VERSION ?= v0.30.1
# Gosec options - default format is sarif so we can integrate with Github code scanning
GOSEC_FMT ?= sarif # for other options, see https://github.com/securego/gosec#output-formats
GOSEC_OUTPUT_FILE ?= gosec.sarif
# Go configuration
GO ?= go
GO_MODULE := github.com/redhat-developer/rhdh-must-gather
GO_BUILD_FLAGS ?= -trimpath -mod=mod
MUST_GATHER_CLEAN_VERSION := $(shell $(GO) list -m -f '{{.Version}}' github.com/openshift/must-gather-clean 2>/dev/null || echo unknown)
GO_LDFLAGS := -X '$(GO_MODULE)/internal/cli.version=$(RHDH_MUST_GATHER_VERSION)' -X 'github.com/openshift/must-gather-clean/pkg/version.versionFromGit=$(MUST_GATHER_CLEAN_VERSION)'
default: build
##@ Development
.PHONY: local-output
local-output:
@mkdir -p ./out
.PHONY: regen-rpm-lockfile
regen-rpm-lockfile: $(RPM_LOCKFILE_PROTOTYPE) ## Regenerate rpms.lock.yaml from rpms.in.yaml
$(RPM_LOCKFILE_PROTOTYPE) -f $(RPM_LOCKFILE_CONTAINERFILE_PATH) rpms.in.yaml
.PHONY: run-local
run-local: local-output ## Run the Go gather binary locally (requires cluster access)
@echo "Running must-gather locally..."
BASE_COLLECTION_PATH=$(BASE_COLLECTION_PATH) \
LOG_LEVEL=$(LOG_LEVEL) \
RHDH_MUST_GATHER_VERSION=$(RHDH_MUST_GATHER_VERSION) \
$(GO) run $(GO_BUILD_FLAGS) -ldflags "$(GO_LDFLAGS)" ./cmd/gather $(OPTS)
LOCAL ?= true ## Set to 'false' to run E2E tests with container image instead of local mode
WITH_HEAP_DUMPS ?= ## Set to 'true' to enable heap dump collection and validation in E2E tests
HEAP_DUMP_METHOD ?= ## Heap dump method: 'inspector' (default) or 'sigusr2'
.PHONY: test-e2e
test-e2e: ## Run E2E tests against a K8s cluster (requires Kind or similar)
ifneq ($(LOCAL),false)
@echo "Running E2E tests in local mode..."
@./tests/e2e/run-e2e-tests.sh --local \
$(if $(TARGET_BRANCH),--target-branch "$(TARGET_BRANCH)") \
$(if $(OPERATOR_BRANCH),--operator-branch "$(OPERATOR_BRANCH)") \
$(if $(CHART_BRANCH),--chart-branch "$(CHART_BRANCH)") \
$(if $(HELM_VALUES_FILE),--helm-values-file "$(HELM_VALUES_FILE)") \
$(if $(filter true,$(WITH_HEAP_DUMPS)),--with-heap-dumps) \
$(if $(HEAP_DUMP_METHOD),--heap-dump-method "$(HEAP_DUMP_METHOD)") \
$(if $(HELM_TIMEOUT),--helm-timeout "$(HELM_TIMEOUT)")
else
@echo "Running E2E tests with image: $(FULL_IMAGE_NAME)..."
@./tests/e2e/run-e2e-tests.sh --image "$(FULL_IMAGE_NAME)" \
$(if $(TARGET_BRANCH),--target-branch "$(TARGET_BRANCH)") \
$(if $(OPERATOR_BRANCH),--operator-branch "$(OPERATOR_BRANCH)") \
$(if $(CHART_BRANCH),--chart-branch "$(CHART_BRANCH)") \
$(if $(HELM_VALUES_FILE),--helm-values-file "$(HELM_VALUES_FILE)") \
$(if $(filter true,$(WITH_HEAP_DUMPS)),--with-heap-dumps) \
$(if $(HEAP_DUMP_METHOD),--heap-dump-method "$(HEAP_DUMP_METHOD)") \
$(if $(HELM_TIMEOUT),--helm-timeout "$(HELM_TIMEOUT)")
endif
##@ Go
.PHONY: build
build: ## Build the Go gather binary
$(GO) build $(GO_BUILD_FLAGS) -ldflags "$(GO_LDFLAGS)" -o gather ./cmd/gather
.PHONY: test
test: ## Run unit tests
$(GO) test -mod=mod -race ./... -v -count=1 -coverprofile=cover.out
.PHONY: lint
lint: ## Run linter (golangci-lint)
golangci-lint run ./...
.PHONY: gosec
gosec: addgosec ## Run the gosec scanner for non-test files in this repo
# we let the report content trigger a failure using the GitHub Security features.
$(GOSEC) -no-fail -exclude-generated -fmt $(GOSEC_FMT) -out $(GOSEC_OUTPUT_FILE) ./...
##@ Build
.PHONY: image-build
image-build: ## Build the must-gather container image
@echo "Building must-gather image..."
$(CONTAINER_TOOL) build $(BUILD_ARGS) $(if $(LABELS),$(LABELS)) --build-arg RHDH_MUST_GATHER_VERSION=$(RHDH_MUST_GATHER_VERSION) -t $(IMAGE_NAME):$(IMAGE_TAG) .
@echo "Image built: $(IMAGE_NAME):$(IMAGE_TAG)"
.PHONY: hermetic-build
hermetic-build: ## Build must-gather image hermetically using Hermeto (matches Konflux/CI)
HERMETO_IMAGE="$(HERMETO_IMAGE)" hack/local-hermeto-build.sh -d . -i $(FULL_IMAGE_NAME) --version "$(RHDH_MUST_GATHER_VERSION)"
.PHONY: image-push
image-push: image-build ## Build and push the image to registry
@echo "Tagging image for registry..."
$(CONTAINER_TOOL) tag $(IMAGE_NAME):$(IMAGE_TAG) $(FULL_IMAGE_NAME)
@echo "Pushing image to registry..."
$(CONTAINER_TOOL) push $(FULL_IMAGE_NAME)
@echo "Image pushed: $(FULL_IMAGE_NAME)"
##@ Deployment
.PHONY: deploy-openshift
deploy-openshift: ## Deploy the must-gather image using the 'oc adm must-gather' command
@echo "Deploying the must-gather image with oc adm must-gather..."
@if ! command -v oc >/dev/null 2>&1; then \
echo "Error: oc command not found. Please install OpenShift CLI."; \
exit 1; \
fi
oc adm must-gather --image=$(FULL_IMAGE_NAME) $(if $(NAMESPACE),--run-namespace=$(NAMESPACE)) $(if $(OPTS),-- /usr/bin/gather $(OPTS))
.PHONY: deploy-k8s
deploy-k8s: ## Deploy the must-gather image on a non-OCP K8s cluster (uses Helm chart)
@if ! command -v kubectl >/dev/null 2>&1; then \
echo "Error: kubectl command not found. Please install kubectl."; \
exit 1; \
fi
@if ! command -v helm >/dev/null 2>&1; then \
echo "Error: helm command not found. Please install Helm."; \
exit 1; \
fi
@./hack/deploy-k8s.sh --image "$(FULL_IMAGE_NAME)" $(if $(NAMESPACE),--namespace "$(NAMESPACE)") $(if $(OPTS),--opts "$(OPTS)") $(if $(HELM_SET),--helm-set "$(HELM_SET)") $(if $(HELM_SET_STRING),--helm-set-string "$(HELM_SET_STRING)") $(if $(OUTPUT_FILE),--output "$(OUTPUT_FILE)") $(if $(HELM_TIMEOUT),--timeout "$(HELM_TIMEOUT)") $(DEPLOY_K8S_EXTRA_OPTS)
##@ Cleanup
.PHONY: clean-out
clean-out: ## Remove the local output directory
-rm -rf ./out
@echo "Local output directory cleaned"
.PHONY: clean
clean: clean-out ## Remove built images, binary, tools, and test output
@echo "Cleaning up..."
-rm -f gather
-rm -rf "$(LOCALBIN)"
-podman rmi $(IMAGE_NAME):$(IMAGE_TAG) 2>/dev/null || true
-podman rmi $(FULL_IMAGE_NAME) 2>/dev/null || true
-rm -rf "$(TEST_RESULTS_DIR)"
@echo "Cleanup complete"
##@ Dependencies
.PHONY: addgosec
addgosec: $(GOSEC) ## Download gosec locally if necessary.
$(GOSEC): $(LOCALBIN)
$(call go-install-tool,$(GOSEC),github.com/securego/gosec/v2/cmd/gosec,$(GOSEC_VERSION))
.PHONY: addrpmlockfileprototype
addrpmlockfileprototype: $(RPM_LOCKFILE_PROTOTYPE) ## Download rpm-lockfile-prototype locally if necessary.
$(RPM_LOCKFILE_PROTOTYPE): $(LOCALBIN)
$(call pip-install-tool,$(RPM_LOCKFILE_PROTOTYPE),konflux-ci/rpm-lockfile-prototype,$(RPM_LOCKFILE_PROTOTYPE_VERSION))
$(LOCALBIN):
mkdir -p $(LOCALBIN)
# go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist
# $1 - target path with name of binary (ideally with version)
# $2 - package url which can be installed
# $3 - specific version of package
define go-install-tool
@[ -f "$(1)-$(3)" ] || { \
set -e; \
package=$(2)@$(3) ;\
echo "Downloading $${package}" ;\
rm -f $(1) || true ;\
GOBIN=$(LOCALBIN) go install $${package} ;\
mv $(1) $(1)-$(3) ;\
} ;\
ln -sf $(1)-$(3) $(1)
endef
# pip-install-tool will install a Python package from a GitHub release into a local venv, if it doesn't exist
# $1 - target path with name of binary
# $2 - GitHub org/repo
# $3 - version tag
define pip-install-tool
@venv_dir="$(1)-venv-$(3)"; \
[ -d "$${venv_dir}" ] || { \
set -e; \
echo "Downloading $(2)@$(3)" ;\
rm -rf $(1)-venv-* ;\
python3 -m venv "$${venv_dir}" ;\
"$${venv_dir}/bin/pip" install --quiet "https://github.com/$(2)/archive/refs/tags/$(3).zip" ;\
sed -i 's/include-system-site-packages = false/include-system-site-packages = true/' "$${venv_dir}/pyvenv.cfg" ;\
} ;\
ln -sf "$$(basename $${venv_dir})/bin/$(notdir $(1))" "$(1)"
endef
##@ General
.PHONY: help
help: ## Display this help.
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
@echo ""
@echo "Variables:"
@echo " VERSION - Must-gather version (default: $(VERSION))"
@echo " RHDH_MUST_GATHER_VERSION - Full version with git SHA (computed: $(RHDH_MUST_GATHER_VERSION))"
@echo " REGISTRY - Container registry (default: $(REGISTRY))"
@echo " IMAGE_NAME - Container image name (default: $(IMAGE_NAME))"
@echo " IMAGE_TAG - Container image tag (default: $(IMAGE_TAG))"
@echo " LOG_LEVEL - Log level (default: $(LOG_LEVEL))"
@echo " OPTS - Additional must-gather options (e.g., --with-heap-dumps --with-secrets)"
@echo " NAMESPACE - Namespace for deploy-k8s/deploy-openshift (default: random for k8s, auto for openshift)"
@echo " HELM_SET - Additional Helm --set flags for deploy-k8s (e.g., \"gather.logLevel=debug\")"
@echo " OUTPUT_FILE - Output file for deploy-k8s (default: rhdh-must-gather-output.k8s.<timestamp>.tar.gz)"
@echo " HELM_TIMEOUT - Timeout for Helm install/upgrade in deploy-k8s (default: 60m)"
@echo " TARGET_BRANCH - Target branch for test-e2e defaults (default: main)"
@echo " OPERATOR_BRANCH - Override RHDH operator branch for test-e2e"
@echo " CHART_BRANCH - Override RHDH Helm chart branch for test-e2e"
@echo " HELM_VALUES_FILE - Override Helm values file for test-e2e"
@echo " LOCAL - Set to 'false' to run test-e2e with container image (default: true, local mode)"
@echo ""
@echo "Examples:"
@echo " make test # Run all unit tests"
@echo " make test-e2e # Run E2E tests in local mode (default)"
@echo " make test-e2e LOCAL=false FULL_IMAGE_NAME=quay.io/org/img:tag # Run E2E tests with container image"
@echo " make deploy-k8s OPTS=\"--with-heap-dumps\" # Run deploy-k8s with heap dumps"
@echo " make deploy-k8s NAMESPACE=my-ns # Run deploy-k8s in a specific namespace"
@echo " make run-local OPTS=\"--with-heap-dumps\""
@echo " make deploy-openshift OPTS=\"--with-heap-dumps --namespaces my-ns\""
@echo " make regen-rpm-lockfile # Regenerate rpms.lock.yaml"
@echo ""
@echo "Prerequisites for regen-rpm-lockfile:"
@echo " System packages: python3, python3-pip, python3-dnf, skopeo, rpm"