def scale_deployment(apps_v1_api: AppsV1Api, name, namespace, value) -> None: """ Scale a deployment. :param apps_v1_api: AppsV1Api :param namespace: namespace name :param name: deployment name :param value: int :return: """ print(f"Scale a deployment '{name}'") body = apps_v1_api.read_namespaced_deployment(name, namespace) body.spec.replicas = value apps_v1_api.patch_namespaced_deployment_scale(name, namespace, body) print(f"Scale a deployment '{name}': complete")
def rollout_status_deployment( api: client.AppsV1Api, name: str, namespace: str, ) -> Tuple[str, bool]: # tbh this is mostly ported from Go into Python from: # https://github.com/kubernetes/kubernetes/blob/master/pkg/kubectl/rollout_status.go#L76-L92 deployment = api.read_namespaced_deployment(name=name, namespace=namespace) if deployment.metadata.generation > deployment.status.observed_generation: return ( f"Waiting for deployment {repr(name)} spec update to be observed...", False, ) # TimedOutReason is added in a deployment when its newest replica set # fails to show any progress within the given deadline (progressDeadlineSeconds). for condition in deployment.status.conditions or []: if condition.type == "Progressing": if condition.reason == "ProgressDeadlineExceeded": return f"deployment {repr(name)} exceeded its progress deadline", False spec_replicas = deployment.spec.replicas status_replicas = deployment.status.replicas or 0 updated_replicas = deployment.status.updated_replicas or 0 available_replicas = deployment.status.available_replicas or 0 if updated_replicas < spec_replicas: return ( f"Waiting for deployment {repr(name)} rollout to finish: {updated_replicas} out of {spec_replicas} new replicas have been updated...", False, ) if status_replicas > updated_replicas: return ( f"Waiting for deployment {repr(name)} rollout to finish: {status_replicas-updated_replicas} old replicas are pending termination...", False, ) if available_replicas < updated_replicas: return ( f"Waiting for deployment {repr(name)} rollout to finish: {available_replicas} of {updated_replicas} updated replicas are available...", False, ) return f"Deployment {repr(name)} successfully rolled out", True