Skip to content

Commit

Permalink
refactor: GetMatchingInstances to take interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Baarsgaard committed Nov 17, 2024
1 parent 7c47700 commit f6c4c23
Show file tree
Hide file tree
Showing 14 changed files with 43 additions and 11 deletions.
7 changes: 7 additions & 0 deletions api/common_interfaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package api

import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

type CommonResource interface {
MatchConditions() (*metav1.LabelSelector, string, bool)
}
4 changes: 4 additions & 0 deletions api/v1beta1/grafanaalertrulegroup_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,7 @@ type GrafanaAlertRuleGroupList struct {
func init() {
SchemeBuilder.Register(&GrafanaAlertRuleGroup{}, &GrafanaAlertRuleGroupList{})
}

func (in *GrafanaAlertRuleGroup) MatchConditions() (*metav1.LabelSelector, string, bool) {
return in.Spec.InstanceSelector, in.ObjectMeta.Namespace, in.Spec.AllowCrossNamespaceImport
}
4 changes: 4 additions & 0 deletions api/v1beta1/grafanacontactpoint_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,7 @@ func (in *GrafanaContactPoint) ResyncPeriodHasElapsed() bool {
deadline := in.Status.LastResync.Add(in.Spec.ResyncPeriod.Duration)
return time.Now().After(deadline)
}

func (in *GrafanaContactPoint) MatchConditions() (*metav1.LabelSelector, string, bool) {
return in.Spec.InstanceSelector, in.ObjectMeta.Namespace, in.Spec.AllowCrossNamespaceImport
}
4 changes: 4 additions & 0 deletions api/v1beta1/grafanadashboard_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,3 +354,7 @@ func (in *GrafanaDashboardList) Find(namespace string, name string) *GrafanaDash
func init() {
SchemeBuilder.Register(&GrafanaDashboard{}, &GrafanaDashboardList{})
}

func (in *GrafanaDashboard) MatchConditions() (*metav1.LabelSelector, string, bool) {
return in.Spec.InstanceSelector, in.ObjectMeta.Namespace, in.Spec.AllowCrossNamespaceImport
}
4 changes: 4 additions & 0 deletions api/v1beta1/grafanadatasource_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,7 @@ func (in *GrafanaDatasourceList) Find(namespace string, name string) *GrafanaDat
func init() {
SchemeBuilder.Register(&GrafanaDatasource{}, &GrafanaDatasourceList{})
}

func (in *GrafanaDatasource) MatchConditions() (*metav1.LabelSelector, string, bool) {
return in.Spec.InstanceSelector, in.ObjectMeta.Namespace, in.Spec.AllowCrossNamespaceImport
}
4 changes: 4 additions & 0 deletions api/v1beta1/grafanafolder_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,7 @@ func (in *GrafanaFolder) ResyncPeriodHasElapsed() bool {
deadline := in.Status.LastResync.Add(in.Spec.ResyncPeriod.Duration)
return time.Now().After(deadline)
}

func (in *GrafanaFolder) MatchConditions() (*metav1.LabelSelector, string, bool) {
return in.Spec.InstanceSelector, in.ObjectMeta.Namespace, in.Spec.AllowCrossNamespaceImport
}
4 changes: 4 additions & 0 deletions api/v1beta1/grafananotificationpolicy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,7 @@ type GrafanaNotificationPolicyList struct {
func init() {
SchemeBuilder.Register(&GrafanaNotificationPolicy{}, &GrafanaNotificationPolicyList{})
}

func (in *GrafanaNotificationPolicy) MatchConditions() (*metav1.LabelSelector, string, bool) {
return in.Spec.InstanceSelector, in.ObjectMeta.Namespace, in.Spec.AllowCrossNamespaceImport
}
11 changes: 6 additions & 5 deletions controllers/controller_shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,16 @@ const (

//+kubebuilder:rbac:groups=coordination.k8s.io,resources=leases,verbs=get;list;watch;create;update;patch;delete

func GetMatchingInstances(log logr.Logger, ctx context.Context, k8sClient client.Client, commonSpec v1beta1.GrafanaCommonSpec, namespace string) ([]v1beta1.Grafana, error) {
if commonSpec.InstanceSelector.MatchLabels == nil {
func GetMatchingInstances(log logr.Logger, ctx context.Context, k8sClient client.Client, cr operatorapi.CommonResource) ([]v1beta1.Grafana, error) {
instanceSelector, namespace, allowCrossNamespaceImport := cr.MatchConditions()
if instanceSelector.MatchLabels == nil {
return []v1beta1.Grafana{}, nil
}

opts := []client.ListOption{
client.MatchingLabels(commonSpec.InstanceSelector.MatchLabels),
client.MatchingLabels(instanceSelector.MatchLabels),
}
if !commonSpec.AllowCrossNamespaceImport {
if !allowCrossNamespaceImport {
// Only query resource namespace
opts = append(opts, client.InNamespace(namespace))
}
Expand All @@ -56,7 +57,7 @@ func GetMatchingInstances(log logr.Logger, ctx context.Context, k8sClient client
selectedList := []v1beta1.Grafana{}
var unready_instances []string
for _, instance := range list.Items {
selected := labelsSatisfyMatchExpressions(instance.Labels, commonSpec.InstanceSelector.MatchExpressions)
selected := labelsSatisfyMatchExpressions(instance.Labels, instanceSelector.MatchExpressions)
if !selected {
continue
}
Expand Down
2 changes: 1 addition & 1 deletion controllers/dashboard_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func (r *GrafanaDashboardReconciler) Reconcile(ctx context.Context, req ctrl.Req
}
}()

instances, err := GetMatchingInstances(controllerLog, ctx, r.Client, cr.Spec.GrafanaCommonSpec, cr.ObjectMeta.Namespace)
instances, err := GetMatchingInstances(controllerLog, ctx, r.Client, cr)
if err != nil || len(instances) == 0 {
NilOrEmptyInstanceListCondition(&cr.Status.Conditions, conditionDashboardSynchronized, cr.Generation, err)
cr.Status.NoMatchingInstances = true
Expand Down
2 changes: 1 addition & 1 deletion controllers/datasource_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func (r *GrafanaDatasourceReconciler) Reconcile(ctx context.Context, req ctrl.Re
// Overwrite OrgID to ensure the field is useless
cr.Spec.Datasource.OrgID = nil

instances, err := GetMatchingInstances(controllerLog, ctx, r.Client, cr.Spec.GrafanaCommonSpec, cr.ObjectMeta.Namespace)
instances, err := GetMatchingInstances(controllerLog, ctx, r.Client, cr)
if err != nil || len(instances) == 0 {
NilOrEmptyInstanceListCondition(&cr.Status.Conditions, conditionDatasourceSynchronized, cr.Generation, err)
cr.Status.NoMatchingInstances = true
Expand Down
2 changes: 1 addition & 1 deletion controllers/grafanaalertrulegroup_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (r *GrafanaAlertRuleGroupReconciler) Reconcile(ctx context.Context, req ctr
}
}()

instances, err := GetMatchingInstances(controllerLog, ctx, r.Client, group.Spec.GrafanaCommonSpec, group.ObjectMeta.Namespace)
instances, err := GetMatchingInstances(controllerLog, ctx, r.Client, group)
if err != nil || len(instances) == 0 {
NilOrEmptyInstanceListCondition(&group.Status.Conditions, conditionAlertGroupSynchronized, group.Generation, err)
controllerLog.Error(err, "could not find matching instances", "name", group.Name, "namespace", group.Namespace)
Expand Down
2 changes: 1 addition & 1 deletion controllers/grafanacontactpoint_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (r *GrafanaContactPointReconciler) Reconcile(ctx context.Context, req ctrl.
}
}()

instances, err := GetMatchingInstances(controllerLog, ctx, r.Client, contactPoint.Spec.GrafanaCommonSpec, contactPoint.ObjectMeta.Namespace)
instances, err := GetMatchingInstances(controllerLog, ctx, r.Client, contactPoint)
if err != nil || len(instances) == 0 {
NilOrEmptyInstanceListCondition(&contactPoint.Status.Conditions, conditionContactPointSynchronized, contactPoint.Generation, err)
controllerLog.Error(err, "could not find matching instances", "name", contactPoint.Name, "namespace", contactPoint.Namespace)
Expand Down
2 changes: 1 addition & 1 deletion controllers/grafanafolder_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func (r *GrafanaFolderReconciler) Reconcile(ctx context.Context, req ctrl.Reques
}
removeInvalidSpec(&folder.Status.Conditions)

instances, err := GetMatchingInstances(controllerLog, ctx, r.Client, folder.Spec.GrafanaCommonSpec, folder.ObjectMeta.Namespace)
instances, err := GetMatchingInstances(controllerLog, ctx, r.Client, folder)
if err != nil || len(instances) == 0 {
NilOrEmptyInstanceListCondition(&folder.Status.Conditions, conditionFolderSynchronized, folder.Generation, err)
folder.Status.NoMatchingInstances = true
Expand Down
2 changes: 1 addition & 1 deletion controllers/notificationpolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (r *GrafanaNotificationPolicyReconciler) Reconcile(ctx context.Context, req
}
}()

instances, err := GetMatchingInstances(controllerLog, ctx, r.Client, notificationPolicy.Spec.GrafanaCommonSpec, notificationPolicy.ObjectMeta.Namespace)
instances, err := GetMatchingInstances(controllerLog, ctx, r.Client, notificationPolicy)
if err != nil || len(instances) == 0 {
NilOrEmptyInstanceListCondition(&notificationPolicy.Status.Conditions, conditionNotificationPolicySynchronized, notificationPolicy.Generation, err)
controllerLog.Error(err, "could not find matching instances", "name", notificationPolicy.Name, "namespace", notificationPolicy.Namespace)
Expand Down

0 comments on commit f6c4c23

Please sign in to comment.