# Generated by confd
{{$route_added := false}}
protocol static {
{{- $block_key := printf "/ipam/v2/host/%s/ipv6/block" (getenv "NODENAME")}}
{{- if ls $block_key}}
{{- $route_added = true}}
   # IP blocks for this host.
{{- range ls $block_key}}
{{- $parts := split . "-"}}
{{- $cidr := join $parts "/"}}
{{- if not (hasSuffix $cidr "/128")}}
   route {{$cidr}} blackhole;
{{- end}}
{{- end}}
{{- end}}
{{- $static_key := "/staticroutesv6"}}
{{- if ls $static_key}}
{{- $route_added = true}}
   # Static routes.
{{- range ls $static_key}}
{{- $parts := split . "-"}}
{{- $cidr := join $parts "/"}}
   route {{$cidr}} blackhole;
{{- end}}
{{- end}}
{{- $global_peers_key := "/bgp/v1/global/peer_v6"}}
{{- if ls $global_peers_key}}
{{- $run_once := true}}
{{- range gets (printf "%s/*" $global_peers_key)}}{{$data := json .Value}}
{{- if and $data.ip $data.reachable_by}}
{{- if $run_once}}
{{- $route_added = true}}
{{- $run_once = false}}
   # Static routes needed for global BGP peers.
{{- end}}
   route {{$data.ip}}/128 via {{$data.reachable_by}};
{{- end}}
{{- end}}
{{- end}}
{{- $node_peers_key := printf "/bgp/v1/host/%s/peer_v6" (getenv "NODENAME")}}
{{- if ls $node_peers_key}}
{{- $run_once := true}}
{{- range gets (printf "%s/*" $node_peers_key)}}{{$data := json .Value}}
{{- if and $data.ip $data.reachable_by}}
{{- if $run_once}}
{{- $route_added = true}}
{{- $run_once = false}}
   # Static routes needed for node specific BGP peers.
{{- end}}
   route {{$data.ip}}/128 via {{$data.reachable_by}};
{{- end}}
{{- end}}
{{- end}}
{{- if not $route_added}}
   # No IP blocks or static routes for this host.
{{- end}}
}

# Aggregation of routes on this host; export the block, nothing beneath it.
function calico_aggr ()
{
{{- range ls $block_key}}
{{- $parts := split . "-"}}
{{- $cidr := join $parts "/"}}
{{- $affinity := json (getv (printf "%s/%s" $block_key .))}}
  {{- if $affinity.state}}
      # Block {{$cidr}} is {{$affinity.state}}
    {{- if eq $affinity.state "confirmed"}}
      if ( net = {{$cidr}} ) then { accept; }
      if ( net ~ {{$cidr}} ) then { reject; }
    {{- end}}
  {{- else }}
      # Block {{$cidr}} is implicitly confirmed.
      if ( net = {{$cidr}} ) then { accept; }
      if ( net ~ {{$cidr}} ) then { reject; }
  {{- end }}
{{- end}}
}
