-
Notifications
You must be signed in to change notification settings - Fork 114
Integrate schema change detection and add some helpers for registering the conversion functions automatically #567
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
Open
sergenyalcin
wants to merge
5
commits into
crossplane:main
Choose a base branch
from
sergenyalcin:integrate-breaking-change-detection
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d1a06f9
Integrate crddiff tool
sergenyalcin 61a5f9a
Add unit and integration tests for conversion registration and core c…
sergenyalcin 95aaf14
Add a detailed documentation and guide
sergenyalcin a28531e
Add missing license files and statements
sergenyalcin 538dd34
Use official-providers-ci version instead of fork
sergenyalcin 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,128 @@ | ||
| // SPDX-FileCopyrightText: 2025 The Crossplane Authors <https://crossplane.io> | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| "github.com/alecthomas/kingpin/v2" | ||
| "github.com/upbound/uptest/pkg/crdschema" | ||
| ) | ||
|
|
||
| var ( | ||
| app = kingpin.New(filepath.Base(os.Args[0]), "CRD Schema Diff JSON File Generator").DefaultEnvars() | ||
| ) | ||
|
|
||
| var ( | ||
| crdDir = app.Flag("crd-dir", "The directory of base CRDs").Short('i').Default("./package/crds").ExistingDir() | ||
| out = app.Flag("out", "Filename for JSON output").Short('o').Default("./config/crd-schema-changes.json").String() | ||
| ) | ||
|
|
||
| // main is the entry point for the schemadiff tool. | ||
| // It processes all CRD files in the specified directory, detects schema changes | ||
| // between API versions, and outputs a JSON report. | ||
| func main() { //nolint:gocyclo // easier to follow as a unit | ||
| kingpin.MustParse(app.Parse(os.Args[1:])) | ||
| if crdDir == nil || *crdDir == "" { | ||
| kingpin.Fatalf("base CRDs directory required") | ||
| } | ||
| if out == nil || *out == "" { | ||
| kingpin.Fatalf("output directory file") | ||
| } | ||
|
|
||
| // List all YAML/YML files in the CRD directory | ||
| crdFilePaths, err := listYAMLFiles(*crdDir) | ||
| if err != nil { | ||
| kingpin.FatalIfError(err, "cannot read CRD files") | ||
| } | ||
|
|
||
| // Configure the schema diff engine | ||
| // EnableUpjetExtensions=false means we only analyze standard Kubernetes CRD schemas | ||
| // without considering upjet-specific extensions (x-kubernetes-* annotations, etc.) | ||
| opts := &crdschema.CommonOptions{ | ||
| EnableUpjetExtensions: false, | ||
| } | ||
|
|
||
| // jsonData will hold all change reports, keyed by "{group}/{kind}" | ||
| // Example key: "ec2.aws.upbound.io/VPC" | ||
| jsonData := map[string]*crdschema.ChangeReport{} | ||
|
|
||
| // Process each CRD file | ||
| for _, cfp := range crdFilePaths { | ||
| // Create a self-diff analyzer for this CRD | ||
| // "Self-diff" means comparing different versions within the same CRD file | ||
| // (e.g., v1beta1 vs v1beta2 in the same CRD) | ||
| sd, err := crdschema.NewSelfDiff(cfp, crdschema.WithSelfDiffCommonOptions(opts)) | ||
| if err != nil { | ||
| kingpin.FatalIfError(err, "cannot create self diff object") | ||
| } | ||
|
|
||
| // Get the raw diff data comparing all version pairs in this CRD | ||
| rawDiff, err := sd.GetRawDiff() | ||
| if err != nil { | ||
| kingpin.FatalIfError(err, "cannot get raw diff object") | ||
| } | ||
|
|
||
| // Convert the raw diff into a structured change report | ||
| // The second parameter (true) indicates we want full change details | ||
| changeReport, err := crdschema.GetChangesAsStructured(rawDiff, true) | ||
| if err != nil { | ||
| kingpin.FatalIfError(err, "cannot get changes as structured diff") | ||
| } | ||
|
|
||
| // Skip CRDs with no changes (all versions are identical) | ||
| if changeReport.Empty() { | ||
| continue | ||
| } | ||
|
|
||
| // Add this CRD's change report to the output map | ||
| // Key format: "{group}/{kind}" matches what conversion.go expects | ||
| crdSpec := sd.GetCRD().Spec | ||
| jsonData[fmt.Sprintf("%s/%s", crdSpec.Group, crdSpec.Names.Kind)] = changeReport | ||
| } | ||
|
|
||
| // Marshal the complete change report map to JSON | ||
| jsonContent, err := json.Marshal(jsonData) | ||
| if err != nil { | ||
| kingpin.FatalIfError(err, "cannot marshal change report") | ||
| } | ||
|
|
||
| // Write the JSON to the output file | ||
| // 0600 permissions = owner read/write only (secure default) | ||
| kingpin.FatalIfError(os.WriteFile(*out, jsonContent, 0600), "cannot write data to json file") | ||
| } | ||
|
|
||
| // listYAMLFiles returns paths to all YAML files in the specified directory. | ||
| // It only processes files (not subdirectories) with .yaml or .yml extensions. | ||
| func listYAMLFiles(dir string) ([]string, error) { | ||
| entries, err := os.ReadDir(dir) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| paths := make([]string, 0, len(entries)) | ||
|
|
||
| for _, entry := range entries { | ||
| // Skip subdirectories - only process files in the top-level directory | ||
| if entry.IsDir() { | ||
| continue | ||
| } | ||
|
|
||
| // Check file extension (case-insensitive) | ||
| ext := strings.ToLower(filepath.Ext(entry.Name())) | ||
| if ext != ".yaml" && ext != ".yml" { | ||
| continue | ||
| } | ||
|
|
||
| // Add the full path to the result list | ||
| paths = append(paths, filepath.Join(dir, entry.Name())) | ||
| } | ||
|
|
||
| return paths, nil | ||
| } | ||
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
Oops, something went wrong.
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.
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.
Fix incomplete error message.
The error message is grammatically incomplete - it should describe what is required.
Proposed fix
if out == nil || *out == "" { - kingpin.Fatalf("output directory file") + kingpin.Fatalf("output file required") }📝 Committable suggestion
🤖 Prompt for AI Agents