def __init__(self, agent, hostname): self.region_string = calico_config.get_region_string() workload_endpoint_prefix = datamodel_v3._build_key( "WorkloadEndpoint", datamodel_v3.get_namespace(self.region_string), "", ) this_host_prefix = (workload_endpoint_prefix + hostname.replace('-', '--') + "-openstack-") super(CalicoEtcdWatcher, self).__init__(this_host_prefix) self.agent = agent self.hostname = hostname self.suppress_dnsmasq_updates = False # Register the etcd paths that we need to watch. self.register_path(workload_endpoint_prefix + "<name>", on_set=self.on_endpoint_set, on_del=self.on_endpoint_delete) # Networks for which Dnsmasq needs updating. self.dirty_networks = set() # Also watch the etcd subnet trees: both the new region-aware # one, and the old pre-region one, so as to support a VM # renewing its DHCP lease while an upgrade is still in # progress. When something in that subtree changes, the # subnet watcher will tell _this_ watcher to resync. self.v1_subnet_watcher = SubnetWatcher(self, datamodel_v1.SUBNET_DIR) self.subnet_watcher = SubnetWatcher( self, datamodel_v2.subnet_dir(self.region_string)) # Cache of the ports that we've asked Dnsmasq to handle, for each # network ID. self._last_dnsmasq_ports = {}
def test_etcd_watchers_init_with_conf_values(self): agent = CalicoDhcpAgent() self.assertEqual( agent.etcd.prefix, "/calico/resources/v3/projectcalico.org/" + "workloadendpoints/openstack/" + self.hostname.replace('-', '--') + "-openstack-") self.assertEqual(agent.etcd.v1_subnet_watcher.prefix, datamodel_v1.SUBNET_DIR) self.assertEqual(agent.etcd.subnet_watcher.prefix, datamodel_v2.subnet_dir())
def test_host_config(self): cfg.CONF.host = "my-special-hostname" agent = CalicoDhcpAgent() self.assertEqual( agent.etcd.prefix, "/calico/resources/v3/projectcalico.org/" + "workloadendpoints/openstack/" + "my--special--hostname" + "-openstack-") self.assertEqual(agent.etcd.v1_subnet_watcher.prefix, datamodel_v1.SUBNET_DIR) self.assertEqual(agent.etcd.subnet_watcher.prefix, datamodel_v2.subnet_dir())
def test_region_config(self): cfg.CONF.set_override('openstack_region', 'asia-central', group='calico') calico_config._reset_globals() agent = CalicoDhcpAgent() self.assertEqual( agent.etcd.prefix, "/calico/resources/v3/projectcalico.org/" + "workloadendpoints/openstack-region-asia-central/" + self.hostname.replace('-', '--') + "-openstack-") self.assertEqual(agent.etcd.v1_subnet_watcher.prefix, datamodel_v1.SUBNET_DIR) self.assertEqual(agent.etcd.subnet_watcher.prefix, datamodel_v2.subnet_dir("region-asia-central")) calico_config._reset_globals()
def get_all_from_etcd(self): return etcdv3.get_prefix(datamodel_v2.subnet_dir(self.region_string))