Ejemplo n.º 1
0
def test_create_deployment():
    mock_client = mock.Mock()
    create_deployment(mock_client, V1Deployment(api_version='some'))
    mock_client.deployments.create_namespaced_deployment.assert_called_with(
        namespace='paasta',
        body=V1Deployment(api_version='some'),
    )
Ejemplo n.º 2
0
def create_kubernetes_application(kube_client: KubeClient, application: Union[V1Deployment, V1StatefulSet]) -> None:
    if isinstance(application, V1Deployment):
        create_deployment(
            kube_client=kube_client,
            formatted_deployment=application,
        )
    elif isinstance(application, V1StatefulSet):
        create_stateful_set(
            kube_client=kube_client,
            formatted_stateful_set=application,
        )
    else:
        raise Exception("Unknown kubernetes object to create")
Ejemplo n.º 3
0
def test_update_deployment():
    mock_client = mock.Mock()
    update_deployment(mock_client,
                      V1Deployment(metadata=V1ObjectMeta(name='kurupt')))
    mock_client.deployments.patch_namespaced_deployment.assert_called_with(
        namespace='paasta',
        name='kurupt',
        body=V1Deployment(metadata=V1ObjectMeta(name='kurupt')),
    )

    mock_client = mock.Mock()
    create_deployment(mock_client, V1Deployment(api_version='some'))
    mock_client.deployments.create_namespaced_deployment.assert_called_with(
        namespace='paasta',
        body=V1Deployment(api_version='some'),
    )
Ejemplo n.º 4
0
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_deployment = service_instance_config.format_kubernetes_app()
    except NoDockerImageError:
        error_msg = (
            "Docker image for {0}.{1} not in deployments.json. Exiting. Has Jenkins deployed it?\n"
        ).format(
            service,
            instance,
        )
        log.error(error_msg)
        return (1, None)

    desired_deployment = KubeDeployment(
        service=service,
        instance=instance,
        git_sha=formatted_deployment.metadata.labels["git_sha"],
        config_sha=formatted_deployment.metadata.labels["config_sha"],
        replicas=formatted_deployment.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_deployment(
            kube_client=kube_client,
            formatted_deployment=formatted_deployment,
        )
        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_deployment(
            kube_client=kube_client,
            formatted_deployment=formatted_deployment,
        )
        return 0, None
    else:
        log.debug(f"{desired_deployment} is up to date, no action taken")
        return 0, None
Ejemplo n.º 5
0
 def create(self, kube_client: KubeClient) -> None:
     create_deployment(kube_client=kube_client,
                       formatted_deployment=self.item)
     self.ensure_pod_disruption_budget(kube_client)
     self.sync_horizontal_pod_autoscaler(kube_client)