Example #1
0
func CheckAndExecuteNotifier(replicationControllerNotifier *ReplicationControllerNotifier) (bool, error) {
	switch replicationControllerNotifier.Kind {
	case "application":
		deployInformation, err := deploy.GetStorage().LoadDeployInformation(replicationControllerNotifier.Namespace, replicationControllerNotifier.Name)
		if err != nil {
			log.Error("Load deploy information failure: %s where replicationControllerNotifier %v", err, replicationControllerNotifier)
			return false, err
		}
		replicationControllerName := deployInformation.ImageInformationName + deployInformation.CurrentVersion
		return CheckAndExecuteNotifierOnReplicationController(replicationControllerNotifier, replicationControllerName)
	case "selector":
		nameSlice, err := monitor.GetReplicationControllerNameFromSelector(
			replicationControllerNotifier.KubeApiServerEndPoint,
			replicationControllerNotifier.KubeApiServerToken,
			replicationControllerNotifier.Namespace,
			replicationControllerNotifier.Name)
		if err != nil {
			return false, errors.New("Could not find replication controller name with selector " + replicationControllerNotifier.Name + " error " + err.Error())
		} else {
			atLeastOneNotify := false
			errorMessage := bytes.Buffer{}
			hasError := false
			for _, name := range nameSlice {
				result, err := CheckAndExecuteNotifierOnReplicationController(replicationControllerNotifier, name)
				atLeastOneNotify = atLeastOneNotify || result
				if err != nil {
					errorMessage.WriteString(err.Error())
					hasError = true
				}
			}

			if hasError == false {
				return atLeastOneNotify, nil
			} else {
				return atLeastOneNotify, errors.New(errorMessage.String())
			}
		}
	case "replicationController":
		return CheckAndExecuteNotifierOnReplicationController(replicationControllerNotifier, replicationControllerNotifier.Name)
	default:
		return false, errors.New("No such kind " + replicationControllerNotifier.Kind)
	}
}
Example #2
0
func CheckAndExecuteAutoScaler(replicationControllerAutoScaler *ReplicationControllerAutoScaler) (bool, int, error) {
	switch replicationControllerAutoScaler.Kind {
	case "application":
		return CheckAndExecuteAutoScalerOnDeployImageInformation(replicationControllerAutoScaler)
	case "selector":
		nameSlice, err := monitor.GetReplicationControllerNameFromSelector(
			replicationControllerAutoScaler.KubeApiServerEndPoint,
			replicationControllerAutoScaler.KubeApiServerToken,
			replicationControllerAutoScaler.Namespace,
			replicationControllerAutoScaler.Name)
		if err != nil {
			return false, -1, errors.New("Could not find replication controller name with selector " + replicationControllerAutoScaler.Name + " error " + err.Error())
		} else {
			resized := false
			hasError := false
			errorMessage := bytes.Buffer{}
			totalSize := 0
			for _, name := range nameSlice {
				result, size, err := CheckAndExecuteAutoScalerOnReplicationController(replicationControllerAutoScaler, name)
				resized = resized || result
				totalSize += size
				if err != nil {
					hasError = true
					errorMessage.WriteString(err.Error())
				}
			}

			if hasError {
				return false, -1, errors.New(errorMessage.String())
			} else {
				return resized, totalSize, nil
			}
		}
	case "replicationController":
		return CheckAndExecuteAutoScalerOnReplicationController(replicationControllerAutoScaler, replicationControllerAutoScaler.Name)
	default:
		return false, -1, errors.New("No such kind " + replicationControllerAutoScaler.Kind)
	}
}
Example #3
0
func putReplicationControllerNotifier(request *restful.Request, response *restful.Response) {
	replicationControllerNotifierSerializable := new(notification.ReplicationControllerNotifierSerializable)
	err := request.ReadEntity(&replicationControllerNotifierSerializable)
	if err != nil {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "Read body failure"
		jsonMap["ErrorMessage"] = err.Error()
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(400, string(errorMessageByteSlice))
		return
	}

	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()
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(404, string(errorMessageByteSlice))
		return
	}

	replicationControllerNotifierSerializable.KubeApiServerEndPoint = kubeApiServerEndPoint
	replicationControllerNotifierSerializable.KubeApiServerToken = kubeApiServerToken

	switch replicationControllerNotifierSerializable.Kind {
	case "application":
		_, err := deploy.GetStorage().LoadDeployInformation(replicationControllerNotifierSerializable.Namespace, replicationControllerNotifierSerializable.Name)
		if err != nil {
			jsonMap := make(map[string]interface{})
			jsonMap["Error"] = "Check whether the application exists or not failure"
			jsonMap["ErrorMessage"] = err.Error()
			jsonMap["replicationControllerNotifierSerializable"] = replicationControllerNotifierSerializable
			errorMessageByteSlice, _ := json.Marshal(jsonMap)
			log.Error(jsonMap)
			response.WriteErrorString(422, string(errorMessageByteSlice))
			return
		}
	case "selector":
		nameSlice, err := monitor.GetReplicationControllerNameFromSelector(replicationControllerNotifierSerializable.KubeApiServerEndPoint, replicationControllerNotifierSerializable.KubeApiServerToken, replicationControllerNotifierSerializable.Namespace, replicationControllerNotifierSerializable.Name)
		if err != nil {
			for _, name := range nameSlice {
				exist, err := monitor.ExistReplicationController(replicationControllerNotifierSerializable.KubeApiServerEndPoint, replicationControllerNotifierSerializable.KubeApiServerToken, replicationControllerNotifierSerializable.Namespace, name)
				if err != nil {
					jsonMap := make(map[string]interface{})
					jsonMap["Error"] = "Check whether the replication controller exists or not failure"
					jsonMap["ErrorMessage"] = err.Error()
					jsonMap["replicationControllerNotifierSerializable"] = replicationControllerNotifierSerializable
					errorMessageByteSlice, _ := json.Marshal(jsonMap)
					log.Error(jsonMap)
					response.WriteErrorString(422, string(errorMessageByteSlice))
					return
				}
				if exist == false {
					jsonMap := make(map[string]interface{})
					jsonMap["Error"] = "The replication controller to notify doesn't exist"
					jsonMap["ErrorMessage"] = err.Error()
					jsonMap["replicationControllerNotifierSerializable"] = replicationControllerNotifierSerializable
					errorMessageByteSlice, _ := json.Marshal(jsonMap)
					log.Error(jsonMap)
					response.WriteErrorString(404, string(errorMessageByteSlice))
					return
				}
			}
		}
	case "replicationController":
		exist, err := monitor.ExistReplicationController(replicationControllerNotifierSerializable.KubeApiServerEndPoint, replicationControllerNotifierSerializable.KubeApiServerToken, replicationControllerNotifierSerializable.Namespace, replicationControllerNotifierSerializable.Name)
		if err != nil {
			jsonMap := make(map[string]interface{})
			jsonMap["Error"] = "Check whether the replication controller exists or not failure"
			jsonMap["ErrorMessage"] = err.Error()
			jsonMap["replicationControllerNotifierSerializable"] = replicationControllerNotifierSerializable
			errorMessageByteSlice, _ := json.Marshal(jsonMap)
			log.Error(jsonMap)
			response.WriteErrorString(422, string(errorMessageByteSlice))
			return
		}
		if exist == false {
			jsonMap := make(map[string]interface{})
			jsonMap["Error"] = "The replication controller to notify doesn't exist"
			jsonMap["ErrorMessage"] = err.Error()
			jsonMap["replicationControllerNotifierSerializable"] = replicationControllerNotifierSerializable
			errorMessageByteSlice, _ := json.Marshal(jsonMap)
			log.Error(jsonMap)
			response.WriteErrorString(400, string(errorMessageByteSlice))
			return
		}
	default:
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "No such kind"
		jsonMap["replicationControllerNotifierSerializable"] = replicationControllerNotifierSerializable
		jsonMap["kind"] = replicationControllerNotifierSerializable.Kind
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(400, string(errorMessageByteSlice))
		return
	}

	replicationControllerNotifier, err := notification.ConvertFromSerializable(*replicationControllerNotifierSerializable)
	if err != nil {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "Convert replication controller notifier failure"
		jsonMap["ErrorMessage"] = err.Error()
		jsonMap["replicationControllerNotifierSerializable"] = replicationControllerNotifierSerializable
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(400, string(errorMessageByteSlice))
		return
	}

	err = notification.GetStorage().SaveReplicationControllerNotifierSerializable(replicationControllerNotifierSerializable)
	if err != nil {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "Save replication controller notifier failure"
		jsonMap["ErrorMessage"] = err.Error()
		jsonMap["replicationControllerNotifierSerializable"] = replicationControllerNotifierSerializable
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(422, string(errorMessageByteSlice))
		return
	}

	execute.AddReplicationControllerNotifier(&replicationControllerNotifier)
}