コード例 #1
0
ファイル: test_metrics_lib.py プロジェクト: Yelp/paasta
 def setUp(self):
     self.metrics = metrics_lib.NoMetrics("paasta.deployd")
コード例 #2
0
def setup_kube_deployments(
    kube_client: KubeClient,
    service_instances: Sequence[str],
    cluster: str,
    rate_limit: int = 0,
    soa_dir: str = DEFAULT_SOA_DIR,
    metrics_interface: metrics_lib.BaseMetrics = metrics_lib.NoMetrics(
        "paasta"),
) -> bool:
    if service_instances:
        existing_kube_deployments = set(list_all_deployments(kube_client))
        existing_apps = {(deployment.service, deployment.instance)
                         for deployment in existing_kube_deployments}
    service_instances_with_valid_names = [
        decompose_job_id(service_instance)
        for service_instance in service_instances
        if validate_job_name(service_instance)
    ]
    applications = [
        create_application_object(
            kube_client=kube_client,
            service=service_instance[0],
            instance=service_instance[1],
            cluster=cluster,
            soa_dir=soa_dir,
        ) for service_instance in service_instances_with_valid_names
    ]

    api_updates = 0
    for _, app in applications:
        if app:
            app_dimensions = {
                "paasta_service": app.kube_deployment.service,
                "paasta_instance": app.kube_deployment.instance,
                "paasta_cluster": cluster,
            }
            try:
                if (
                        app.kube_deployment.service,
                        app.kube_deployment.instance,
                ) not in existing_apps:
                    log.info(f"Creating {app} because it does not exist yet.")
                    app.create(kube_client)
                    app_dimensions["deploy_event"] = "create"
                    metrics_interface.emit_event(
                        name="deploy",
                        dimensions=app_dimensions,
                    )
                    api_updates += 1
                elif app.kube_deployment not in existing_kube_deployments:
                    log.info(f"Updating {app} because configs have changed.")
                    app.update(kube_client)
                    app_dimensions["deploy_event"] = "update"
                    metrics_interface.emit_event(
                        name="deploy",
                        dimensions=app_dimensions,
                    )
                    api_updates += 1
                else:
                    log.info(f"{app} is up-to-date!")

                log.info(f"Ensuring related API objects for {app} are in sync")
                app.update_related_api_objects(kube_client)
            except Exception:
                log.exception(f"Error while processing: {app}")
        if rate_limit > 0 and api_updates >= rate_limit:
            log.info(
                f"Not doing any further updates as we reached the limit ({api_updates})"
            )
            break

    return (False, None) not in applications and len(
        service_instances_with_valid_names) == len(service_instances)