Skip to content

Commit

Permalink
Merge branch 'master' into dependabot/go_modules/github.com/grafana/g…
Browse files Browse the repository at this point in the history
…rafana-api-golang-client-0.20.1
  • Loading branch information
NissesSenap authored Jun 9, 2023
2 parents 0480267 + 459fed4 commit c3b858c
Show file tree
Hide file tree
Showing 20 changed files with 435 additions and 299 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
fetch-depth: 0
- id: changed-files
name: Get changed files
uses: tj-actions/changed-files@b2d17f51244a144849c6b37a3a6791b98a51d86f #v35.9.2
uses: tj-actions/changed-files@b13786805affca18e536ed489687d3d8d1f05d21 #v36.0.17
with:
files_ignore: |
**/*.md
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pr-validation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ jobs:
go-version-file: "go.mod"

- name: golangci-lint
uses: golangci/golangci-lint-action@v3.4.0
uses: golangci/golangci-lint-action@v3.5.0
with:
version: "v1.51.2"

Expand Down
28 changes: 4 additions & 24 deletions api/v1beta1/grafanadashboard_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ package v1beta1
import (
"bytes"
"compress/gzip"
"crypto/sha256"
"encoding/json"
"fmt"
"io"
"time"

Expand Down Expand Up @@ -140,12 +137,6 @@ type GrafanaDashboardList struct {
Items []GrafanaDashboard `json:"items"`
}

func (in *GrafanaDashboard) Hash(dashboardJson []byte) string {
hash := sha256.New()
hash.Write(dashboardJson)
return fmt.Sprintf("%x", hash.Sum(nil))
}

func (in *GrafanaDashboard) Unchanged(hash string) bool {
return in.Status.Hash == hash
}
Expand Down Expand Up @@ -226,28 +217,17 @@ func (in *GrafanaDashboard) IsAllowCrossNamespaceImport() bool {
return false
}

func (in *GrafanaDashboard) IsUpdatedUID(dashboardJson []byte) bool {
func (in *GrafanaDashboard) IsUpdatedUID(uid string) bool {
// Dashboard has just been created, status is not yet updated
if in.Status.UID == "" {
return false
}

type DashboardUID struct {
UID string `json:"uid,omitempty"`
}

dashboardUID := DashboardUID{}
err := json.Unmarshal(dashboardJson, &dashboardUID)
// here, we don't really care about catching json errors
if err != nil {
return false
}

if dashboardUID.UID == "" {
dashboardUID.UID = string(in.UID)
if uid == "" {
uid = string(in.UID)
}

return in.Status.UID != dashboardUID.UID
return in.Status.UID != uid
}

func Gunzip(compressed []byte) ([]byte, error) {
Expand Down
81 changes: 81 additions & 0 deletions api/v1beta1/grafanadashboard_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)

func TestGrafanaDashboardStatus_getContentCache(t *testing.T) {
Expand Down Expand Up @@ -101,3 +102,83 @@ func Test_Gzip(t *testing.T) {

assert.Equal(t, dashboardJSON, decompressed, "Decompressed dashboard should match the original")
}

func TestGrafanaDashboardIsUpdatedUID(t *testing.T) {
crUID := "crUID"
tests := []struct {
name string
crUID string
statusUID string
dashboardUID string
want bool
}{
{
name: "Status.UID and dashboard UID are empty",
crUID: crUID,
statusUID: "",
dashboardUID: "newUID",
want: false,
},
{
name: "Status.UID is empty, dashboard UID is not",
crUID: crUID,
statusUID: "",
dashboardUID: "newUID",
want: false,
},
{
name: "Status.UID is not empty (same as CR uid), new UID is empty",
crUID: crUID,
statusUID: crUID,
dashboardUID: "",
want: false,
},
{
name: "Status.UID is not empty (different from CR uid), new UID is empty",
crUID: crUID,
statusUID: "oldUID",
dashboardUID: "",
want: true,
},
{
name: "Status.UID is not empty, new UID is different",
crUID: crUID,
statusUID: "oldUID",
dashboardUID: "newUID",
want: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cr := getDashboardCR(t, tt.crUID, tt.statusUID)
got := cr.IsUpdatedUID(tt.dashboardUID)
assert.Equal(t, tt.want, got)
})
}
}

func getDashboardCR(t *testing.T, crUID string, statusUID string) GrafanaDashboard {
t.Helper()

cr := GrafanaDashboard{
TypeMeta: metav1.TypeMeta{},
ObjectMeta: metav1.ObjectMeta{
Name: "mydashboard",
Namespace: "grafana-operator-system",
UID: types.UID(crUID),
},
Spec: GrafanaDashboardSpec{
InstanceSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"dashboard": "grafana",
},
},
},
Status: GrafanaDashboardStatus{
UID: statusUID,
},
}

return cr
}
73 changes: 24 additions & 49 deletions api/v1beta1/grafanadatasource_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package v1beta1

import (
"bytes"
"crypto/sha256"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -100,13 +99,17 @@ type GrafanaDatasourceStatus struct {
LastMessage string `json:"lastMessage,omitempty"`
// The datasource instanceSelector can't find matching grafana instances
NoMatchingInstances bool `json:"NoMatchingInstances,omitempty"`
// Last time the datasource was resynced
LastResync metav1.Time `json:"lastResync,omitempty"`
UID string `json:"uid,omitempty"`
}

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status

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

func (in *GrafanaDatasource) Hash() string {
hash := sha256.New()

if in.Spec.Datasource != nil {
hash.Write([]byte(in.Spec.Datasource.Name))
hash.Write([]byte(in.Spec.Datasource.Access))
hash.Write([]byte(in.Spec.Datasource.BasicAuthUser))
hash.Write(in.Spec.Datasource.JSONData)
hash.Write(in.Spec.Datasource.SecureJSONData)
hash.Write([]byte(in.Spec.Datasource.Database))
hash.Write([]byte(in.Spec.Datasource.Type))
hash.Write([]byte(in.Spec.Datasource.User))
hash.Write([]byte(in.Spec.Datasource.URL))

for _, valueRef := range in.Spec.ValuesFrom {
hash.Write([]byte(valueRef.TargetPath))
if valueRef.ValueFrom.ConfigMapKeyRef != nil {
hash.Write([]byte(valueRef.ValueFrom.ConfigMapKeyRef.Name))
hash.Write([]byte(valueRef.ValueFrom.ConfigMapKeyRef.Key))
}
if valueRef.ValueFrom.SecretKeyRef != nil {
hash.Write([]byte(valueRef.ValueFrom.SecretKeyRef.Name))
hash.Write([]byte(valueRef.ValueFrom.SecretKeyRef.Key))
}
}

if in.Spec.Datasource.BasicAuth != nil && *in.Spec.Datasource.BasicAuth {
hash.Write([]byte("_"))
}

if in.Spec.Datasource.IsDefault != nil && *in.Spec.Datasource.IsDefault {
hash.Write([]byte("_"))
}

if in.Spec.Datasource.OrgID != nil {
hash.Write([]byte(fmt.Sprint(*in.Spec.Datasource.OrgID)))
}

if in.Spec.Datasource.Editable != nil && *in.Spec.Datasource.Editable {
hash.Write([]byte("_"))
}
}

return fmt.Sprintf("%x", hash.Sum(nil))
}

func (in *GrafanaDatasource) GetResyncPeriod() time.Duration {
if in.Spec.ResyncPeriod == "" {
in.Spec.ResyncPeriod = DefaultResyncPeriod
Expand All @@ -186,8 +143,26 @@ func (in *GrafanaDatasource) GetResyncPeriod() time.Duration {
return duration
}

func (in *GrafanaDatasource) Unchanged() bool {
return in.Hash() == in.Status.Hash
func (in *GrafanaDatasource) ResyncPeriodHasElapsed() bool {
deadline := in.Status.LastResync.Add(in.GetResyncPeriod())
return time.Now().After(deadline)
}

func (in *GrafanaDatasource) Unchanged(hash string) bool {
return in.Status.Hash == hash
}

func (in *GrafanaDatasource) IsUpdatedUID(uid string) bool {
// Datasource has just been created, status is not yet updated
if in.Status.UID == "" {
return false
}

if uid == "" {
uid = string(in.UID)
}

return in.Status.UID != uid
}

func (in *GrafanaDatasource) ExpandVariables(variables map[string][]byte) ([]byte, error) {
Expand Down
3 changes: 2 additions & 1 deletion api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ spec:
- jsonPath: .status.NoMatchingInstances
name: No matching instances
type: boolean
- format: date-time
jsonPath: .status.lastResync
name: Last resync
type: date
- jsonPath: .metadata.creationTimestamp
name: Age
type: date
Expand Down Expand Up @@ -155,6 +159,11 @@ spec:
type: string
lastMessage:
type: string
lastResync:
format: date-time
type: string
uid:
type: string
type: object
type: object
served: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ spec:
- jsonPath: .status.NoMatchingInstances
name: No matching instances
type: boolean
- format: date-time
jsonPath: .status.lastResync
name: Last resync
type: date
- jsonPath: .metadata.creationTimestamp
name: Age
type: date
Expand Down Expand Up @@ -156,6 +160,11 @@ spec:
type: string
lastMessage:
type: string
lastResync:
format: date-time
type: string
uid:
type: string
type: object
type: object
served: true
Expand Down
10 changes: 10 additions & 0 deletions config/grafana.integreatly.org_grafanadatasources.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ spec:
- jsonPath: .status.NoMatchingInstances
name: No matching instances
type: boolean
- format: date-time
jsonPath: .status.lastResync
name: Last resync
type: date
- jsonPath: .metadata.creationTimestamp
name: Age
type: date
Expand Down Expand Up @@ -207,6 +211,12 @@ spec:
type: string
lastMessage:
type: string
lastResync:
description: Last time the datasource was resynced
format: date-time
type: string
uid:
type: string
type: object
type: object
served: true
Expand Down
Loading

0 comments on commit c3b858c

Please sign in to comment.