Skip to content

Commit 459fed4

Browse files
authored
Merge pull request #1070 from grafana-operator/feat/datasource-hardcoded-uid
feat/fix(datasource): support for hardcoded uid, multiple fixes
2 parents 72a0521 + c817b5a commit 459fed4

9 files changed

+182
-111
lines changed

api/v1beta1/grafanadatasource_types.go

+24-49
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package v1beta1
1818

1919
import (
2020
"bytes"
21-
"crypto/sha256"
2221
"encoding/json"
2322
"errors"
2423
"fmt"
@@ -100,13 +99,17 @@ type GrafanaDatasourceStatus struct {
10099
LastMessage string `json:"lastMessage,omitempty"`
101100
// The datasource instanceSelector can't find matching grafana instances
102101
NoMatchingInstances bool `json:"NoMatchingInstances,omitempty"`
102+
// Last time the datasource was resynced
103+
LastResync metav1.Time `json:"lastResync,omitempty"`
104+
UID string `json:"uid,omitempty"`
103105
}
104106

105107
//+kubebuilder:object:root=true
106108
//+kubebuilder:subresource:status
107109

108110
// GrafanaDatasource is the Schema for the grafanadatasources API
109111
// +kubebuilder:printcolumn:name="No matching instances",type="boolean",JSONPath=".status.NoMatchingInstances",description=""
112+
// +kubebuilder:printcolumn:name="Last resync",type="date",format="date-time",JSONPath=".status.lastResync",description=""
110113
// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp",description=""
111114
type GrafanaDatasource struct {
112115
metav1.TypeMeta `json:",inline"`
@@ -125,52 +128,6 @@ type GrafanaDatasourceList struct {
125128
Items []GrafanaDatasource `json:"items"`
126129
}
127130

128-
func (in *GrafanaDatasource) Hash() string {
129-
hash := sha256.New()
130-
131-
if in.Spec.Datasource != nil {
132-
hash.Write([]byte(in.Spec.Datasource.Name))
133-
hash.Write([]byte(in.Spec.Datasource.Access))
134-
hash.Write([]byte(in.Spec.Datasource.BasicAuthUser))
135-
hash.Write(in.Spec.Datasource.JSONData)
136-
hash.Write(in.Spec.Datasource.SecureJSONData)
137-
hash.Write([]byte(in.Spec.Datasource.Database))
138-
hash.Write([]byte(in.Spec.Datasource.Type))
139-
hash.Write([]byte(in.Spec.Datasource.User))
140-
hash.Write([]byte(in.Spec.Datasource.URL))
141-
142-
for _, valueRef := range in.Spec.ValuesFrom {
143-
hash.Write([]byte(valueRef.TargetPath))
144-
if valueRef.ValueFrom.ConfigMapKeyRef != nil {
145-
hash.Write([]byte(valueRef.ValueFrom.ConfigMapKeyRef.Name))
146-
hash.Write([]byte(valueRef.ValueFrom.ConfigMapKeyRef.Key))
147-
}
148-
if valueRef.ValueFrom.SecretKeyRef != nil {
149-
hash.Write([]byte(valueRef.ValueFrom.SecretKeyRef.Name))
150-
hash.Write([]byte(valueRef.ValueFrom.SecretKeyRef.Key))
151-
}
152-
}
153-
154-
if in.Spec.Datasource.BasicAuth != nil && *in.Spec.Datasource.BasicAuth {
155-
hash.Write([]byte("_"))
156-
}
157-
158-
if in.Spec.Datasource.IsDefault != nil && *in.Spec.Datasource.IsDefault {
159-
hash.Write([]byte("_"))
160-
}
161-
162-
if in.Spec.Datasource.OrgID != nil {
163-
hash.Write([]byte(fmt.Sprint(*in.Spec.Datasource.OrgID)))
164-
}
165-
166-
if in.Spec.Datasource.Editable != nil && *in.Spec.Datasource.Editable {
167-
hash.Write([]byte("_"))
168-
}
169-
}
170-
171-
return fmt.Sprintf("%x", hash.Sum(nil))
172-
}
173-
174131
func (in *GrafanaDatasource) GetResyncPeriod() time.Duration {
175132
if in.Spec.ResyncPeriod == "" {
176133
in.Spec.ResyncPeriod = DefaultResyncPeriod
@@ -186,8 +143,26 @@ func (in *GrafanaDatasource) GetResyncPeriod() time.Duration {
186143
return duration
187144
}
188145

189-
func (in *GrafanaDatasource) Unchanged() bool {
190-
return in.Hash() == in.Status.Hash
146+
func (in *GrafanaDatasource) ResyncPeriodHasElapsed() bool {
147+
deadline := in.Status.LastResync.Add(in.GetResyncPeriod())
148+
return time.Now().After(deadline)
149+
}
150+
151+
func (in *GrafanaDatasource) Unchanged(hash string) bool {
152+
return in.Status.Hash == hash
153+
}
154+
155+
func (in *GrafanaDatasource) IsUpdatedUID(uid string) bool {
156+
// Datasource has just been created, status is not yet updated
157+
if in.Status.UID == "" {
158+
return false
159+
}
160+
161+
if uid == "" {
162+
uid = string(in.UID)
163+
}
164+
165+
return in.Status.UID != uid
191166
}
192167

193168
func (in *GrafanaDatasource) ExpandVariables(variables map[string][]byte) ([]byte, error) {

api/v1beta1/zz_generated.deepcopy.go

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bundle/manifests/grafana.integreatly.org_grafanadatasources.yaml

+9
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ spec:
1818
- jsonPath: .status.NoMatchingInstances
1919
name: No matching instances
2020
type: boolean
21+
- format: date-time
22+
jsonPath: .status.lastResync
23+
name: Last resync
24+
type: date
2125
- jsonPath: .metadata.creationTimestamp
2226
name: Age
2327
type: date
@@ -155,6 +159,11 @@ spec:
155159
type: string
156160
lastMessage:
157161
type: string
162+
lastResync:
163+
format: date-time
164+
type: string
165+
uid:
166+
type: string
158167
type: object
159168
type: object
160169
served: true

config/crd/bases/grafana.integreatly.org_grafanadatasources.yaml

+9
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ spec:
1919
- jsonPath: .status.NoMatchingInstances
2020
name: No matching instances
2121
type: boolean
22+
- format: date-time
23+
jsonPath: .status.lastResync
24+
name: Last resync
25+
type: date
2226
- jsonPath: .metadata.creationTimestamp
2327
name: Age
2428
type: date
@@ -156,6 +160,11 @@ spec:
156160
type: string
157161
lastMessage:
158162
type: string
163+
lastResync:
164+
format: date-time
165+
type: string
166+
uid:
167+
type: string
159168
type: object
160169
type: object
161170
served: true

config/grafana.integreatly.org_grafanadatasources.yaml

+10
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ spec:
1919
- jsonPath: .status.NoMatchingInstances
2020
name: No matching instances
2121
type: boolean
22+
- format: date-time
23+
jsonPath: .status.lastResync
24+
name: Last resync
25+
type: date
2226
- jsonPath: .metadata.creationTimestamp
2327
name: Age
2428
type: date
@@ -207,6 +211,12 @@ spec:
207211
type: string
208212
lastMessage:
209213
type: string
214+
lastResync:
215+
description: Last time the datasource was resynced
216+
format: date-time
217+
type: string
218+
uid:
219+
type: string
210220
type: object
211221
type: object
212222
served: true

0 commit comments

Comments
 (0)