From 69ceafcd3fe866f2542a53f9d2e9f5c6841be05c Mon Sep 17 00:00:00 2001 From: hstefans Date: Wed, 21 Feb 2024 11:43:06 +0100 Subject: [PATCH] fix nil pointer exception in jsonnet logic --- .chainsaw.yaml | 2 +- controllers/dashboard_controller.go | 17 +++++++++++------ tests/e2e/example-test/05-dashboard.yaml | 3 ++- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/.chainsaw.yaml b/.chainsaw.yaml index 888729b47..3c52639ad 100755 --- a/.chainsaw.yaml +++ b/.chainsaw.yaml @@ -6,7 +6,7 @@ metadata: spec: timeouts: assert: 2m0s - cleanup: 2m0s + cleanup: 3m0s delete: 2m0s error: 2m0s exec: 2m0s diff --git a/controllers/dashboard_controller.go b/controllers/dashboard_controller.go index fd8a17976..04c555851 100644 --- a/controllers/dashboard_controller.go +++ b/controllers/dashboard_controller.go @@ -473,6 +473,9 @@ func (r *GrafanaDashboardReconciler) fetchDashboardJson(ctx context.Context, das func (r *GrafanaDashboardReconciler) getDashboardEnvs(ctx context.Context, dashboard *v1beta1.GrafanaDashboard) (map[string]string, error) { envs := make(map[string]string) + if dashboard.Spec.EnvsFrom == nil && dashboard.Spec.Envs == nil { + return nil, fmt.Errorf("dashboard.Spec.Envs or dashboard.Spec.EnvFrom nil, can't get envs for dashboard: %s", dashboard.Name) + } if dashboard.Spec.EnvsFrom != nil { for _, ref := range dashboard.Spec.EnvsFrom { key, val, err := r.getReferencedValue(ctx, dashboard, ref) @@ -508,20 +511,22 @@ func (r *GrafanaDashboardReconciler) getReferencedValue(ctx context.Context, cr if val, ok := s.Data[source.SecretKeyRef.Key]; ok { return source.SecretKeyRef.Key, string(val), nil } else { - return "", "", fmt.Errorf("missing key %s in secret %s", source.SecretKeyRef.Key, source.ConfigMapKeyRef.Name) + return "", "", fmt.Errorf("missing key %s in secret %s", source.SecretKeyRef.Key, source.SecretKeyRef.Name) } - } else { + } + if source.ConfigMapKeyRef != nil { s := &v1.ConfigMap{} - err := r.Client.Get(ctx, client.ObjectKey{Namespace: cr.Namespace, Name: source.SecretKeyRef.Name}, s) + err := r.Client.Get(ctx, client.ObjectKey{Namespace: cr.Namespace, Name: source.ConfigMapKeyRef.Name}, s) if err != nil { return "", "", err } - if val, ok := s.Data[source.SecretKeyRef.Key]; ok { - return source.SecretKeyRef.Key, val, nil + if val, ok := s.Data[source.ConfigMapKeyRef.Key]; ok { + return source.ConfigMapKeyRef.Key, val, nil } else { - return "", "", fmt.Errorf("missing key %s in configmap %s", source.SecretKeyRef.Key, source.ConfigMapKeyRef.Name) + return "", "", fmt.Errorf("missing key %s in configmap %s", source.ConfigMapKeyRef.Key, source.ConfigMapKeyRef.Name) } } + return "", "", fmt.Errorf("source couldn't be parsed source: %s", source) } // getDashboardModel resolves datasources, updates uid (if needed) and converts raw json to type grafana client accepts diff --git a/tests/e2e/example-test/05-dashboard.yaml b/tests/e2e/example-test/05-dashboard.yaml index 1af80d108..787cf4d7c 100644 --- a/tests/e2e/example-test/05-dashboard.yaml +++ b/tests/e2e/example-test/05-dashboard.yaml @@ -14,8 +14,9 @@ spec: matchLabels: dashboards: "grafana" envFrom: - - configMapRef: + - configMapKeyRef: name: grafana-user-envs + key: "CUSTOM_RANGE_ENV" plugins: - name: grafana-piechart-panel version: 1.3.9