def create_application_object( kube_client: KubeClient, service: str, instance: str, cluster: str, soa_dir: str, ) -> Tuple[bool, Optional[Application]]: try: service_instance_config = load_kubernetes_service_config_no_cache( service, instance, cluster, soa_dir=soa_dir, ) except NoDeploymentsAvailable: log.debug("No deployments found for %s.%s in cluster %s. Skipping." % (service, instance, cluster)) return True, None except NoConfigurationForServiceError: error_msg = ( f"Could not read kubernetes configuration file for %s.%s in cluster %s" % (service, instance, cluster)) log.error(error_msg) return False, None try: formatted_application = service_instance_config.format_kubernetes_app() except InvalidKubernetesConfig as e: log.error(str(e)) return False, None app = get_application_wrapper(formatted_application) app.load_local_config(soa_dir, cluster) return True, app
def reconcile_kubernetes_deployment( kube_client: KubeClient, service: str, instance: str, kube_deployments: Sequence[KubeDeployment], soa_dir: str, ) -> Tuple[int, Optional[int]]: try: service_instance_config = load_kubernetes_service_config_no_cache( service, instance, load_system_paasta_config().get_cluster(), soa_dir=soa_dir, ) except NoDeploymentsAvailable: log.debug("No deployments found for %s.%s in cluster %s. Skipping." % (service, instance, load_system_paasta_config().get_cluster())) return 0, None except NoConfigurationForServiceError: error_msg = "Could not read kubernetes configuration file for %s.%s in cluster %s" % \ (service, instance, load_system_paasta_config().get_cluster()) log.error(error_msg) return 1, None try: formatted_application = service_instance_config.format_kubernetes_app() except InvalidKubernetesConfig as e: log.error(str(e)) return (1, None) desired_deployment = KubeDeployment( service=service, instance=instance, git_sha=formatted_application.metadata.labels["git_sha"], config_sha=formatted_application.metadata.labels["config_sha"], replicas=formatted_application.spec.replicas, ) if not (service, instance) in [(kd.service, kd.instance) for kd in kube_deployments]: log.debug(f"{desired_deployment} does not exist so creating") create_kubernetes_application( kube_client=kube_client, application=formatted_application, ) return 0, None elif desired_deployment not in kube_deployments: log.debug(f"{desired_deployment} exists but config_sha or git_sha doesn't match or number of instances changed") update_kubernetes_application( kube_client=kube_client, application=formatted_application, ) return 0, None else: log.debug(f"{desired_deployment} is up to date, no action taken") return 0, None
def load_local_config( self, soa_dir: str, cluster: str) -> Optional[KubernetesDeploymentConfig]: if not self.soa_config: self.soa_config = load_kubernetes_service_config_no_cache( service=self.kube_deployment.service, instance=self.kube_deployment.instance, cluster=cluster, soa_dir=soa_dir, ) return self.soa_config
def load_local_config( self, soa_dir: str, system_paasta_config: SystemPaastaConfig ) -> Optional[KubernetesDeploymentConfig]: if not self.soa_config: self.soa_config = load_kubernetes_service_config_no_cache( service=self.kube_deployment.service, instance=self.kube_deployment.instance, cluster=system_paasta_config.get_cluster(), soa_dir=soa_dir, ) return self.soa_config
def create_application_object( kube_client: KubeClient, service: str, instance: str, soa_dir: str ) -> Tuple[bool, Optional[Application]]: try: service_instance_config = load_kubernetes_service_config_no_cache( service, instance, load_system_paasta_config().get_cluster(), soa_dir=soa_dir, ) except NoDeploymentsAvailable: log.debug( "No deployments found for %s.%s in cluster %s. Skipping." % (service, instance, load_system_paasta_config().get_cluster()) ) return True, None except NoConfigurationForServiceError: error_msg = ( "Could not read kubernetes configuration file for %s.%s in cluster %s" % (service, instance, load_system_paasta_config().get_cluster()) ) log.error(error_msg) return False, None try: formatted_application = service_instance_config.format_kubernetes_app() except InvalidKubernetesConfig as e: log.error(str(e)) return False, None app = None if isinstance(formatted_application, V1Deployment): app = DeploymentWrapper(formatted_application) elif isinstance(formatted_application, V1StatefulSet): app = StatefulSetWrapper(formatted_application) else: raise Exception("Unknown kubernetes object to update") app.load_local_config(soa_dir, load_system_paasta_config()) return True, app
def test_load_kubernetes_service_config_no_cache(): with mock.patch( 'service_configuration_lib.read_service_configuration', autospec=True, ) as mock_read_service_configuration, mock.patch( 'service_configuration_lib.read_extra_service_information', autospec=True, ) as mock_read_extra_service_information, mock.patch( 'paasta_tools.kubernetes_tools.load_v2_deployments_json', autospec=True, ) as mock_load_v2_deployments_json, mock.patch( 'paasta_tools.kubernetes_tools.KubernetesDeploymentConfig', autospec=True, ) as mock_kube_deploy_config: with pytest.raises(NoConfigurationForServiceError): load_kubernetes_service_config_no_cache( service='kurupt', instance='fm', cluster='brentford', load_deployments=False, ) with pytest.raises(InvalidJobNameError): load_kubernetes_service_config_no_cache( service='kurupt', instance='_fm', cluster='brentford', load_deployments=False, ) mock_config = {'freq': '108.9'} mock_read_extra_service_information.return_value = {'fm': mock_config} mock_read_service_configuration.return_value = {} ret = load_kubernetes_service_config_no_cache( service='kurupt', instance='fm', cluster='brentford', load_deployments=False, soa_dir='/nail/blah', ) mock_kube_deploy_config.assert_called_with( service='kurupt', instance='fm', cluster='brentford', config_dict={'freq': '108.9'}, branch_dict=None, soa_dir='/nail/blah', ) assert not mock_load_v2_deployments_json.called assert ret == mock_kube_deploy_config.return_value mock_kube_deploy_config.reset_mock() ret = load_kubernetes_service_config_no_cache( service='kurupt', instance='fm', cluster='brentford', load_deployments=True, soa_dir='/nail/blah', ) mock_load_v2_deployments_json.assert_called_with( service='kurupt', soa_dir='/nail/blah', ) mock_kube_deploy_config.assert_called_with( service='kurupt', instance='fm', cluster='brentford', config_dict={'freq': '108.9'}, branch_dict=mock_load_v2_deployments_json.return_value. get_branch_dict(), soa_dir='/nail/blah', ) assert ret == mock_kube_deploy_config.return_value