Skip to content

Commit ae091ef

Browse files
committed
chore: Migrate datasource_values_from
1 parent b763102 commit ae091ef

File tree

3 files changed

+90
-195
lines changed

3 files changed

+90
-195
lines changed

controllers/datasource_controller_test.go

Lines changed: 90 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"testing"
66

77
v1beta1 "github.com/grafana/grafana-operator/v5/api/v1beta1"
8+
grafanaclient "github.com/grafana/grafana-operator/v5/controllers/client"
89
"github.com/stretchr/testify/assert"
910
"github.com/stretchr/testify/require"
1011
corev1 "k8s.io/api/core/v1"
@@ -42,64 +43,132 @@ func TestGetDatasourceContent(t *testing.T) {
4243
})
4344
}
4445

45-
var _ = Describe("Datasource: Reconciler", func() {
46+
var _ = Describe("Datasource: substitute reference values", func() {
4647
It("Correctly substitutes valuesFrom", func() {
47-
cm := corev1.ConfigMap{
48+
cm := &corev1.ConfigMap{
4849
ObjectMeta: metav1.ObjectMeta{
49-
Name: "test-valuesfrom-plain",
5050
Namespace: "default",
51+
Name: "ds-valuesfrom-configmap",
5152
},
5253
Data: map[string]string{
53-
"CUSTOM_URL": "https://demo.promlabs.com",
54-
"CUSTOM_TRACEID": "substituted",
54+
"customTraceId": "substituted",
5555
},
5656
}
57-
err := k8sClient.Create(testCtx, &cm)
58-
Expect(err).ToNot(HaveOccurred())
59-
cr := &v1beta1.GrafanaDatasource{
57+
sc := &corev1.Secret{
58+
ObjectMeta: metav1.ObjectMeta{
59+
Namespace: "default",
60+
Name: "ds-values-from-secret",
61+
},
62+
StringData: map[string]string{
63+
"PROMETHEUS_TOKEN": "secret_token",
64+
"URL": "https://demo.promlabs.com",
65+
},
66+
}
67+
ds := &v1beta1.GrafanaDatasource{
6068
ObjectMeta: metav1.ObjectMeta{
6169
Namespace: "default",
70+
Name: "substitute-reference-values",
6271
},
6372
Spec: v1beta1.GrafanaDatasourceSpec{
73+
GrafanaCommonSpec: v1beta1.GrafanaCommonSpec{
74+
InstanceSelector: &metav1.LabelSelector{
75+
MatchLabels: map[string]string{
76+
"dashboards": "grafana",
77+
},
78+
},
79+
},
80+
CustomUID: "substitute",
6481
ValuesFrom: []v1beta1.ValueFrom{
82+
{
83+
TargetPath: "secureJsonData.httpHeaderValue1",
84+
ValueFrom: v1beta1.ValueFromSource{
85+
SecretKeyRef: &corev1.SecretKeySelector{
86+
LocalObjectReference: corev1.LocalObjectReference{
87+
Name: sc.Name,
88+
},
89+
Key: "PROMETHEUS_TOKEN",
90+
},
91+
},
92+
},
6593
{
6694
TargetPath: "url",
6795
ValueFrom: v1beta1.ValueFromSource{
68-
ConfigMapKeyRef: &corev1.ConfigMapKeySelector{
96+
SecretKeyRef: &corev1.SecretKeySelector{
6997
LocalObjectReference: corev1.LocalObjectReference{
70-
Name: cm.Name,
98+
Name: sc.Name,
7199
},
72-
Key: "CUSTOM_URL",
100+
Key: "URL",
73101
},
74102
},
75103
},
76104
{
77-
TargetPath: "jsonData.list[0].value",
105+
TargetPath: "jsonData.exemplarTraceIdDestinations[1].name",
78106
ValueFrom: v1beta1.ValueFromSource{
79107
ConfigMapKeyRef: &corev1.ConfigMapKeySelector{
80108
LocalObjectReference: corev1.LocalObjectReference{
81109
Name: cm.Name,
82110
},
83-
Key: "CUSTOM_TRACEID",
111+
Key: "customTraceId",
84112
},
85113
},
86114
},
87115
},
88116
Datasource: &v1beta1.GrafanaDatasourceInternal{
89-
URL: "${CUSTOM_URL}",
90-
JSONData: json.RawMessage([]byte(`{"list":[{"value":"${CUSTOM_TRACEID}"}]}`)),
117+
Name: "substitute-prometheus",
118+
Type: "prometheus",
119+
Access: "proxy",
120+
URL: "${URL}",
121+
JSONData: json.RawMessage([]byte(`{
122+
"tlsSkipVerify": true,
123+
"timeInterval": "10s",
124+
"httpHeaderName1": "Authorization",
125+
"exemplarTraceIdDestinations": [
126+
{"name": "traceID"},
127+
{"name": "${customTraceId}"}
128+
]
129+
}`)),
130+
SecureJSONData: json.RawMessage([]byte(`{
131+
"httpHeaderValue1": "Bearer ${PROMETHEUS_TOKEN}"
132+
}`)),
91133
},
92134
},
93135
}
136+
Expect(k8sClient.Create(testCtx, cm)).Should(Succeed())
137+
Expect(k8sClient.Create(testCtx, sc)).Should(Succeed())
138+
Expect(k8sClient.Create(testCtx, ds)).Should(Succeed())
94139

95-
r := GrafanaDatasourceReconciler{Client: k8sClient}
96-
content, hash, err := r.buildDatasourceModel(testCtx, cr)
140+
req := requestFromMeta(ds.ObjectMeta)
141+
r := GrafanaDatasourceReconciler{Client: k8sClient, Scheme: k8sClient.Scheme()}
142+
_, err := r.Reconcile(testCtx, req)
97143
Expect(err).ToNot(HaveOccurred())
98-
Expect(hash).ToNot(BeEmpty())
99-
Expect(content.URL).To(Equal(cm.Data["CUSTOM_URL"]))
100-
marshaled, err := json.Marshal(content.JSONData)
144+
145+
Expect(r.Get(testCtx, req.NamespacedName, ds)).Should(Succeed())
146+
Expect(ds.Status.Conditions).Should(ContainElement(HaveField("Type", conditionDatasourceSynchronized)))
147+
Expect(ds.Status.Conditions).Should(ContainElement(HaveField("Reason", conditionReasonApplySuccessful)))
148+
149+
cl, err := grafanaclient.NewGeneratedGrafanaClient(testCtx, k8sClient, externalGrafanaCr)
150+
Expect(err).ToNot(HaveOccurred())
151+
152+
model, err := cl.Datasources.GetDataSourceByUID(ds.Spec.CustomUID)
153+
Expect(err).ToNot(HaveOccurred())
154+
155+
Expect(model.Payload.URL).To(Equal("https://demo.promlabs.com"))
156+
Expect(model.Payload.SecureJSONFields["httpHeaderValue1"]).To(BeTrue())
157+
158+
// Serialize and Derserialize jsonData
159+
b, err := json.Marshal(model.Payload.JSONData)
160+
Expect(err).ToNot(HaveOccurred())
161+
162+
type ExemplarTraceIDDestination struct {
163+
Name string `json:"name"`
164+
}
165+
type SubstitutedJSONData struct {
166+
ExemplarTraceIDDestinations []ExemplarTraceIDDestination `json:"exemplarTraceIdDestinations"`
167+
}
168+
var jsonData SubstitutedJSONData // map with array of
169+
err = json.Unmarshal(b, &jsonData)
101170
Expect(err).ToNot(HaveOccurred())
102-
Expect(marshaled).To(ContainSubstring(cm.Data["CUSTOM_TRACEID"]))
171+
Expect(jsonData.ExemplarTraceIDDestinations[1].Name).To(Equal("substituted"))
103172
})
104173
})
105174

tests/e2e/datasource_values_from/chainsaw-test.yaml

Lines changed: 0 additions & 100 deletions
This file was deleted.

tests/e2e/datasource_values_from/resources.yaml

Lines changed: 0 additions & 74 deletions
This file was deleted.

0 commit comments

Comments
 (0)