func getReplicationControllerMetric(request *restful.Request, response *restful.Response) { namespace := request.PathParameter("namespace") replicationControllerName := request.PathParameter("replicationcontroller") 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["replicationControllerName"] = replicationControllerName errorMessageByteSlice, _ := json.Marshal(jsonMap) log.Error(jsonMap) response.WriteErrorString(404, string(errorMessageByteSlice)) return } replicationControllerMetric, err := monitor.MonitorReplicationController(kubeApiServerEndPoint, kubeApiServerToken, namespace, replicationControllerName) if err != nil { jsonMap := make(map[string]interface{}) jsonMap["Error"] = "Get replication controller metric failure" jsonMap["ErrorMessage"] = err.Error() jsonMap["kubeApiServerEndPoint"] = kubeApiServerEndPoint jsonMap["namespace"] = namespace jsonMap["replicationControllerName"] = replicationControllerName errorMessageByteSlice, _ := json.Marshal(jsonMap) log.Error(jsonMap) response.WriteErrorString(422, string(errorMessageByteSlice)) return } response.WriteJson(replicationControllerMetric, "ReplicationControllerMetric") }
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 } }
func getAllReplicationControllerMetric(request *restful.Request, response *restful.Response) { namespace := request.PathParameter("namespace") 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 errorMessageByteSlice, _ := json.Marshal(jsonMap) log.Error(jsonMap) response.WriteErrorString(404, string(errorMessageByteSlice)) return } nameSlice, err := control.GetAllReplicationControllerName(kubeApiServerEndPoint, kubeApiServerToken, namespace) if err != nil { jsonMap := make(map[string]interface{}) jsonMap["Error"] = "Get all replication controller name failure" jsonMap["ErrorMessage"] = err.Error() jsonMap["kubeApiServerEndPoint"] = kubeApiServerEndPoint jsonMap["namespace"] = namespace errorMessageByteSlice, _ := json.Marshal(jsonMap) log.Error(jsonMap) response.WriteErrorString(422, string(errorMessageByteSlice)) return } replicationControllerMetricSlice := make([]monitor.ReplicationControllerMetric, 0) errorSlice := make([]error, 0) for _, name := range nameSlice { replicationControllerMetric, err := monitor.MonitorReplicationController(kubeApiServerEndPoint, kubeApiServerToken, namespace, name) if replicationControllerMetric != nil { replicationControllerMetricSlice = append(replicationControllerMetricSlice, *replicationControllerMetric) } errorSlice = append(errorSlice, err) } returnedJsonMap := make(map[string]interface{}) returnedJsonMap["ReplicationControllerMetricSlice"] = replicationControllerMetricSlice returnedJsonMap["ErrorSlice"] = errorSlice response.WriteJson(returnedJsonMap, "ReplicationControllerMetricSlice/ErrorSlice") }
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 } }
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 } }