Skip to content

Conversation

@a-hilaly
Copy link
Member

@a-hilaly a-hilaly commented Mar 5, 2025

ResourceGraphDefinitions generate CRDs on the fly, which means one wrong
modification in the schema can break existing resources/crds. e.g deleting a required
field or changing types can be problematic.

This patch introduces a DiffSchema function that systematically compares
old and new schemas, identifying breaking changes like property removals,
type modifications, and constraint updates. It categorizes changes as
breaking or non breaking, allowing safer schema evolution while preventing
accidental resource invalidation.

resolves #186

`ResourceGraphDefinitions` generate CRDs on the fly, which means one wrong
move in the schema can break existing resources. Deleting a critical field
or switching types can cause immediate deployment failures.

This patch introduces a `DiffSchema` function that systematically compares
old and new schemas, identifying breaking changes like property removals,
type modifications, and constraint updates. It categorizes changes as
breaking or non breaking, allowing safer schema evolution while preventing
accidental resource invalidation.
Copy link
Contributor

@michaelhtm michaelhtm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job on this @a-hilaly!! 🚀🚀🚀
I left a few inline comments

Comment on lines +124 to +128
// Check for breaking changes
if diffResult.HasBreakingChanges() {
w.log.Info("Breaking changes detected in CRD update", "name", desired.Name, "breakingChanges", len(diffResult.BreakingChanges), "summary", diffResult)
return fmt.Errorf("cannot update CRD %s: breaking changes detected: %s", desired.Name, diffResult)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could there be a way to override this? if someone doesn't care about the breaking changes

Comment on lines +25 to +37
// Breaking change types
PropertyRemoved ChangeType = "PROPERTY_REMOVED"
TypeChanged ChangeType = "TYPE_CHANGED"
RequiredAdded ChangeType = "REQUIRED_ADDED"
EnumRestricted ChangeType = "ENUM_RESTRICTED"
PatternChanged ChangeType = "PATTERN_CHANGED"

// Non-breaking change types
PropertyAdded ChangeType = "PROPERTY_ADDED"
DescriptionChanged ChangeType = "DESCRIPTION_CHANGED"
DefaultChanged ChangeType = "DEFAULT_CHANGED"
RequiredRemoved ChangeType = "REQUIRED_REMOVED"
EnumExpanded ChangeType = "ENUM_EXPANDED"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can add number changes as well..I saw a PR approved for adding maximum and minimum to the schema. We should also track those changes..

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are those considered breaking changes? should we really inspect the defaults/max/min and basically every attribute in https://kro.run/docs/concepts/simple-schema#supported-markers ? cc @jakobmoellerdev

Comment on lines +82 to +86
if i >= maxBreakingChangesSummary {
remaining := len(r.BreakingChanges) - i
if remaining > 0 {
changeDescs = append(changeDescs, fmt.Sprintf("and %d more changes", remaining))
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

@a-hilaly a-hilaly requested review from justinsb and matthchr March 12, 2025 20:17
Copy link
Contributor

@n3wscott n3wscott left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM


// If there are no changes at all, we can skip the update
if !diffResult.HasChanges() {
w.log.Info("CRD is up-to-date", "name", desired.Name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might want to make this a trace level comment to reduce the log chatter

// DiffSchema compares schema versions and returns compatibility details.
// This function expects exactly one version in each slice. If more versions are present,
// it will return an error as multi-version support is not implemented.
func DiffSchema(oldVersions []v1.CustomResourceDefinitionVersion, newVersions []v1.CustomResourceDefinitionVersion) (*DiffResult, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at https://github.com/kubernetes/kubernetes/blob/dd43c3d29d5203378ce456de9466597234a83e66/staging/src/k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/types.go#L178

  • removal of sub-resources might matter?
  • flopping Deprecated could be bad
  • setting served or storage to false would break KRO

path+".type",
TypeChanged,
string(oldSchema.Type),
string(newSchema.Type),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: .Type is a string already

// existing properties, since new properties are handled in compareProperties.
func compareRequiredFields(path string, oldSchema, newSchema *v1.JSONSchemaProps, result *DiffResult) {
// Use length checks instead of nil checks
if len(oldSchema.Required) == 0 && len(newSchema.Required) == 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you might want to test that old and new schema are not nil as well just to protect yourself. It is likely someone attempts to delete the schema to work around something.

for req := range newRequiredSet {
// Only consider requirements for properties that already existed
if existingProps[req] && !oldRequiredSet[req] {
result.AddBreakingChange(path+".required", RequiredAdded, "", req)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is too bad this is not easier to use but Knative made FieldError to help with pathed errrors: https://github.com/knative/pkg/blob/b7bbf4be5dbd9a8b8b60e06094bd8930cf241357/apis/field_error.go#L63

@matthchr matthchr added this to the 0.4 milestone Jul 9, 2025
@matthchr matthchr self-assigned this Jul 9, 2025
@a-hilaly a-hilaly modified the milestones: 0.4, 0.5 Jul 30, 2025
@k8s-triage-robot
Copy link

Unknown CLA label state. Rechecking for CLA labels.

Send feedback to sig-contributor-experience at kubernetes/community.

/check-cla
/easycla

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. labels Sep 12, 2025
@k8s-ci-robot
Copy link
Contributor

PR needs rebase.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@a-hilaly a-hilaly modified the milestones: 0.5, 0.6 Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

detect and handle CRD breaking changes

6 participants