Repository: istio/istio
File: pilot/pkg/xds/filters/filters.go (Go)

Code before:
					"istio_headers": map[string]any{
						"skip_external_clusters": true,
					},
				},
			},
		}
		additionalLabels(cfg)

		return &hcm.HttpFilter{
			Name: MxFilterName,
			ConfigType: &hcm.HttpFilter_TypedConfig{
				TypedConfig: protoconv.TypedStructWithFields(PeerMetadataTypeURL, cfg),
			},
		}
	}()

	waypointUpstreamMetadataFilterOnce   sync.Once
	waypointUpstreamMetadataFilter       *hcm.HttpFilter
	waypointDownstreamMetadataFilterOnce sync.Once
	waypointDownstreamMetadataFilter     *hcm.HttpFilter
)

// ResetWaypointFiltersForTest clears the cached waypoint metadata filters,
// allowing them to be reinitialized with new feature flag values.
// This function is for testing only and should not be called in production code.
func ResetWaypointFiltersForTest() {
	waypointUpstreamMetadataFilterOnce = sync.Once{}
	waypointDownstreamMetadataFilterOnce = sync.Once{}
}

// GenerateWaypointUpstreamMetadataFilter returns the waypoint upstream peer metadata filter.
// The filter is lazily initialized and cached.
func GenerateWaypointUpstreamMetadataFilter() *hcm.HttpFilter {
	waypointUpstreamMetadataFilterOnce.Do(func() {
		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)
		}

		waypointUpstreamMetadataFilter = &hcm.HttpFilter{
			Name: "waypoint_upstream_peer_metadata",
			ConfigType: &hcm.HttpFilter_TypedConfig{

Review comment (suggestion):
We can take a test context here to ensure it's only used in tests

```suggestion
func ResetWaypointFiltersForTest(_ *testing.T) {
```

Code after:
				map[string]any{
					"istio_headers": map[string]any{
						"skip_external_clusters": true,
					},
				},
			},
		}
		additionalLabels(cfg)

		return &hcm.HttpFilter{
			Name: MxFilterName,
			ConfigType: &hcm.HttpFilter_TypedConfig{
				TypedConfig: protoconv.TypedStructWithFields(PeerMetadataTypeURL, cfg),
			},
		}
	}()

	waypointUpstreamMetadataFilter   = sync.OnceValue(generateWaypointUpstreamMetadataFilter)
	waypointDownstreamMetadataFilter = sync.OnceValue(generateWaypointDownstreamMetadataFilter)
)

// ResetWaypointFiltersForTest clears the cached waypoint metadata filters,
// allowing them to be reinitialized with new feature flag values.
// This function is for testing only and should not be called in production code.
func ResetWaypointFiltersForTest(_ *testing.T) {
	waypointUpstreamMetadataFilter = sync.OnceValue(generateWaypointUpstreamMetadataFilter)
	waypointDownstreamMetadataFilter = sync.OnceValue(generateWaypointDownstreamMetadataFilter)
}

// GenerateWaypointUpstreamMetadataFilter returns the waypoint upstream peer metadata filter.
// The filter is lazily initialized and cached.
func GenerateWaypointUpstreamMetadataFilter() *hcm.HttpFilter {
	return waypointUpstreamMetadataFilter()
}

func 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{