From e4801048e0f7eb0d3b9ac1663cd4633fa6b3896c Mon Sep 17 00:00:00 2001 From: Lency Qian Date: Thu, 10 Jul 2025 12:50:42 +0800 Subject: [PATCH] Add index filter for Instance --- k8s/apis/solution/v1/instance_webhook.go | 6 ++ .../solution/instance_controller.go | 61 ++++++++++++++----- 2 files changed, 51 insertions(+), 16 deletions(-) diff --git a/k8s/apis/solution/v1/instance_webhook.go b/k8s/apis/solution/v1/instance_webhook.go index 84bef2c20..46b385f0f 100644 --- a/k8s/apis/solution/v1/instance_webhook.go +++ b/k8s/apis/solution/v1/instance_webhook.go @@ -57,10 +57,16 @@ var instanceHistory history.InstanceHistory func (r *Instance) SetupWebhookWithManager(mgr ctrl.Manager) error { myInstanceClient = mgr.GetAPIReader() k8sClient = mgr.GetClient() + mgr.GetFieldIndexer().IndexField(context.Background(), &Instance{}, "spec.solution", func(rawObj client.Object) []string { instance := rawObj.(*Instance) return []string{instance.Spec.Solution} }) + mgr.GetFieldIndexer().IndexField(context.Background(), &Instance{}, "spec.target.name", func(rawObj client.Object) []string { + instance := rawObj.(*Instance) + return []string{instance.Spec.Target.Name} + }) + myConfig, err := configutils.GetProjectConfig() if err != nil { return err diff --git a/k8s/controllers/solution/instance_controller.go b/k8s/controllers/solution/instance_controller.go index c2a0b3cd9..42da4af76 100644 --- a/k8s/controllers/solution/instance_controller.go +++ b/k8s/controllers/solution/instance_controller.go @@ -148,24 +148,25 @@ func (r *InstanceReconciler) handleTarget(ctx context.Context, obj client.Object tarObj := obj.(*fabric_v1.Target) var instances solution_v1.InstanceList - options := []client.ListOption{client.InNamespace(tarObj.Namespace)} - err := r.List(context.Background(), &instances, options...) - if err != nil { - diagnostic.ErrorWithCtx(log.Log, ctx, err, "Failed to list instances") - return ret - } - - targetList := fabric_v1.TargetList{} - targetList.Items = append(targetList.Items, *tarObj) - + // Check if target has AzureResourceIdKey annotation for different processing logic updatedInstanceNames := make([]string, 0) - for _, instance := range instances.Items { - if !utils.NeedWatchInstance(instance) { - continue + if azureResourceId, exists := tarObj.Annotations[constants.AzureResourceIdKey]; exists && azureResourceId != "" { + // Use field index to query instances by Azure resource ID + options := []client.ListOption{ + client.InNamespace(tarObj.Namespace), + client.MatchingFields{"spec.target.name": azureResourceId}, + } + err := r.List(ctx, &instances, options...) + if err != nil { + diagnostic.ErrorWithCtx(log.Log, ctx, err, "Failed to list instances by Azure resource ID index") + return ret } - targetCandidates := utils.MatchTargets(instance, targetList) - if len(targetCandidates) > 0 { + // For Azure targets, directly add all matching instances to result + for _, instance := range instances.Items { + if !utils.NeedWatchInstance(instance) { + continue + } ret = append(ret, ctrl.Request{ NamespacedName: types.NamespacedName{ Name: instance.Name, @@ -174,6 +175,34 @@ func (r *InstanceReconciler) handleTarget(ctx context.Context, obj client.Object }) updatedInstanceNames = append(updatedInstanceNames, instance.Name) } + } else { + // Use full namespace listing and MatchTargets + options := []client.ListOption{client.InNamespace(tarObj.Namespace)} + err := r.List(ctx, &instances, options...) + if err != nil { + diagnostic.ErrorWithCtx(log.Log, ctx, err, "Failed to list instances") + return ret + } + + targetList := fabric_v1.TargetList{} + targetList.Items = append(targetList.Items, *tarObj) + + for _, instance := range instances.Items { + if !utils.NeedWatchInstance(instance) { + continue + } + + targetCandidates := utils.MatchTargets(instance, targetList) + if len(targetCandidates) > 0 { + ret = append(ret, ctrl.Request{ + NamespacedName: types.NamespacedName{ + Name: instance.Name, + Namespace: instance.Namespace, + }, + }) + updatedInstanceNames = append(updatedInstanceNames, instance.Name) + } + } } if len(ret) > 0 { @@ -196,7 +225,7 @@ func (r *InstanceReconciler) findRelatedInstances(ctx context.Context, solutionR client.InNamespace(solutionRefNamespace), client.MatchingFields{"spec.solution": solutionRef}, } - error := r.List(context.Background(), &instances, options...) + error := r.List(ctx, &instances, options...) if error != nil { diagnostic.ErrorWithCtx(log.Log, ctx, error, "Failed to list instances") return ret, updatedInstanceNames