예제 #1
0
    def __init__(self, config, ip_type, iptables_updater, workload_disp_chains,
                 host_disp_chains, rules_manager, fip_manager,
                 status_reporter):
        super(EndpointManager, self).__init__(qualifier=ip_type)

        # Configuration and version to use
        self.config = config
        self.ip_type = ip_type
        self.ip_version = futils.IP_TYPE_TO_VERSION[ip_type]

        # Peers/utility classes.
        self.iptables_updater = iptables_updater
        self.workload_disp_chains = workload_disp_chains
        self.host_disp_chains = host_disp_chains
        self.rules_mgr = rules_manager
        self.status_reporter = status_reporter
        self.fip_manager = fip_manager

        # All endpoint dicts that are on this host.
        self.endpoints_by_id = {}
        # Dict that maps from interface name ("tap1234") to endpoint ID.
        self.endpoint_id_by_iface_name = {}

        # Cache of IPs applied to host endpoints.  (I.e. any interfaces that
        # aren't workload interfaces.)
        self.host_ep_ips_by_iface = {}
        # Host interface dicts by ID.  We'll resolve these with the IPs above
        # and inject the (resolved) ones as endpoints.
        self.host_eps_by_id = {}
        # Cache of interfaces that we've resolved and injected as endpoints.
        self.resolved_host_eps = {}

        # Set of endpoints that are live on this host.  I.e. ones that we've
        # increffed.
        self.local_endpoint_ids = set()

        # Index tracking what policy applies to what endpoints.
        self.policy_index = LabelValueIndex()
        self.policy_index.on_match_started = self.on_policy_match_started
        self.policy_index.on_match_stopped = self.on_policy_match_stopped
        self._label_inherit_idx = LabelInheritanceIndex(self.policy_index)
        # Tier orders by tier ID.  We use this to look up the order when we're
        # sorting the tiers.
        self.tier_orders = {}
        # Cache of the current ordering of tier IDs.
        self.tier_sequence = []
        # And their associated orders.
        self.profile_orders = {}
        # Set of profile IDs to apply to each endpoint ID.
        self.pol_ids_by_ep_id = MultiDict()
        self.endpoints_with_dirty_policy = set()

        self._data_model_in_sync = False
        self._iface_poll_greenlet = gevent.Greenlet(self._interface_poll_loop)
        self._iface_poll_greenlet.link_exception(self._on_worker_died)
예제 #2
0
    def test_inheritance_index_mainline(self):
        ii = LabelInheritanceIndex(self.index)

        ii.on_item_update("item_1", {}, [])
        ii.on_item_update("item_2", {"a": "a1"}, [])
        ii.on_item_update("item_3", {}, ["parent_1"])
        ii.on_item_update("item_4", {"a": "a1"}, ["parent_2"])

        self.index.on_expression_update("e1", parse_selector("a == 'a1'"))
        self.index.on_expression_update("e2", parse_selector("a != 'a1'"))
        self.index.on_expression_update("e3", parse_selector("a == 'p1'"))

        self.assert_add("e1", "item_2")
        self.assert_add("e1", "item_4")
        self.assert_add("e2", "item_1")
        self.assert_add("e2", "item_3")
        self.assert_no_updates()

        # Now make a parent change, should cause a match.
        ii.on_parent_labels_update("parent_1", {"a": "p1"})
        self.assert_add("e3", "item_3")
        # Then, remove the parent label, should remove the match.
        ii.on_parent_labels_update("parent_1", {})
        self.assert_remove("e3", "item_3")

        # Now make a parent change, should cause a match.
        ii.on_parent_labels_update("parent_1", {"a": "p1"})
        self.assert_add("e3", "item_3")
        # Then, remove the parent labels entirely, should remove the match.
        ii.on_parent_labels_update("parent_1", None)
        self.assert_remove("e3", "item_3")

        # Now make a parent change for parent_2; the per-item labels should
        # override.
        ii.on_parent_labels_update("parent_2", {"a": "p1"})
        ii.on_parent_labels_update("parent_2", None)
        self.assert_no_updates()

        # Now make a parent change, should cause a match.
        ii.on_parent_labels_update("parent_1", {"a": "p1"})
        self.assert_add("e3", "item_3")
        # But then remove the item.
        ii.on_item_update("item_3", None, None)
        self.assert_remove("e3", "item_3")
        self.assert_remove("e2", "item_3")
예제 #3
0
    def __init__(self, config, ip_type, iptables_updater, dispatch_chains,
                 rules_manager, fip_manager, status_reporter):
        super(EndpointManager, self).__init__(qualifier=ip_type)

        # Configuration and version to use
        self.config = config
        self.ip_type = ip_type
        self.ip_version = futils.IP_TYPE_TO_VERSION[ip_type]

        # Peers/utility classes.
        self.iptables_updater = iptables_updater
        self.dispatch_chains = dispatch_chains
        self.rules_mgr = rules_manager
        self.status_reporter = status_reporter
        self.fip_manager = fip_manager

        # All endpoint dicts that are on this host.
        self.endpoints_by_id = {}
        # Dict that maps from interface name ("tap1234") to endpoint ID.
        self.endpoint_id_by_iface_name = {}

        # Set of endpoints that are live on this host.  I.e. ones that we've
        # increffed.
        self.local_endpoint_ids = set()

        # Index tracking what policy applies to what endpoints.
        self.policy_index = LabelValueIndex()
        self.policy_index.on_match_started = self.on_policy_match_started
        self.policy_index.on_match_stopped = self.on_policy_match_stopped
        self._label_inherit_idx = LabelInheritanceIndex(self.policy_index)
        # Tier orders by tier ID.  We use this to look up the order when we're
        # sorting the tiers.
        self.tier_orders = {}
        # Cache of the current ordering of tier IDs.
        self.tier_sequence = []
        # And their associated orders.
        self.profile_orders = {}
        # Set of profile IDs to apply to each endpoint ID.
        self.pol_ids_by_ep_id = MultiDict()
        self.endpoints_with_dirty_policy = set()

        self._data_model_in_sync = False
예제 #4
0
    def __init__(self, ip_type, config):
        """
        Manages all the ipsets for tags for either IPv4 or IPv6.

        :param ip_type: IP type (IPV4 or IPV6)
        """
        super(IpsetManager, self).__init__(qualifier=ip_type)

        self.ip_type = ip_type
        self._config = config

        # State.
        # Tag IDs indexed by profile IDs
        self.tags_by_prof_id = {}
        # EndpointData "structs" indexed by WloadEndpointId.
        self.endpoint_data_by_ep_id = {}

        # Main index.  Tracks which IPs are currently in each tag.
        self.tag_membership_index = TagMembershipIndex()
        # Take copies of the key functions; avoids messy long lines.
        self._add_mapping = self.tag_membership_index.add_mapping
        self._remove_mapping = self.tag_membership_index.remove_mapping

        # Set of WloadEndpointId objects referenced by profile IDs.
        self.endpoint_ids_by_profile_id = defaultdict(set)

        # LabelNode index, used to cross-reference endpoint labels against
        # selectors.
        self._label_index = LabelValueIndex()
        self._label_index.on_match_started = self._on_label_match_started
        self._label_index.on_match_stopped = self._on_label_match_stopped
        self._label_inherit_idx = LabelInheritanceIndex(self._label_index)
        # Sets used to defer updates of the label match cache until we're ready
        # to handle them.
        self._started_label_matches = set()
        self._stopped_label_matches = set()

        # One-way flag set when we know the datamodel is in sync.  We can't
        # rewrite any ipsets before we're in sync or we risk omitting some
        # values.
        self._datamodel_in_sync = False