Пример #1
0
func LaunchStatelessApplication(kubeApiServerEndPoint string, kubeApiServerToken string, namespace string, name string, environmentSlice []interface{}) error {
	stateless, err := GetStorage().LoadStatelessApplication(name)
	if err != nil {
		log.Error("Load stateless application error %s", err)
		return err
	}

	replicationControllerJsonMap := make(map[string]interface{})
	err = json.Unmarshal(stateless.replicationControllerJson, &replicationControllerJsonMap)
	if err != nil {
		log.Error("Unmarshal replication controller json for stateless application error %s", err)
		return err
	}
	serviceJsonMap := make(map[string]interface{})
	err = json.Unmarshal(stateless.serviceJson, &serviceJsonMap)
	if err != nil {
		log.Error("Unmarshal service json for stateless application error %s", err)
		return err
	}

	// Add environment variable
	if environmentSlice != nil {
		containerSlice := replicationControllerJsonMap["spec"].(map[string]interface{})["template"].(map[string]interface{})["spec"].(map[string]interface{})["containers"].([]interface{})
		for i := 0; i < len(containerSlice); i++ {
			_, ok := containerSlice[i].(map[string]interface{})["env"].([]interface{})
			if ok {
				for _, environment := range environmentSlice {
					containerSlice[i].(map[string]interface{})["env"] = append(containerSlice[i].(map[string]interface{})["env"].([]interface{}), environment)
				}
			} else {
				containerSlice[i].(map[string]interface{})["env"] = environmentSlice
			}
		}
	}

	err = control.CreateReplicationControllerWithJson(kubeApiServerEndPoint, kubeApiServerToken, namespace, replicationControllerJsonMap)
	if err != nil {
		log.Error("CreateReplicationControllerWithJson error %s", err)
		return err
	}
	err = control.CreateServiceWithJson(kubeApiServerEndPoint, kubeApiServerToken, namespace, serviceJsonMap)
	if err != nil {
		log.Error("CreateReplicationControllerWithJson error %s", err)
		return err
	}
	return nil
}
Пример #2
0
func postKubernetesServiceFromJson(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
	}

	service := make(map[string]interface{})
	err = request.ReadEntity(&service)
	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.CreateServiceWithJson(kubeApiServerEndPoint, kubeApiServerToken, namespace, service)
	if err != nil {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "Create service failure"
		jsonMap["ErrorMessage"] = err.Error()
		jsonMap["kubeApiServerEndPoint"] = kubeApiServerEndPoint
		jsonMap["namespace"] = namespace
		jsonMap["service"] = service
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(422, string(errorMessageByteSlice))
		return
	}
}
Пример #3
0
func LaunchClusterApplicationNoScript(kubeApiServerEndPoint string, kubeApiServerToken string, namespace string, cluster *Cluster, environmentSlice []interface{}, size int, replicationControllerExtraJsonMap map[string]interface{}) error {
	replicationControllerJsonMap := make(map[string]interface{})
	err := json.Unmarshal([]byte(cluster.ReplicationControllerJson), &replicationControllerJsonMap)
	if err != nil {
		log.Error("Unmarshal replication controller json for cluster application error %s", err)
		return err
	}

	serviceJsonMap := make(map[string]interface{})
	err = json.Unmarshal([]byte(cluster.ServiceJson), &serviceJsonMap)
	if err != nil {
		log.Error("Unmarshal service json for cluster application error %s", err)
		return err
	}

	// Configure extra json body
	// It is used for user to input any configuration
	if replicationControllerExtraJsonMap != nil {
		deepcopy.DeepOverwriteJsonMap(replicationControllerExtraJsonMap, replicationControllerJsonMap)
	}

	// Add environment variable
	if environmentSlice != nil {
		specJsonMap, ok := replicationControllerJsonMap["spec"].(map[string]interface{})
		if ok {
			templateJsonMap, ok := specJsonMap["template"].(map[string]interface{})
			if ok {
				specJsonMap, ok := templateJsonMap["spec"].(map[string]interface{})
				if ok {
					containerSlice, ok := specJsonMap["containers"].([]interface{})
					if ok {
						for i := 0; i < len(containerSlice); i++ {
							oldEnvironmentSlice, ok := containerSlice[i].(map[string]interface{})["env"].([]interface{})
							if ok {
								// Track old environment
								environmentMap := make(map[string]interface{})
								for _, oldEnvironment := range oldEnvironmentSlice {
									oldEnvironmentJsonMap, _ := oldEnvironment.(map[string]interface{})
									name, _ := oldEnvironmentJsonMap["name"].(string)
									environmentMap[name] = oldEnvironment
								}

								// Add or overwrite with new value
								for _, environment := range environmentSlice {
									environmentJsonMap, _ := environment.(map[string]interface{})
									name, _ := environmentJsonMap["name"].(string)
									environmentMap[name] = environmentJsonMap
								}

								// Use new slice which the user input overwrite the old data
								newEnvironmentSlice := make([]interface{}, 0)
								for _, value := range environmentMap {
									newEnvironmentSlice = append(newEnvironmentSlice, value)
								}
								containerSlice[i].(map[string]interface{})["env"] = newEnvironmentSlice
							} else {
								containerSlice[i].(map[string]interface{})["env"] = environmentSlice
							}
						}
					}
				}
			}
		}
	}

	// Change size
	specJsonMap, ok := replicationControllerJsonMap["spec"].(map[string]interface{})
	if ok {
		specJsonMap["replicas"] = size
	}

	err = control.CreateReplicationControllerWithJson(kubeApiServerEndPoint, kubeApiServerToken, namespace, replicationControllerJsonMap)
	if err != nil {
		log.Error("CreateReplicationControllerWithJson error %s", err)
		return err
	}
	err = control.CreateServiceWithJson(kubeApiServerEndPoint, kubeApiServerToken, namespace, serviceJsonMap)
	if err != nil {
		log.Error("CreateReplicationControllerWithJson error %s", err)
		return err
	}

	return nil
}