Repository: istio/istio
File: pilot/pkg/networking/core/listener_waypoint.go (Go)

Code before:
	var orderedWPS []*model.Service
	wls, wps := findWaypointResources(lb.node, lb.push)
	if wps != nil {
		orderedWPS = wps.orderedServices
	}

	listeners := []*listener.Listener{
		lb.buildWaypointInboundConnectTerminate(),
		lb.buildWaypointInternal(wls, orderedWPS),
	}

	if features.EnableAmbientMultiNetwork && isEastWestGateway(lb.node) {
		listeners = append(listeners, buildWaypointForwardInnerConnectListener(lb.push, lb.node))
	} else {
		listeners = append(listeners, buildWaypointConnectOriginateListener(lb.push, lb.node))
	}

	if model.ShouldCreateDoubleHBONEResources(lb.node) {
		listeners = append(listeners,
			buildInnerConnectOriginateListener(lb.push, lb.node),
			buildOuterConnectOriginateListener(lb.push, lb.node))
	}
	return listeners
}

func (lb *ListenerBuilder) generateWaypointUpstreamMetadataFilter() *hcm.HttpFilter {
	workloadDiscovery := map[string]any{
		"workload_discovery": map[string]any{},
	}
	baggageDiscovery := map[string]any{
		"upstream_filter_state": map[string]any{
			"peer_metadata_key": "upstream_peer",
		},
	}

	discoveryMethods := []any{workloadDiscovery}
	if features.EnableAmbientBaggage {
		discoveryMethods = append(discoveryMethods, baggageDiscovery)
	}

	return &hcm.HttpFilter{
		Name: "waypoint_upstream_peer_metadata",
		ConfigType: &hcm.HttpFilter_TypedConfig{
			TypedConfig: protoconv.TypedStructWithFields(xdsfilters.PeerMetadataTypeURL,
				map[string]any{
					"upstream_discovery": discoveryMethods,
				}),
		},
	}
}


Review comment (suggestion):
Neither of these use anything from lb, it would be nice to have them be package vars in parallel with the existing ones and do something like

```
if featureGate {
  return NewPackageVar
}
return OldPackageVar
```

and maybe not make them instance methods, or inline that logic

Code after:
	var orderedWPS []*model.Service
	wls, wps := findWaypointResources(lb.node, lb.push)
	if wps != nil {
		orderedWPS = wps.orderedServices
	}

	listeners := []*listener.Listener{
		lb.buildWaypointInboundConnectTerminate(),
		lb.buildWaypointInternal(wls, orderedWPS),
	}

	if features.EnableAmbientMultiNetwork && isEastWestGateway(lb.node) {
		listeners = append(listeners, buildWaypointForwardInnerConnectListener(lb.push, lb.node))
	} else {
		listeners = append(listeners, buildWaypointConnectOriginateListener(lb.push, lb.node))
	}

	if model.ShouldCreateDoubleHBONEResources(lb.node) {
		listeners = append(listeners,
			buildInnerConnectOriginateListener(lb.push, lb.node),
			buildOuterConnectOriginateListener(lb.push, lb.node))
	}
	return listeners
}

func (lb *ListenerBuilder) buildHCMConnectTerminateChain(routes []*route.Route) []*listener.Filter {
	ph := util.GetProxyHeaders(lb.node, lb.push, istionetworking.ListenerClassSidecarInbound)
	h := &hcm.HttpConnectionManager{
		StatPrefix: ConnectTerminate,
		RouteSpecifier: &hcm.HttpConnectionManager_RouteConfig{
			RouteConfig: &route.RouteConfiguration{
				Name: "default",
				VirtualHosts: []*route.VirtualHost{{
					Name:    "default",
					Domains: []string{"*"},
					Routes:  routes,
				}},
			},
		},
		// Append and forward client cert to backend, if configured
		ForwardClientCertDetails: ph.ForwardedClientCert,
		SetCurrentClientCertDetails: &hcm.HttpConnectionManager_SetCurrentClientCertDetails{
			Subject: proto.BoolTrue,
			Uri:     true,
			Dns:     true,
		},
		ServerName:                 ph.ServerName,
		ServerHeaderTransformation: ph.ServerHeaderTransformation,
		GenerateRequestId:          ph.GenerateRequestID,
		UseRemoteAddress:           proto.BoolFalse,
	}