def _on_snapshot_loaded(self, etcd_snapshot_response): """Called whenever a snapshot is loaded from etcd. Updates the driver with the current state. """ LOG.info("Started processing status-reporting snapshot from etcd") endpoints_by_host = collections.defaultdict(set) hosts_with_live_felix = set() # First pass: find all the Felixes that are alive. for etcd_node in etcd_snapshot_response.leaves: key = etcd_node.key felix_hostname = datamodel_v1.hostname_from_status_key(key) if felix_hostname: # Defer to the code for handling an event. hosts_with_live_felix.add(felix_hostname) self._on_status_set(etcd_node, felix_hostname) continue # Second pass: find all the endpoints associated with a live Felix. for etcd_node in etcd_snapshot_response.leaves: key = etcd_node.key endpoint_id = datamodel_v1.get_endpoint_id_from_key(key) if endpoint_id: if endpoint_id.host in hosts_with_live_felix: LOG.debug("Endpoint %s is on a host with a live Felix.", endpoint_id) self._report_status( endpoints_by_host, endpoint_id, etcd_node.value ) else: LOG.debug("Endpoint %s is not on a host with live Felix;" "marking it down.", endpoint_id) self.calico_driver.on_port_status_changed( endpoint_id.host, endpoint_id.endpoint, None, ) continue # Find any removed endpoints. for host, endpoints in self._endpoints_by_host.items(): current_endpoints = endpoints_by_host.get(host, set()) removed_endpoints = endpoints - current_endpoints for endpoint_id in removed_endpoints: LOG.debug("Endpoint %s removed by resync.") self.calico_driver.on_port_status_changed( host, endpoint_id.endpoint, None, ) # Swap in the newly-loaded state. self._endpoints_by_host = endpoints_by_host LOG.info("Finished processing status-reporting snapshot from etcd")
def _on_snapshot_loaded(self, etcd_snapshot_response): """Called whenever a snapshot is loaded from etcd. Updates the driver with the current state. """ LOG.info("Started processing status-reporting snapshot from etcd") endpoints_by_host = collections.defaultdict(set) hosts_with_live_felix = set() # First pass: find all the Felixes that are alive. for etcd_node in etcd_snapshot_response.leaves: key = etcd_node.key felix_hostname = datamodel_v1.hostname_from_status_key(key) if felix_hostname: # Defer to the code for handling an event. hosts_with_live_felix.add(felix_hostname) self._on_status_set(etcd_node, felix_hostname) continue # Second pass: find all the endpoints associated with a live Felix. for etcd_node in etcd_snapshot_response.leaves: key = etcd_node.key endpoint_id = datamodel_v1.get_endpoint_id_from_key(key) if endpoint_id: if endpoint_id.host in hosts_with_live_felix: LOG.debug("Endpoint %s is on a host with a live Felix.", endpoint_id) self._report_status(endpoints_by_host, endpoint_id, etcd_node.value) else: LOG.debug( "Endpoint %s is not on a host with live Felix;" "marking it down.", endpoint_id) self.calico_driver.on_port_status_changed( endpoint_id.host, endpoint_id.endpoint, None, ) continue # Find any removed endpoints. for host, endpoints in self._endpoints_by_host.items(): current_endpoints = endpoints_by_host.get(host, set()) removed_endpoints = endpoints - current_endpoints for endpoint_id in removed_endpoints: LOG.debug("Endpoint %s removed by resync.") self.calico_driver.on_port_status_changed( host, endpoint_id.endpoint, None, ) # Swap in the newly-loaded state. self._endpoints_by_host = endpoints_by_host LOG.info("Finished processing status-reporting snapshot from etcd")
def _on_ep_set(self, response, hostname, workload, endpoint): """Called when the status key for a particular endpoint is updated. Reports the status to the driver and caches the existence of the endpoint. """ ep_id = datamodel_v1.get_endpoint_id_from_key(response.key) if not ep_id: LOG.error( "Failed to extract endpoint ID from: %s. Ignoring " "update!", response.key) return self._report_status(self._endpoints_by_host, ep_id, response.value)
def _on_ep_set(self, response, hostname, workload, endpoint): """Called when the status key for a particular endpoint is updated. Reports the status to the driver and caches the existence of the endpoint. """ ep_id = datamodel_v1.get_endpoint_id_from_key(response.key) if not ep_id: LOG.error("Failed to extract endpoint ID from: %s. Ignoring " "update!", response.key) return self._report_status(self._endpoints_by_host, ep_id, response.value)
def _on_ep_delete(self, response, hostname, workload, endpoint): """Called when the status key for an endpoint is deleted. This typically means the endpoint has been deleted. Reports the deletion to the driver. """ LOG.debug("Port %s/%s/%s deleted", hostname, workload, endpoint) endpoint_id = datamodel_v1.get_endpoint_id_from_key(response.key) self._endpoints_by_host[hostname].discard(endpoint_id) if not self._endpoints_by_host[hostname]: del self._endpoints_by_host[hostname] self.calico_driver.on_port_status_changed( hostname, endpoint, None, )
def _on_ep_delete(self, response, hostname, workload, endpoint): """Called when the status key for an endpoint is deleted. This typically means the endpoint has been deleted. Reports the deletion to the driver. """ LOG.debug("Port %s/%s/%s deleted", hostname, workload, endpoint) endpoint_id = datamodel_v1.get_endpoint_id_from_key(response.key) self._endpoints_by_host[hostname].discard(endpoint_id) if not self._endpoints_by_host[hostname]: del self._endpoints_by_host[hostname] self.calico_driver.on_port_status_changed( hostname, endpoint, None, )