func addCommandFromAllBlueGreenDeployment(command *slb.Command, kubeApiServerEndPoint string, kubeApiServerToken string) error { deployBlueGreenSlice, err := deploy.GetStorage().LoadAllDeployBlueGreen() if err != nil { log.Error(err) return err } kubernetesServiceHTTPSlice := make([]slb.KubernetesServiceHTTP, 0) for _, deployBlueGreen := range deployBlueGreenSlice { deployInformation, err := deploy.GetStorage().LoadDeployInformation(deployBlueGreen.Namespace, deployBlueGreen.ImageInformation) if err != nil { log.Error(err) return err } serviceName := deploy.GetBlueGreenServiceName(deployBlueGreen.ImageInformation) service, err := control.GetService(kubeApiServerEndPoint, kubeApiServerToken, deployBlueGreen.Namespace, serviceName) if err != nil { log.Error(err) return err } for _, servicePort := range service.PortSlice { // Get protocol protocol := "" for _, containerPort := range deployInformation.ContainerPortSlice { if servicePort.TargetPort == strconv.Itoa(containerPort.ContainerPort) { protocol = containerPort.Protocol } } // HTTP if protocol == deploy.ProtocolTypeHTTP && servicePort.NodePort >= 0 { kubernetesServiceHTTP := slb.KubernetesServiceHTTP{ deployBlueGreen.Namespace, BlueGreenDeploymentPrefix + deployBlueGreen.ImageInformation, servicePort.Port, servicePort.NodePort, } kubernetesServiceHTTPSlice = append(kubernetesServiceHTTPSlice, kubernetesServiceHTTP) } } } if command.KubernetesServiceHTTPSlice == nil { command.KubernetesServiceHTTPSlice = kubernetesServiceHTTPSlice } else { command.KubernetesServiceHTTPSlice = append(command.KubernetesServiceHTTPSlice, kubernetesServiceHTTPSlice...) } return nil }
func deleteDeployBlueGreen(request *restful.Request, response *restful.Response) { imageInformation := request.PathParameter("imageinformation") 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() errorMessageByteSlice, _ := json.Marshal(jsonMap) log.Error(jsonMap) response.WriteErrorString(404, string(errorMessageByteSlice)) return } err = deploy.GetStorage().DeleteDeployBlueGreen(imageInformation) if err != nil { jsonMap := make(map[string]interface{}) jsonMap["Error"] = "Delete blue green deployment failure" jsonMap["ErrorMessage"] = err.Error() jsonMap["kubeApiServerEndPoint"] = kubeApiServerEndPoint jsonMap["imageInformation"] = imageInformation errorMessageByteSlice, _ := json.Marshal(jsonMap) log.Error(jsonMap) response.WriteErrorString(422, string(errorMessageByteSlice)) return } err = deploy.CleanAllServiceUnderBlueGreenDeployment(kubeApiServerEndPoint, kubeApiServerToken, imageInformation) if err != nil { jsonMap := make(map[string]interface{}) jsonMap["Error"] = "Delete all services under blue green deployment failure" jsonMap["ErrorMessage"] = err.Error() jsonMap["kubeApiServerEndPoint"] = kubeApiServerEndPoint jsonMap["imageInformation"] = imageInformation errorMessageByteSlice, _ := json.Marshal(jsonMap) log.Error(jsonMap) response.WriteErrorString(422, string(errorMessageByteSlice)) return } err = slb.SendCommandToAllSLBDaemon() if err != nil { jsonMap := make(map[string]interface{}) jsonMap["Error"] = "Configure SLB failure" jsonMap["ErrorMessage"] = err.Error() errorMessageByteSlice, _ := json.Marshal(jsonMap) log.Error(jsonMap) response.WriteErrorString(422, string(errorMessageByteSlice)) return } }
func getAllDeployInformation(request *restful.Request, response *restful.Response) { deployInformationSlice, err := deploy.GetStorage().LoadAllDeployInformation() if err != nil { jsonMap := make(map[string]interface{}) jsonMap["Error"] = "Get all deployment failure" jsonMap["ErrorMessage"] = err.Error() errorMessageByteSlice, _ := json.Marshal(jsonMap) log.Error(jsonMap) response.WriteErrorString(404, string(errorMessageByteSlice)) return } response.WriteJson(deployInformationSlice, "[]DeployInformation") }
func getDeployBlueGreen(request *restful.Request, response *restful.Response) { imageInformation := request.PathParameter("imageinformation") deployBlueGreen, err := deploy.GetStorage().LoadDeployBlueGreen(imageInformation) if err != nil { jsonMap := make(map[string]interface{}) jsonMap["Error"] = "Get blue green deployment failure" jsonMap["ErrorMessage"] = err.Error() jsonMap["imageInformation"] = imageInformation errorMessageByteSlice, _ := json.Marshal(jsonMap) log.Error(jsonMap) response.WriteErrorString(404, string(errorMessageByteSlice)) return } response.WriteJson(deployBlueGreen, "DeployBlueGreen") }
func CheckAndExecuteNotifier(replicationControllerNotifier *ReplicationControllerNotifier) (bool, error) { switch replicationControllerNotifier.Kind { case "application": deployInformation, err := deploy.GetStorage().LoadDeployInformation(replicationControllerNotifier.Namespace, replicationControllerNotifier.Name) if err != nil { log.Error("Load deploy information failure: %s where replicationControllerNotifier %v", err, replicationControllerNotifier) return false, err } replicationControllerName := deployInformation.ImageInformationName + deployInformation.CurrentVersion return CheckAndExecuteNotifierOnReplicationController(replicationControllerNotifier, replicationControllerName) case "selector": nameSlice, err := monitor.GetReplicationControllerNameFromSelector( replicationControllerNotifier.KubeApiServerEndPoint, replicationControllerNotifier.KubeApiServerToken, replicationControllerNotifier.Namespace, replicationControllerNotifier.Name) if err != nil { return false, errors.New("Could not find replication controller name with selector " + replicationControllerNotifier.Name + " error " + err.Error()) } else { atLeastOneNotify := false errorMessage := bytes.Buffer{} hasError := false for _, name := range nameSlice { result, err := CheckAndExecuteNotifierOnReplicationController(replicationControllerNotifier, name) atLeastOneNotify = atLeastOneNotify || result if err != nil { errorMessage.WriteString(err.Error()) hasError = true } } if hasError == false { return atLeastOneNotify, nil } else { return atLeastOneNotify, errors.New(errorMessage.String()) } } case "replicationController": return CheckAndExecuteNotifierOnReplicationController(replicationControllerNotifier, replicationControllerNotifier.Name) default: return false, errors.New("No such kind " + replicationControllerNotifier.Kind) } }
func postDeployCreate(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 } deployCreateInput := new(DeployCreateInput) err = request.ReadEntity(&deployCreateInput) 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 } deploymentInformation, _ := deploy.GetStorage().LoadDeployInformation(namespace, deployCreateInput.ImageInformationName) if deploymentInformation != nil { jsonMap := make(map[string]interface{}) jsonMap["Error"] = "Duplicate deployment error" jsonMap["ErrorMessage"] = "Already exists" jsonMap["kubeApiServerToken"] = kubeApiServerToken jsonMap["namespace"] = namespace jsonMap["deployCreateInput"] = deployCreateInput errorMessageByteSlice, _ := json.Marshal(jsonMap) log.Error(jsonMap) response.WriteErrorString(422, string(errorMessageByteSlice)) return } err = deploy.DeployCreate( kubeApiServerEndPoint, kubeApiServerToken, namespace, deployCreateInput.ImageInformationName, deployCreateInput.Version, deployCreateInput.Description, deployCreateInput.ReplicaAmount, deployCreateInput.PortSlice, deployCreateInput.EnvironmentSlice, deployCreateInput.ResourceMap, deployCreateInput.ExtraJsonMap, deployCreateInput.AutoUpdateForNewBuild, ) if err != nil { jsonMap := make(map[string]interface{}) jsonMap["Error"] = "Create deployment failure" jsonMap["ErrorMessage"] = err.Error() jsonMap["kubeApiServerToken"] = kubeApiServerToken jsonMap["namespace"] = namespace jsonMap["deployCreateInput"] = deployCreateInput errorMessageByteSlice, _ := json.Marshal(jsonMap) log.Error(jsonMap) response.WriteErrorString(422, string(errorMessageByteSlice)) return } err = slb.SendCommandToAllSLBDaemon() if err != nil { jsonMap := make(map[string]interface{}) jsonMap["Error"] = "Configure SLB failure" jsonMap["ErrorMessage"] = err.Error() errorMessageByteSlice, _ := json.Marshal(jsonMap) log.Error(jsonMap) response.WriteErrorString(422, string(errorMessageByteSlice)) return } }
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 } }
func putReplicationControllerNotifier(request *restful.Request, response *restful.Response) { replicationControllerNotifierSerializable := new(notification.ReplicationControllerNotifierSerializable) err := request.ReadEntity(&replicationControllerNotifierSerializable) if err != nil { jsonMap := make(map[string]interface{}) jsonMap["Error"] = "Read body failure" jsonMap["ErrorMessage"] = err.Error() errorMessageByteSlice, _ := json.Marshal(jsonMap) log.Error(jsonMap) response.WriteErrorString(400, string(errorMessageByteSlice)) return } 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() errorMessageByteSlice, _ := json.Marshal(jsonMap) log.Error(jsonMap) response.WriteErrorString(404, string(errorMessageByteSlice)) return } replicationControllerNotifierSerializable.KubeApiServerEndPoint = kubeApiServerEndPoint replicationControllerNotifierSerializable.KubeApiServerToken = kubeApiServerToken switch replicationControllerNotifierSerializable.Kind { case "application": _, err := deploy.GetStorage().LoadDeployInformation(replicationControllerNotifierSerializable.Namespace, replicationControllerNotifierSerializable.Name) if err != nil { jsonMap := make(map[string]interface{}) jsonMap["Error"] = "Check whether the application exists or not failure" jsonMap["ErrorMessage"] = err.Error() jsonMap["replicationControllerNotifierSerializable"] = replicationControllerNotifierSerializable errorMessageByteSlice, _ := json.Marshal(jsonMap) log.Error(jsonMap) response.WriteErrorString(422, string(errorMessageByteSlice)) return } case "selector": nameSlice, err := monitor.GetReplicationControllerNameFromSelector(replicationControllerNotifierSerializable.KubeApiServerEndPoint, replicationControllerNotifierSerializable.KubeApiServerToken, replicationControllerNotifierSerializable.Namespace, replicationControllerNotifierSerializable.Name) if err != nil { for _, name := range nameSlice { exist, err := monitor.ExistReplicationController(replicationControllerNotifierSerializable.KubeApiServerEndPoint, replicationControllerNotifierSerializable.KubeApiServerToken, replicationControllerNotifierSerializable.Namespace, name) if err != nil { jsonMap := make(map[string]interface{}) jsonMap["Error"] = "Check whether the replication controller exists or not failure" jsonMap["ErrorMessage"] = err.Error() jsonMap["replicationControllerNotifierSerializable"] = replicationControllerNotifierSerializable errorMessageByteSlice, _ := json.Marshal(jsonMap) log.Error(jsonMap) response.WriteErrorString(422, string(errorMessageByteSlice)) return } if exist == false { jsonMap := make(map[string]interface{}) jsonMap["Error"] = "The replication controller to notify doesn't exist" jsonMap["ErrorMessage"] = err.Error() jsonMap["replicationControllerNotifierSerializable"] = replicationControllerNotifierSerializable errorMessageByteSlice, _ := json.Marshal(jsonMap) log.Error(jsonMap) response.WriteErrorString(404, string(errorMessageByteSlice)) return } } } case "replicationController": exist, err := monitor.ExistReplicationController(replicationControllerNotifierSerializable.KubeApiServerEndPoint, replicationControllerNotifierSerializable.KubeApiServerToken, replicationControllerNotifierSerializable.Namespace, replicationControllerNotifierSerializable.Name) if err != nil { jsonMap := make(map[string]interface{}) jsonMap["Error"] = "Check whether the replication controller exists or not failure" jsonMap["ErrorMessage"] = err.Error() jsonMap["replicationControllerNotifierSerializable"] = replicationControllerNotifierSerializable errorMessageByteSlice, _ := json.Marshal(jsonMap) log.Error(jsonMap) response.WriteErrorString(422, string(errorMessageByteSlice)) return } if exist == false { jsonMap := make(map[string]interface{}) jsonMap["Error"] = "The replication controller to notify doesn't exist" jsonMap["ErrorMessage"] = err.Error() jsonMap["replicationControllerNotifierSerializable"] = replicationControllerNotifierSerializable errorMessageByteSlice, _ := json.Marshal(jsonMap) log.Error(jsonMap) response.WriteErrorString(400, string(errorMessageByteSlice)) return } default: jsonMap := make(map[string]interface{}) jsonMap["Error"] = "No such kind" jsonMap["replicationControllerNotifierSerializable"] = replicationControllerNotifierSerializable jsonMap["kind"] = replicationControllerNotifierSerializable.Kind errorMessageByteSlice, _ := json.Marshal(jsonMap) log.Error(jsonMap) response.WriteErrorString(400, string(errorMessageByteSlice)) return } replicationControllerNotifier, err := notification.ConvertFromSerializable(*replicationControllerNotifierSerializable) if err != nil { jsonMap := make(map[string]interface{}) jsonMap["Error"] = "Convert replication controller notifier failure" jsonMap["ErrorMessage"] = err.Error() jsonMap["replicationControllerNotifierSerializable"] = replicationControllerNotifierSerializable errorMessageByteSlice, _ := json.Marshal(jsonMap) log.Error(jsonMap) response.WriteErrorString(400, string(errorMessageByteSlice)) return } err = notification.GetStorage().SaveReplicationControllerNotifierSerializable(replicationControllerNotifierSerializable) if err != nil { jsonMap := make(map[string]interface{}) jsonMap["Error"] = "Save replication controller notifier failure" jsonMap["ErrorMessage"] = err.Error() jsonMap["replicationControllerNotifierSerializable"] = replicationControllerNotifierSerializable errorMessageByteSlice, _ := json.Marshal(jsonMap) log.Error(jsonMap) response.WriteErrorString(422, string(errorMessageByteSlice)) return } execute.AddReplicationControllerNotifier(&replicationControllerNotifier) }