Skip to content
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

[Backport release-1.14] Accept version changes in composed templates #5370

Merged
merged 1 commit into from
Feb 12, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ const (
errGenerateName = "cannot generate a name for a composed resource"
errSetControllerRef = "cannot set controller reference"

errFmtKindChanged = "cannot change the kind of a composed resource from %s to %s (possible composed resource template mismatch)"
errFmtNamePrefixLabel = "cannot find top-level composite resource name label %q in composite resource metadata"
errFmtKindOrGroupChanged = "cannot change the kind or group of a composed resource from %s to %s (possible composed resource template mismatch)"
errFmtNamePrefixLabel = "cannot find top-level composite resource name label %q in composite resource metadata"

// TODO(negz): Include more detail such as field paths if they exist.
// Perhaps require each patch type to have a String() method to help
Expand Down Expand Up @@ -68,13 +68,16 @@ func RenderFromJSON(o resource.Object, data []byte) error {
o.SetName(name)
o.SetNamespace(namespace)

// This resource already had a GVK (probably because it already exists), but
// This resource already had a GK (probably because it already exists), but
// when we rendered its template it changed. This shouldn't happen. Either
// someone changed the kind in the template or we're trying to use the wrong
// template (e.g. because the order of an array of anonymous templates
// someone changed the kind or group in the template, or we're trying to use the
// wrong template (e.g. because the order of an array of anonymous templates
// changed).
if !gvk.Empty() && o.GetObjectKind().GroupVersionKind() != gvk {
return errors.Errorf(errFmtKindChanged, gvk, o.GetObjectKind().GroupVersionKind())
// Please note, we don't check for version changes, as versions can change. For example,
// if a composed resource was created with a template that has a version of "v1alpha1",
// and then the template is updated to "v1beta1", the composed resource will still be valid.
if !gvk.Empty() && o.GetObjectKind().GroupVersionKind().GroupKind() != gvk.GroupKind() {
return errors.Errorf(errFmtKindOrGroupChanged, gvk, o.GetObjectKind().GroupVersionKind())
}

return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,25 @@ func TestRenderFromJSON(t *testing.T) {
err: errors.Wrap(errInvalidChar, errUnmarshalJSON),
},
},
"ExistingGVKChanged": {
reason: "We should return an error if unmarshalling the base template changed the composed resource's group, version, or kind",
"ExistingGroupChanged": {
reason: "We should return an error if unmarshalling the base template changed the composed resource's group.",
args: args{
o: composed.New(composed.FromReference(corev1.ObjectReference{
APIVersion: "example.org/v1",
Kind: "Potato",
})),
data: []byte(`{"apiVersion": "foo.io/v1", "kind": "Potato"}`),
},
want: want{
o: composed.New(composed.FromReference(corev1.ObjectReference{
APIVersion: "foo.io/v1",
Kind: "Potato",
})),
err: errors.Errorf(errFmtKindOrGroupChanged, "example.org/v1, Kind=Potato", "foo.io/v1, Kind=Potato"),
},
},
"ExistingKindChanged": {
reason: "We should return an error if unmarshalling the base template changed the composed resource's kind.",
args: args{
o: composed.New(composed.FromReference(corev1.ObjectReference{
APIVersion: "example.org/v1",
Expand All @@ -81,7 +98,23 @@ func TestRenderFromJSON(t *testing.T) {
APIVersion: "example.org/v1",
Kind: "Different",
})),
err: errors.Errorf(errFmtKindChanged, "example.org/v1, Kind=Potato", "example.org/v1, Kind=Different"),
err: errors.Errorf(errFmtKindOrGroupChanged, "example.org/v1, Kind=Potato", "example.org/v1, Kind=Different"),
},
},
"VersionCanChange": {
reason: "We should accept version changes in the base template.",
args: args{
o: composed.New(composed.FromReference(corev1.ObjectReference{
APIVersion: "example.org/v1alpha1",
Kind: "Potato",
})),
data: []byte(`{"apiVersion": "example.org/v1beta1", "kind": "Potato"}`),
},
want: want{
o: composed.New(composed.FromReference(corev1.ObjectReference{
APIVersion: "example.org/v1beta1",
Kind: "Potato",
})),
},
},
"NewComposedResource": {
Expand Down