def scale_down( resource: NamespacedAPIObject, replicas: int, target_replicas: int, uptime, downtime, dry_run: bool, enable_events: bool, ): event_message = "Scaling down replicas" if resource.kind == "CronJob": resource.obj["spec"]["suspend"] = True logger.info( f"Suspending {resource.kind} {resource.namespace}/{resource.name} (uptime: {uptime}, downtime: {downtime})" ) event_message = "Suspending CronJob" elif resource.kind == "HorizontalPodAutoscaler": resource.obj["spec"]["minReplicas"] = target_replicas logger.info( f"Scaling down {resource.kind} {resource.namespace}/{resource.name} from {replicas} to {target_replicas} minReplicas (uptime: {uptime}, downtime: {downtime})" ) else: resource.replicas = target_replicas logger.info( f"Scaling down {resource.kind} {resource.namespace}/{resource.name} from {replicas} to {target_replicas} replicas (uptime: {uptime}, downtime: {downtime})" ) if enable_events: helper.add_event( resource, event_message, "ScaleDown", "Normal", dry_run, ) resource.annotations[ORIGINAL_REPLICAS_ANNOTATION] = str(replicas)
def scale_up( resource: NamespacedAPIObject, replicas: int, original_replicas: int, uptime, downtime, dry_run: bool, enable_events: bool, upscale_step_size: int, ): # Increase replicas by upscale_step_size, but not greater than original_replicas. if upscale_step_size > 0: new_replicas = replicas + upscale_step_size if new_replicas > original_replicas: new_replicas = original_replicas else: new_replicas = original_replicas event_message = "Scaling up replicas" if resource.kind == "CronJob": resource.obj["spec"]["suspend"] = False logger.info( f"Unsuspending {resource.kind} {resource.namespace}/{resource.name} (uptime: {uptime}, downtime: {downtime})" ) event_message = "Unsuspending CronJob" elif resource.kind == "HorizontalPodAutoscaler": resource.obj["spec"]["minReplicas"] = new_replicas logger.info( f"Scaling up {resource.kind} {resource.namespace}/{resource.name} from {replicas} to {new_replicas} minReplicas (uptime: {uptime}, downtime: {downtime})" ) else: resource.replicas = new_replicas logger.info( f"Scaling up {resource.kind} {resource.namespace}/{resource.name} from {replicas} to {new_replicas} replicas (uptime: {uptime}, downtime: {downtime})" ) if enable_events: helper.add_event( resource, event_message, "ScaleUp", "Normal", dry_run, ) if new_replicas == original_replicas: # If scaling up is done. resource.annotations[ORIGINAL_REPLICAS_ANNOTATION] = None
def scale_down(resource: NamespacedAPIObject, replicas: int, target_replicas: int, uptime, downtime): if resource.kind == "CronJob": resource.obj["spec"]["suspend"] = True logger.info( f"Suspending {resource.kind} {resource.namespace}/{resource.name} (uptime: {uptime}, downtime: {downtime})" ) elif resource.kind == "HorizontalPodAutoscaler": resource.obj["spec"]["minReplicas"] = target_replicas logger.info( f"Scaling down {resource.kind} {resource.namespace}/{resource.name} from {replicas} to {target_replicas} minReplicas (uptime: {uptime}, downtime: {downtime})" ) else: resource.replicas = target_replicas logger.info( f"Scaling down {resource.kind} {resource.namespace}/{resource.name} from {replicas} to {target_replicas} replicas (uptime: {uptime}, downtime: {downtime})" ) resource.annotations[ORIGINAL_REPLICAS_ANNOTATION] = str(replicas)
def scale_up( resource: NamespacedAPIObject, replicas: int, original_replicas: int, uptime, downtime, ): if resource.kind == "CronJob": resource.obj["spec"]["suspend"] = False logger.info( f"Unsuspending {resource.kind} {resource.namespace}/{resource.name} (uptime: {uptime}, downtime: {downtime})" ) elif resource.kind == "HorizontalPodAutoscaler": resource.obj["spec"]["minReplicas"] = original_replicas logger.info( f"Scaling up {resource.kind} {resource.namespace}/{resource.name} from {replicas} to {original_replicas} minReplicas (uptime: {uptime}, downtime: {downtime})" ) else: resource.replicas = original_replicas logger.info( f"Scaling up {resource.kind} {resource.namespace}/{resource.name} from {replicas} to {original_replicas} replicas (uptime: {uptime}, downtime: {downtime})" ) resource.annotations[ORIGINAL_REPLICAS_ANNOTATION] = None