Skip to content

Commit

Permalink
Merge pull request #1435 from grafana/jsonnet-configmapref-fix
Browse files Browse the repository at this point in the history
fix nil pointer exception in jsonnet logic
  • Loading branch information
theSuess committed Feb 21, 2024
2 parents 75e056d + 69ceafc commit e1aa86d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .chainsaw.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ metadata:
spec:
timeouts:
assert: 2m0s
cleanup: 2m0s
cleanup: 3m0s
delete: 2m0s
error: 2m0s
exec: 2m0s
17 changes: 11 additions & 6 deletions controllers/dashboard_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion tests/e2e/example-test/05-dashboard.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e1aa86d

Please sign in to comment.