func()

in pkg/initalizer/executor.go [30:99]


func (e *Executor) Initialize(ctx context.Context) error {
	log := logger.FromContext(ctx)

	ipv4LinkLocalAddr, err := netlink.ParseAddr(configuration.DefaultIpv4TargetHost + "/32")
	if err != nil {
		panic(err)
	}

	ipv6LinkLocalAddr, err := netlink.ParseAddr(configuration.DefaultIpv6TargetHost + "/128")
	if err != nil {
		panic(err)
	}

	// first create the interface
	link, err := e.agentLinkRetriever.CreateOrGetLink(ctx)
	if err != nil {
		log.Errorf("Cannot setup link: %v", err)
		return err
	}

	ctx = logger.ContextWithField(ctx, "link", link.Name())
	supportedFamilies := []iproute.AddrFamily{
		{
			Family:        unix.AF_INET,
			LinkLocalAddr: ipv4LinkLocalAddr,
		},
		{
			Family:        unix.AF_INET6,
			LinkLocalAddr: ipv6LinkLocalAddr,
		},
	}

	// attach the required ip addresses to the interface before bringing it up
	for _, fam := range supportedFamilies {
		ctx := logger.ContextWithField(ctx, "ip", fam.LinkLocalAddr)

		err := link.SetupForAddrFamily(ctx, fam)
		if err != nil {
			if isOptionalFamily(fam.Family) {
				// swallow the error if the family we are trying to associate is optional
				log.Errorf("Unable to configure family %02x: %v", fam, err)
			} else {
				log.Fatalf("Stopping execution, unable to configure required family %02x: %v", fam, err)
			}
		}
	}

	// bring the interface up
	err = link.BringUp(ctx)
	if err != nil {
		log.Errorf("Error bringing up link: %v", err)
		return err
	}

	// add the routes to the interface to the default routing table
	for _, fam := range supportedFamilies {
		ctx := logger.ContextWithField(ctx, "ip", fam.LinkLocalAddr)

		err := link.SetupRouteTableForAddrFamily(ctx, fam)
		if err != nil {
			if isOptionalFamily(fam.Family) {
				log.Errorf("Unable to configure family %02x: %v", fam, err)
			} else {
				log.Fatalf("Stopping execution, unable to configure required family %02x: %v", fam, err)
			}
		}
	}

	return nil
}