func putReplicationControllerSize(request *restful.Request, response *restful.Response) { namespace := request.PathParameter("namespace") replicationController := 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["replicationController"] = replicationController errorMessageByteSlice, _ := json.Marshal(jsonMap) log.Error(jsonMap) response.WriteErrorString(404, string(errorMessageByteSlice)) return } sizeInput := SizeInput{} err = request.ReadEntity(&sizeInput) if err != nil { jsonMap := make(map[string]interface{}) jsonMap["Error"] = "Read body failure" jsonMap["ErrorMessage"] = err.Error() jsonMap["kubeApiServerEndPoint"] = kubeApiServerEndPoint jsonMap["namespace"] = namespace jsonMap["replicationController"] = replicationController errorMessageByteSlice, _ := json.Marshal(jsonMap) log.Error(jsonMap) response.WriteErrorString(400, string(errorMessageByteSlice)) return } err = control.UpdateReplicationControllerSize(kubeApiServerEndPoint, kubeApiServerToken, namespace, replicationController, sizeInput.Size) if err != nil { jsonMap := make(map[string]interface{}) jsonMap["Error"] = "Resize replication controller failure" jsonMap["ErrorMessage"] = err.Error() jsonMap["kubeApiServerEndPoint"] = kubeApiServerEndPoint jsonMap["namespace"] = namespace jsonMap["replicationController"] = replicationController jsonMap["size"] = sizeInput.Size errorMessageByteSlice, _ := json.Marshal(jsonMap) log.Error(jsonMap) response.WriteErrorString(422, string(errorMessageByteSlice)) return } }
func ResizeDeployClusterApplication(kubeApiServerEndPoint string, kubeApiServerToken string, namespace string, name string, environmentSlice []interface{}, size int) error { cluster, err := application.GetStorage().LoadClusterApplication(name) if err != nil { log.Error("Load cluster application error %s", err) return err } 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 } // Add environment variable if environmentSlice != nil { if replicationControllerJsonMap["spec"] != 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 } } } } replicationControllerByteSlice, err := json.Marshal(replicationControllerJsonMap) if err != nil { log.Error("Marshal replication controller json for cluster application error %s", err) return err } environmentByteSlice, err := json.Marshal(environmentSlice) if err != nil { log.Error("Marshal environment json for cluster application error %s", err) return err } // Generate random work space workingDirectory := "/tmp/tmp_" + random.UUID() replicationControllerFileName := "replication-controller.json" scriptFileName := "script" environmentFileName := "environment.json" // Check working space if _, err := os.Stat(workingDirectory); os.IsNotExist(err) { err := os.MkdirAll(workingDirectory, os.ModePerm) if err != nil { log.Error("Create non-existing directory %s error: %s", workingDirectory, err) return err } } err = ioutil.WriteFile(workingDirectory+string(os.PathSeparator)+environmentFileName, environmentByteSlice, os.ModePerm) if err != nil { log.Error("Write environment json file for cluster application error %s", err) return err } err = ioutil.WriteFile(workingDirectory+string(os.PathSeparator)+replicationControllerFileName, replicationControllerByteSlice, os.ModePerm) if err != nil { log.Error("Write replication controller json file for cluster application error %s", err) return err } err = ioutil.WriteFile(workingDirectory+string(os.PathSeparator)+scriptFileName, []byte(cluster.ScriptContent), os.ModePerm) if err != nil { log.Error("Write script file for cluster application error %s", err) return err } switch cluster.ScriptType { case "none": deployClusterApplication, err := GetDeployClusterApplication(namespace, name) if err != nil { log.Error("Get deploy cluster application error %s", err) return err } for _, replicationControllerName := range deployClusterApplication.ReplicationControllerNameSlice { err := control.UpdateReplicationControllerSize(kubeApiServerEndPoint, kubeApiServerToken, namespace, replicationControllerName, size) if err != nil { log.Error("Resize replication controller %s error %s", replicationControllerName, err) return err } } case "python": command := exec.Command("python", scriptFileName, "--application_name="+name, "--kube_apiserver_endpoint="+kubeApiServerEndPoint, "--kube_apiserver_token="+kubeApiServerToken, "--namespace="+namespace, "--size="+strconv.Itoa(size), "--replication_controller_file_name="+replicationControllerFileName, "--environment_file_name="+environmentFileName, "--timeout_in_second=120", "--action=resize") command.Dir = workingDirectory out, err := command.CombinedOutput() log.Debug(string(out)) if err != nil { log.Error("Run python script file for cluster application error %s", err) return errors.New("Error: " + err.Error() + " Output: " + string(out)) } default: log.Error("No such script type: %s", cluster.ScriptType) return errors.New("No such script type: " + cluster.ScriptType) } // Remove working space err = os.RemoveAll(workingDirectory) if err != nil { log.Error("Remove the working directory for cluster application error %s", err) return err } // Update deploy data deployClusterApplication, err := GetStorage().LoadDeployClusterApplication(namespace, name) if err != nil { log.Error("Update the deploy cluster application with error %s", err) return err } _, serviceName, replicationControllerNameSlice, err := getServiceNameAndReplicationControllerNameSlice(kubeApiServerEndPoint, kubeApiServerToken, namespace, name) if err != nil { log.Error(err) return err } deployClusterApplication.Size = size deployClusterApplication.EnvironmentSlice = environmentSlice deployClusterApplication.ServiceName = serviceName deployClusterApplication.ReplicationControllerNameSlice = replicationControllerNameSlice err = GetStorage().SaveDeployClusterApplication(deployClusterApplication) if err != nil { log.Error("Save the deploy cluster application error %s", err) return err } return nil }