This directory contains reconcilers that connect cluster monitoring resources with backend registration and Grafana APIs.
Controller registration is split by process:
pkg/apiserver/apiserver.go- Always registers the CRD bootstrap reconciler from
kmodules.xyz/client-go/apiextensions. - Registers
ranchertoken.TokenRefresheronly whenExtraConfig.RancherAuthSecretis set. - Registers Prometheus, Alertmanager, and ServiceMonitor controllers through
apiextensions.RegisterSetup(...), so they are activated after the corresponding CRD/API kind is available.
- Always registers the CRD bootstrap reconciler from
pkg/cmds/server/grafana.go- Registers
openviz.GrafanaDashboardReconcilerandopenviz.GrafanaDatasourceReconciler.
- Registers
Across controllers, these patterns are heavily used:
- Detector readiness gating (
d.Ready()) before doing work. - Finalizer-based external cleanup.
CreateOrPatchreconciliation for owned resources.- Marker annotations to avoid repeated backend registration.
- Additional watches with predicates to requeue on dependency changes.
| Reconciler | Primary resource | Watches | Key outputs |
|---|---|---|---|
prometheus.PrometheusReconciler |
monitoringv1.Prometheus |
Prometheus + selected ConfigMap + optional Rancher auth Secret | Finalizer, RBAC, presets, backend registration, default Prometheus/Grafana AppBindings |
alertmanager.AlertmanagerReconciler |
monitoringv1.Alertmanager |
Alertmanager + inbox APIService | Alertmanager selector patch + AlertmanagerConfig/inbox-agent |
servicemonitor.AutoReconciler |
monitoringv1.ServiceMonitor (.../prometheus=auto) |
Matching ServiceMonitors + Prometheus | Label patching to bind to chosen Prometheus |
servicemonitor.FederationReconciler |
monitoringv1.ServiceMonitor (.../prometheus=federated) |
Matching ServiceMonitors + Services + Prometheus | Fan-out ServiceMonitor copies, plus copied Service/Endpoints/Secret objects |
namespace.ClientOrgReconciler |
corev1.Namespace (client-org labels) |
Matching Namespaces + trickster ServiceAccount | Monitoring namespace/bootstrap RBAC, backend registration, client-org Grafana AppBinding, dashboard copy/transform |
ranchertoken.TokenRefresher |
corev1.Secret |
Configured Rancher auth Secret | Token renewal scheduling and refresh |
openviz.GrafanaDashboardReconciler |
openvizapi.GrafanaDashboard |
GrafanaDashboard + AppBinding | External dashboard CRUD + status and events |
openviz.GrafanaDatasourceReconciler |
openvizapi.GrafanaDatasource |
GrafanaDatasource | External datasource CRUD + status and events |
Primary behavior:
- Fetches
Prometheus, waits for detector readiness, and computes default/project mode. - Adds finalizer
monitoring.appscode.com/prometheusfor active objects. - On deletion:
- Deletes related chart preset (
ClusterChartPresetor projectChartPreset). - Unregisters backend context when backend client is configured.
- Removes the finalizer.
- Deletes related chart preset (
- Reconciles in-cluster access setup:
- Chooses Rancher token auth or service-account token auth.
- Ensures cluster role
appscode:trickster:proxy. - Ensures rolebinding
appscode:trickster:proxyin Prometheus namespace.
- Registers Prometheus endpoint to backend when state markers changed.
- For default Prometheus, creates/updates:
- AppBinding
default-prometheus. - AppBinding
default-grafana. - Secret
default-grafana-auth.
- AppBinding
Watch details:
For(Prometheus).- Watches
ConfigMapkube-public/ace-infoand enqueues all Prometheus objects. - Optionally watches configured Rancher auth secret and enqueues all Prometheus objects.
Primary behavior:
- Fetches
Alertmanager, waits for detector readiness. - Applies OpenShift-specific defaulting: only reconciles user-workload Alertmanager.
- Skips deleted objects.
- Locates inbox APIService by group
inbox.monitoring.appscode.com. - Patches Alertmanager to enforce:
spec.alertmanagerConfigSelector.matchLabels[app.kubernetes.io/name]=inbox-agent.- matcher strategy type
None.
- Ensures
AlertmanagerConfignamedinbox-agentwith webhook receiver URL targeting inbox service.
Watch details:
For(Alertmanager).- Watches matching
APIServiceobjects and requeues all Alertmanagers.
Scope: ServiceMonitors with monitoring.appscode.com/prometheus=auto.
Primary behavior:
- Exits unless label value is
auto. - Lists Prometheus objects and exits if none.
- If any Prometheus already selects the ServiceMonitor (namespace selector + label selector), does nothing.
- Otherwise selects a target Prometheus:
- non-Rancher: first sorted Prometheus.
- Rancher: chooses system/project Prometheus using namespace project metadata.
- Patches ServiceMonitor labels to satisfy target Prometheus selector.
Watch details:
Named("servicemonitor-auto").For(ServiceMonitor)with predicate.../prometheus=auto.- Watches
Prometheusand requeues all matching auto ServiceMonitors.
Scope: ServiceMonitors with monitoring.appscode.com/prometheus=federated.
Primary behavior:
- Exits unless label value is
federated. - Lists Prometheus objects and waits for detector readiness.
- Non-federated mode: only patches labels for first Prometheus.
- Federated mode:
- Updates labels on default Prometheus path.
- For each non-default Prometheus:
- Creates/patches copied ServiceMonitor in Prometheus namespace.
- Injects namespace keep relabel rule at endpoint metricRelabel config head.
- Copies selected Service objects.
- Copies Endpoints and rewrites targets to copied service ClusterIP/ports.
- Copies referenced TLS CA secrets.
- Aggregates copy errors and returns combined error.
Watch details:
Named("servicemonitor-federation").For(ServiceMonitor)with predicate.../prometheus=federated.- Watches
Serviceand requeues matching federated ServiceMonitors. - Watches
Prometheusand requeues all matching federated ServiceMonitors.
Scope: Namespaces with kmapi.ClientOrgKey=true and kmapi.ClientOrgMonitoringKey!=false.
Primary behavior:
- Validates required client-org annotation (
kmapi.AceOrgIDKey) and detector readiness. - Fails fast for unsupported Rancher + federated combination.
- Adds finalizer
monitoring.appscode.com/prometheus. - On deletion:
- Unregisters client-org backend context.
- Deletes all copied GrafanaDashboards in
<namespace>-monitoring, then deletes the monitoring namespace itself (authoritative cleanup; removing the finalizer only after this succeeds avoids racing namespace GC against in-flight copy reconciles). - Removes finalizer.
- Re-reads the namespace from the API server (uncached) and bails if it is terminating or no longer client-org labeled, so a stale cached read can't recreate copies mid-teardown.
- Ensures monitoring namespace
<namespace>-monitoring. - Ensures rolebinding
appscode:client-org:monitoringin monitoring namespace. - Verifies trickster registration marker exists before proceeding.
- Registers backend context (issue token mode), then creates/updates:
- AppBinding
grafanain client monitoring namespace. - Secret
grafana-auth.
- AppBinding
- Copies source GrafanaDashboard objects into client monitoring namespace and transforms:
- dashboard title (
ClientDashboardTitle). - templating variable
namespaceas constant client namespace value. - grafanaRef to local appbinding.
- source/hash annotations for traceability.
- Skips objects that are already copies (carry the source annotation) or live in a
-monitoringnamespace, so copies are not re-copied into an unstable loop.
- dashboard title (
Watch details:
Named("namespace").For(Namespace)with client-org predicate.- Watches
ServiceAccount tricksterwith registration label and requeues all non-terminating client-org namespaces.
Primary behavior:
- Watches only the configured Rancher auth secret in pod namespace.
- Parses token using cluster state aware lookup.
- Renews token if expiry is within 24 hours.
- Requeues at
next_expiry - 24h.
Primary behavior:
- Ensures finalizer
grafanadashboard.openviz.dev/finalizer. - On delete:
- Sets terminating phase.
- Deletes remote dashboard by UID (treats 404 as already deleted).
- Removes finalizer.
- On active objects:
- Sets processing phase (unless already failed).
- Resolves Grafana AppBinding and computed Grafana state hash.
- Optionally templatizes datasource references in dashboard JSON.
- Upserts dashboard via Grafana API.
- Patches status dashboard reference, state, phase, reason, and ready condition.
- On failures:
- Records warning events.
- Marks failed condition and reason.
- Requeues using configurable backoff (
RequeueAfterDuration).
Watch details:
For(GrafanaDashboard)with predicate:- first reconcile always,
- later reconcile when associated Grafana state changed.
- Watches
AppBindingand requeues dashboards that reference that appbinding and are failed/stale.
Primary behavior:
- Ensures finalizer
grafanadatasource.openviz.dev/finalizer. - On delete:
- Sets terminating phase.
- Deletes remote datasource when status has remote datasource ID.
- Removes finalizer.
- On first active reconcile:
- Initializes processing status and clears conditions.
- Creates datasource if status ID absent, else updates existing datasource.
- Patches current phase/reason/observed generation on success.
- On failures:
- Emits warning event.
- Patches failed phase/reason and condition.
Watch details:
For(GrafanaDatasource).
clientorg/clientorg.go: computes per-client monitoring namespace names.config.go: ensures CRDs for GrafanaDashboard, GrafanaDatasource, and AppBinding.