Example #1
0
func putDeployResize(request *restful.Request, response *restful.Response) {
	sizeText := request.QueryParameter("size")
	namespace := request.PathParameter("namespace")
	imageinformation := request.PathParameter("imageinformation")

	kubeApiServerEndPoint, kubeApiServerToken, err := configuration.GetAvailablekubeApiServerEndPoint()
	if err != nil {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "Get kube apiserver endpoint and token failure"
		jsonMap["ErrorMessage"] = err.Error()
		jsonMap["namespace"] = namespace
		jsonMap["imageinformation"] = imageinformation
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(404, string(errorMessageByteSlice))
		return
	}

	if sizeText == "" {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "Input is incorrect. The fields sizeText is required."
		jsonMap["sizeText"] = sizeText
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(400, string(errorMessageByteSlice))
		return
	}
	size, err := strconv.Atoi(sizeText)
	if err != nil {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "Could not parse sizeText"
		jsonMap["ErrorMessage"] = err.Error()
		jsonMap["sizeText"] = sizeText
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(400, string(errorMessageByteSlice))
		return
	}

	err = deploy.DeployResize(kubeApiServerEndPoint, kubeApiServerToken, namespace, imageinformation, size)
	if err != nil {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "Resize deployment failure"
		jsonMap["ErrorMessage"] = err.Error()
		jsonMap["kubeApiServerEndPoint"] = kubeApiServerEndPoint
		jsonMap["namespace"] = namespace
		jsonMap["imageinformation"] = imageinformation
		jsonMap["size"] = size
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(422, string(errorMessageByteSlice))
		return
	}
}
Example #2
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
	}
}