Skip to content

Commit

Permalink
Refactor tenants logic (#897)
Browse files Browse the repository at this point in the history
* Refactor tenants logic

* Small optimization for l2leaf logic
  • Loading branch information
ClausHolbechArista authored Apr 26, 2021
1 parent 921758d commit 421c80e
Show file tree
Hide file tree
Showing 17 changed files with 280 additions and 448 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,65 +106,66 @@ switch:
mlag: false
{% endif %}

{# switch.vrfs, switch.svis and switch.vlans #}
{# get allowed vrfs, svis and vlans from parent L3 Leafs #}
{% set parentl3leaf = namespace() %}
{% for l3leaf_node_group in l3leaf.node_groups | arista.avd.natural_sort %}
{% for node in l3leaf.node_groups[l3leaf_node_group].nodes | arista.avd.natural_sort %}
{% if node == switch_parent_l3leafs[0] %}
{% set parentl3leaf.group = l3leaf_node_group %}
{% if l3leaf.node_groups[l3leaf_node_group].filter.tenants is defined %}
{% set parentl3leaf.filter_tenants = l3leaf.node_groups[l3leaf_node_group].filter.tenants %}
{% else %}
{% set parentl3leaf.filter_tenants = ['all'] %}
{% endif %}
{% if l3leaf.node_groups[l3leaf_node_group].filter.tags is defined %}
{% set parentl3leaf.filter_tags = l3leaf.node_groups[l3leaf_node_group].filter.tags %}
{% else %}
{% set parentl3leaf.filter_tags = ['all'] %}
{% endif %}
{% endif %}
{% endfor %}
{% endfor %}
{# set leaf allowed vrfs, svis and vlans #}
{% set leaf = namespace() %}
{% set leaf.vrfs = [] %}
{% set leaf.svis = [] %}
{% set leaf.vlans = [] %}
{% for tenant in tenants | arista.avd.natural_sort if tenant in switch_filter_tenants or "all" in switch_filter_tenants %}
{% if tenant in parentl3leaf.filter_tenants or "all" in parentl3leaf.filter_tenants %}
{% if tenants[tenant].vrfs is defined %}
{% for vrf in tenants[tenant].vrfs | arista.avd.natural_sort %}
{% for svi in tenants[tenant].vrfs[vrf].svis | arista.avd.natural_sort %}
{% for svi_tag in tenants[tenant].vrfs[vrf].svis[svi].tags %}
{% if svi_tag in switch_filter_tags or svi_tag == l2leaf_node_group or "all" in switch_filter_tags %}
{% if svi_tag in parentl3leaf.filter_tags or svi_tag == parentl3leaf.group or "all" in parentl3leaf.filter_tags %}
{% do leaf.vrfs.append(vrf) %}
{% do leaf.svis.append(svi | int) %}
{% do leaf.vlans.append(svi | int) %}
{% break %}
{% endif %}
{% endif %}
{% endfor %}
{% set parentl3leaf = namespace() %}
{% for l3leaf_node_group in l3leaf.node_groups | arista.avd.natural_sort %}
{% for node in l3leaf.node_groups[l3leaf_node_group].nodes | arista.avd.natural_sort %}
{% if node == switch_parent_l3leafs[0] %}
{% set parentl3leaf.group = l3leaf_node_group %}
{% set parentl3leaf.filter_tenants = l3leaf.node_groups[l3leaf_node_group].filter.tenants | arista.avd.default(
['all'])%}
{% set parentl3leaf.filter_tags = l3leaf.node_groups[l3leaf_node_group].filter.tags | arista.avd.default(
['all'])%}
{% endif %}
{% endfor %}
{% endfor %}
{% endif %}
{% if tenants[tenant].l2vlans is defined %}
{% for l2vlan in tenants[tenant].l2vlans | arista.avd.natural_sort %}
{% for vlan_tag in tenants[tenant].l2vlans[l2vlan].tags %}
{% if vlan_tag in switch_filter_tags or vlan_tag == l2leaf_node_group or "all" in switch_filter_tags %}
{% if vlan_tag in parentl3leaf.filter_tags or vlan_tag == l2leaf_node_group or "all" in parentl3leaf.filter_tags %}
{% do leaf.vlans.append(l2vlan | int) %}
{% break %}
{# set leaf allowed vrfs, svis and vlans #}
{% set leaf = namespace() %}
{% set leaf.vlans = [] %}
{% set leaf.tenants = {} %}
{% for tenant in tenants | arista.avd.natural_sort if tenant in switch_filter_tenants or "all" in switch_filter_tenants %}
{% if tenant in parentl3leaf.filter_tenants or "all" in parentl3leaf.filter_tenants %}
{% set add_vrfs = {} %}
{% for vrf in tenants[tenant].vrfs | arista.avd.natural_sort %}
{% set add_svis = [] %}
{% for svi in tenants[tenant].vrfs[vrf].svis | arista.avd.natural_sort %}
{% for svi_tag in tenants[tenant].vrfs[vrf].svis[svi].tags | arista.avd.natural_sort %}
{% if svi_tag in switch_filter_tags or svi_tag == l2leaf_node_group or "all" in switch_filter_tags %}
{% if svi_tag in parentl3leaf.filter_tags or svi_tag == parentl3leaf.group or "all" in parentl3leaf.filter_tags %}
{% do add_svis.append(svi) %}
{% do leaf.vlans.append(svi | int) %}
{% break %}
{% endif %}
{% endif %}
{% endfor %}
{% endfor %}
{# Append VRF if we found SVIs #}
{% if add_svis | length > 0 %}
{% do add_vrfs.update({ vrf: {"svis": add_svis}}) %}
{% endif %}
{% endfor %}
{% set add_l2vlans = [] %}
{% for l2vlan in tenants[tenant].l2vlans | arista.avd.natural_sort %}
{% for vlan_tag in tenants[tenant].l2vlans[l2vlan].tags | arista.avd.natural_sort %}
{% if vlan_tag in switch_filter_tags or vlan_tag == l2leaf_node_group or "all" in switch_filter_tags %}
{% if vlan_tag in parentl3leaf.filter_tags or vlan_tag == l2leaf_node_group or "all" in parentl3leaf.filter_tags %}
{% do add_l2vlans.append(l2vlan) %}
{% do leaf.vlans.append(l2vlan | int) %}
{% break %}
{% endif %}
{% endif %}
{% endfor %}
{% endfor %}
{% if add_vrfs | length > 0 or add_l2vlans | length > 0 %}
{% do leaf.tenants.update({ tenant: {"vrfs": add_vrfs, "l2vlans": add_l2vlans}}) %}
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
{% endif %}
{% endif %}
{% endfor %}
vrfs: {{ leaf.vrfs | unique }}
svis: {{ leaf.svis | unique }}

{# switch.tenants #}
tenants: {{ leaf.tenants }}

{# switch.vlans #}
vlans: {{ leaf.vlans | unique }}

{% break %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,52 +177,56 @@ switch:
switch_spines,
[]) }}

{# switch.vrfs, switch.svis and switch.vlans #}
{% set leaf = namespace() %}
{% set leaf.vrfs = [] %}
{% set leaf.svis = [] %}
{% set leaf.vlans = [] %}
{% set leaf.tenants = {} %}
{% for tenant in tenants | arista.avd.natural_sort if tenant in switch_filter_tenants or "all" in switch_filter_tenants %}
{% set add_vrfs = {} %}
{% for vrf in tenants[tenant].vrfs | arista.avd.natural_sort %}
{% set add_svis = [] %}
{% for svi in tenants[tenant].vrfs[vrf].svis | arista.avd.natural_sort %}
{% for svi_tag in tenants[tenant].vrfs[vrf].svis[svi].tags %}
{% for svi_tag in tenants[tenant].vrfs[vrf].svis[svi].tags | arista.avd.natural_sort %}
{% if svi_tag in switch_filter_tags or svi_tag == l3leaf_node_group or "all" in switch_filter_tags %}
{% do leaf.vrfs.append(vrf) %}
{% do leaf.svis.append(svi | int) %}
{% do add_svis.append(svi) %}
{% do leaf.vlans.append(svi | int) %}
{% break %}
{% endif %}
{% endfor %}
{% endfor %}
{# Append VRFs if there is a BGP neighbor defined for this switch #}
{% for l3_interface in tenants[tenant].vrfs[vrf].l3_interfaces | arista.avd.natural_sort %}
{% if l3_interface.nodes is arista.avd.defined and l3_interface.ip_addresses is arista.avd.defined and l3_interface.interfaces is arista.avd.defined and inventory_hostname in l3_interface.nodes %}
{% do leaf.vrfs.append(vrf) %}
{% endif %}
{% endfor %}
{# Append VRF if we found SVIs #}
{% if add_svis | length > 0 %}
{% do add_vrfs.update({ vrf: {"svis": add_svis}}) %}
{# Or Append VRF for tenants set in "always_include_vrfs_in_tenants" is set #}
{% elif switch_always_include_vrfs_in_tenants is arista.avd.contains([tenant, 'all']) %}
{% do add_vrfs.update({ vrf: {}}) %}
{% else %}
{# Or Append VRF if there is a BGP neighbor defined for this switch #}
{% for l3_interface in tenants[tenant].vrfs[vrf].l3_interfaces | arista.avd.natural_sort %}
{% if l3_interface.nodes is arista.avd.defined and l3_interface.ip_addresses is arista.avd.defined and l3_interface.interfaces is arista.avd.defined and inventory_hostname in l3_interface.nodes %}
{% do add_vrfs.update({ vrf: {}}) %}
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
{% set add_l2vlans = [] %}
{% for l2vlan in tenants[tenant].l2vlans | arista.avd.natural_sort %}
{% for vlan_tag in tenants[tenant].l2vlans[l2vlan].tags %}
{% for vlan_tag in tenants[tenant].l2vlans[l2vlan].tags | arista.avd.natural_sort %}
{% if vlan_tag in switch_filter_tags or vlan_tag == l3leaf_node_group or "all" in switch_filter_tags %}
{% do add_l2vlans.append(l2vlan) %}
{% do leaf.vlans.append(l2vlan | int) %}
{% break %}
{% endif %}
{% endfor %}
{% endfor %}
{% if add_vrfs | length > 0 or add_l2vlans | length > 0 %}
{% do leaf.tenants.update({ tenant: {"vrfs": add_vrfs, "l2vlans": add_l2vlans}}) %}
{% endif %}
{% endfor %}
{# Append all VRFs for tenants set in "always_include_vrfs_in_tenants" is set #}
{% if switch_always_include_vrfs_in_tenants is arista.avd.defined %}
{% for tenant in tenants | arista.avd.natural_sort if tenant in switch_filter_tenants or "all" in switch_filter_tenants %}
{% if tenant in switch_always_include_vrfs_in_tenants or "all" in switch_always_include_vrfs_in_tenants %}
{% for vrf in tenants[tenant].vrfs | arista.avd.natural_sort %}
{% do leaf.vrfs.append(vrf) %}
{% endfor %}
{% endif %}
{% endfor %}
{% endif %}

vrfs: {{ leaf.vrfs | unique }}
svis: {{ leaf.svis | unique }}
{# switch.tenants #}
tenants: {{ leaf.tenants }}

{# switch.vlans #}
vlans: {{ leaf.vlans | unique }}

{% break %}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,30 @@
{# Leaf Tenant l3 interfaces #}
ethernet_interfaces:
{% for tenant in tenants | arista.avd.natural_sort if tenant in switch.filter_tenants or "all" in switch.filter_tenants %}
{% if tenants[tenant].vrfs is arista.avd.defined %}
{% for vrf in tenants[tenant].vrfs %}
{% if tenants[tenant].vrfs[vrf].l3_interfaces is arista.avd.defined %}
{% for l3_interface in tenants[tenant].vrfs[vrf].l3_interfaces %}
{% if l3_interface.nodes is arista.avd.defined and l3_interface.ip_addresses is arista.avd.defined and l3_interface.interfaces is arista.avd.defined and inventory_hostname in l3_interface.nodes %}
{% for node in l3_interface.nodes %}
{% if node == inventory_hostname %}
{% for tenant in switch.tenants | arista.avd.natural_sort %}
{% for vrf in switch.tenants[tenant].vrfs | arista.avd.natural_sort %}
{% for l3_interface in tenants[tenant].vrfs[vrf].l3_interfaces | arista.avd.default([]) %}
{% if l3_interface.nodes is arista.avd.defined and l3_interface.ip_addresses is arista.avd.defined and l3_interface.interfaces is arista.avd.defined and inventory_hostname in l3_interface.nodes %}
{% for node in l3_interface.nodes %}
{% if node == inventory_hostname %}
{{ l3_interface.interfaces[loop.index0] }}:
type: routed
peer_type: l3_interface
vrf: {{ vrf }}
ip_address: {{ l3_interface.ip_addresses[loop.index0] }}
{% if l3_interface.mtu is arista.avd.defined %}
{% if l3_interface.mtu is arista.avd.defined %}
mtu: {{ l3_interface.mtu }}
{% endif %}
{% if l3_interface.enabled is arista.avd.defined(false) %}
{% endif %}
{% if l3_interface.enabled is arista.avd.defined(false) %}
shutdown: true
{% else %}
{% else %}
shutdown: false
{% endif %}
{% if l3_interface.description is arista.avd.defined %}
{% endif %}
{% if l3_interface.description is arista.avd.defined %}
description: {{ l3_interface.description }}
{% endif %}
{% endif %}
{% endfor %}
{% endif %}
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
{% endfor %}
Original file line number Diff line number Diff line change
@@ -1,77 +1,38 @@
{# Leaf tenant IGMP snooping #}
{% set igmp_snooping = namespace() %}
{% set igmp_snooping.configured = false %}
{% if switch.igmp_snooping_enabled == true %}
{% set igmp_snooping.configured = true %}
{% endif %}
{# Detect if igmp-snopping is globally activated #}
{% for tenant in tenants | arista.avd.natural_sort if tenant in switch.filter_tenants or "all" in switch.filter_tenants %}
{% if tenants[tenant].vrfs is defined %}
{% for vrf in tenants[tenant].vrfs | arista.avd.natural_sort if switch.vrfs is not none and vrf in switch.vrfs %}
{% for svi in tenants[tenant].vrfs[vrf].svis | arista.avd.natural_sort if switch.svis is not none and svi | int in switch.svis %}
{% if tenants[tenant].vrfs[vrf].svis[svi].igmp_snooping is defined %}
{% set igmp_snooping.configured = true %}
{% endif %}
{% endfor %}
{% endfor %}
{% endif %}
{% if tenants[tenant].l2vlans is defined %}
{% for l2vlan in tenants[tenant].l2vlans | arista.avd.natural_sort %}
{% if tenants[tenant].l2vlans[l2vlan].igmp_snooping is defined %}
{% set igmp_snooping.configured = true %}
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
{# End igmp-snopping status detection #}
ip_igmp_snooping:
globally_enabled: {{ switch.igmp_snooping_enabled }}
{% if igmp_snooping.configured == true %}
{% if switch.igmp_snooping_enabled is arista.avd.defined(true) %}
vlans:
{# ---------------------------- #}
{# SVI & L3VLANs services #}
{# ---------------------------- #}
{% for tenant in tenants | arista.avd.natural_sort if tenant in switch.filter_tenants or "all" in switch.filter_tenants %}
## {{ tenant }} ##
{% if tenants[tenant].vrfs is defined %}
{% for vrf in tenants[tenant].vrfs | arista.avd.natural_sort if switch.vrfs is not none and vrf in switch.vrfs %}
{% for tenant in switch.tenants | arista.avd.natural_sort %}
{% for vrf in switch.tenants[tenant].vrfs | arista.avd.natural_sort %}
{# Tenant VLANs w/SVIs #}
{% for svi in tenants[tenant].vrfs[vrf].svis | arista.avd.natural_sort if switch.svis is not none and svi | int in switch.svis %}
{% for svi in switch.tenants[tenant].vrfs[vrf].svis | arista.avd.natural_sort %}
{% set svi_config = tenants[tenant].vrfs[vrf].svis[svi] %}
{# Detect if a svi_profile exists #}
{# If exists, create a shortpath to access profile data #}
{% set per_svi = namespace() %}
{% set profile = namespace() %}
{% set per_svi.values = tenants[tenant].vrfs[vrf].svis[svi] %}
{% if svi_profiles is defined and svi_profiles is not none %}
{% if tenants[tenant].vrfs[vrf].svis[svi].profile is defined %}
{% if tenants[tenant].vrfs[vrf].svis[svi].profile in svi_profiles %}
{% set profile.values = svi_profiles[tenants[tenant].vrfs[vrf].svis[svi].profile] %}
{% endif %}
{% endif %}
{% endif %}
{# End svi_profile detection #}
{# IGMP SNOOPING detection #}
{% if per_svi.values.igmp_snooping_enabled is defined or profile.values.igmp_snooping_enabled is defined %}
{% if switch.igmp_snooping_enabled == true %}
{% if svi_config.profile is arista.avd.defined %}
{% set svi_profile = svi_profiles[svi_config.profile] | arista.avd.default() %}
{% endif %}
{% set svi_igmp_snooping_enabled = svi_config.igmp_snooping_enabled | arista.avd.default(
svi_profile.igmp_snooping_enabled) %}
{% if svi_igmp_snooping_enabled is arista.avd.defined %}
{{ svi | int }}:
enabled: {{ per_svi.values.igmp_snooping_enabled if per_svi.values.igmp_snooping_enabled is defined else profile.values.igmp_snooping_enabled | default(true) }}
{% endif %}
{% endif %}
{% endfor %}
{% endfor %}
{% endif %}
enabled: {{ svi_igmp_snooping_enabled }}
{% endif %}
{% endfor %}
{% endfor %}
{# ---------------------------- #}
{# L2VLANs services #}
{# ---------------------------- #}
{% if tenants[tenant].l2vlans is defined %}
{% for l2vlan in tenants[tenant].l2vlans | arista.avd.natural_sort %}
{% if tenants[tenant].l2vlans[l2vlan].igmp_snooping_enabled is defined %}
{% if tenants[tenant].l2vlans[l2vlan].igmp_snooping_enabled == false and switch.igmp_snooping_enabled == true %}
{% for l2vlan in switch.tenants[tenant].l2vlans | arista.avd.natural_sort %}
{% set l2vlan_igmp_snooping_enabled = tenants[tenant].l2vlans[l2vlan].igmp_snooping_enabled | arista.avd.default() %}
{% if l2vlan_igmp_snooping_enabled is arista.avd.defined %}
{{ l2vlan | int }}:
enabled: false
{% endif %}
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
{% endfor %}
{% endif %}
Loading

0 comments on commit 421c80e

Please sign in to comment.