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

Handle empty converts #181

Merged
merged 4 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
17 changes: 13 additions & 4 deletions apis/object/v1alpha1/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
package v1alpha1

import (
"errors"
"fmt"

"k8s.io/apimachinery/pkg/util/sets"
"sigs.k8s.io/controller-runtime/pkg/conversion"
Expand Down Expand Up @@ -101,7 +101,7 @@ func (src *Object) ConvertTo(dstRaw conversion.Hub) error { // nolint:golint //
case Observe:
dst.Spec.ManagementPolicies = xpv1.ManagementPolicies{xpv1.ManagementActionObserve}
default:
return errors.New("unknown management policy")
return fmt.Errorf("unknown management policy: %v", src.Spec.ManagementPolicy)
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: could we use errors.Errorf instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

sure we can!

}

return nil
Expand Down Expand Up @@ -169,6 +169,14 @@ func (dst *Object) ConvertFrom(srcRaw conversion.Hub) error { // nolint:golint,
},
}

// Policies are unset and the object is not yet created.
// As the managementPolicies would default to ["*"], we can set
// the management policy to Default.
if src.GetManagementPolicies() == nil && src.CreationTimestamp.IsZero() {
dst.Spec.ManagementPolicy = Default
return nil
}

// handle management policies migration
policySet := sets.New[xpv1.ManagementAction](src.GetManagementPolicies()...)

Expand All @@ -187,8 +195,9 @@ func (dst *Object) ConvertFrom(srcRaw conversion.Hub) error { // nolint:golint,
!policySet.HasAny(xpv1.ManagementActionCreate, xpv1.ManagementActionUpdate, xpv1.ManagementActionDelete):
dst.Spec.ManagementPolicy = Observe
default:
// TODO(turkenh): Should we default to something here instead of erroring out?
return errors.New("unsupported management policy")
// NOTE(lsviben): Other combinations of v1alpha2 management policies
// were not supported in v1alpha1. Leaving it empty to avoid
// errors during conversion instead of failing.
}

return nil
Expand Down
67 changes: 61 additions & 6 deletions apis/object/v1alpha1/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ func TestConvertTo(t *testing.T) {
},
},
want: want{
err: errors.New("unknown management policy"),
err: errors.New("unknown management policy: unknown"),
},
},
}
Expand Down Expand Up @@ -293,7 +293,7 @@ func TestConvertFrom(t *testing.T) {
want want
}{
{
name: "converts to v1alpha2",
name: "converts to v1alpha1",
args: args{
src: &v1alpha2.Object{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -386,7 +386,7 @@ func TestConvertFrom(t *testing.T) {
},
},
{
name: "converts to v1alpha2 - nil checks",
name: "converts to v1alpha1 - nil checks",
args: args{
src: &v1alpha2.Object{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -453,21 +453,76 @@ func TestConvertFrom(t *testing.T) {
},
},
{
name: "errors if management policy is unknown",
name: "converts to v1alpha1 - empty policy",
args: args{
src: &v1alpha2.Object{
ObjectMeta: metav1.ObjectMeta{
Name: "coolobject",
},
Spec: v1alpha2.ObjectSpec{
ResourceSpec: v1.ResourceSpec{
DeletionPolicy: v1.DeletionDelete,
},
ForProvider: v1alpha2.ObjectParameters{
Manifest: runtime.RawExtension{Raw: []byte("apiVersion: v1\nkind: Secret\nmetadata:\n name: topsecret\n")},
},
},
},
},
want: want{
dst: &v1alpha1.Object{
ObjectMeta: metav1.ObjectMeta{
Name: "coolobject",
},
Spec: v1alpha1.ObjectSpec{
ResourceSpec: v1alpha1.ResourceSpec{
DeletionPolicy: v1.DeletionDelete,
},
ForProvider: v1alpha1.ObjectParameters{
Manifest: runtime.RawExtension{Raw: []byte("apiVersion: v1\nkind: Secret\nmetadata:\n name: topsecret\n")},
},
ManagementPolicy: v1alpha1.Default,
ConnectionDetails: []v1alpha1.ConnectionDetail{},
References: []v1alpha1.Reference{},
},
},
},
},
{
name: "converts to v1alpha1 - unsupported policy",
args: args{
src: &v1alpha2.Object{
ObjectMeta: metav1.ObjectMeta{
Name: "coolobject",
},
Spec: v1alpha2.ObjectSpec{
ResourceSpec: v1.ResourceSpec{
ManagementPolicies: []v1.ManagementAction{},
DeletionPolicy: v1.DeletionDelete,
ManagementPolicies: []v1.ManagementAction{v1.ManagementActionDelete},
},
ForProvider: v1alpha2.ObjectParameters{
Manifest: runtime.RawExtension{Raw: []byte("apiVersion: v1\nkind: Secret\nmetadata:\n name: topsecret\n")},
},
},
},
},
want: want{
err: errors.New("unsupported management policy"),
dst: &v1alpha1.Object{
ObjectMeta: metav1.ObjectMeta{
Name: "coolobject",
},
Spec: v1alpha1.ObjectSpec{
ResourceSpec: v1alpha1.ResourceSpec{
DeletionPolicy: v1.DeletionDelete,
},
ForProvider: v1alpha1.ObjectParameters{
Manifest: runtime.RawExtension{Raw: []byte("apiVersion: v1\nkind: Secret\nmetadata:\n name: topsecret\n")},
},
ManagementPolicy: "",
ConnectionDetails: []v1alpha1.ConnectionDetail{},
References: []v1alpha1.Reference{},
},
},
},
},
}
Expand Down
2 changes: 1 addition & 1 deletion examples/in-composition/composition.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ spec:
readinessChecks:
- type: None
- base:
apiVersion: kubernetes.crossplane.io/v1beta1
apiVersion: kubernetes.crossplane.io/v1alpha2
kind: Object
spec:
forProvider:
Expand Down
Loading