Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions k8s/apis/solution/v1/instance_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
61 changes: 45 additions & 16 deletions k8s/controllers/solution/instance_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand All @@ -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
Expand Down
Loading