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] Ensure ownerRef on objects for inactive package revisions #5189

Merged
merged 1 commit into from
Jan 2, 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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/sso v1.12.10 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.14.10 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.19.0 // indirect
github.com/aws/smithy-go v1.13.5 // indirect
github.com/aws/smithy-go v1.13.5
github.com/awslabs/amazon-ecr-credential-helper/ecr-login v0.0.0-20230510185313-f5e39e5f34c7 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bufbuild/protocompile v0.6.0 // indirect
Expand Down
7 changes: 7 additions & 0 deletions internal/controller/pkg/revision/establisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,11 @@ func (e *APIEstablisher) ReleaseObjects(ctx context.Context, parent v1.PackageRe
return errors.Wrapf(err, errFmtGetOwnedObject, u.GetKind(), u.GetName())
}
ors := u.GetOwnerReferences()
found := false
changed := false
for i := range ors {
if ors[i].UID == parent.GetUID() {
found = true
if ors[i].Controller != nil && *ors[i].Controller {
ors[i].Controller = ptr.To(false)
changed = true
Expand All @@ -182,6 +184,11 @@ func (e *APIEstablisher) ReleaseObjects(ctx context.Context, parent v1.PackageRe
// happens active revision or the package itself will still take
// over the ownership of such resources.
}
if !found {
// Make sure the package revision exists as an owner.
ors = append(ors, meta.AsOwner(meta.TypedReferenceTo(parent, parent.GetObjectKind().GroupVersionKind())))
changed = true
}
if changed {
u.SetOwnerReferences(ors)
if err := e.client.Update(ctx, &u); err != nil {
Expand Down
62 changes: 62 additions & 0 deletions internal/controller/pkg/revision/establisher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"testing"

"github.com/aws/smithy-go/ptr"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
admv1 "k8s.io/api/admissionregistration/v1"
Expand Down Expand Up @@ -669,6 +670,67 @@ func TestAPIEstablisherReleaseObjects(t *testing.T) {
err: nil,
},
},
"OwnedIfNotAlready": {
reason: "ReleaseObjects should put owner reference back if we are not already the owner.",
args: args{
est: &APIEstablisher{
client: &test.MockClient{
MockGet: func(ctx context.Context, key client.ObjectKey, obj client.Object) error {
o := obj.(*unstructured.Unstructured)
o.SetOwnerReferences([]metav1.OwnerReference{
{
APIVersion: "pkg.crossplane.io/v1",
Kind: "Provider",
Name: "provider-helm",
UID: "some-other-uid-1234",
Controller: &noControl,
},
})
return nil
},
MockUpdate: func(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error {
o := obj.(*unstructured.Unstructured)
if len(o.GetOwnerReferences()) != 2 {
t.Errorf("expected 2 owner references, got %d", len(o.GetOwnerReferences()))
}
found := false
for _, ref := range o.GetOwnerReferences() {
if ref.Kind == "ProviderRevision" && ref.UID == "some-unique-uid-2312" {
found = true
if ptr.ToBool(ref.Controller) {
t.Errorf("expected controller to be false, got %t", *ref.Controller)
}
}
}
if !found {
t.Errorf("expected to find owner reference for revision with uid some-unique-uid-2312")
}
return nil
},
},
},
parent: &v1.ProviderRevision{
TypeMeta: metav1.TypeMeta{
Kind: "ProviderRevision",
},
ObjectMeta: metav1.ObjectMeta{
UID: "some-unique-uid-2312",
},
Status: v1.PackageRevisionStatus{
ObjectRefs: []xpv1.TypedReference{
{
APIVersion: "apiextensions.k8s.io/v1",
Kind: "CustomResourceDefinition",
Name: "releases.helm.crossplane.io",
},
},
},
},
},
want: want{
err: nil,
},
},
"SuccessfulRelease": {
reason: "ReleaseObjects should be successful if we can release control of existing objects",
args: args{
Expand Down