Ejemplo n.º 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
	}
}
Ejemplo n.º 2
0
func CheckAndExecuteNotifierOnReplicationController(replicationControllerNotifier *ReplicationControllerNotifier, replicationControllerName string) (bool, error) {
	replicationControllerMetric, err := monitor.MonitorReplicationController(replicationControllerNotifier.KubeApiServerEndPoint, replicationControllerNotifier.KubeApiServerToken, replicationControllerNotifier.Namespace, replicationControllerName)
	if err != nil {
		log.Error("Get ReplicationController %s data failure: %s where replicationControllerNotifier %v", replicationControllerName, err, replicationControllerNotifier)
	}
	if replicationControllerMetric == nil {
		return false, err
	}

	message := bytes.Buffer{}
	message.WriteString("Replication Controller: " + replicationControllerName + "\n")
	toNotify := false
	for _, indicator := range replicationControllerNotifier.IndicatorSlice {
		toNotifyAbove := monitor.CheckThresholdReplicationController(indicator.Type, true, indicator.AboveAllOrOne, replicationControllerMetric, indicator.AbovePercentageOfData, indicator.AboveThreshold)
		if toNotifyAbove {
			message.WriteString(generateMessage(indicator.Type, true, indicator.AboveAllOrOne, indicator.AbovePercentageOfData, indicator.AboveThreshold))
		}
		toNotifyBelow := monitor.CheckThresholdReplicationController(indicator.Type, false, indicator.BelowAllOrOne, replicationControllerMetric, indicator.BelowPercentageOfData, indicator.BelowThreshold)
		if toNotifyBelow {
			message.WriteString(generateMessage(indicator.Type, false, indicator.BelowAllOrOne, indicator.BelowPercentageOfData, indicator.BelowThreshold))
		}
		toNotify = toNotify || toNotifyAbove || toNotifyBelow
	}

	errorBuffer := bytes.Buffer{}
	if toNotify {
		for _, notifier := range replicationControllerNotifier.NotifierSlice {
			err := notifier.notify(message.String())
			if err != nil {
				errorBuffer.WriteString(err.Error())
			}
		}
	}

	if errorBuffer.Len() > 0 {
		return toNotify, errors.New(errorBuffer.String())
	} else {
		return toNotify, nil
	}
}
Ejemplo n.º 3
0
func CheckAndExecuteAutoScalerOnDeployImageInformation(replicationControllerAutoScaler *ReplicationControllerAutoScaler) (bool, int, error) {
	deployInformation, err := deploy.GetStorage().LoadDeployInformation(replicationControllerAutoScaler.Namespace, replicationControllerAutoScaler.Name)
	if err != nil {
		log.Error("Load deploy information failure: %s where replicationControllerAutoScaler %v", err.Error(), replicationControllerAutoScaler)
		return false, -1, err
	}

	replicationControllerName := deployInformation.ImageInformationName + deployInformation.CurrentVersion

	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)
	}
	if replicationControllerMetric == nil {
		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 && deployInformation.ReplicaAmount < replicationControllerAutoScaler.MaximumReplica {
		newSize := deployInformation.ReplicaAmount + 1
		err := deploy.DeployResize(
			replicationControllerAutoScaler.KubeApiServerEndPoint,
			replicationControllerAutoScaler.KubeApiServerToken,
			replicationControllerAutoScaler.Namespace,
			replicationControllerAutoScaler.Name,
			newSize,
		)
		if err != nil {
			return false, deployInformation.ReplicaAmount, err
		} else {
			return true, newSize, err
		}
	} else if toDecrease && deployInformation.ReplicaAmount > replicationControllerAutoScaler.MinimumReplica {
		newSize := deployInformation.ReplicaAmount - 1
		err := deploy.DeployResize(
			replicationControllerAutoScaler.KubeApiServerEndPoint,
			replicationControllerAutoScaler.KubeApiServerToken,
			replicationControllerAutoScaler.Namespace,
			replicationControllerAutoScaler.Name,
			newSize,
		)
		if err != nil {
			return false, deployInformation.ReplicaAmount, err
		} else {
			return true, newSize, err
		}
	} else {
		return false, deployInformation.ReplicaAmount, nil
	}
}