Skip to content

Commit d5e8d86

Browse files
authored
Merge pull request #994 from rabbitmq/tweak-owner-reference-behaviour
Recover integration test for owner reference
2 parents bc4ccba + ce4b4d6 commit d5e8d86

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export KUBEBUILDER_ASSETS = $(LOCAL_TESTBIN)/k8s/$(ENVTEST_K8S_VERSION)-$(platfo
5454

5555
.PHONY: kubebuilder-assets
5656
kubebuilder-assets: $(KUBEBUILDER_ASSETS)
57-
@echo "export KUBEBUILDER_ASSETS = $(LOCAL_TESTBIN)/k8s/$(ENVTEST_K8S_VERSION)-$(platform)-$(ARCHITECTURE)"
57+
@echo "export KUBEBUILDER_ASSETS=$(LOCAL_TESTBIN)/k8s/$(ENVTEST_K8S_VERSION)-$(platform)-$(ARCHITECTURE)"
5858

5959
$(KUBEBUILDER_ASSETS):
6060
setup-envtest --os $(platform) --arch $(ARCHITECTURE) --bin-dir $(LOCAL_TESTBIN) use $(ENVTEST_K8S_VERSION)

controllers/user_controller.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@ func (r *UserReconciler) declareCredentials(ctx context.Context, user *topology.
9797
// https://github.com/rabbitmq/cluster-operator/blob/057b61eb50102a66f504b31464e5956526cbdc90/internal/resource/statefulset.go#L220-L226
9898
// https://github.com/rabbitmq/messaging-topology-operator/issues/194
9999
for i := range credentialSecret.ObjectMeta.OwnerReferences {
100-
credentialSecret.ObjectMeta.OwnerReferences[i].BlockOwnerDeletion = ptr.To(false)
100+
if credentialSecret.ObjectMeta.OwnerReferences[i].Kind == user.Kind {
101+
credentialSecret.ObjectMeta.OwnerReferences[i].BlockOwnerDeletion = ptr.To(false)
102+
}
101103
}
102104
return nil
103105
})

controllers/user_controller_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/rabbitmq/cluster-operator/v2/api/v1beta1"
99
"io"
1010
"k8s.io/apimachinery/pkg/labels"
11+
"k8s.io/utils/ptr"
1112
"net/http"
1213
"time"
1314

@@ -16,6 +17,7 @@ import (
1617
"sigs.k8s.io/controller-runtime/pkg/cache"
1718
runtimeClient "sigs.k8s.io/controller-runtime/pkg/client"
1819
"sigs.k8s.io/controller-runtime/pkg/config"
20+
k "sigs.k8s.io/controller-runtime/pkg/envtest/komega"
1921
"sigs.k8s.io/controller-runtime/pkg/metrics/server"
2022

2123
rabbithole "github.com/michaelklishin/rabbit-hole/v3"
@@ -105,6 +107,15 @@ var _ = Describe("UserController", func() {
105107
}
106108
}
107109

110+
objectStatus := func() []topology.Condition {
111+
_ = k8sClient.Get(
112+
ctx,
113+
types.NamespacedName{Name: user.Name, Namespace: user.Namespace},
114+
&user,
115+
)
116+
return user.Status.Conditions
117+
}
118+
108119
AfterEach(func() {
109120
managerCancel()
110121
// Sad workaround to avoid controllers racing for the reconciliation of other's
@@ -188,6 +199,11 @@ var _ = Describe("UserController", func() {
188199
})
189200

190201
Context("user limits", func() {
202+
AfterEach(func() {
203+
userLimits.Connections = nil
204+
userLimits.Channels = nil
205+
})
206+
191207
When("the user has limits defined", func() {
192208
BeforeEach(func() {
193209
userName = "test-user-limits"
@@ -422,4 +438,39 @@ var _ = Describe("UserController", func() {
422438
})
423439
})
424440
})
441+
442+
It("sets an owner reference and does not block owner deletion", func() {
443+
userName = "test-owner-reference"
444+
initialiseUser()
445+
user.Labels = map[string]string{"test": userName}
446+
fakeRabbitMQClient.PutUserReturns(&http.Response{Status: "201 Created", StatusCode: http.StatusCreated}, nil)
447+
initialiseManager("test", userName)
448+
449+
Expect(k8sClient.Create(ctx, &user)).To(Succeed())
450+
Eventually(objectStatus).
451+
Within(statusEventsUpdateTimeout).
452+
WithPolling(time.Second).
453+
Should(ContainElement(MatchFields(IgnoreExtras, Fields{
454+
"Type": Equal(topology.ConditionType("Ready")),
455+
"Reason": Equal("SuccessfulCreateOrUpdate"),
456+
"Status": Equal(corev1.ConditionTrue),
457+
})), "User should have been created and have a True Ready condition")
458+
459+
generatedSecret := &corev1.Secret{ObjectMeta: metav1.ObjectMeta{Name: user.Name + "-user-credentials", Namespace: user.Namespace}}
460+
Eventually(k.Get(generatedSecret)).
461+
Within(10 * time.Second).
462+
Should(Succeed())
463+
464+
idFn := func(e any) string {
465+
ownRef := e.(metav1.OwnerReference)
466+
return ownRef.Kind
467+
}
468+
Expect(generatedSecret.OwnerReferences).To(MatchElements(idFn, IgnoreExtras,
469+
Elements{
470+
"User": MatchFields(IgnoreExtras, Fields{
471+
"BlockOwnerDeletion": Equal(ptr.To(false)),
472+
}),
473+
},
474+
))
475+
})
425476
})

0 commit comments

Comments
 (0)