Skip to content

Commit

Permalink
refactor: Apply review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Baarsgaard committed Nov 26, 2024
1 parent 9ec113d commit 60e5c8a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
7 changes: 0 additions & 7 deletions api/common_interfaces.go

This file was deleted.

9 changes: 9 additions & 0 deletions api/v1beta1/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
15 changes: 13 additions & 2 deletions api/v1beta1/grafanafolder_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
13 changes: 7 additions & 6 deletions controllers/controller_shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 60e5c8a

Please sign in to comment.