Ejemplo n.º 1
0
func MonitorReplicationController(kubeApiServerEndPoint string, kubeApiServerToken string, namespace string, replicationControllerName string) (returnedReplicationControllerMetric *ReplicationControllerMetric, returnedError error) {
	defer func() {
		if err := recover(); err != nil {
			log.Error("MonitorReplicationController Error: %s", err)
			log.Error(logger.GetStackTrace(4096, false))
			returnedReplicationControllerMetric = nil
			returnedError = err.(error)
		}
	}()

	exist, err := ExistReplicationController(kubeApiServerEndPoint, kubeApiServerToken, namespace, replicationControllerName)
	if err != nil {
		log.Error("Fail to get the replication controller with endpoint: %s, token: %s, namespace: %s, replication controller name: %s", kubeApiServerEndPoint, kubeApiServerToken, namespace, replicationControllerName)
		return nil, err
	}
	if exist == false {
		log.Error("Replication controller doesn't exist with endpoint: %s, token: %s, namespace: %s, replication controller name: %s", kubeApiServerEndPoint, kubeApiServerToken, namespace, replicationControllerName)
		return nil, err
	}

	podNameSlice, err := control.GetAllPodNameBelongToReplicationController(kubeApiServerEndPoint, kubeApiServerToken, namespace, replicationControllerName)
	if err != nil {
		log.Error("Fail to get all pod name belong to the replication controller with endpoint: %s, token: %s, namespace: %s, replication controller name: %s", kubeApiServerEndPoint, kubeApiServerToken, namespace, replicationControllerName)
		return nil, err
	}

	replicationControllerMetric := &ReplicationControllerMetric{}
	replicationControllerMetric.Namespace = namespace
	replicationControllerMetric.ReplicationControllerName = replicationControllerName
	replicationControllerMetric.Size = len(podNameSlice)
	replicationControllerMetric.ValidPodSlice = make([]bool, replicationControllerMetric.Size)
	replicationControllerMetric.PodMetricSlice = make([]PodMetric, replicationControllerMetric.Size)
	errorMessage := "The following index of pod has error: "
	errorHappened := false
	for index, podName := range podNameSlice {
		podMetric, err := MonitorPod(kubeApiServerEndPoint, kubeApiServerToken, namespace, podName)
		if err != nil {
			errorMessage = errorMessage + err.Error()
			errorHappened = true
			replicationControllerMetric.ValidPodSlice[index] = false
		} else {
			replicationControllerMetric.ValidPodSlice[index] = true
			replicationControllerMetric.PodMetricSlice[index] = *podMetric
		}
	}

	if errorHappened {
		log.Error("Fail to get all pod inofrmation with endpoint: %s, token: %s, namespace: %s, replication controller name: %s, error %s", kubeApiServerEndPoint, kubeApiServerToken, namespace, replicationControllerName, errorMessage)
		return replicationControllerMetric, errors.New(errorMessage)
	} else {
		return replicationControllerMetric, nil
	}
}
Ejemplo n.º 2
0
func putReplicationControllerFromJson(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
	}

	replicationController := make(map[string]interface{})
	err = request.ReadEntity(&replicationController)
	if err != nil {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "Read body failure"
		jsonMap["ErrorMessage"] = err.Error()
		jsonMap["kubeApiServerEndPoint"] = kubeApiServerEndPoint
		jsonMap["namespace"] = namespace
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(400, string(errorMessageByteSlice))
		return
	}

	err = control.UpdateReplicationControllerWithJson(kubeApiServerEndPoint, kubeApiServerToken, namespace, replicationcontrollerName, replicationController)
	if err != nil {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "Update replication controller failure"
		jsonMap["ErrorMessage"] = err.Error()
		jsonMap["kubeApiServerEndPoint"] = kubeApiServerEndPoint
		jsonMap["namespace"] = namespace
		jsonMap["replicationController"] = replicationController
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(422, string(errorMessageByteSlice))
		return
	}

	podNameSlice, err := control.GetAllPodNameBelongToReplicationController(kubeApiServerEndPoint, kubeApiServerToken, namespace, replicationcontrollerName)
	if err != nil {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "Get all pod name belonging to replication controller failure"
		jsonMap["ErrorMessage"] = err.Error()
		jsonMap["kubeApiServerEndPoint"] = kubeApiServerEndPoint
		jsonMap["namespace"] = namespace
		jsonMap["replicationController"] = replicationController
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(422, string(errorMessageByteSlice))
		return
	}

	hasError := false
	errorByteBuffer := bytes.Buffer{}
	for _, podName := range podNameSlice {
		err := control.DeletePod(kubeApiServerEndPoint, kubeApiServerToken, namespace, podName)
		if err != nil {
			hasError = true
			errorByteBuffer.WriteString(err.Error())
			errorByteBuffer.WriteString(" ")
		}
	}

	if hasError {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "Delete pods belonging to replication controller failure"
		jsonMap["ErrorMessage"] = errorByteBuffer.String()
		jsonMap["kubeApiServerEndPoint"] = kubeApiServerEndPoint
		jsonMap["namespace"] = namespace
		jsonMap["replicationController"] = replicationController
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(422, string(errorMessageByteSlice))
		return
	}
}