-
Notifications
You must be signed in to change notification settings - Fork 2.8k
chore(tools): CRD generation script moved and add validation workflow #6079
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
8bbb9b3
chore(tools): refactor CRD generation and add validation workflow
ivankatliarchuk 0476f43
chore(tools): refactor CRD generation and add validation workflow
ivankatliarchuk 4bba7c4
chore(tools): refactor CRD generation and add validation workflow
ivankatliarchuk 49be876
chore(tools): refactor CRD generation and add validation workflow
ivankatliarchuk 8310685
chore(tools): refactor CRD generation and add validation workflow
ivankatliarchuk ef46299
chore(tools): refactor CRD generation and add validation workflow
ivankatliarchuk 06d23d1
chore(tools): refactor CRD generation and add validation workflow
ivankatliarchuk c909bda
Merge branch 'master' into chore-tools
ivankatliarchuk a8ab7de
chore(tools): refactor CRD generation and add validation workflow
ivankatliarchuk 8123747
chore(tools): refactor CRD generation and add validation workflow
ivankatliarchuk fce2464
chore(tools): refactor CRD generation and add validation workflow
ivankatliarchuk 11324b1
chore(tools): refactor CRD generation and add validation workflow
ivankatliarchuk 63b3b03
chore(tools): refactor CRD generation and add validation workflow
ivankatliarchuk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| name: Validate CRD Generation | ||
|
|
||
| # This workflow validates that generated CRD files are up-to-date when tool | ||
| # dependencies change. It ensures that if go.tool.mod or go.tool.sum are updated, | ||
| # the corresponding generated files (CRDs and deepcopy code) are also regenerated | ||
| # and committed in the same PR. | ||
| # | ||
| # Why this is needed: | ||
| # - controller-gen (from go.tool.mod) generates CRD YAML and deepcopy Go code | ||
| # - Different versions of controller-gen may produce different output | ||
| # - When tool versions change, generated code must be regenerated | ||
| # - This prevents CI failures and runtime issues from stale generated code | ||
|
|
||
| on: | ||
| pull_request: | ||
| paths: | ||
| - 'go.tool.mod' | ||
| - 'go.tool.sum' | ||
| - 'scripts/generate-crd.sh' | ||
| - '**/dnsendpoints.externaldns.k8s.io.yaml' | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| validate-crd: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Checkout repository | ||
| uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1 | ||
|
|
||
| - name: Set up Go | ||
| uses: actions/setup-go@41dfa10bad2bb2ae585af6ee5bb4d7d973ad74ed # v5.1.0 | ||
| with: | ||
| go-version-file: 'go.mod' | ||
|
|
||
| - name: Regenerate CRDs | ||
| run: ./scripts/generate-crd.sh | ||
|
|
||
| - name: Check for uncommitted changes | ||
| id: check_changes | ||
| run: | | ||
| # Check if there are any changes to generated files | ||
| if ! git diff --quiet; then | ||
| echo "::error::Generated CRD files are out of sync with go.tool.mod" | ||
| echo "" | ||
| echo "The following files have uncommitted changes after running 'make crd':" | ||
| git diff . | ||
| echo "" | ||
| echo "This usually means:" | ||
| echo "1. go.tool.mod or go.tool.sum was updated (new controller-gen version)" | ||
| echo "2. The generated CRD files were not regenerated" | ||
| echo "" | ||
| echo "To fix this:" | ||
| echo " make crd" | ||
| echo " git diff ." | ||
| echo " commit, push and update your PR:" | ||
| exit 1 | ||
| fi | ||
|
|
||
| - name: Success | ||
| if: success() | ||
| run: | | ||
| echo "✅ Generated CRD files are up-to-date" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -16,10 +16,6 @@ | |||||||||
| .PHONY: cover cover-html | ||||||||||
| .DEFAULT_GOAL := build | ||||||||||
|
|
||||||||||
| CONTROLLER_GEN := go tool -modfile=go.tool.mod controller-gen | ||||||||||
| YQ := go tool -modfile=go.tool.mod yq | ||||||||||
| YAMLFMT := go tool -modfile=go.tool.mod yamlfmt | ||||||||||
|
|
||||||||||
| cover: | ||||||||||
| @go test -cover -coverprofile=cover.out -v ./... | ||||||||||
|
|
||||||||||
|
|
@@ -64,13 +60,12 @@ lint: licensecheck go-lint | |||||||||
| #? crd: Generates CRD using controller-gen and copy it into chart | ||||||||||
| .PHONY: crd | ||||||||||
| crd: | ||||||||||
| $(CONTROLLER_GEN) object crd:crdVersions=v1 paths="./endpoint/..." | ||||||||||
| $(CONTROLLER_GEN) object crd:crdVersions=v1 paths="./apis/..." output:crd:stdout | \ | ||||||||||
| $(YAMLFMT) - | \ | ||||||||||
| $(YQ) eval '.' --no-doc --split-exp '"./config/crd/standard/" + .metadata.name + ".yaml"' | ||||||||||
| $(YQ) eval '.metadata.annotations |= with_entries(select(.key | test("kubernetes\.io")))' \ | ||||||||||
| --no-doc --split-exp '"./charts/external-dns/crds/" + .metadata.name + ".yaml"' \ | ||||||||||
| ./config/crd/standard/*.yaml | ||||||||||
| @./scripts/generate-crd.sh | ||||||||||
|
|
||||||||||
| # Required while depedabot is not supporting go.tool.mod https://github.com/dependabot/dependabot-core/issues/12050 | ||||||||||
| #? tools-deps: Update go tools defined in go.tool.mod to latest versions | ||||||||||
| tools-deps: | ||||||||||
|
||||||||||
| tools-deps: | |
| update-tools-deps: |
or
Suggested change
| tools-deps: | |
| update-tools: |
To clarify what it does?
Member
Author
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| module sigs.k8s.io/external-dns/tools | ||
|
|
||
| go 1.25 | ||
| go 1.25.0 | ||
|
|
||
| tool ( | ||
| github.com/google/yamlfmt/cmd/yamlfmt | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| #!/usr/bin/env bash | ||
| # Copyright 2026 The Kubernetes Authors. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| # generate-crd.sh | ||
| # | ||
| # This script generates Kubernetes Custom Resource Definitions (CRDs) and related | ||
| # deepcopy code for external-dns using controller-gen from controller-tools. | ||
| # | ||
| # What this script does: | ||
| # 1. Generates DeepCopy methods for types in the endpoint package | ||
| # - Creates zz_generated.deepcopy.go files with DeepCopy, DeepCopyInto methods | ||
| # - Required for Kubernetes runtime.Object interface implementation | ||
| # | ||
| # 2. Generates CRD manifests for API types in the apis package | ||
| # - Creates CRD YAML files in config/crd/standard/ | ||
| # - Formats them using yamlfmt for consistency | ||
| # - Extracts individual CRD files using yq based on metadata.name | ||
| # | ||
| # 3. Copies CRDs to the Helm chart directory | ||
| # - Filters metadata.annotations to only include kubernetes.io/* annotations | ||
| # - Outputs to charts/external-dns/crds/ for Helm chart packaging | ||
| # | ||
| # Dependencies (from go.tool.mod): | ||
| # - sigs.k8s.io/controller-tools/cmd/controller-gen - CRD and deepcopy generation | ||
| # - github.com/google/yamlfmt/cmd/yamlfmt - YAML formatting | ||
| # - github.com/mikefarah/yq/v4 - YAML processing and splitting | ||
ivankatliarchuk marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # | ||
| # Usage: | ||
| # ./scripts/generate-crd.sh | ||
| # make crd # calls this script | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| # Get the script directory | ||
| SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | ||
| # Get the project root (parent of scripts directory) | ||
| PROJECT_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)" | ||
|
|
||
| cd "${PROJECT_ROOT}" | ||
|
|
||
| # Define tool commands (using tools from go.tool.mod) | ||
| CONTROLLER_GEN="go tool -modfile=go.tool.mod controller-gen" | ||
| YQ="go tool -modfile=go.tool.mod yq" | ||
| YAMLFMT="go tool -modfile=go.tool.mod yamlfmt" | ||
|
|
||
| echo " Generating CRDs using controller-gen..." | ||
|
|
||
| # Step 1: Generate deepcopy methods for endpoint types | ||
| # This creates zz_generated.deepcopy.go with DeepCopy/DeepCopyInto/DeepCopyObject methods | ||
| # The 'object' generator adds these methods for types marked with +kubebuilder:object markers | ||
| echo " → Generating deepcopy for endpoint package..." | ||
| ${CONTROLLER_GEN} object crd:crdVersions=v1 paths="./endpoint/..." | ||
|
|
||
| # Clean up empty import statements from generated files | ||
| # controller-gen sometimes adds empty import() blocks which create noise in diffs | ||
| find ./endpoint -name "zz_generated.deepcopy.go" -exec gofmt -s -w {} \; | ||
|
|
||
| # Step 2: Generate CRD manifests for API types | ||
| # - Generates CRDs from Go types with kubebuilder markers | ||
| # - Outputs to stdout, formats with yamlfmt, then splits into individual files | ||
| # - Each CRD is saved to config/crd/standard/<crd-name>.yaml | ||
| echo " → Generating CRDs for apis package..." | ||
| ${CONTROLLER_GEN} object crd:crdVersions=v1 paths="./apis/..." output:crd:stdout | \ | ||
| ${YAMLFMT} - | \ | ||
| ${YQ} eval '.' --no-doc --split-exp '"./config/crd/standard/" + .metadata.name + ".yaml"' | ||
|
|
||
| # Clean up empty import statements from generated files | ||
| find ./apis -name "zz_generated.deepcopy.go" -exec gofmt -s -w {} \; | ||
|
|
||
| # Step 3: Copy CRDs to Helm chart with filtered annotations | ||
| # - Reads CRDs from config/crd/standard/ | ||
| # - Filters annotations to only keep kubernetes.io/* (removes controller-gen annotations) | ||
| # - Splits and saves to charts/external-dns/crds/ for Helm chart packaging | ||
| echo " → Copying CRDs to chart directory..." | ||
| ${YQ} eval '.metadata.annotations |= with_entries(select(.key | test("kubernetes\.io")))' \ | ||
| --no-doc --split-exp '"./charts/external-dns/crds/" + .metadata.name + ".yaml"' \ | ||
| ./config/crd/standard/*.yaml | ||
|
|
||
| echo -e " ✅ CRD generation complete" | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.

Uh oh!
There was an error while loading. Please reload this page.