def update(self, kube_client: KubeClient) -> None: # If HPA is enabled, do not update replicas. # In all other cases, replica is set to max(instances, min_instances) if self.soa_config.config_dict.get("bounce_method", "") == "brutal": threading.Thread(target=self.deep_delete_and_create, args=[KubeClient()]).start() return update_deployment(kube_client=kube_client, formatted_deployment=self.item)
def update(self, kube_client: KubeClient) -> None: # If autoscaling is enabled, do not update replicas. # In all other cases, replica is set to max(instances, min_instances) if not self.get_soa_config().get("instances"): self.item.spec.replicas = self.get_existing_app( kube_client).spec.replicas update_deployment(kube_client=kube_client, formatted_deployment=self.item) self.ensure_pod_disruption_budget(kube_client) self.sync_horizontal_pod_autoscaler(kube_client)
def update_kubernetes_application(kube_client: KubeClient, application: Union[V1Deployment, V1StatefulSet]) -> None: if isinstance(application, V1Deployment): update_deployment( kube_client=kube_client, formatted_deployment=application, ) elif isinstance(application, V1StatefulSet): update_stateful_set( kube_client=kube_client, formatted_stateful_set=application, ) else: raise Exception("Unknown kubernetes object to update")
def update(self, kube_client: KubeClient) -> None: # If HPA is enabled, do not update replicas. # In all other cases, replica is set to max(instances, min_instances) if self.soa_config.config_dict.get("bounce_method", "") == "brutal": threading.Thread( target=self.deep_delete_and_create, args=[KubeClient()] ).start() return if self.should_have_hpa(): self.item.spec.replicas = self.get_existing_app(kube_client).spec.replicas update_deployment(kube_client=kube_client, formatted_deployment=self.item) self.ensure_pod_disruption_budget(kube_client) self.sync_horizontal_pod_autoscaler(kube_client)
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'), )
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