Repository: istio/istio
File: pilot/pkg/config/kube/gateway/route_collections.go (Go)

Code before:
			// Because a lot of code assumes the object key is 'namespace/name', and the key always has slashes, we also translate the /
			nm.Name = strings.ReplaceAll(object.Key, "/", "~")
			return ptr.Of(&config.Config{
				Meta:   nm,
				Spec:   base.Spec,
				Status: base.Status,
				Extra:  base.Extra,
			})
		}
		sortRoutesByCreationTime(configs)
		base := configs[0].DeepCopy()
		baseVS := base.Spec.(*istio.VirtualService)
		for i, config := range configs[1:] {
			thisVS := config.Spec.(*istio.VirtualService)
			baseVS.Http = append(baseVS.Http, thisVS.Http...)
			// append parents
			base.Annotations[constants.InternalParentNames] = fmt.Sprintf("%s,%s",
				base.Annotations[constants.InternalParentNames], config.Annotations[constants.InternalParentNames])
			// Merge Extra field (especially for InferencePool configs)
			if config.Extra != nil {
				if base.Extra == nil {
					base.Extra = make(map[string]any)
				}
				for k, v := range config.Extra {
					// For InferencePool configs, merge the maps
					if k == constants.ConfigExtraPerRouteRuleInferencePoolConfigs {
						if baseMap, ok := base.Extra[k].(map[string]kube.InferencePoolRouteRuleConfig); ok {
							if configMap, ok := v.(map[string]kube.InferencePoolRouteRuleConfig); ok {
								log.Infof("Merging InferencePool configs: adding %d route configs from VirtualService %d to base (namespace=%s)",
									len(configMap), i+1, config.Namespace)
								for routeName, routeConfig := range configMap {
									baseMap[routeName] = routeConfig
								}
							}
						} else {
							log.Infof("Creating new InferencePool config map from VirtualService %d (namespace=%s)", i+1, config.Namespace)
							base.Extra[k] = v
						}
					} else {
						base.Extra[k] = v
					}
				}
			}
		}
		// Log final merged InferencePool configs
		if base.Extra != nil {
			if ipConfigs, ok := base.Extra[constants.ConfigExtraPerRouteRuleInferencePoolConfigs].(map[string]kube.InferencePoolRouteRuleConfig); ok {
				log.Infof("Final merged VirtualService for key %s has %d InferencePool route configs", object.Key, len(ipConfigs))
			}
		}
		sortHTTPRoutes(baseVS.Http)

Review comment (suggestion):
We should exit early here with something like:
```suggestion
					if k != constants.ConfigExtraPerRouteRuleInferencePoolConfigs {
					  continue
					}
```

Code after:
			nm.Name = strings.ReplaceAll(object.Key, "/", "~")
			return ptr.Of(&config.Config{
				Meta:   nm,
				Spec:   base.Spec,
				Status: base.Status,
				Extra:  base.Extra,
			})
		}
		sortRoutesByCreationTime(configs)
		base := configs[0].DeepCopy()
		baseVS := base.Spec.(*istio.VirtualService)
		// Deep copy the InferencePool configs map to avoid race conditions
		// The default DeepCopy() only does shallow copy of Extra field
		if base.Extra != nil {
			if ipConfigs, ok := base.Extra[constants.ConfigExtraPerRouteRuleInferencePoolConfigs].(map[string]kube.InferencePoolRouteRuleConfig); ok {
				// Create a new map to avoid modifying the shared underlying map
				newIPConfigs := make(map[string]kube.InferencePoolRouteRuleConfig, len(ipConfigs))
				for k, v := range ipConfigs {
					newIPConfigs[k] = v
				}
				base.Extra[constants.ConfigExtraPerRouteRuleInferencePoolConfigs] = newIPConfigs
			}
		}
		for i, config := range configs[1:] {
			thisVS := config.Spec.(*istio.VirtualService)
			baseVS.Http = append(baseVS.Http, thisVS.Http...)
			// append parents
			base.Annotations[constants.InternalParentNames] = fmt.Sprintf("%s,%s",
				base.Annotations[constants.InternalParentNames], config.Annotations[constants.InternalParentNames])
			// Merge Extra field (especially for InferencePool configs)
			if base.Extra == nil && config.Extra != nil {
				base.Extra = make(map[string]any)
			}
			if config.Extra != nil {
				for k, v := range config.Extra {
					// For non-InferencePool configs, keep the first value for stability
					if k != constants.ConfigExtraPerRouteRuleInferencePoolConfigs {
						if _, exists := base.Extra[k]; !exists {
							base.Extra[k] = v
						}
						continue
					}
					// For InferencePool configs, merge the maps
					baseMap, baseOk := base.Extra[k].(map[string]kube.InferencePoolRouteRuleConfig)
					configMap, configOk := v.(map[string]kube.InferencePoolRouteRuleConfig)
					if baseOk && configOk {
						log.Debugf("Merging InferencePool configs: adding %d route configs from VirtualService %d to base (namespace=%s)",
							len(configMap), i+1, config.Namespace)
						// Route names are composed of the HTTPRoute/VirtualService namespaced name so they can't possibly conflict
						for routeName, routeConfig := range configMap {
							baseMap[routeName] = routeConfig