Minor fixes to FromInputsToCmdParams function
- commit
- 0fbbe4d84966baa53bfa3671f3679a721c1c69f2
- author
- bnema · · 2023-09-26 19:01 UTC
- parent
- 4e0dac8
- signature
- unsigned
2 files · +15 -9
M internal/gotemplate/render/cmdparams.go +13 -7
| @@ -2,14 +2,17 @@ package render | |||
| 2 | 2 | ||
| 3 | 3 | import ( | |
| 4 | 4 | "fmt" | |
| 5 | + | "strings" | |
| 5 | 6 | ||
| 6 | 7 | "github.com/bnema/gordon/pkg/utils/docker" | |
| 7 | 8 | ) | |
| 8 | 9 | ||
| 9 | - | // FromInputsToCmdParams transforms a map to a ContainerCommandParams struct | |
| 10 | + | // FromInputsToCmdParams transforms the inputs map into a ContainerCommandParams struct | |
| 10 | 11 | func FromInputsToCmdParams(inputs map[string]string) (docker.ContainerCommandParams, error) { | |
| 11 | - | fmt.Println("FromInputsToCmdParams:", inputs) | |
| 12 | - | // Populate the ContainerCommandParams struct directly from the map | |
| 12 | + | ||
| 13 | + | volumeStr := inputs["volumes"] | |
| 14 | + | volumeSlice := strings.Split(volumeStr, ",") | |
| 15 | + | ||
| 13 | 16 | params := docker.ContainerCommandParams{ | |
| 14 | 17 | IsSSL: inputs["container_protocol"] == "https", | |
| 15 | 18 | ContainerName: inputs["container_name"], | |
| @@ -19,23 +22,26 @@ func FromInputsToCmdParams(inputs map[string]string) (docker.ContainerCommandPar | |||
| 19 | 22 | ImageID: inputs["image_id"], | |
| 20 | 23 | Ports: inputs["ports"], | |
| 21 | 24 | Restart: inputs["restart"], | |
| 22 | - | Volumes: inputs["volumes"], | |
| 25 | + | Volumes: volumeSlice, | |
| 23 | 26 | Network: ("traefik"), | |
| 24 | 27 | } | |
| 25 | 28 | ||
| 29 | + | // Retain the port number for the container host for the load balancer | |
| 30 | + | exposedPort := strings.Split(params.Ports, ":")[1] | |
| 31 | + | ||
| 26 | 32 | entryPoint := "web" | |
| 27 | 33 | if params.IsSSL { | |
| 28 | 34 | entryPoint = "websecure" | |
| 29 | 35 | } | |
| 30 | 36 | ||
| 31 | - | baseRouter := "traefik.http.routers." + params.ContainerName + "-router" | |
| 32 | - | baseService := "traefik.http.services." + params.ContainerName + "-service" | |
| 37 | + | baseRouter := "traefik.http.routers." + params.ContainerName | |
| 38 | + | baseService := "traefik.http.services." + params.ContainerName | |
| 33 | 39 | ||
| 34 | 40 | params.Labels = []string{ | |
| 35 | 41 | "traefik.enable=true", | |
| 36 | 42 | fmt.Sprintf("%s.rule=Host(`%s.%s`)", baseRouter, params.ServiceName, params.Domain), | |
| 37 | 43 | fmt.Sprintf("%s.entrypoints=%s", baseRouter, entryPoint), | |
| 38 | - | fmt.Sprintf("%s.loadbalancer.server.port=%s", baseService, params.Ports), | |
| 44 | + | fmt.Sprintf("%s.loadbalancer.server.port=%s", baseService, exposedPort), | |
| 39 | 45 | } | |
| 40 | 46 | ||
| 41 | 47 | if params.IsSSL { | |
| @@ -2,14 +2,17 @@ package render | |||||
| 2 | 2 | ||||
| 3 | import ( | 3 | import ( | ||
| 4 | "fmt" | 4 | "fmt" | ||
| 5 | + | "strings" | |||
| 5 | 6 | ||||
| 6 | "github.com/bnema/gordon/pkg/utils/docker" | 7 | "github.com/bnema/gordon/pkg/utils/docker" | ||
| 7 | ) | 8 | ) | ||
| 8 | 9 | ||||
| 9 | - | // FromInputsToCmdParams transforms a map to a ContainerCommandParams struct | 10 | + | // FromInputsToCmdParams transforms the inputs map into a ContainerCommandParams struct |
| 10 | func FromInputsToCmdParams(inputs map[string]string) (docker.ContainerCommandParams, error) { | 11 | func FromInputsToCmdParams(inputs map[string]string) (docker.ContainerCommandParams, error) { | ||
| 11 | - | fmt.Println("FromInputsToCmdParams:", inputs) | 12 | + | |
| 12 | - | // Populate the ContainerCommandParams struct directly from the map | 13 | + | volumeStr := inputs["volumes"] |
| 14 | + | volumeSlice := strings.Split(volumeStr, ",") | |||
| 15 | + | ||||
| 13 | params := docker.ContainerCommandParams{ | 16 | params := docker.ContainerCommandParams{ | ||
| 14 | IsSSL: inputs["container_protocol"] == "https", | 17 | IsSSL: inputs["container_protocol"] == "https", | ||
| 15 | ContainerName: inputs["container_name"], | 18 | ContainerName: inputs["container_name"], | ||
| @@ -19,23 +22,26 @@ func FromInputsToCmdParams(inputs map[string]string) (docker.ContainerCommandPar | |||||
| 19 | ImageID: inputs["image_id"], | 22 | ImageID: inputs["image_id"], | ||
| 20 | Ports: inputs["ports"], | 23 | Ports: inputs["ports"], | ||
| 21 | Restart: inputs["restart"], | 24 | Restart: inputs["restart"], | ||
| 22 | - | Volumes: inputs["volumes"], | 25 | + | Volumes: volumeSlice, |
| 23 | Network: ("traefik"), | 26 | Network: ("traefik"), | ||
| 24 | } | 27 | } | ||
| 25 | 28 | ||||
| 29 | + | // Retain the port number for the container host for the load balancer | |||
| 30 | + | exposedPort := strings.Split(params.Ports, ":")[1] | |||
| 31 | + | ||||
| 26 | entryPoint := "web" | 32 | entryPoint := "web" | ||
| 27 | if params.IsSSL { | 33 | if params.IsSSL { | ||
| 28 | entryPoint = "websecure" | 34 | entryPoint = "websecure" | ||
| 29 | } | 35 | } | ||
| 30 | 36 | ||||
| 31 | - | baseRouter := "traefik.http.routers." + params.ContainerName + "-router" | 37 | + | baseRouter := "traefik.http.routers." + params.ContainerName |
| 32 | - | baseService := "traefik.http.services." + params.ContainerName + "-service" | 38 | + | baseService := "traefik.http.services." + params.ContainerName |
| 33 | 39 | ||||
| 34 | params.Labels = []string{ | 40 | params.Labels = []string{ | ||
| 35 | "traefik.enable=true", | 41 | "traefik.enable=true", | ||
| 36 | fmt.Sprintf("%s.rule=Host(`%s.%s`)", baseRouter, params.ServiceName, params.Domain), | 42 | fmt.Sprintf("%s.rule=Host(`%s.%s`)", baseRouter, params.ServiceName, params.Domain), | ||
| 37 | fmt.Sprintf("%s.entrypoints=%s", baseRouter, entryPoint), | 43 | fmt.Sprintf("%s.entrypoints=%s", baseRouter, entryPoint), | ||
| 38 | - | fmt.Sprintf("%s.loadbalancer.server.port=%s", baseService, params.Ports), | 44 | + | fmt.Sprintf("%s.loadbalancer.server.port=%s", baseService, exposedPort), |
| 39 | } | 45 | } | ||
| 40 | 46 | ||||
| 41 | if params.IsSSL { | 47 | if params.IsSSL { | ||
M pkg/utils/docker/containers.go +2 -2
| @@ -22,7 +22,7 @@ type ContainerCommandParams struct { | |||
| 22 | 22 | ImageName string | |
| 23 | 23 | ImageID string | |
| 24 | 24 | Ports string | |
| 25 | - | Volumes string | |
| 25 | + | Volumes []string | |
| 26 | 26 | Labels []string | |
| 27 | 27 | Network string | |
| 28 | 28 | Restart string | |
| @@ -140,7 +140,7 @@ func CreateContainer(cmdParams ContainerCommandParams) error { | |||
| 140 | 140 | }, | |
| 141 | 141 | &container.HostConfig{ | |
| 142 | 142 | PortBindings: portBindings, | |
| 143 | - | Binds: []string{cmdParams.Volumes}, | |
| 143 | + | Binds: cmdParams.Volumes, | |
| 144 | 144 | RestartPolicy: container.RestartPolicy{ | |
| 145 | 145 | Name: cmdParams.Restart, | |
| 146 | 146 | }, | |
| @@ -22,7 +22,7 @@ type ContainerCommandParams struct { | |||||
| 22 | ImageName string | 22 | ImageName string | ||
| 23 | ImageID string | 23 | ImageID string | ||
| 24 | Ports string | 24 | Ports string | ||
| 25 | - | Volumes string | 25 | + | Volumes []string |
| 26 | Labels []string | 26 | Labels []string | ||
| 27 | Network string | 27 | Network string | ||
| 28 | Restart string | 28 | Restart string | ||
| @@ -140,7 +140,7 @@ func CreateContainer(cmdParams ContainerCommandParams) error { | |||||
| 140 | }, | 140 | }, | ||
| 141 | &container.HostConfig{ | 141 | &container.HostConfig{ | ||
| 142 | PortBindings: portBindings, | 142 | PortBindings: portBindings, | ||
| 143 | - | Binds: []string{cmdParams.Volumes}, | 143 | + | Binds: cmdParams.Volumes, |
| 144 | RestartPolicy: container.RestartPolicy{ | 144 | RestartPolicy: container.RestartPolicy{ | ||
| 145 | Name: cmdParams.Restart, | 145 | Name: cmdParams.Restart, | ||
| 146 | }, | 146 | }, | ||