From 925b9e7e2e8c9d1920c549dd2f86ea7b7e61ea44 Mon Sep 17 00:00:00 2001 From: UMEZAWA Takeshi Date: Wed, 18 Sep 2024 10:13:42 +0900 Subject: [PATCH] grafana-operator: fix status of Grafana resources during reconciliation Signed-off-by: UMEZAWA Takeshi --- grafana-operator/Dockerfile | 5 +++ grafana-operator/TAG | 2 +- grafana-operator/grafana-status-fix.patch | 44 +++++++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 grafana-operator/grafana-status-fix.patch diff --git a/grafana-operator/Dockerfile b/grafana-operator/Dockerfile index 6c99e3354..d512aaf26 100644 --- a/grafana-operator/Dockerfile +++ b/grafana-operator/Dockerfile @@ -4,6 +4,10 @@ FROM ghcr.io/cybozu/golang:1.22-jammy AS build ARG VERSION=5.10.0 +# After https://github.com/grafana/grafana-operator/issues/1678 is resolved, +# this patch will not be needed. +COPY grafana-status-fix.patch /work + WORKDIR /work/grafana-operator SHELL ["/bin/bash", "-o", "pipefail", "-c"] @@ -13,6 +17,7 @@ SHELL ["/bin/bash", "-o", "pipefail", "-c"] # https://github.com/grafana-operator/grafana-operator/blob/master/.gitmodules RUN git clone --depth 1 --single-branch -b v${VERSION} https://github.com/grafana/grafana-operator.git . RUN git submodule update --init --recursive --recommend-shallow --depth 1 +RUN patch -p1 < ../grafana-status-fix.patch RUN CGO_ENABLED=0 GO111MODULE=on go build -o /usr/local/bin/grafana-operator main.go # Stage2: setup runtime container diff --git a/grafana-operator/TAG b/grafana-operator/TAG index 75e97043c..2a275f732 100644 --- a/grafana-operator/TAG +++ b/grafana-operator/TAG @@ -1 +1 @@ -5.10.0.1 +5.10.0.2 diff --git a/grafana-operator/grafana-status-fix.patch b/grafana-operator/grafana-status-fix.patch new file mode 100644 index 000000000..31512824a --- /dev/null +++ b/grafana-operator/grafana-status-fix.patch @@ -0,0 +1,44 @@ +diff --git a/controllers/dashboard_controller.go b/controllers/dashboard_controller.go +index 1bd4b466..39a6781f 100644 +--- a/controllers/dashboard_controller.go ++++ b/controllers/dashboard_controller.go +@@ -398,6 +398,11 @@ func (r *GrafanaDashboardReconciler) onDashboardCreated(ctx context.Context, gra + } + + if exists && cr.Unchanged(hash) && !cr.ResyncPeriodHasElapsed() { ++ if found, _ := grafana.Status.Dashboards.Find(cr.Namespace, cr.Name); !found { ++ r.Log.Info("fixing inconsistent Grafana.status.dashboards filed", "grafana", map[string]string{"name": grafana.Name, "namespace": grafana.Namespace}) ++ grafana.Status.Dashboards = grafana.Status.Dashboards.Add(cr.Namespace, cr.Name, uid) ++ return r.Client.Status().Update(ctx, grafana) ++ } + return nil + } + +@@ -407,6 +412,11 @@ func (r *GrafanaDashboardReconciler) onDashboardCreated(ctx context.Context, gra + } + + if !remoteChanged { ++ if found, _ := grafana.Status.Dashboards.Find(cr.Namespace, cr.Name); !found { ++ r.Log.Info("fixing inconsistent Grafana.status.dashboards filed", "grafana", map[string]string{"name": grafana.Name, "namespace": grafana.Namespace}) ++ grafana.Status.Dashboards = grafana.Status.Dashboards.Add(cr.Namespace, cr.Name, uid) ++ return r.Client.Status().Update(ctx, grafana) ++ } + return nil + } + +diff --git a/controllers/datasource_controller.go b/controllers/datasource_controller.go +index cdf4e7ce..4f5753a7 100644 +--- a/controllers/datasource_controller.go ++++ b/controllers/datasource_controller.go +@@ -335,6 +335,11 @@ func (r *GrafanaDatasourceReconciler) onDatasourceCreated(ctx context.Context, g + } + + if exists && cr.Unchanged(hash) && !cr.ResyncPeriodHasElapsed() { ++ if found, _ := grafana.Status.Datasources.Find(cr.Namespace, cr.Name); !found { ++ r.Log.Info("fixing inconsistent Grafana.status.datasources filed", "grafana", map[string]string{"name": grafana.Name, "namespace": grafana.Namespace}) ++ grafana.Status.Datasources = grafana.Status.Datasources.Add(cr.Namespace, cr.Name, datasource.UID) ++ return r.Client.Status().Update(ctx, grafana) ++ } + return nil + } +