func platformEarlyInit()

in cmd/core_plugin/network/network_early_windows.go [30:80]


func platformEarlyInit(ctx context.Context, data any) error {
	table, err := route.Table()
	if err != nil {
		return fmt.Errorf("failed to get route table: %w", err)
	}

	// On Windows, we want to allow users to see the route table in the logs in
	// case there's some configuration conflict etc.
	galog.Infof("Route table: %+v", table)

	if len(table) == 0 {
		return fmt.Errorf("no routes found in the route table")
	}

	defRoute, err := defaultRouteFromTable(table)
	if err != nil {
		return fmt.Errorf("failed to get default route: %w", err)
	}

	dest, err := address.ParseIP(route.MetadataRouteDestination)
	if err != nil {
		return fmt.Errorf("failed to parse metadata route destination: %w", err)
	}

	gateway, err := address.ParseIP(route.MetadataRouteGateway)
	if err != nil {
		return fmt.Errorf("failed to parse metadata route gateway: %w", err)
	}

	mdsRoute := route.Handle{
		Destination:    dest,
		Gateway:        gateway,
		InterfaceIndex: defRoute.InterfaceIndex,
		Metric:         defRoute.Metric,
		// Persistent for windows translates as "immortal route" and is persistent
		// across reboots.
		Persistent: true,
	}

	contains := slices.ContainsFunc(table, func(r route.Handle) bool {
		return r.Destination.String() == mdsRoute.Destination.String() && r.Gateway.String() == mdsRoute.Gateway.String()
	})

	if !contains {
		if err := route.Add(ctx, mdsRoute); err != nil {
			return fmt.Errorf("failed to add route for metadata server: %w", err)
		}
	}

	return nil
}