Skip to content

🐛 fix(crd-upgrade-safety): Safely handle changes to description fields #2023

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,13 @@ func Type(diff FieldDiff) (bool, error) {

return isHandled(diff, reset), err
}

// Description changes are considered safe and non-breaking.
func Description(diff FieldDiff) (bool, error) {
reset := func(diff FieldDiff) FieldDiff {
diff.Old.Description = ""
diff.New.Description = ""
return diff
}
return isHandled(diff, reset), nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -904,3 +904,105 @@ func TestType(t *testing.T) {
})
}
}

func TestDescription(t *testing.T) {
for _, tc := range []testcase{
{
name: "no diff, no error, handled",
diff: FieldDiff{
Old: &apiextensionsv1.JSONSchemaProps{
Description: "some field",
},
New: &apiextensionsv1.JSONSchemaProps{
Description: "some field",
},
},
err: nil,
handled: true,
},
{
name: "description changed, no error, handled",
diff: FieldDiff{
Old: &apiextensionsv1.JSONSchemaProps{
Description: "old description",
},
New: &apiextensionsv1.JSONSchemaProps{
Description: "new description",
},
},
err: nil,
handled: true,
},
{
name: "description added, no error, handled",
diff: FieldDiff{
Old: &apiextensionsv1.JSONSchemaProps{},
New: &apiextensionsv1.JSONSchemaProps{
Description: "a new description was added",
},
},
err: nil,
handled: true,
},
{
name: "description removed, no error, handled",
diff: FieldDiff{
Old: &apiextensionsv1.JSONSchemaProps{
Description: "this description will be removed",
},
New: &apiextensionsv1.JSONSchemaProps{},
},
err: nil,
handled: true,
},
{
name: "different field changed, no error, not handled",
diff: FieldDiff{
Old: &apiextensionsv1.JSONSchemaProps{
ID: "foo",
},
New: &apiextensionsv1.JSONSchemaProps{
ID: "bar",
},
},
err: nil,
handled: false,
},
{
name: "different field changed with description, no error, not handled",
Copy link
Member

Choose a reason for hiding this comment

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

What if both ID and Description change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

handled will be false. Added a test case for it 👍🏽

diff: FieldDiff{
Old: &apiextensionsv1.JSONSchemaProps{
ID: "foo",
Description: "description",
},
New: &apiextensionsv1.JSONSchemaProps{
ID: "bar",
Description: "description",
},
},
err: nil,
handled: false,
},
{
name: "description and ID changed, no error, not handled",
diff: FieldDiff{
Old: &apiextensionsv1.JSONSchemaProps{
ID: "foo",
Description: "old description",
},
New: &apiextensionsv1.JSONSchemaProps{
ID: "bar",
Description: "new description",
},
},
err: nil,
handled: false,
},
} {
t.Run(tc.name, func(t *testing.T) {
handled, err := Description(tc.diff)
require.Equal(t, tc.err, err)
require.Equal(t, tc.handled, handled)
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Preflight struct {

func NewPreflight(crdCli apiextensionsv1client.CustomResourceDefinitionInterface, opts ...Option) *Preflight {
changeValidations := []ChangeValidation{
Description,
Enum,
Required,
Maximum,
Expand Down
Loading