diff --git a/api/common_interfaces.go b/api/common_interfaces.go deleted file mode 100644 index fb819d5ad..000000000 --- a/api/common_interfaces.go +++ /dev/null @@ -1,7 +0,0 @@ -package api - -import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - -type CommonResource interface { - MatchConditions() (*metav1.LabelSelector, string, *bool) -} diff --git a/api/v1beta1/common.go b/api/v1beta1/common.go index 3872b6c19..84925e2fe 100644 --- a/api/v1beta1/common.go +++ b/api/v1beta1/common.go @@ -39,3 +39,12 @@ type GrafanaCommonSpec struct { // +optional AllowCrossNamespaceImport *bool `json:"allowCrossNamespaceImport,omitempty"` } + +// Common Functions that all CRs should implement, excluding Grafana +// +kubebuilder:object:generate=false +type CommonResource interface { + MatchLabels() *metav1.LabelSelector + MatchNamespace() string + AllowCrossNamespace() bool + ResyncPeriodHasElapsed() bool +} diff --git a/api/v1beta1/grafanafolder_types.go b/api/v1beta1/grafanafolder_types.go index 3ab12a876..b2d79b406 100644 --- a/api/v1beta1/grafanafolder_types.go +++ b/api/v1beta1/grafanafolder_types.go @@ -164,6 +164,17 @@ func (in *GrafanaFolder) ResyncPeriodHasElapsed() bool { return time.Now().After(deadline) } -func (in *GrafanaFolder) MatchConditions() (*metav1.LabelSelector, string, *bool) { - return in.Spec.InstanceSelector, in.ObjectMeta.Namespace, in.Spec.AllowCrossNamespaceImport +func (in *GrafanaFolder) MatchLabels() *metav1.LabelSelector { + return in.Spec.InstanceSelector +} + +func (in *GrafanaFolder) MatchNamespace() string { + return in.ObjectMeta.Namespace +} + +func (in *GrafanaFolder) AllowCrossNamespace() bool { + if in.Spec.AllowCrossNamespaceImport != nil { + return *in.Spec.AllowCrossNamespaceImport + } + return false } diff --git a/controllers/controller_shared.go b/controllers/controller_shared.go index e51360c4b..276eacdac 100644 --- a/controllers/controller_shared.go +++ b/controllers/controller_shared.go @@ -61,8 +61,9 @@ func GetMatchingInstances(ctx context.Context, k8sClient client.Client, labelSel // Only matching instances in the scope of the resource are returned // Resources with allowCrossNamespaceImport expands the scope to the entire cluster // Intended to be used in reconciler functions -func GetScopedMatchingInstances(log logr.Logger, ctx context.Context, k8sClient client.Client, cr operatorapi.CommonResource) ([]v1beta1.Grafana, error) { - instanceSelector, namespace, allowCrossNamespaceImport := cr.MatchConditions() +func GetScopedMatchingInstances(log logr.Logger, ctx context.Context, k8sClient client.Client, cr v1beta1.CommonResource) ([]v1beta1.Grafana, error) { + instanceSelector := cr.MatchLabels() + if instanceSelector.MatchLabels == nil { return []v1beta1.Grafana{}, nil } @@ -71,9 +72,9 @@ func GetScopedMatchingInstances(log logr.Logger, ctx context.Context, k8sClient client.MatchingLabels(instanceSelector.MatchLabels), } - if allowCrossNamespaceImport != nil && !*allowCrossNamespaceImport { + if !cr.AllowCrossNamespace() { // Only query resource namespace - opts = append(opts, client.InNamespace(namespace)) + opts = append(opts, client.InNamespace(cr.MatchNamespace())) } var list v1beta1.GrafanaList @@ -107,8 +108,8 @@ func GetScopedMatchingInstances(log logr.Logger, ctx context.Context, k8sClient // Same as GetScopedMatchingInstances, except the scope is always global // Intended to be used in finalizer and onDelete functions due to allowCrossNamespaceImport being a mutable field // Not using this may leave behind resources in instances no longer in scope. -func GetAllMatchingInstances(ctx context.Context, k8sClient client.Client, cr operatorapi.CommonResource) ([]v1beta1.Grafana, error) { - instanceSelector, _, _ := cr.MatchConditions() +func GetAllMatchingInstances(ctx context.Context, k8sClient client.Client, cr v1beta1.CommonResource) ([]v1beta1.Grafana, error) { + instanceSelector := cr.MatchLabels() if instanceSelector.MatchLabels == nil { return []v1beta1.Grafana{}, nil }