class AutoScalerBuilder(object): """ AutoScalerBuilder is a builder to create an autoscaler for application. You should offer many necessary arguments. Parameters: min_replicas, max_replicas, cpu_target: integer parameters, -1 represents infinite big value. """ def __init__(self, application, min_replicas=-1, max_replicas=-1, cpu_target=-1): self.application = application self.application_name = get_application_instance_name(self.application) self.namespace = self.application.image.project.name self.min_replicas = min_replicas self.max_replicas = max_replicas self.cpu_target = cpu_target self.kubeclient = KubeClient("http://{}:{}{}".format( settings.MASTER_IP, settings.K8S_PORT, settings.K8S_V1BETA1_API_PATH)) def create_autoscaler(self): """ Create autoscaler instance for application by multiple threading. """ creating_thread = Thread(target=self._create_autoscaler_instance) creating_thread.start() def _create_autoscaler_instance(self): ok = self.kubeclient.create_autoscaler(namespace=self.namespace, name=self.application_name, minReplicas=self.min_replicas, maxReplicas=self.max_replicas, cpu_target=self.cpu_target) if not ok: logger.error("Create autoscaler {} failed.".format( self.application_name)) return None logger.debug("Create autoscaler {} successfully.".format( self.application_name)) scaler = AutoScaler(app=self.application, min_replicas=self.min_replicas, max_replicas=self.max_replicas, cpu_target=self.cpu_target) scaler.save()
class AutoScalerBuilder(object): """ AutoScalerBuilder is a builder to create an autoscaler for application. You should offer many necessary arguments. Parameters: min_replicas, max_replicas, cpu_target: integer parameters, -1 represents infinite big value. """ def __init__(self, application, min_replicas=-1, max_replicas=-1, cpu_target=-1): self.application = application self.application_name = get_application_instance_name(self.application) self.namespace = self.application.image.project.name self.min_replicas = min_replicas self.max_replicas = max_replicas self.cpu_target = cpu_target self.kubeclient = KubeClient("http://{}:{}{}".format(settings.MASTER_IP, settings.K8S_PORT, settings.K8S_V1BETA1_API_PATH)) def create_autoscaler(self): """ Create autoscaler instance for application by multiple threading. """ creating_thread = Thread(target=self._create_autoscaler_instance) creating_thread.start() def _create_autoscaler_instance(self): ok = self.kubeclient.create_autoscaler( namespace=self.namespace, name=self.application_name, minReplicas=self.min_replicas, maxReplicas=self.max_replicas, cpu_target=self.cpu_target) if not ok: logger.error("Create autoscaler {} failed.".format( self.application_name)) return None logger.debug("Create autoscaler {} successfully.".format( self.application_name)) scaler = AutoScaler(app=self.application, min_replicas=self.min_replicas, max_replicas=self.max_replicas, cpu_target=self.cpu_target) scaler.save()
def test_create_autoscaler(self): beta_client = KubeClient( "http://192.168.0.10:8080/apis/extensions/v1beta1/") res = beta_client.create_autoscaler('user', 'project0-nginx-test', 1, 5, 50) print(res)
def test_create_autoscaler(self): beta_client = KubeClient("http://192.168.0.10:8080/apis/extensions/v1beta1/") res = beta_client.create_autoscaler('user', 'project0-nginx-test', 1, 5, 50) print(res)