Skip to content

Commit 168abfe

Browse files
committed
Do not allow for CloudKitty deployment if the PrometheusEndpoint secret is not present
1 parent 36cb4c9 commit 168abfe

File tree

1 file changed

+74
-2
lines changed

1 file changed

+74
-2
lines changed

controllers/cloudkitty_controller.go

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,43 @@ func (r *CloudKittyReconciler) SetupWithManager(mgr ctrl.Manager) error {
338338
return nil
339339
}
340340

341+
prometheusEndpointSecretFn := func(ctx context.Context, o client.Object) []reconcile.Request {
342+
Log := r.GetLogger(ctx)
343+
344+
result := []reconcile.Request{}
345+
346+
// Only reconcile if this is the PrometheusEndpoint secret
347+
if o.GetName() != cloudkitty.PrometheusEndpointSecret {
348+
return nil
349+
}
350+
351+
// get all CloudKitty CRs
352+
cloudkitties := &telemetryv1.CloudKittyList{}
353+
listOpts := []client.ListOption{
354+
client.InNamespace(o.GetNamespace()),
355+
}
356+
if err := r.List(ctx, cloudkitties, listOpts...); err != nil {
357+
Log.Error(err, "Unable to retrieve CloudKitty CRs %w")
358+
return nil
359+
}
360+
361+
for _, cr := range cloudkitties.Items {
362+
// Only reconcile CloudKitty CRs that are using MetricStorage (PrometheusHost is empty)
363+
if cr.Spec.PrometheusHost == "" {
364+
name := client.ObjectKey{
365+
Namespace: o.GetNamespace(),
366+
Name: cr.Name,
367+
}
368+
Log.Info(fmt.Sprintf("PrometheusEndpoint Secret %s is used by CloudKitty CR %s", o.GetName(), cr.Name))
369+
result = append(result, reconcile.Request{NamespacedName: name})
370+
}
371+
}
372+
if len(result) > 0 {
373+
return result
374+
}
375+
return nil
376+
}
377+
341378
control, err := ctrl.NewControllerManagedBy(mgr).
342379
For(&telemetryv1.CloudKitty{}).
343380
Owns(&mariadbv1.MariaDBDatabase{}).
@@ -355,6 +392,9 @@ func (r *CloudKittyReconciler) SetupWithManager(mgr ctrl.Manager) error {
355392
// Watch for TransportURL Secrets which belong to any TransportURLs created by CloudKitty CRs
356393
Watches(&corev1.Secret{},
357394
handler.EnqueueRequestsFromMapFunc(transportURLSecretFn)).
395+
// Watch for PrometheusEndpoint Secret created by MetricStorage
396+
Watches(&corev1.Secret{},
397+
handler.EnqueueRequestsFromMapFunc(prometheusEndpointSecretFn)).
358398
Watches(&memcachedv1.Memcached{},
359399
handler.EnqueueRequestsFromMapFunc(memcachedFn)).
360400
Watches(&keystonev1.KeystoneAPI{},
@@ -825,6 +865,37 @@ func (r *CloudKittyReconciler) reconcileNormal(ctx context.Context, instance *te
825865
condition.MemcachedReadyCondition, condition.MemcachedReadyMessage)
826866
// run check memcached - end
827867

868+
//
869+
// Check for PrometheusEndpoint secret if using MetricStorage
870+
//
871+
if instance.Spec.PrometheusHost == "" {
872+
prometheusEndpointSecret := &corev1.Secret{}
873+
err = r.Get(ctx, client.ObjectKey{
874+
Name: cloudkitty.PrometheusEndpointSecret,
875+
Namespace: instance.Namespace,
876+
}, prometheusEndpointSecret)
877+
if err != nil {
878+
if k8s_errors.IsNotFound(err) {
879+
Log.Info("PrometheusEndpoint Secret not found. CloudKitty will not be deployed until MetricStorage creates it.")
880+
instance.Status.Conditions.Set(condition.FalseCondition(
881+
condition.ServiceConfigReadyCondition,
882+
condition.Reason("PrometheusEndpoint secret not found. The MetricStorage probably hasn't been created yet or isn't ready"),
883+
condition.SeverityError,
884+
"PrometheusEndpoint secret %s not found. Waiting for MetricStorage to create it",
885+
cloudkitty.PrometheusEndpointSecret))
886+
return ctrl.Result{RequeueAfter: telemetryv1.PauseBetweenWatchAttempts}, nil
887+
}
888+
instance.Status.Conditions.Set(condition.FalseCondition(
889+
condition.ServiceConfigReadyCondition,
890+
condition.ErrorReason,
891+
condition.SeverityWarning,
892+
condition.ServiceConfigReadyErrorMessage,
893+
err.Error()))
894+
return ctrl.Result{}, err
895+
}
896+
}
897+
// run check PrometheusEndpoint secret - end
898+
828899
//
829900
// check for required OpenStack secret holding passwords for service/admin user and add hash to the vars map
830901
//
@@ -1030,7 +1101,6 @@ func (r *CloudKittyReconciler) generateServiceConfigs(
10301101
memcached *memcachedv1.Memcached,
10311102
db *mariadbv1.Database,
10321103
) error {
1033-
Log := r.GetLogger(ctx)
10341104
//
10351105
// create Secret required for cloudkitty input
10361106
// - %-scripts holds scripts to e.g. bootstrap the service
@@ -1075,13 +1145,14 @@ func (r *CloudKittyReconciler) generateServiceConfigs(
10751145

10761146
if instance.Spec.PrometheusHost == "" {
10771147
// We're using MetricStorage for Prometheus.
1148+
// Note: The secret existence is already checked in reconcileNormal(), so we can safely get it here
10781149
prometheusEndpointSecret := &corev1.Secret{}
10791150
err = r.Get(ctx, client.ObjectKey{
10801151
Name: cloudkitty.PrometheusEndpointSecret,
10811152
Namespace: instance.Namespace,
10821153
}, prometheusEndpointSecret)
10831154
if err != nil {
1084-
Log.Info("Prometheus Endpoint Secret not found")
1155+
return err
10851156
}
10861157
if prometheusEndpointSecret.Data != nil {
10871158
instance.Status.PrometheusHost = string(prometheusEndpointSecret.Data[metricstorage.PrometheusHost])
@@ -1104,6 +1175,7 @@ func (r *CloudKittyReconciler) generateServiceConfigs(
11041175
condition.SeverityWarning,
11051176
condition.ServiceConfigReadyErrorMessage,
11061177
err.Error()))
1178+
return err
11071179
}
11081180
instance.Status.PrometheusTLS = metricStorage.Spec.PrometheusTLS.Enabled()
11091181
}

0 commit comments

Comments
 (0)