예제 #1
0
func getAllKubernetesService(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
	}

	serviceSlice, err := control.GetAllService(kubeApiServerEndPoint, kubeApiServerToken, namespace)
	if err != nil {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "Get all service failure"
		jsonMap["ErrorMessage"] = err.Error()
		jsonMap["kubeApiServerEndPoint"] = kubeApiServerEndPoint
		jsonMap["namespace"] = namespace
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(422, string(errorMessageByteSlice))
		return
	}

	response.WriteJson(serviceSlice, "[]Service")
}
func getServiceNameAndReplicationControllerNameSlice(kubeApiServerEndPoint string, kubeApiServerToken string, namespace string, name string) (bool, string, []string, error) {
	replicationControllerAndRelatedPodSlice, err := control.GetAllReplicationControllerAndRelatedPodSlice(kubeApiServerEndPoint, kubeApiServerToken, namespace)
	if err != nil {
		log.Error("Fail to get all replication controller name error %s", err)
		return false, "", nil, err
	}

	serviceSlice, err := control.GetAllService(kubeApiServerEndPoint, kubeApiServerToken, namespace)
	if err != nil {
		log.Error("Fail to get all service error %s", err)
		return false, "", nil, err
	}

	// Service
	serviceName := name
	serviceExist := false
	selectorMap := make(map[string]interface{})
	for _, service := range serviceSlice {
		if service.Name == serviceName {
			serviceExist = true
			for key, value := range service.Selector {
				selectorMap[key] = value
			}
		}
	}

	// Replication Controller
	replicationControllerNameSlice := make([]string, 0)
	if len(selectorMap) > 0 {
		for _, replicationControllerAndRelatedPod := range replicationControllerAndRelatedPodSlice {
			// if all selectors in service are in the replication controller, the service owns the replication controller
			allFit := true
			for key, value := range selectorMap {
				if value != replicationControllerAndRelatedPod.Selector[key] {
					allFit = false
					break
				}
			}
			if allFit {
				replicationControllerNameSlice = append(replicationControllerNameSlice, replicationControllerAndRelatedPod.Name)
			}
		}
	}

	return serviceExist, serviceName, replicationControllerNameSlice, nil
}