def on_present(self, namespace): ns_name = namespace['metadata']['name'] current_namespace_labels = namespace['metadata'].get('labels') previous_namespace_labels = driver_utils.get_annotated_labels( namespace, constants.K8S_ANNOTATION_NAMESPACE_LABEL) LOG.debug("Got previous namespace labels from annotation: %r", previous_namespace_labels) project_id = self._drv_project.get_project(namespace) if current_namespace_labels != previous_namespace_labels: crd_selectors = self._drv_sg.update_namespace_sg_rules(namespace) self._set_namespace_labels(namespace, current_namespace_labels) if (self._is_network_policy_enabled() and crd_selectors and oslo_cfg.CONF.octavia_defaults.enforce_sg_rules): services = driver_utils.get_services() self._update_services(services, crd_selectors, project_id) net_crd_id = self._get_net_crd_id(namespace) if net_crd_id: LOG.debug("CRD existing at the new namespace") return net_crd_name = 'ns-' + ns_name net_crd = self._get_net_crd(net_crd_name) if net_crd: LOG.debug("Previous CRD existing at the new namespace. " "Deleting namespace resources and retying its creation.") self.on_deleted(namespace, net_crd) raise exceptions.ResourceNotReady(namespace) # NOTE(ltomasbo): Ensure there is no previously created networks # leftovers due to a kuryr-controller crash/restart LOG.debug("Deleting leftovers network resources for namespace: %s", ns_name) self._drv_subnets.cleanup_namespace_networks(ns_name) LOG.debug("Creating network resources for namespace: %s", ns_name) net_crd_spec = self._drv_subnets.create_namespace_network( ns_name, project_id) # create CRD resource for the network try: net_crd = self._add_kuryrnet_crd(ns_name, net_crd_spec) self._drv_sg.create_namespace_sg_rules(namespace) self._set_net_crd(namespace, net_crd) except (exceptions.K8sClientException, exceptions.K8sResourceNotFound): LOG.exception("Kuryrnet CRD creation failed. Rolling back " "resources created for the namespace.") self._drv_subnets.rollback_network_resources(net_crd_spec, ns_name) try: self._del_kuryrnet_crd(net_crd_name) except exceptions.K8sClientException: LOG.exception( "Error when trying to rollback the KuryrNet CRD " "object %s", net_crd_name) raise exceptions.ResourceNotReady(namespace)
def on_present(self, namespace): ns_name = namespace['metadata']['name'] current_namespace_labels = namespace['metadata'].get('labels') previous_namespace_labels = drivers_utils.get_annotated_labels( namespace, constants.K8S_ANNOTATION_NAMESPACE_LABEL) LOG.debug("Got previous namespace labels from annotation: %r", previous_namespace_labels) if current_namespace_labels != previous_namespace_labels: self._drv_sg.update_namespace_sg_rules(namespace) self._set_namespace_labels(namespace, current_namespace_labels) project_id = self._drv_project.get_project(namespace) net_crd_id = self._get_net_crd_id(namespace) if net_crd_id: LOG.debug("CRD existing at the new namespace") return net_crd_name = 'ns-' + ns_name net_crd = self._get_net_crd(net_crd_name) if net_crd: LOG.debug("Previous CRD existing at the new namespace. " "Deleting namespace resources and retying its creation.") self.on_deleted(namespace, net_crd) raise exceptions.ResourceNotReady(namespace) LOG.debug("Creating network resources for namespace: %s", ns_name) net_crd_spec = self._drv_subnets.create_namespace_network( ns_name, project_id) try: net_crd_sg = self._drv_sg.create_namespace_sg( ns_name, project_id, net_crd_spec) except n_exc.NeutronClientException: LOG.exception("Error creating security group for the namespace. " "Rolling back created network resources.") self._drv_subnets.rollback_network_resources(net_crd_spec, ns_name) raise if net_crd_sg: net_crd_spec.update(net_crd_sg) else: LOG.debug("No SG created for the namespace. Namespace isolation " "will not be enforced.") # create CRD resource for the network try: net_crd = self._add_kuryrnet_crd(ns_name, net_crd_spec) self._set_net_crd(namespace, net_crd) self._drv_sg.create_namespace_sg_rules(namespace) self._set_namespace_labels(namespace, current_namespace_labels) except exceptions.K8sClientException: LOG.exception("Kubernetes client exception. Rolling back " "resources created for the namespace.") self._drv_subnets.rollback_network_resources(net_crd_spec, ns_name) self._drv_sg.delete_sg(net_crd_sg['sgId']) self._del_kuryrnet_crd(net_crd_name)