Skip to content

Commit

Permalink
fix: create redis bpt failed (#8218)
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyelei authored Sep 27, 2024
1 parent 7c93f30 commit 6773818
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
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)
}
2 changes: 1 addition & 1 deletion controllers/apps/componentversion_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (r *ComponentVersionReconciler) compatibleCompVersion(ctx context.Context,
func (r *ComponentVersionReconciler) isCompatibleWith(compDef appsv1alpha1.ComponentDefinition, compVer appsv1alpha1.ComponentVersion) bool {
for _, rule := range compVer.Spec.CompatibilityRules {
for _, name := range rule.CompDefs {
if strings.HasPrefix(compDef.Name, name) {
if component.CompDefMatched(compDef.Name, name) {
return true
}
}
Expand Down

0 comments on commit 6773818

Please sign in to comment.