From eda8a86bb50ff374ddb5301abdf317d45accca07 Mon Sep 17 00:00:00 2001 From: Venkata Gunapati Date: Fri, 31 Jan 2020 11:44:00 -0800 Subject: [PATCH] Event Only Update (#60) * bug fix: spot recommendation * bug fix: sport price * price as string in event --- cloud_provider/aws/aws_minion_manager.py | 30 ++++++++++++++++-------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/cloud_provider/aws/aws_minion_manager.py b/cloud_provider/aws/aws_minion_manager.py index 94a8337..bfcf84f 100644 --- a/cloud_provider/aws/aws_minion_manager.py +++ b/cloud_provider/aws/aws_minion_manager.py @@ -168,7 +168,7 @@ def _describe_launch_configuration(): launch_config.LaunchConfigurationName, bid_info) def log_k8s_event(self, asg_name, price="", useSpot=False): - msg_str = '{"apiVersion":"v1alpha1","spotPrice":' + price + ', "useSpot": ' + str(useSpot) + '}' + msg_str = '{"apiVersion":"v1alpha1","spotPrice":"' + price + '", "useSpot": ' + str(useSpot).lower() + '}' if not self.incluster: logger.info(msg_str) return @@ -202,32 +202,44 @@ def log_k8s_event(self, asg_name, price="", useSpot=False): except Exception as e: logger.info("Failed to log event: " + str(e)) + def get_new_bid_info(self, asg_meta): + """ get new bid price. """ + new_bid_info = self.bid_advisor.get_new_bid( + zones=asg_meta.asg_info.AvailabilityZones, + instance_type=asg_meta.lc_info.InstanceType) + return new_bid_info + def update_needed(self, asg_meta): """ Checks if an ASG needs to be updated. """ try: + current_price = "" asg_tag = asg_meta.get_mm_tag() bid_info = asg_meta.get_bid_info() + if not bid_info.get("price"): + if self.get_new_bid_info(asg_meta).get("price"): + current_price = self.get_new_bid_info(asg_meta).get("price") + if asg_tag == "no-spot": if bid_info["type"] == "spot": logger.info("ASG %s configured with on-demand but currently using spot. Update needed", asg_meta.get_name()) - # '{"apiVersion":"v1alpha1","spotPrice":bid_info["price"], "useSpot": true}' - self.log_k8s_event(asg_meta.get_name(), bid_info.get("price", ""), True) + # '{"apiVersion":"v1alpha1","spotPrice":bid_info["price"], "useSpot": False}' + self.log_k8s_event(asg_meta.get_name(), current_price, False) return True elif bid_info["type"] == "on-demand": logger.info("ASG %s configured with on-demand and currently using on-demand. No update needed", asg_meta.get_name()) - # '{"apiVersion":"v1alpha1","spotPrice":"", "useSpot": false}' + # '{"apiVersion":"v1alpha1","spotPrice":"", "useSpot": False}' self.log_k8s_event(asg_meta.get_name(), "", False) return False # The asg_tag is "spot". if bid_info["type"] == "on-demand": logger.info("ASG %s configured with spot but currently using on-demand. Update needed", asg_meta.get_name()) - # '{"apiVersion":"v1alpha1","spotPrice":"", "useSpot": false}' - self.log_k8s_event(asg_meta.get_name(), "", True) + # '{"apiVersion":"v1alpha1","spotPrice":"", "useSpot": true}' + self.log_k8s_event(asg_meta.get_name(), current_price, True) return True else: # Continue to use spot - self.log_k8s_event(asg_meta.get_name(), bid_info.get("price", ""), True) + self.log_k8s_event(asg_meta.get_name(), current_price, True) assert bid_info["type"] == "spot" if self.check_scaling_group_instances(asg_meta): # Desired # of instances running. No updates needed. @@ -664,9 +676,7 @@ def minion_manager_work(self): self.update_scaling_group(asg_meta, new_bid_info) continue - new_bid_info = self.bid_advisor.get_new_bid( - zones=asg_meta.asg_info.AvailabilityZones, - instance_type=asg_meta.lc_info.InstanceType) + new_bid_info = self.get_new_bid_info(asg_meta) # Change ASG to on-demand if insufficient capacity if self.check_insufficient_capacity(asg_meta):