func deleteKubernetesService(request *restful.Request, response *restful.Response) { namespace := request.PathParameter("namespace") service := request.PathParameter("service") 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["service"] = service errorMessageByteSlice, _ := json.Marshal(jsonMap) log.Error(jsonMap) response.WriteErrorString(404, string(errorMessageByteSlice)) return } err = control.DeleteService(kubeApiServerEndPoint, kubeApiServerToken, namespace, service) if err != nil { jsonMap := make(map[string]interface{}) jsonMap["Error"] = "Delete 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 } }
func DeployDelete(kubeApiServerEndPoint string, kubeApiServerToken string, namespace string, imageInformation string) error { if lock.AcquireLock(LockKind, getLockName(namespace, imageInformation), 0) == false { return errors.New("Deployment is controlled by the other command") } defer lock.ReleaseLock(LockKind, getLockName(namespace, imageInformation)) deployInformation, err := GetStorage().LoadDeployInformation(namespace, imageInformation) if err != nil { log.Error(err) return err } err = GetStorage().DeleteDeployInformation(namespace, imageInformation) if err != nil { log.Error(err) return err } replicationControllerName := deployInformation.ImageInformationName + deployInformation.CurrentVersion err = control.DeleteReplicationControllerAndRelatedPod(kubeApiServerEndPoint, kubeApiServerToken, namespace, replicationControllerName) if err != nil { log.Error(err) return err } err = control.DeleteService(kubeApiServerEndPoint, kubeApiServerToken, namespace, deployInformation.ImageInformationName) if err != nil { log.Error(err) return err } return nil }
func CleanAllServiceUnderBlueGreenDeployment(kubeApiServerEndPoint string, kubeApiServerToken string, imageInformationName string) error { // Clean all service with this deployment name namespaceSlice, err := control.GetAllNamespaceName(kubeApiServerEndPoint, kubeApiServerToken) if err != nil { log.Error("Fail to get all namesapce with error %s", err) return err } for _, namespace := range namespaceSlice { service, _ := control.GetService(kubeApiServerEndPoint, kubeApiServerToken, namespace, GetBlueGreenServiceName(imageInformationName)) if service != nil { err := control.DeleteService(kubeApiServerEndPoint, kubeApiServerToken, namespace, service.Name) if err != nil { log.Error("Fail to delete service %s in namesapce %s with error %s", imageInformationName, namespace, err) return err } } } return nil }
func DeleteDeployClusterApplication(kubeApiServerEndPoint string, kubeApiServerToken string, namespace string, name string) error { deployClusterApplication, err := GetDeployClusterApplication(namespace, name) if err != nil { log.Error("Get deploy cluster application error %s", err) return err } // Remove deploy cluster application err = GetStorage().DeleteDeployClusterApplication(namespace, name) if err != nil { log.Error("Delete the deploy cluster application error %s", err) return err } cluster, err := application.GetStorage().LoadClusterApplication(name) if err != nil { log.Error("Load cluster application error %s", err) return err } // Generate random work space workingDirectory := "/tmp/tmp_" + random.UUID() scriptFileName := "script" // 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)+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": for _, replicationControllerName := range deployClusterApplication.ReplicationControllerNameSlice { err := control.DeleteReplicationControllerAndRelatedPod(kubeApiServerEndPoint, kubeApiServerToken, namespace, replicationControllerName) if err != nil { log.Error("Delete replication controller %s error %s", replicationControllerName, err) return err } } err = control.DeleteService(kubeApiServerEndPoint, kubeApiServerToken, namespace, deployClusterApplication.ServiceName) if err != nil { log.Error("Delete service %s error %s", deployClusterApplication.ServiceName, err) return err } case "python": command := exec.Command("python", scriptFileName, "--application_name="+name, "--kube_apiserver_endpoint="+kubeApiServerEndPoint, "--kube_apiserver_token="+kubeApiServerToken, "--namespace="+namespace, "--timeout_in_second=120", "--action=delete") 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 } return nil }