# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

# Shared kube-vip infrastructure for local container-based tenant nodes. The
# FRR process acts as their BGP peer. CAPD and NICo containers can both use
# these resources without either infrastructure provider owning or depending on
# the other.

BGP_RESOURCE = "bgp-containers"

def register(ctx):
    container_name = ctx.cfg.get("bgp_containers_container_name", BGP_RESOURCE)
    controller_resource_deps = []
    bgp_dir = os.path.join(ctx.main_dir, "hack", "bgp-containers")

    # FRR runs in the Docker daemon's host network namespace so kube-vip pods
    # on any container-based tenant node can peer through the kind gateway.
    if ctx.cfg.get("bgp_containers_enabled", True):
        local_resource(
            BGP_RESOURCE,
            cmd=[
                "env",
                "CONTAINER_NAME=" + container_name,
                "bash",
                os.path.join(bgp_dir, "up.sh"),
            ],
            deps=[
                os.path.join(bgp_dir, "up.sh"),
                os.path.join(bgp_dir, "down.sh"),
                os.path.join(bgp_dir, "frr.conf.tmpl"),
                os.path.join(bgp_dir, "vtysh.conf"),
                os.path.join(bgp_dir, "daemons"),
            ],
            labels=["1-infra"],
        )
        controller_resource_deps.append(BGP_RESOURCE)

    return struct(
        controller_resource_deps=controller_resource_deps,
        controller_env_overrides={},
    )
