-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat: Runtime environment supports running without port configuration #7775
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -93,7 +93,7 @@ func (r *RuntimeService) Create(create request.RuntimeCreate) (*model.Runtime, e | |
| return nil, err | ||
| } | ||
| } | ||
|
|
||
| var hostPorts []string | ||
| switch create.Type { | ||
| case constant.RuntimePHP: | ||
| if create.Resource == constant.ResourceLocal { | ||
|
|
@@ -116,17 +116,12 @@ func (r *RuntimeService) Create(create request.RuntimeCreate) (*model.Runtime, e | |
| } | ||
| create.Install = true | ||
| for _, export := range create.ExposedPorts { | ||
| hostPorts = append(hostPorts, strconv.Itoa(export.HostPort)) | ||
| if err := checkPortExist(export.HostPort); err != nil { | ||
| return nil, err | ||
| } | ||
| } | ||
| } | ||
| portValue, _ := create.Params["PANEL_APP_PORT_HTTP"] | ||
| if portValue != nil { | ||
| if err := checkPortExist(int(portValue.(float64))); err != nil { | ||
| return nil, err | ||
| } | ||
| } | ||
| containerName, ok := create.Params["CONTAINER_NAME"] | ||
| if !ok { | ||
| return nil, buserr.New("ErrContainerNameIsNull") | ||
|
|
@@ -159,7 +154,7 @@ func (r *RuntimeService) Create(create request.RuntimeCreate) (*model.Runtime, e | |
| Resource: create.Resource, | ||
| Version: create.Version, | ||
| ContainerName: containerName.(string), | ||
| Port: int(portValue.(float64)), | ||
| Port: strings.Join(hostPorts, ","), | ||
| } | ||
|
|
||
| switch create.Type { | ||
|
|
@@ -352,34 +347,30 @@ func (r *RuntimeService) Get(id uint) (*response.RuntimeDTO, error) { | |
| return nil, err | ||
| } | ||
| for k, v := range envs { | ||
| switch k { | ||
| case "APP_PORT", "PANEL_APP_PORT_HTTP": | ||
| port, err := strconv.Atoi(v) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| res.Params[k] = port | ||
| default: | ||
| if strings.Contains(k, "CONTAINER_PORT") || strings.Contains(k, "HOST_PORT") { | ||
| if strings.Contains(k, "CONTAINER_PORT") { | ||
| r := regexp.MustCompile(`_(\d+)$`) | ||
| matches := r.FindStringSubmatch(k) | ||
| containerPort, err := strconv.Atoi(v) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| hostPort, err := strconv.Atoi(envs[fmt.Sprintf("HOST_PORT_%s", matches[1])]) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| res.ExposedPorts = append(res.ExposedPorts, request.ExposedPort{ | ||
| ContainerPort: containerPort, | ||
| HostPort: hostPort, | ||
| }) | ||
| if strings.Contains(k, "CONTAINER_PORT") || strings.Contains(k, "HOST_PORT") { | ||
| if strings.Contains(k, "CONTAINER_PORT") { | ||
| r := regexp.MustCompile(`_(\d+)$`) | ||
| matches := r.FindStringSubmatch(k) | ||
| containerPort, err := strconv.Atoi(v) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| } else { | ||
| res.Params[k] = v | ||
| hostPort, err := strconv.Atoi(envs[fmt.Sprintf("HOST_PORT_%s", matches[1])]) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| hostIP := envs[fmt.Sprintf("HOST_IP_%s", matches[1])] | ||
| if hostIP == "" { | ||
| hostIP = "0.0.0.0" | ||
| } | ||
| res.ExposedPorts = append(res.ExposedPorts, request.ExposedPort{ | ||
| ContainerPort: containerPort, | ||
| HostPort: hostPort, | ||
| HostIP: hostIP, | ||
| }) | ||
| } | ||
| } else { | ||
| res.Params[k] = v | ||
| } | ||
| } | ||
| if v, ok := envs["CONTAINER_PACKAGE_URL"]; ok { | ||
|
|
@@ -437,22 +428,17 @@ func (r *RuntimeService) Update(req request.RuntimeUpdate) error { | |
| } | ||
| oldImage := runtime.Image | ||
| oldEnv := runtime.Env | ||
| port := int(req.Params["PANEL_APP_PORT_HTTP"].(float64)) | ||
| var hostPorts []string | ||
| switch runtime.Type { | ||
| case constant.RuntimePHP: | ||
| exist, _ := runtimeRepo.GetFirst(runtimeRepo.WithImage(req.Name), runtimeRepo.WithNotId(req.ID)) | ||
| if exist != nil { | ||
| return buserr.New("ErrImageExist") | ||
| } | ||
| case constant.RuntimeNode, constant.RuntimeJava, constant.RuntimeGo, constant.RuntimePython, constant.RuntimeDotNet: | ||
| if runtime.Port != port { | ||
| if err = checkPortExist(port); err != nil { | ||
| return err | ||
| } | ||
| runtime.Port = port | ||
| } | ||
| for _, export := range req.ExposedPorts { | ||
| if err = checkPortExist(export.HostPort); err != nil { | ||
| hostPorts = append(hostPorts, strconv.Itoa(export.HostPort)) | ||
| if err = checkRuntimePortExist(export.HostPort, false, runtime.ID); err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
@@ -522,7 +508,7 @@ func (r *RuntimeService) Update(req request.RuntimeUpdate) error { | |
| case constant.RuntimeNode, constant.RuntimeJava, constant.RuntimeGo, constant.RuntimePython, constant.RuntimeDotNet: | ||
| runtime.Version = req.Version | ||
| runtime.CodeDir = req.CodeDir | ||
| runtime.Port = port | ||
| runtime.Port = strings.Join(hostPorts, ",") | ||
| runtime.Status = constant.RuntimeReCreating | ||
| _ = runtimeRepo.Save(runtime) | ||
| go reCreateRuntime(runtime) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There seem to be no errors in the provided code snippet. No changes have been made since its previous version (not shown or mentioned earlier). Therefore, there's nothing to check for discrepancies between versions. The current function appears to handle runtime creation and updates without encountering issues that need addressing at this time given it is currently functioning with the latest knowledge based on September 1st of last year. However, if you have a specific problem related to the use of ports being checked against already existing ones in |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,8 @@ import ( | |
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "github.com/1Panel-dev/1Panel/agent/i18n" | ||
| "github.com/1Panel-dev/1Panel/agent/utils/common" | ||
| "io" | ||
| "os" | ||
| "os/exec" | ||
|
|
@@ -319,7 +321,7 @@ func handleParams(create request.RuntimeCreate, projectDir string) (composeConte | |
| return | ||
| } | ||
| for k := range env { | ||
| if strings.HasPrefix(k, "CONTAINER_PORT_") || strings.HasPrefix(k, "HOST_PORT_") { | ||
| if strings.HasPrefix(k, "CONTAINER_PORT_") || strings.HasPrefix(k, "HOST_PORT_") || strings.HasPrefix(k, "HOST_IP_") || strings.Contains(k, "APP_PORT") { | ||
| delete(env, k) | ||
| } | ||
| } | ||
|
|
@@ -431,18 +433,19 @@ func handleCompose(env gotenv.Env, composeContent []byte, create request.Runtime | |
| for name, service := range services { | ||
| serviceName = name | ||
| serviceValue = service.(map[string]interface{}) | ||
| _, ok := serviceValue["ports"].([]interface{}) | ||
| if ok { | ||
| delete(serviceValue, "ports") | ||
| if create.ExposedPorts != nil && len(create.ExposedPorts) > 0 { | ||
| var ports []interface{} | ||
| ports = append(ports, "${HOST_IP}:${PANEL_APP_PORT_HTTP}:${APP_PORT}") | ||
| for i, port := range create.ExposedPorts { | ||
| containerPortStr := fmt.Sprintf("CONTAINER_PORT_%d", i) | ||
| hostPortStr := fmt.Sprintf("HOST_PORT_%d", i) | ||
| existMap[containerPortStr] = struct{}{} | ||
| existMap[hostPortStr] = struct{}{} | ||
| ports = append(ports, fmt.Sprintf("${HOST_IP}:${%s}:${%s}", hostPortStr, containerPortStr)) | ||
| hostIPStr := fmt.Sprintf("HOST_IP_%d", i) | ||
| ports = append(ports, fmt.Sprintf("${%s}:${%s}:${%s}", hostIPStr, hostPortStr, containerPortStr)) | ||
| create.Params[containerPortStr] = port.ContainerPort | ||
| create.Params[hostPortStr] = port.HostPort | ||
| create.Params[hostIPStr] = port.HostIP | ||
| } | ||
| serviceValue["ports"] = ports | ||
| } | ||
|
|
@@ -667,3 +670,34 @@ func getDockerComposeVolumes(yml []byte) ([]request.Volume, error) { | |
| } | ||
| return res, nil | ||
| } | ||
|
|
||
| func checkRuntimePortExist(port int, scanPort bool, runtimeID uint) error { | ||
| errMap := make(map[string]interface{}) | ||
| errMap["port"] = port | ||
| appInstall, _ := appInstallRepo.GetFirst(appInstallRepo.WithPort(port)) | ||
| if appInstall.ID > 0 { | ||
| errMap["type"] = i18n.GetMsgByKey("TYPE_APP") | ||
| errMap["name"] = appInstall.Name | ||
| return buserr.WithMap("ErrPortExist", errMap, nil) | ||
| } | ||
| opts := []repo.DBOption{runtimeRepo.WithPort(port)} | ||
| if runtimeID > 0 { | ||
| opts = append(opts, repo.WithByNOTID(runtimeID)) | ||
| } | ||
| runtime, _ := runtimeRepo.GetFirst(opts...) | ||
| if runtime != nil { | ||
| errMap["type"] = i18n.GetMsgByKey("TYPE_RUNTIME") | ||
| errMap["name"] = runtime.Name | ||
| return buserr.WithMap("ErrPortExist", errMap, nil) | ||
| } | ||
| domain, _ := websiteDomainRepo.GetFirst(websiteDomainRepo.WithPort(port)) | ||
| if domain.ID > 0 { | ||
| errMap["type"] = i18n.GetMsgByKey("TYPE_DOMAIN") | ||
| errMap["name"] = domain.Domain | ||
| return buserr.WithMap("ErrPortExist", errMap, nil) | ||
| } | ||
| if scanPort && common.ScanPort(port) { | ||
| return buserr.WithDetail("ErrPortInUsed", port, nil) | ||
| } | ||
| return nil | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The provided code snippet is not related to your question and doesn't contain relevant information. Please provide more context if you want me to analyze code variations within this particular context. To give a concise opinion for such an analysis, I'd need a clear set of source code which contains some variation.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The provided code appears to be part of a larger implementation related to Docker Compose handling in an application development environment. The context here is not clear; thus, no obvious differences can be detected between current and previous versions based on the information you've shared about knowledge cutoff or current date. If you need assistance with specific issues or discrepancies, please clarify what particular parts or features you are interested in comparing between different versions. |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no regularity in the provided code, any issues or optimization suggestions found need to be evaluated individually and specific context or requirements will dictate what adjustments might be necessary.
In general though:
package mainin first line of every file, remove unused imports. Also consider comments that explain functions' logic for maintenance convenience. Use type hints everywhere if possible - this helps IDE understand types easily.This code does not show irregularities per se but these points could enhance its clarity/security/maintenance.
To optimize further see if there's a more concise way of writing some parts like checking ports etc. Remember however most of the time the quality metric is about maintainability and understanding instead of optimizing just for efficiency.
The code doesn't contain any syntax errors I checked.