diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index f6e9e1ab..62e01657 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -15,6 +15,7 @@ By participating in this project, you agree to abide by our [Code of Conduct](.g - [Commit Message Guidelines](#commit-message-guidelines) - [Testing](#testing) - [Linting](#linting) +- [Pre-Commit Hooks](#pre-commit-hooks) - [Documentation](#documentation) - [Component-Specific Guidelines](#component-specific-guidelines) - [Backporting Changes](#backporting-changes) @@ -38,19 +39,19 @@ If you discover a bug, have a feature request, or need to provide feedback, plea When you're ready to contribute code: -1. **Fork and Clone** +1. **Fork and Clone** Fork the repository and clone it to your local machine. -2. **Create a Branch** +2. **Create a Branch** Create a feature branch from the appropriate base branch (typically `main`). -3. **Make Changes** +3. **Make Changes** Implement your changes, ensuring they adhere to the project's coding standards. -4. **Run Tests** +4. **Run Tests** Verify that all tests pass locally. Refer to the testing section below for more details. -5. **Update Documentation** +5. **Update Documentation** Update or add documentation as needed in the corresponding directories. -6. **Commit and Push** +6. **Commit and Push** Write clear commit messages that reference the related issue (e.g., "fix: Description of fix"). -7. **Open a Pull Request** +7. **Open a Pull Request** Submit a pull request (PR) with a detailed description of your changes and link any related issues. --- @@ -106,6 +107,35 @@ Linting rules are configured in `.golangci.yml` at the repository root. The conf --- +## Pre-Commit Hooks + +We use pre-commit hooks to catch issues before code is committed. +Local hooks are optional; by default nothing runs on commit unless you install hooks yourself. + +### Quick setup + +Run: `make pre-commit-setup` (bootstraps `.venv` and installs Go tools into `.tools/bin`). + +### Run hooks manually + +Run: `make pre-commit`. + +### Optional: enable pre-commit on every commit + +Run: `make pre-commit-install`. + +### Troubleshooting + +If setup fails, you can run the steps individually: + +- Run: `make pre-commit-install-tool`. +- Run: `make pre-commit-tools`. +- Run: `make pre-commit-install`. + + + +--- + ## Documentation Contributions to documentation are highly valued. If you find any documentation gaps or errors: diff --git a/.github/workflows/pre-commit.yml b/.github/workflows/pre-commit.yml new file mode 100644 index 00000000..8f6b4c89 --- /dev/null +++ b/.github/workflows/pre-commit.yml @@ -0,0 +1,46 @@ +name: pre-commit + +on: + push: + branches: + - main + pull_request: + merge_group: + workflow_dispatch: + +permissions: + contents: read + +jobs: + pre-commit: + name: pre-commit + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: "3.12" + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version-file: "go.mod" + cache: true + + - name: Add Go bin to PATH + run: echo "${{ github.workspace }}/.tools/bin" >> $GITHUB_PATH + + - name: Install pre-commit + run: python -m pip install --upgrade pip pre-commit + + - name: Install Go tools + run: | + mkdir -p .tools/bin + GOBIN="${{ github.workspace }}/.tools/bin" go install golang.org/x/tools/cmd/goimports@v0.30.0 + GOBIN="${{ github.workspace }}/.tools/bin" go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.62.2 + + - name: Run pre-commit + run: pre-commit run --all-files --show-diff-on-failure diff --git a/.gitignore b/.gitignore index c151c339..28ca7beb 100644 --- a/.gitignore +++ b/.gitignore @@ -29,6 +29,8 @@ config/**/charts *~ .claude +.venv +.tools /zxporter-netmon *.o diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 00000000..32c29529 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,32 @@ +default_stages: + - pre-commit + +repos: + - repo: local + hooks: + - id: gofmt + name: gofmt + entry: gofmt -w + language: system + types: [go] + - id: goimports + name: goimports + entry: ./.tools/bin/goimports -w + language: system + types: [go] + - id: golangci-lint + name: golangci-lint + entry: ./.tools/bin/golangci-lint run --timeout=5m + language: system + pass_filenames: false + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.6.0 + hooks: + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-json + - id: check-yaml + args: ["--allow-multiple-documents"] + exclude: ^helm-chart/.*/templates/ + - id: check-added-large-files + args: ["--maxkb=1024"] diff --git a/Makefile b/Makefile index a78ef5a8..523b206a 100644 --- a/Makefile +++ b/Makefile @@ -60,7 +60,7 @@ DAKR_URL ?= https://dakr.devzero.io # PROMETHEUS URL for metrics collection PROMETHEUS_URL ?= http://prometheus-dz-prometheus-server.$(DEVZERO_MONITORING_NAMESPACE).svc.cluster.local:80 # TARGET_NAMESPACES for limiting collection to specific namespaces (comma-separated) -TARGET_NAMESPACES ?= +TARGET_NAMESPACES ?= # COLLECTION_FILE is used to control the collectionpolicies. COLLECTION_FILE ?= env_configmap.yaml # ENV_CONFIGMAP_FILE is used to control the zxporter-manager deployment. @@ -162,6 +162,32 @@ lint: golangci-lint ## Run golangci-lint linter lint-fix: golangci-lint ## Run golangci-lint linter and perform fixes $(GOLANGCI_LINT) run --fix +PRE_COMMIT ?= .venv/bin/pre-commit + +.venv/bin/pre-commit: + python3 -m venv .venv + .venv/bin/pip install --upgrade pip pre-commit + +.PHONY: pre-commit-install +pre-commit-install: .venv/bin/pre-commit ## Install pre-commit hooks (optional). + $(PRE_COMMIT) install --install-hooks + +.PHONY: pre-commit +pre-commit: pre-commit-tools .venv/bin/pre-commit ## Run pre-commit on all files. + PATH="$(PWD)/.tools/bin:$$PATH" $(PRE_COMMIT) run --all-files + +.PHONY: pre-commit-tools +pre-commit-tools: ## Install Go tools needed by pre-commit. + @mkdir -p .tools/bin + GOBIN="$(PWD)/.tools/bin" go install golang.org/x/tools/cmd/goimports@v0.30.0 + GOBIN="$(PWD)/.tools/bin" go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.62.2 + +.PHONY: pre-commit-install-tool +pre-commit-install-tool: .venv/bin/pre-commit ## Install pre-commit into a local virtualenv. + +.PHONY: pre-commit-setup +pre-commit-setup: pre-commit-install-tool pre-commit-tools ## Install pre-commit and tools (hooks are optional). + GITVERSION ?= $(shell git describe --tags 2>/dev/null || echo "v0.0.0-$(shell git rev-parse --short HEAD)") ifeq ($(shell echo $(GITVERSION) | grep -E "^v[0-9]+\.[0-9]+\.[0-9]+"),) @@ -354,7 +380,7 @@ build-installer: manifests generate kustomize yq ## Generate a consolidated YAML # @cat $(METRICS_SERVER) >> $(DIST_INSTALL_BUNDLE) # @echo "# ----- END METRICS SERVER -----" >> $(DIST_INSTALL_BUNDLE) @echo "---" >> $(DIST_INSTALL_BUNDLE) - + @echo "[INFO] Append zxporter-manager to the installer bundle" @cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG} @@ -396,11 +422,11 @@ HELM_CHART_PACKAGE_DIR := helm-chart/packages helm-chart-build: helm ## Build and package the Helm chart @echo "[INFO] Building Helm chart..." @mkdir -p $(HELM_CHART_PACKAGE_DIR) - + # Update chart version and appVersion @$(YQ) eval '.version = "$(VERSION)"' -i $(HELM_CHART_DIR)/Chart.yaml @$(YQ) eval '.appVersion = "$(VERSION)"' -i $(HELM_CHART_DIR)/Chart.yaml - + # Package the chart @$(HELM) package $(HELM_CHART_DIR) --destination $(HELM_CHART_PACKAGE_DIR) @echo "[INFO] Helm chart packaged successfully" @@ -681,7 +707,7 @@ install-buf: ## Install buf if not already installed echo "$(BUF_BINARY_NAME) is already installed."; \ fi -# (for local dev) Get metadata to generate a Dakr client. +# (for local dev) Get metadata to generate a Dakr client. .PHONY: generate-proto generate-proto: install-buf ## Fetch latest Dakr protobuf @PROTO_DIR="$(PWD)/proto"; \ @@ -693,7 +719,7 @@ generate-proto: install-buf ## Fetch latest Dakr protobuf cp "$(DAKR_MPA_PROTO)" "$$PROTO_DIR/api/v1"; \ find "$$PROTO_DIR" -type f -name "*.yaml" -exec perl -pi -e 's|github.com/devzero-inc/services/dakr/gen|github.com/devzero-inc/zxporter/gen|g' {} +; \ buf build "$(DAKR_DIR)" --path "$(DAKR_METRICS_COLLECTOR_PROTO)" --path "$(DAKR_CLUSTER_PROTO)" -o "$$PROTO_DIR"/dakr_proto_descriptor.bin; \ - buf generate --include-imports "$$PROTO_DIR"/dakr_proto_descriptor.bin; + buf generate --include-imports "$$PROTO_DIR"/dakr_proto_descriptor.bin; buf generate --verbose --include-imports --timeout=5m . # Lima Verification @@ -714,7 +740,7 @@ verify-local: setup-lima @chmod +x verification/verify_local.sh @./verification/verify_local.sh $(LIMA_INSTANCE) -# for local, this will be something like: +# for local, this will be something like: # make verify-e2e CLUSTER_CONTEXT=kind-zxporter-e2e NAMESPACE=devzero-zxporter .PHONY: verify-e2e verify-e2e: ## Run E2E verification on a Kind or Cloud cluster