Skip to content

Commit c0eb53b

Browse files
committed
chore: Use "Exists" for a simpler implementation
1 parent 014f626 commit c0eb53b

7 files changed

+20
-24
lines changed

api/v1beta1/grafanacontactpoint_types.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,13 @@ type GrafanaContactPointList struct {
7777
Items []GrafanaContactPoint `json:"items"`
7878
}
7979

80-
func (in *GrafanaContactPointList) Find(namespace string, name string) *GrafanaContactPoint {
80+
func (in *GrafanaContactPointList) Exists(namespace, name string) bool {
8181
for _, contactpoint := range in.Items {
8282
if contactpoint.Namespace == namespace && contactpoint.Name == name {
83-
return &contactpoint
83+
return true
8484
}
8585
}
86-
return nil
86+
return false
8787
}
8888

8989
// Wrapper around CustomUID or default metadata.uid

api/v1beta1/grafanadashboard_types.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,13 @@ func (in *GrafanaDashboard) GrafanaContentStatus() *GrafanaContentStatus {
128128

129129
var _ GrafanaContentResource = &GrafanaDashboard{}
130130

131-
func (in *GrafanaDashboardList) Find(namespace string, name string) *GrafanaDashboard {
131+
func (in *GrafanaDashboardList) Exists(namespace, name string) bool {
132132
for _, dashboard := range in.Items {
133133
if dashboard.Namespace == namespace && dashboard.Name == name {
134-
return &dashboard
134+
return true
135135
}
136136
}
137-
return nil
137+
return false
138138
}
139139

140140
func (in *GrafanaDashboard) MatchLabels() *metav1.LabelSelector {

api/v1beta1/grafanadatasource_types.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,13 @@ func (in *GrafanaDatasource) CustomUIDOrUID() string {
152152
return string(in.UID)
153153
}
154154

155-
func (in *GrafanaDatasourceList) Find(namespace string, name string) *GrafanaDatasource {
155+
func (in *GrafanaDatasourceList) Exists(namespace, name string) bool {
156156
for _, datasource := range in.Items {
157157
if datasource.Namespace == namespace && datasource.Name == name {
158-
return &datasource
158+
return true
159159
}
160160
}
161-
return nil
161+
return false
162162
}
163163

164164
func (in *GrafanaDatasource) MatchLabels() *metav1.LabelSelector {

api/v1beta1/grafanafolder_types.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,13 @@ func init() {
133133
SchemeBuilder.Register(&GrafanaFolder{}, &GrafanaFolderList{})
134134
}
135135

136-
func (in *GrafanaFolderList) Find(namespace string, name string) *GrafanaFolder {
136+
func (in *GrafanaFolderList) Exists(namespace, name string) bool {
137137
for _, folder := range in.Items {
138138
if folder.Namespace == namespace && folder.Name == name {
139-
return &folder
139+
return true
140140
}
141141
}
142-
return nil
142+
return false
143143
}
144144

145145
func (in *GrafanaFolder) Hash() string {

api/v1beta1/grafanalibrarypanel_types.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,13 @@ func (in *GrafanaLibraryPanel) GrafanaContentStatus() *GrafanaContentStatus {
116116

117117
var _ GrafanaContentResource = &GrafanaLibraryPanel{}
118118

119-
func (in *GrafanaLibraryPanelList) Find(namespace string, name string) *GrafanaLibraryPanel {
119+
func (in *GrafanaLibraryPanelList) Exists(namespace, name string) bool {
120120
for _, e := range in.Items {
121121
if e.Namespace == namespace && e.Name == name {
122-
return &e
122+
return true
123123
}
124124
}
125-
return nil
125+
return false
126126
}
127127

128128
func init() {

api/v1beta1/namespaced_resource.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ type NamespacedResource string
1010
type NamespacedResourceList []NamespacedResource
1111

1212
// +kubebuilder:object:generate=false
13-
type NamespacedResourceImpl[T interface{}] interface {
14-
Find(namespace string, name string) *T
13+
type NamespacedResourceImpl interface {
14+
Exists(namespace string, name string) bool
1515
}
1616

1717
func (in NamespacedResource) Split() (string, string, string) {

controllers/grafana_controller.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,10 @@ func (r *GrafanaReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
135135
return ctrl.Result{}, nil
136136
}
137137

138-
func removeMissingCRs[T interface{}](statusList *grafanav1beta1.NamespacedResourceList, crs grafanav1beta1.NamespacedResourceImpl[T], updateStatus *bool) {
138+
func removeMissingCRs(statusList *grafanav1beta1.NamespacedResourceList, crs grafanav1beta1.NamespacedResourceImpl, updateStatus *bool) {
139139
for _, namespacedCR := range *statusList {
140140
namespace, name, _ := namespacedCR.Split()
141-
if crs.Find(namespace, name) == nil {
141+
if !crs.Exists(namespace, name) {
142142
*statusList = statusList.Remove(namespace, name)
143143
*updateStatus = true
144144
}
@@ -159,35 +159,31 @@ func (r *GrafanaReconciler) syncStatuses(ctx context.Context) error {
159159
return nil
160160
}
161161

162-
// folders
162+
// Fetch all resources
163163
folders := &grafanav1beta1.GrafanaFolderList{}
164164
err = r.List(ctx, folders)
165165
if err != nil {
166166
return err
167167
}
168168

169-
// dashboards
170169
dashboards := &grafanav1beta1.GrafanaDashboardList{}
171170
err = r.List(ctx, dashboards)
172171
if err != nil {
173172
return err
174173
}
175174

176-
// library libraryPanels
177175
libraryPanels := &grafanav1beta1.GrafanaLibraryPanelList{}
178176
err = r.List(ctx, libraryPanels)
179177
if err != nil {
180178
return err
181179
}
182180

183-
// datasources
184181
datasources := &grafanav1beta1.GrafanaDatasourceList{}
185182
err = r.List(ctx, datasources)
186183
if err != nil {
187184
return err
188185
}
189186

190-
// contact points
191187
contactPoints := &grafanav1beta1.GrafanaContactPointList{}
192188
err = r.List(ctx, contactPoints)
193189
if err != nil {

0 commit comments

Comments
 (0)