Preheat the oven to 200°C/fan 180°C/gas mark 6. Cut the tops off the garlic bulbs so the cloves are slightly exposed. Put them in the middle of a large square of foil and drizzle over 1 tbsp oil. Seal the foil around the bulbs and place in a roasting tray. Roast in the oven for 40-45 minutes, or until the garlic cloves are completely soft. Remove and leave to cool. When cool, carefully remove the outer casing to reveal the cloves, keeping them as in tact as you can.
Place the flour and butter in a food processor and pulse until it resembles fine breadcrumbs. Add 2 tbsp cold water to the 2 separated egg yolks, reserving the whites, and pulse again. Turn out on to a lightly floured surface and knead lightly until it all comes together. Wrap in cling film and leave to chill in the fridge for 30 minutes.
Meanwhile, heat the remaining oil in a saucepan and fry the onions for 8-10 minutes until very soft. Add the balsamic vinegar and sugar and continue to cook for another 5 minutes. Season and set aside.
Roll out the pastry until it's 3mm thick. Line a 23cm tart case and trim off the excess. Prick the base with a fork and cover with a sheet of baking paper. Fill with baking beans and bake in the oven for 15 minutes. Remove the baking beans and paper and brush the base with one of the separated egg whites. Return to the oven for 5-10 minutes, or until the base has completely dried out and is light golden. Reduce the oven temperature to 160°C/fan 140°C/gas mark 3.
Place the onions in the bottom of the tart case and top with slices of the goat's cheese. Dot with the garlic cloves. Whisk the remaining 2 eggs along with the last egg white, milk and rosemary, season with black pepper and pour into the tart case. Return to the oven and bake for 35-40 minutes, or until just set. Serve warm or cold.