Skip to content

Commit

Permalink
Merge pull request #1430 from cybozu/grafana-operator-grafana-status-fix
Browse files Browse the repository at this point in the history
grafana-operator: fix status of Grafana resources during reconciliation
  • Loading branch information
hiroki-mogi-cy authored Sep 18, 2024
2 parents 90b0ea0 + 925b9e7 commit b2b907e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
5 changes: 5 additions & 0 deletions grafana-operator/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion grafana-operator/TAG
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5.10.0.1
5.10.0.2
44 changes: 44 additions & 0 deletions grafana-operator/grafana-status-fix.patch
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit b2b907e

Please sign in to comment.