Example #1
0
func CheckAndExecuteAutoScalerOnReplicationController(replicationControllerAutoScaler *ReplicationControllerAutoScaler, replicationControllerName string) (bool, int, error) {
	replicationControllerMetric, err := monitor.MonitorReplicationController(replicationControllerAutoScaler.KubeApiServerEndPoint, replicationControllerAutoScaler.KubeApiServerToken, replicationControllerAutoScaler.Namespace, replicationControllerName)
	if err != nil {
		log.Error("Get ReplicationController data failure: %s where replicationControllerAutoScaler %v", err.Error(), replicationControllerAutoScaler)
		return false, -1, err
	}
	toIncrease, toDecrease := false, false
	for _, indicator := range replicationControllerAutoScaler.IndicatorSlice {
		toIncrease = monitor.CheckThresholdReplicationController(indicator.Type, true, indicator.AboveAllOrOne, replicationControllerMetric, indicator.AbovePercentageOfData, indicator.AboveThreshold)
		if toIncrease {
			break
		}
		toDecrease = monitor.CheckThresholdReplicationController(indicator.Type, false, indicator.BelowAllOrOne, replicationControllerMetric, indicator.BelowPercentageOfData, indicator.BelowThreshold)
		if toDecrease {
			break
		}
	}

	if toIncrease {
		resized, size, err := control.ResizeReplicationController(replicationControllerAutoScaler.KubeApiServerEndPoint, replicationControllerAutoScaler.KubeApiServerToken, replicationControllerAutoScaler.Namespace, replicationControllerName, 1, replicationControllerAutoScaler.MaximumReplica, replicationControllerAutoScaler.MinimumReplica)
		if err != nil {
			log.Error("ResizeReplicationController failure: %s where ReplicationControllerAutoScaler %v", err.Error(), replicationControllerAutoScaler)
		}

		// Change deployment data
		if resized {
			if err := deploy.ChangeDeployInformationReplicaAmount(replicationControllerAutoScaler.Namespace, replicationControllerName, size); err != nil {
				log.Error(err)
			}
		}

		return resized, size, err
	} else if toDecrease {
		resized, size, err := control.ResizeReplicationController(replicationControllerAutoScaler.KubeApiServerEndPoint, replicationControllerAutoScaler.KubeApiServerToken, replicationControllerAutoScaler.Namespace, replicationControllerName, -1, replicationControllerAutoScaler.MaximumReplica, replicationControllerAutoScaler.MinimumReplica)
		if err != nil {
			log.Error("ResizeReplicationController failure: %s where ReplicationControllerAutoScaler %v", err.Error(), replicationControllerAutoScaler)
		}

		// Change deployment data
		if resized {
			if err := deploy.ChangeDeployInformationReplicaAmount(replicationControllerAutoScaler.Namespace, replicationControllerName, size); err != nil {
				log.Error(err)
			}
		}

		return resized, size, err
	} else {
		return false, replicationControllerMetric.Size, nil
	}
}
Example #2
0
func DeployResize(kubeApiServerEndPoint string, kubeApiServerToken string, namespace string, imageInformation string, size int) error {
	if lock.AcquireLock(LockKind, getLockName(namespace, imageInformation), 0) == false {
		return errors.New("Deployment is controlled by the other command")
	}

	defer lock.ReleaseLock(LockKind, getLockName(namespace, imageInformation))

	deployInformation, err := GetStorage().LoadDeployInformation(namespace, imageInformation)
	if err != nil {
		log.Error(err)
		return err
	}

	replicationControllerName := deployInformation.ImageInformationName + deployInformation.CurrentVersion

	delta := size - deployInformation.ReplicaAmount

	_, _, err = control.ResizeReplicationController(kubeApiServerEndPoint, kubeApiServerToken, namespace, replicationControllerName, delta, deployInformation.ReplicaAmount+delta, 1)
	if err != nil {
		log.Error(err)
		return err
	}

	deployInformation.ReplicaAmount = size
	err = GetStorage().saveDeployInformation(deployInformation)
	if err != nil {
		log.Error(err)
		return err
	}

	return nil
}