Beispiel #1
0
def decompose_job_id(job_id):
    """ A custom implementation of utils.decompose_job_id, accounting for the
    possiblity of TMP_JOB_IDENTIFIER being prepended to the job name """
    decomposed = job_id.split(SPACER)
    if len(decomposed) == 3:
        if not decomposed[0].startswith(TMP_JOB_IDENTIFIER):
            raise InvalidJobNameError('invalid job id %s' % job_id)
        else:
            return (decomposed[1], decomposed[2])
    elif len(decomposed) == 2:
        return (decomposed[0], decomposed[1])
    else:
        raise InvalidJobNameError('invalid job id %s' % job_id)
Beispiel #2
0
    def test_get_priority(self):
        with mock.patch(
                'paasta_tools.deployd.common.load_marathon_service_config',
                autospec=True,
        ) as mock_load_marathon_service_config:
            mock_load_marathon_service_config.return_value = mock.Mock(
                get_bounce_priority=mock.Mock(return_value=1))
            assert get_priority('universe', 'c137', 'westeros-prod') == 1
            mock_load_marathon_service_config.assert_called_with(
                service='universe',
                instance='c137',
                cluster='westeros-prod',
                soa_dir='/nail/etc/services',
            )

            mock_load_marathon_service_config.side_effect = NoDockerImageError(
            )
            assert get_priority('universe', 'c137', 'westeros-prod') == 0

            mock_load_marathon_service_config.side_effect = InvalidJobNameError(
            )
            assert get_priority('universe', 'c137', 'westeros-prod') == 0

            mock_load_marathon_service_config.side_effect = NoDeploymentsAvailable(
            )
            assert get_priority('universe', 'c137', 'westeros-prod') == 0
Beispiel #3
0
def load_chronos_job_config(
    service: str,
    instance: str,
    cluster: str,
    load_deployments: bool = True,
    soa_dir: str = DEFAULT_SOA_DIR,
) -> 'ChronosJobConfig':
    general_config = service_configuration_lib.read_service_configuration(
        service,
        soa_dir=soa_dir,
    )

    if instance.startswith('_'):
        raise InvalidJobNameError(
            "Unable to load chronos job config for %s.%s as instance name starts with '_'"
            % (service, instance), )
    service_chronos_jobs = read_chronos_jobs_for_service(service,
                                                         cluster,
                                                         soa_dir=soa_dir)
    if instance not in service_chronos_jobs:
        raise NoConfigurationForServiceError(
            'No job named "%s" in config file chronos-%s.yaml' %
            (instance, cluster))
    branch_dict = None
    general_config = deep_merge_dictionaries(
        overrides=service_chronos_jobs[instance], defaults=general_config)

    if load_deployments:
        deployments_json = load_v2_deployments_json(service, soa_dir=soa_dir)
        temp_instance_config = ChronosJobConfig(
            service=service,
            cluster=cluster,
            instance=instance,
            config_dict=general_config,
            branch_dict=None,
            soa_dir=soa_dir,
        )
        branch = temp_instance_config.get_branch()
        deploy_group = temp_instance_config.get_deploy_group()
        branch_dict = deployments_json.get_branch_dict(service, branch,
                                                       deploy_group)

    return ChronosJobConfig(
        service=service,
        cluster=cluster,
        instance=instance,
        config_dict=general_config,
        branch_dict=branch_dict,
        soa_dir=soa_dir,
    )
def test_get_deployment_names_from_list():
    with mock.patch(
            "paasta_tools.delete_kubernetes_deployments.decompose_job_id",
            autospec=True) as mock_decompose_job_id:
        # Test get_deployment_names_from_list() success
        mock_decompose_job_id.return_value = (
            "fake-service",
            "fake_instance",
            "fake_hash",
            "fake_hash",
        )
        output = get_deployment_names_from_list(["fake-service.fake_instance"])
        assert output[0] == "fake-service-fake--instance"

        # Test get_deployment_names_from_list() failed
        mock_decompose_job_id.side_effect = InvalidJobNameError()
        with raises(SystemExit) as e:
            get_deployment_names_from_list(["fake-service.fake_instance"])
        assert e.value.code == 1
Beispiel #5
0
def load_kubernetes_service_config_no_cache(
    service: str,
    instance: str,
    cluster: str,
    load_deployments: bool = True,
    soa_dir: str = DEFAULT_SOA_DIR,
) -> "KubernetesDeploymentConfig":
    """Read a service instance's configuration for kubernetes.

    If a branch isn't specified for a config, the 'branch' key defaults to
    paasta-${cluster}.${instance}.

    :param name: The service name
    :param instance: The instance of the service to retrieve
    :param cluster: The cluster to read the configuration for
    :param load_deployments: A boolean indicating if the corresponding deployments.json for this service
                             should also be loaded
    :param soa_dir: The SOA configuration directory to read from
    :returns: A dictionary of whatever was in the config for the service instance"""
    general_config = service_configuration_lib.read_service_configuration(
        service,
        soa_dir=soa_dir,
    )
    kubernetes_conf_file = "kubernetes-%s" % cluster
    instance_configs = service_configuration_lib.read_extra_service_information(
        service,
        kubernetes_conf_file,
        soa_dir=soa_dir,
    )

    if instance.startswith('_'):
        raise InvalidJobNameError(
            f"Unable to load kubernetes job config for {service}.{instance} as instance name starts with '_'",
        )
    if instance not in instance_configs:
        raise NoConfigurationForServiceError(
            f"{instance} not found in config file {soa_dir}/{service}/{kubernetes_conf_file}.yaml.",
        )

    general_config = deep_merge_dictionaries(
        overrides=instance_configs[instance], defaults=general_config)

    branch_dict: Optional[BranchDictV2] = None
    if load_deployments:
        deployments_json = load_v2_deployments_json(service, soa_dir=soa_dir)
        temp_instance_config = KubernetesDeploymentConfig(
            service=service,
            cluster=cluster,
            instance=instance,
            config_dict=general_config,
            branch_dict=None,
            soa_dir=soa_dir,
        )
        branch = temp_instance_config.get_branch()
        deploy_group = temp_instance_config.get_deploy_group()
        branch_dict = deployments_json.get_branch_dict(service, branch,
                                                       deploy_group)

    return KubernetesDeploymentConfig(
        service=service,
        cluster=cluster,
        instance=instance,
        config_dict=general_config,
        branch_dict=branch_dict,
        soa_dir=soa_dir,
    )