Skip to content

Commit 69ceafc

Browse files
committed
fix nil pointer exception in jsonnet logic
1 parent 75e056d commit 69ceafc

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

.chainsaw.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ metadata:
66
spec:
77
timeouts:
88
assert: 2m0s
9-
cleanup: 2m0s
9+
cleanup: 3m0s
1010
delete: 2m0s
1111
error: 2m0s
1212
exec: 2m0s

controllers/dashboard_controller.go

+11-6
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,9 @@ func (r *GrafanaDashboardReconciler) fetchDashboardJson(ctx context.Context, das
473473

474474
func (r *GrafanaDashboardReconciler) getDashboardEnvs(ctx context.Context, dashboard *v1beta1.GrafanaDashboard) (map[string]string, error) {
475475
envs := make(map[string]string)
476+
if dashboard.Spec.EnvsFrom == nil && dashboard.Spec.Envs == nil {
477+
return nil, fmt.Errorf("dashboard.Spec.Envs or dashboard.Spec.EnvFrom nil, can't get envs for dashboard: %s", dashboard.Name)
478+
}
476479
if dashboard.Spec.EnvsFrom != nil {
477480
for _, ref := range dashboard.Spec.EnvsFrom {
478481
key, val, err := r.getReferencedValue(ctx, dashboard, ref)
@@ -508,20 +511,22 @@ func (r *GrafanaDashboardReconciler) getReferencedValue(ctx context.Context, cr
508511
if val, ok := s.Data[source.SecretKeyRef.Key]; ok {
509512
return source.SecretKeyRef.Key, string(val), nil
510513
} else {
511-
return "", "", fmt.Errorf("missing key %s in secret %s", source.SecretKeyRef.Key, source.ConfigMapKeyRef.Name)
514+
return "", "", fmt.Errorf("missing key %s in secret %s", source.SecretKeyRef.Key, source.SecretKeyRef.Name)
512515
}
513-
} else {
516+
}
517+
if source.ConfigMapKeyRef != nil {
514518
s := &v1.ConfigMap{}
515-
err := r.Client.Get(ctx, client.ObjectKey{Namespace: cr.Namespace, Name: source.SecretKeyRef.Name}, s)
519+
err := r.Client.Get(ctx, client.ObjectKey{Namespace: cr.Namespace, Name: source.ConfigMapKeyRef.Name}, s)
516520
if err != nil {
517521
return "", "", err
518522
}
519-
if val, ok := s.Data[source.SecretKeyRef.Key]; ok {
520-
return source.SecretKeyRef.Key, val, nil
523+
if val, ok := s.Data[source.ConfigMapKeyRef.Key]; ok {
524+
return source.ConfigMapKeyRef.Key, val, nil
521525
} else {
522-
return "", "", fmt.Errorf("missing key %s in configmap %s", source.SecretKeyRef.Key, source.ConfigMapKeyRef.Name)
526+
return "", "", fmt.Errorf("missing key %s in configmap %s", source.ConfigMapKeyRef.Key, source.ConfigMapKeyRef.Name)
523527
}
524528
}
529+
return "", "", fmt.Errorf("source couldn't be parsed source: %s", source)
525530
}
526531

527532
// getDashboardModel resolves datasources, updates uid (if needed) and converts raw json to type grafana client accepts

tests/e2e/example-test/05-dashboard.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ spec:
1414
matchLabels:
1515
dashboards: "grafana"
1616
envFrom:
17-
- configMapRef:
17+
- configMapKeyRef:
1818
name: grafana-user-envs
19+
key: "CUSTOM_RANGE_ENV"
1920
plugins:
2021
- name: grafana-piechart-panel
2122
version: 1.3.9

0 commit comments

Comments
 (0)