func postKubernetesService(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 := new(control.Service) 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 } if service.Namespace != namespace { jsonMap := make(map[string]interface{}) jsonMap["Error"] = "Path parameter namespace is different from namespace in the body" jsonMap["path"] = namespace jsonMap["body"] = service.Namespace errorMessageByteSlice, _ := json.Marshal(jsonMap) log.Error(jsonMap) response.WriteErrorString(400, string(errorMessageByteSlice)) return } err = control.CreateService(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 } }
func UpdateDeployBlueGreen(kubeApiServerEndPoint string, kubeApiServerToken string, deployBlueGreen *DeployBlueGreen) error { deployInformation, err := GetStorage().LoadDeployInformation( deployBlueGreen.Namespace, deployBlueGreen.ImageInformation) if err != nil { log.Error("Fail to load deploy information %s in namespace %s with error %s", deployBlueGreen.ImageInformation, deployBlueGreen.Namespace, err) return err } replicationController, err := control.GetReplicationController( kubeApiServerEndPoint, kubeApiServerToken, deployBlueGreen.Namespace, getBlueGreenReplicationControllerName(deployInformation.ImageInformationName, deployInformation.CurrentVersion)) if err != nil { log.Error("Fail to load target replication controller information %s in namespace %s with error %s", deployInformation.ImageInformationName+deployInformation.CurrentVersion, deployBlueGreen.Namespace, err) return err } // TODO support multiple ports portName := replicationController.ContainerSlice[0].PortSlice[0].Name containerPort := replicationController.ContainerSlice[0].PortSlice[0].ContainerPort // Clean all the previous blue green deployment CleanAllServiceUnderBlueGreenDeployment(kubeApiServerEndPoint, kubeApiServerToken, deployBlueGreen.ImageInformation) selector := make(map[string]interface{}) selector["name"] = deployBlueGreen.ImageInformation labelMap := make(map[string]interface{}) labelMap["name"] = deployBlueGreen.ImageInformation portSlice := make([]control.ServicePort, 0) // TODO read protocol rather than TCP portSlice = append(portSlice, control.ServicePort{ portName, "TCP", containerPort, strconv.Itoa(containerPort), deployBlueGreen.NodePort}) service := control.Service{ GetBlueGreenServiceName(deployBlueGreen.ImageInformation), deployBlueGreen.Namespace, portSlice, selector, "", labelMap, deployBlueGreen.SessionAffinity, } err = control.CreateService(kubeApiServerEndPoint, kubeApiServerToken, deployBlueGreen.Namespace, service) if err != nil { log.Error("Create target service failure service %s with error %s", service, err) return err } // Update DeployBlueGreen err = GetStorage().saveDeployBlueGreen(deployBlueGreen) if err != nil { log.Error("Save deploy blude grenn %s with error %s", deployBlueGreen, err) return err } return nil }
func DeployCreate( kubeApiServerEndPoint string, kubeApiServerToken string, namespace string, imageInformationName string, version string, description string, replicaAmount int, deployContainerPortSlice []DeployContainerPort, replicationControllerContainerEnvironmentSlice []control.ReplicationControllerContainerEnvironment, resourceMap map[string]interface{}, extraJsonMap map[string]interface{}, autoUpdateForNewBuild bool) error { if lock.AcquireLock(LockKind, getLockName(namespace, imageInformationName), 0) == false { return errors.New("Deployment is controlled by the other command") } defer lock.ReleaseLock(LockKind, getLockName(namespace, imageInformationName)) imageRecord, err := image.GetStorage().LoadImageRecord(imageInformationName, version) if err != nil { log.Error("Load image record error: %s imageInformationName %s version %s", err, imageInformationName, version) return err } // Check whether the image in the private-registry privateRegistry, err := registry.GetPrivateRegistryFromPathAndTestAvailable(imageRecord.Path) if err != nil { log.Error("Get private registry access error: " + err.Error()) return err } if privateRegistry.IsImageTagAvailable(imageRecord.ImageInformation, imageRecord.Version) == false { return errors.New("The image is not in the private-registry") } selectorName := imageInformationName replicationControllerName := selectorName + version image := imageRecord.Path // Automatically generate the basic default service. For advanced configuration, it should be modified in the service servicePortSlice := make([]control.ServicePort, 0) for _, deployContainerPort := range deployContainerPortSlice { containerPort := strconv.Itoa(deployContainerPort.ContainerPort) servicePort := control.ServicePort{ deployContainerPort.Name, "TCP", deployContainerPort.ContainerPort, containerPort, deployContainerPort.NodePort, // -1 means not to use. 0 means auto-generated. > 0 means the port number to use } servicePortSlice = append(servicePortSlice, servicePort) } selectorLabelMap := make(map[string]interface{}) selectorLabelMap["name"] = selectorName serviceLabelMap := make(map[string]interface{}) serviceLabelMap["name"] = imageInformationName service := control.Service{ imageInformationName, namespace, servicePortSlice, selectorLabelMap, "", serviceLabelMap, "", } err = control.CreateService(kubeApiServerEndPoint, kubeApiServerToken, namespace, service) if err != nil { log.Error("Create service error: %s", err) return err } // Replication controller replicationControllerContainerPortSlice := make([]control.ReplicationControllerContainerPort, 0) for _, deployContainerPort := range deployContainerPortSlice { replicationControllerContainerPortSlice = append(replicationControllerContainerPortSlice, control.ReplicationControllerContainerPort{deployContainerPort.Name, deployContainerPort.ContainerPort}) } replicationControllerContainerSlice := make([]control.ReplicationControllerContainer, 0) replicationControllerContainerSlice = append( replicationControllerContainerSlice, control.ReplicationControllerContainer{ replicationControllerName, image, replicationControllerContainerPortSlice, replicationControllerContainerEnvironmentSlice, resourceMap, }) replicationController := control.ReplicationController{ replicationControllerName, replicaAmount, control.ReplicationControllerSelector{ selectorName, version, }, control.ReplicationControllerLabel{ replicationControllerName, }, replicationControllerContainerSlice, extraJsonMap, } err = control.CreateReplicationController(kubeApiServerEndPoint, kubeApiServerToken, namespace, replicationController) if err != nil { log.Error("Create replication controller error: %s", err) return err } deployInformation := &DeployInformation{ namespace, imageInformationName, version, imageRecord.Description, description, replicaAmount, deployContainerPortSlice, replicationControllerContainerEnvironmentSlice, resourceMap, extraJsonMap, time.Now(), autoUpdateForNewBuild, } err = GetStorage().saveDeployInformation(deployInformation) if err != nil { log.Error("Save deploy information error: %s", err) return err } return nil }