|
5 | 5 | "testing"
|
6 | 6 |
|
7 | 7 | v1beta1 "github.com/grafana/grafana-operator/v5/api/v1beta1"
|
| 8 | + grafanaclient "github.com/grafana/grafana-operator/v5/controllers/client" |
8 | 9 | "github.com/stretchr/testify/assert"
|
9 | 10 | "github.com/stretchr/testify/require"
|
10 | 11 | corev1 "k8s.io/api/core/v1"
|
@@ -42,64 +43,132 @@ func TestGetDatasourceContent(t *testing.T) {
|
42 | 43 | })
|
43 | 44 | }
|
44 | 45 |
|
45 |
| -var _ = Describe("Datasource: Reconciler", func() { |
| 46 | +var _ = Describe("Datasource: substitute reference values", func() { |
46 | 47 | It("Correctly substitutes valuesFrom", func() {
|
47 |
| - cm := corev1.ConfigMap{ |
| 48 | + cm := &corev1.ConfigMap{ |
48 | 49 | ObjectMeta: metav1.ObjectMeta{
|
49 |
| - Name: "test-valuesfrom-plain", |
50 | 50 | Namespace: "default",
|
| 51 | + Name: "ds-valuesfrom-configmap", |
51 | 52 | },
|
52 | 53 | Data: map[string]string{
|
53 |
| - "CUSTOM_URL": "https://demo.promlabs.com", |
54 |
| - "CUSTOM_TRACEID": "substituted", |
| 54 | + "customTraceId": "substituted", |
55 | 55 | },
|
56 | 56 | }
|
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{ |
60 | 68 | ObjectMeta: metav1.ObjectMeta{
|
61 | 69 | Namespace: "default",
|
| 70 | + Name: "substitute-reference-values", |
62 | 71 | },
|
63 | 72 | 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", |
64 | 81 | 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 | + }, |
65 | 93 | {
|
66 | 94 | TargetPath: "url",
|
67 | 95 | ValueFrom: v1beta1.ValueFromSource{
|
68 |
| - ConfigMapKeyRef: &corev1.ConfigMapKeySelector{ |
| 96 | + SecretKeyRef: &corev1.SecretKeySelector{ |
69 | 97 | LocalObjectReference: corev1.LocalObjectReference{
|
70 |
| - Name: cm.Name, |
| 98 | + Name: sc.Name, |
71 | 99 | },
|
72 |
| - Key: "CUSTOM_URL", |
| 100 | + Key: "URL", |
73 | 101 | },
|
74 | 102 | },
|
75 | 103 | },
|
76 | 104 | {
|
77 |
| - TargetPath: "jsonData.list[0].value", |
| 105 | + TargetPath: "jsonData.exemplarTraceIdDestinations[1].name", |
78 | 106 | ValueFrom: v1beta1.ValueFromSource{
|
79 | 107 | ConfigMapKeyRef: &corev1.ConfigMapKeySelector{
|
80 | 108 | LocalObjectReference: corev1.LocalObjectReference{
|
81 | 109 | Name: cm.Name,
|
82 | 110 | },
|
83 |
| - Key: "CUSTOM_TRACEID", |
| 111 | + Key: "customTraceId", |
84 | 112 | },
|
85 | 113 | },
|
86 | 114 | },
|
87 | 115 | },
|
88 | 116 | 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 | + }`)), |
91 | 133 | },
|
92 | 134 | },
|
93 | 135 | }
|
| 136 | + Expect(k8sClient.Create(testCtx, cm)).Should(Succeed()) |
| 137 | + Expect(k8sClient.Create(testCtx, sc)).Should(Succeed()) |
| 138 | + Expect(k8sClient.Create(testCtx, ds)).Should(Succeed()) |
94 | 139 |
|
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) |
97 | 143 | 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) |
101 | 170 | Expect(err).ToNot(HaveOccurred())
|
102 |
| - Expect(marshaled).To(ContainSubstring(cm.Data["CUSTOM_TRACEID"])) |
| 171 | + Expect(jsonData.ExemplarTraceIDDestinations[1].Name).To(Equal("substituted")) |
103 | 172 | })
|
104 | 173 | })
|
105 | 174 |
|
|
0 commit comments