Skip to content

Commit

Permalink
Refactor: Update GrafanaDatasource reconcile loop (#1826)
Browse files Browse the repository at this point in the history
* chore: Implement CommonResource for GrafanaDatasource

* feat: GetScopedMatchingInstances for Reconcile

feat: NoMatchingInstances condition

* fix: Datasource logging of errors

refactor: Call Update Status once in Defer

feat: Add NoMatchingInstances condition

* chore: Datasource field cannot be nil, field is neither nullable or optional

test: Verify spec.Datasource is never nil

* feat: Add finalizers and cleanup check

chore: Update Datasource finalize function

* fix: Ignore status updates

* chore: Mark status.lastMessage Deprecated

test: Fix e2e assert failing after lastMessage deprecation

* chore: Move model mutation into getDatasourceContent

* feat: InvalidSpec condition when json or valuesFrom are invalid

refactor: BuildDatasourceModel aligned with contactpoint

* test: Add GrafanaDatasource assert

* fix: Ignore missing datasources on finalize

* fix: Apply review comments
  • Loading branch information
Baarsgaard authored Jan 30, 2025
1 parent ff3fb07 commit cadf108
Show file tree
Hide file tree
Showing 12 changed files with 229 additions and 139 deletions.
19 changes: 14 additions & 5 deletions api/v1beta1/grafanadatasource_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ type GrafanaDatasourceSpec struct {
type GrafanaDatasourceStatus struct {
GrafanaCommonStatus `json:",inline"`

Hash string `json:"hash,omitempty"`
Hash string `json:"hash,omitempty"`
// Deprecated: Check status.conditions or operator logs
LastMessage string `json:"lastMessage,omitempty"`
// The datasource instanceSelector can't find matching grafana instances
NoMatchingInstances bool `json:"NoMatchingInstances,omitempty"`
Expand Down Expand Up @@ -149,10 +150,6 @@ func (in *GrafanaDatasource) CustomUIDOrUID() string {
return string(in.ObjectMeta.UID)
}

func (in *GrafanaDatasource) IsAllowCrossNamespaceImport() bool {
return in.Spec.AllowCrossNamespaceImport
}

func (in *GrafanaDatasourceList) Find(namespace string, name string) *GrafanaDatasource {
for _, datasource := range in.Items {
if datasource.Namespace == namespace && datasource.Name == name {
Expand All @@ -162,6 +159,18 @@ func (in *GrafanaDatasourceList) Find(namespace string, name string) *GrafanaDat
return nil
}

func (in *GrafanaDatasource) MatchLabels() *metav1.LabelSelector {
return in.Spec.InstanceSelector
}

func (in *GrafanaDatasource) MatchNamespace() string {
return in.ObjectMeta.Namespace
}

func (in *GrafanaDatasource) AllowCrossNamespace() bool {
return in.Spec.AllowCrossNamespaceImport
}

func init() {
SchemeBuilder.Register(&GrafanaDatasource{}, &GrafanaDatasourceList{})
}
24 changes: 24 additions & 0 deletions api/v1beta1/grafanadatasource_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,27 @@ var _ = Describe("Datasource type", func() {
})
})
})

var _ = Describe("Fail on field behavior changes", func() {
emptyDatasource := &GrafanaDatasource{
TypeMeta: v1.TypeMeta{
APIVersion: APIVersion,
Kind: "GrafanaDatasource",
},
ObjectMeta: v1.ObjectMeta{
Name: "test-nil-datasource",
Namespace: "default",
},
Spec: GrafanaDatasourceSpec{
GrafanaCommonSpec: GrafanaCommonSpec{
InstanceSelector: &v1.LabelSelector{},
},
Datasource: nil,
},
}

ctx := context.Background()
It("Fails creating GrafanaDatasource with undefined spec.datasource", func() {
Expect(k8sClient.Create(ctx, emptyDatasource)).To(HaveOccurred())
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ spec:
hash:
type: string
lastMessage:
description: 'Deprecated: Check status.conditions or operator logs'
type: string
lastResync:
description: Last time the resource was synchronized with Grafana
Expand Down
Loading

0 comments on commit cadf108

Please sign in to comment.