Skip to content

Commit 4126dac

Browse files
committed
Set ReconcileSuccess to false if reconcile_tls() fails
1 parent 5936825 commit 4126dac

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

controllers/rabbitmqcluster_controller.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,14 @@ func (r *RabbitmqClusterReconciler) Reconcile(ctx context.Context, req ctrl.Requ
118118
return ctrl.Result{}, err
119119
}
120120

121-
if err := r.reconcileTLS(ctx, rabbitmqCluster); err != nil {
122-
return ctrl.Result{}, err
123-
}
124-
125121
if requeueAfter, err := r.updateStatus(ctx, rabbitmqCluster); err != nil || requeueAfter > 0 {
126122
return ctrl.Result{RequeueAfter: requeueAfter}, err
127123
}
128124

125+
if err := r.reconcileTLS(ctx, rabbitmqCluster); err != nil {
126+
return ctrl.Result{}, err
127+
}
128+
129129
sts, err := r.statefulSet(ctx, rabbitmqCluster)
130130
// The StatefulSet may not have been created by this point, so ignore Not Found errors
131131
if client.IgnoreNotFound(err) != nil {

controllers/reconcile_tls.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ func (r *RabbitmqClusterReconciler) reconcileTLS(ctx context.Context, rabbitmqCl
1717
err := errors.NewBadRequest("TLS must be enabled if disableNonTLSListeners is set to true")
1818
r.Recorder.Event(rabbitmqCluster, corev1.EventTypeWarning, "TLSError", err.Error())
1919
ctrl.LoggerFrom(ctx).Error(err, "Error setting up TLS")
20+
r.setReconcileSuccess(ctx, rabbitmqCluster, corev1.ConditionFalse, "TLSError", err.Error())
2021
return err
2122
}
2223

2324
if rabbitmqCluster.TLSEnabled() {
2425
if err := r.checkTLSSecrets(ctx, rabbitmqCluster); err != nil {
26+
r.setReconcileSuccess(ctx, rabbitmqCluster, corev1.ConditionFalse, "TLSError", err.Error())
2527
return err
2628
}
2729
}

controllers/reconcile_tls_test.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package controllers_test
33
import (
44
"context"
55
"fmt"
6+
"github.com/rabbitmq/cluster-operator/internal/status"
67
"k8s.io/utils/pointer"
8+
runtimeClient "sigs.k8s.io/controller-runtime/pkg/client"
79

810
rabbitmqv1beta1 "github.com/rabbitmq/cluster-operator/api/v1beta1"
911

@@ -91,6 +93,7 @@ var _ = Describe("Reconcile TLS", func() {
9193
cluster = rabbitmqClusterWithTLS(ctx, "tls-secret-missing", defaultNamespace, tlsSpec)
9294

9395
verifyTLSErrorEvents(ctx, cluster, fmt.Sprintf("TLS secret tls-secret-missing in namespace %s does not have the field ca.crt", defaultNamespace))
96+
verifyReconcileSuccessFalse(cluster.Name, cluster.Namespace)
9497
})
9598
})
9699

@@ -104,6 +107,7 @@ var _ = Describe("Reconcile TLS", func() {
104107
}
105108
cluster = rabbitmqClusterWithTLS(ctx, "rabbitmq-tls-secret-does-not-exist", defaultNamespace, tlsSpec)
106109
verifyTLSErrorEvents(ctx, cluster, "Failed to get CA certificate secret")
110+
verifyReconcileSuccessFalse(cluster.Name, cluster.Namespace)
107111

108112
_, err := clientSet.AppsV1().StatefulSets(cluster.Namespace).Get(ctx, cluster.ChildResourceName("server"), metav1.GetOptions{})
109113
Expect(err).To(HaveOccurred())
@@ -136,6 +140,7 @@ var _ = Describe("Reconcile TLS", func() {
136140
}
137141
cluster = rabbitmqClusterWithTLS(ctx, "rabbitmq-mutual-tls-missing", defaultNamespace, tlsSpec)
138142
verifyTLSErrorEvents(ctx, cluster, fmt.Sprintf("TLS secret ca-cert-secret-invalid in namespace %s does not have the field ca.crt", defaultNamespace))
143+
verifyReconcileSuccessFalse(cluster.Name, cluster.Namespace)
139144
})
140145
})
141146
})
@@ -191,18 +196,19 @@ var _ = Describe("Reconcile TLS", func() {
191196

192197
It("fails to deploy the RabbitmqCluster", func() {
193198
verifyTLSErrorEvents(ctx, cluster, fmt.Sprintf("TLS secret rabbitmq-tls-malformed in namespace %s does not have the fields tls.crt and tls.key", defaultNamespace))
199+
verifyReconcileSuccessFalse(cluster.Name, cluster.Namespace)
194200
})
195201
})
196202

197203
When("the TLS secret does not exist", func() {
198204
It("fails to deploy the RabbitmqCluster until the secret is detected", func() {
199-
200205
tlsSpec := rabbitmqv1beta1.TLSSpec{
201206
SecretName: "tls-secret-does-not-exist",
202207
}
203208
cluster = rabbitmqClusterWithTLS(ctx, "rabbitmq-tls-secret-does-not-exist", defaultNamespace, tlsSpec)
204209

205210
verifyTLSErrorEvents(ctx, cluster, "Failed to get TLS secret")
211+
verifyReconcileSuccessFalse(cluster.Name, cluster.Namespace)
206212

207213
_, err := clientSet.AppsV1().StatefulSets(cluster.Namespace).Get(ctx, cluster.ChildResourceName("server"), metav1.GetOptions{})
208214
Expect(err).To(HaveOccurred())
@@ -222,7 +228,7 @@ var _ = Describe("Reconcile TLS", func() {
222228
})
223229

224230
When("DiableNonTLSListeners set to true", func() {
225-
It("errors and logs TLSError when TLS is not enabled", func() {
231+
It("returns an error, logs TLSError and set ReconcileSuccess to false when TLS is not enabled", func() {
226232
tlsSpec := rabbitmqv1beta1.TLSSpec{
227233
DisableNonTLSListeners: true,
228234
}
@@ -232,10 +238,28 @@ var _ = Describe("Reconcile TLS", func() {
232238

233239
_, err := clientSet.AppsV1().StatefulSets(cluster.Namespace).Get(ctx, cluster.ChildResourceName("server"), metav1.GetOptions{})
234240
Expect(err).To(HaveOccurred())
241+
verifyReconcileSuccessFalse(cluster.Name, cluster.Namespace)
235242
})
236243
})
237244
})
238245

246+
func verifyReconcileSuccessFalse(name, namespace string) bool {
247+
return EventuallyWithOffset(1, func() string {
248+
rabbit := &rabbitmqv1beta1.RabbitmqCluster{}
249+
Expect(client.Get(ctx, runtimeClient.ObjectKey{
250+
Name: name,
251+
Namespace: namespace,
252+
}, rabbit)).To(Succeed())
253+
254+
for i := range rabbit.Status.Conditions {
255+
if rabbit.Status.Conditions[i].Type == status.ReconcileSuccess {
256+
return fmt.Sprintf("ReconcileSuccess status: %s", rabbit.Status.Conditions[i].Status)
257+
}
258+
}
259+
return "ReconcileSuccess status: condition not present"
260+
}, 5).Should(Equal("ReconcileSuccess status: False"))
261+
}
262+
239263
func tlsSecretWithCACert(ctx context.Context, secretName, namespace string) {
240264
tlsData := map[string]string{
241265
"tls.crt": "this is a tls cert",

0 commit comments

Comments
 (0)