Skip to content

Commit

Permalink
fix: create redis bpt failed
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyelei committed Sep 27, 2024
1 parent 7c93f30 commit 5cc2d62
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions controllers/apps/backuppolicytemplate_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ import (
"context"

k8sruntime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

Expand Down Expand Up @@ -89,9 +91,43 @@ func (r *BackupPolicyTemplateReconciler) getMatchedComponentDefs(compDefList *ap
return compDefNames
}

func (r *BackupPolicyTemplateReconciler) isCompatibleWith(compDef appsv1alpha1.ComponentDefinition, bpt *appsv1alpha1.BackupPolicyTemplate) bool {
for _, bp := range bpt.Spec.BackupPolicies {
for _, compDefRegex := range bp.ComponentDefs {
if component.CompDefMatched(compDef.Name, compDefRegex) {
return true
}
}
}
return false
}

func (r *BackupPolicyTemplateReconciler) compatibleBackupPolicyTemplate(ctx context.Context, obj client.Object) []reconcile.Request {
compDef, ok := obj.(*appsv1alpha1.ComponentDefinition)
if !ok {
return nil
}
bpts := &appsv1alpha1.BackupPolicyTemplateList{}
if err := r.Client.List(ctx, bpts); err != nil {
return nil
}
requests := make([]reconcile.Request, 0)
for i := range bpts.Items {
if r.isCompatibleWith(*compDef, &bpts.Items[i]) {
requests = append(requests, reconcile.Request{
NamespacedName: types.NamespacedName{
Name: bpts.Items[i].Name,
},
})
}
}
return requests
}

// SetupWithManager sets up the controller with the Manager.
func (r *BackupPolicyTemplateReconciler) SetupWithManager(mgr ctrl.Manager) error {
return intctrlutil.NewNamespacedControllerManagedBy(mgr).
For(&appsv1alpha1.BackupPolicyTemplate{}).
Watches(&appsv1alpha1.ComponentDefinition{}, handler.EnqueueRequestsFromMapFunc(r.compatibleBackupPolicyTemplate)).
Complete(r)
}

0 comments on commit 5cc2d62

Please sign in to comment.