@@ -18,7 +18,6 @@ package v1beta1
18
18
19
19
import (
20
20
"bytes"
21
- "crypto/sha256"
22
21
"encoding/json"
23
22
"errors"
24
23
"fmt"
@@ -100,13 +99,17 @@ type GrafanaDatasourceStatus struct {
100
99
LastMessage string `json:"lastMessage,omitempty"`
101
100
// The datasource instanceSelector can't find matching grafana instances
102
101
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"`
103
105
}
104
106
105
107
//+kubebuilder:object:root=true
106
108
//+kubebuilder:subresource:status
107
109
108
110
// GrafanaDatasource is the Schema for the grafanadatasources API
109
111
// +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=""
110
113
// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp",description=""
111
114
type GrafanaDatasource struct {
112
115
metav1.TypeMeta `json:",inline"`
@@ -125,52 +128,6 @@ type GrafanaDatasourceList struct {
125
128
Items []GrafanaDatasource `json:"items"`
126
129
}
127
130
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
-
174
131
func (in * GrafanaDatasource ) GetResyncPeriod () time.Duration {
175
132
if in .Spec .ResyncPeriod == "" {
176
133
in .Spec .ResyncPeriod = DefaultResyncPeriod
@@ -186,8 +143,26 @@ func (in *GrafanaDatasource) GetResyncPeriod() time.Duration {
186
143
return duration
187
144
}
188
145
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
191
166
}
192
167
193
168
func (in * GrafanaDatasource ) ExpandVariables (variables map [string ][]byte ) ([]byte , error ) {
0 commit comments