Exemplo n.º 1
0
def ensure_pod_disruption_budget(
    kube_client: KubeClient,
    service: str,
    instance: str,
    min_instances: int,
) -> V1beta1PodDisruptionBudget:
    pdr = pod_disruption_budget_for_service_instance(
        service=service,
        instance=instance,
        min_instances=min_instances,
    )
    try:
        existing_pdr = kube_client.policy.read_namespaced_pod_disruption_budget(
            name=pdr.metadata.name,
            namespace=pdr.metadata.namespace,
        )
    except ApiException as e:
        if e.status == 404:
            existing_pdr = None
        else:
            raise

    if existing_pdr:
        if existing_pdr.spec.min_available != pdr.spec.min_available:
            # poddisruptionbudget objects are not mutable like most things in the kubernetes api,
            # so we have to do a delete/replace.
            # unfortunately we can't really do this transactionally, but I guess we'll just hope for the best?
            logging.debug(
                f'existing poddisruptionbudget {pdr.metadata.name} is out of date; deleting'
            )
            kube_client.policy.delete_namespaced_pod_disruption_budget(
                name=pdr.metadata.name,
                namespace=pdr.metadata.namespace,
                body=V1DeleteOptions(),
            )
            logging.debug(f'creating poddisruptionbudget {pdr.metadata.name}')
            return create_pod_disruption_budget(
                kube_client=kube_client,
                pod_disruption_budget=pdr,
            )
        else:
            logging.debug(
                f'poddisruptionbudget {pdr.metadata.name} up to date')
    else:
        logging.debug(f'creating poddisruptionbudget {pdr.metadata.name}')
        return create_pod_disruption_budget(
            kube_client=kube_client,
            pod_disruption_budget=pdr,
        )
Exemplo n.º 2
0
    def ensure_pod_disruption_budget(
            self, kube_client: KubeClient) -> V1beta1PodDisruptionBudget:
        pdr = pod_disruption_budget_for_service_instance(
            service=self.kube_deployment.service,
            instance=self.kube_deployment.instance,
            max_unavailable="{}%".format(
                int((1 - self.soa_config.get_bounce_margin_factor()) * 100)),
        )
        try:
            existing_pdr = kube_client.policy.read_namespaced_pod_disruption_budget(
                name=pdr.metadata.name, namespace=pdr.metadata.namespace)
        except ApiException as e:
            if e.status == 404:
                existing_pdr = None
            else:
                raise

        if existing_pdr:
            if existing_pdr.spec.max_unavailable != pdr.spec.max_unavailable:
                logging.debug(
                    f"Updating poddisruptionbudget {pdr.metadata.name}")
                return kube_client.policy.patch_namespaced_pod_disruption_budget(
                    name=pdr.metadata.name,
                    namespace=pdr.metadata.namespace,
                    body=pdr)
            else:
                logging.debug(
                    f"poddisruptionbudget {pdr.metadata.name} up to date")
        else:
            logging.debug(f"creating poddisruptionbudget {pdr.metadata.name}")
            return create_pod_disruption_budget(kube_client=kube_client,
                                                pod_disruption_budget=pdr)
Exemplo n.º 3
0
    def ensure_pod_disruption_budget(
        self, kube_client: KubeClient
    ) -> V1beta1PodDisruptionBudget:
        max_unavailable: Union[str, int]
        if "bounce_margin_factor" in self.soa_config.config_dict:
            max_unavailable = (
                f"{int((1 - self.soa_config.get_bounce_margin_factor()) * 100)}%"
            )
        else:
            system_paasta_config = load_system_paasta_config()
            max_unavailable = system_paasta_config.get_pdb_max_unavailable()

        pdr = pod_disruption_budget_for_service_instance(
            service=self.kube_deployment.service,
            instance=self.kube_deployment.instance,
            max_unavailable=max_unavailable,
        )
        try:
            existing_pdr = kube_client.policy.read_namespaced_pod_disruption_budget(
                name=pdr.metadata.name, namespace=pdr.metadata.namespace
            )
        except ApiException as e:
            if e.status == 404:
                existing_pdr = None
            else:
                raise

        if existing_pdr:
            if existing_pdr.spec.min_available is not None:
                logging.debug(
                    "Not updating poddisruptionbudget: can't have both "
                    "min_available and max_unavailable"
                )
            elif existing_pdr.spec.max_unavailable != pdr.spec.max_unavailable:
                logging.debug(f"Updating poddisruptionbudget {pdr.metadata.name}")
                return kube_client.policy.patch_namespaced_pod_disruption_budget(
                    name=pdr.metadata.name, namespace=pdr.metadata.namespace, body=pdr
                )
            else:
                logging.debug(f"poddisruptionbudget {pdr.metadata.name} up to date")
        else:
            logging.debug(f"creating poddisruptionbudget {pdr.metadata.name}")
            return create_pod_disruption_budget(
                kube_client=kube_client, pod_disruption_budget=pdr
            )