Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions generator/containers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestContainers_TemplateData(t *testing.T) {
dockerClient.ContainersData = []types.Container{
{
Names: []string{
"container-name",
"/container-name",
},
NetworkSettings: &types.SummaryNetworkSettings{
Networks: map[string]*network.EndpointSettings{
Expand All @@ -24,7 +24,7 @@ func TestContainers_TemplateData(t *testing.T) {
},
},
Labels: map[string]string{
fmtLabel("%s"): "{{index .Names 0}}.testdomain.com",
fmtLabel("%s"): "{{trimPrefix \"/\" (index .Names 0)}}.testdomain.com",
fmtLabel("%s.reverse_proxy"): "{{(index .NetworkSettings.Networks \"caddy-network\").IPAddress}}:5000/api",
},
},
Expand Down
47 changes: 20 additions & 27 deletions generator/labels.go
Original file line number Diff line number Diff line change
@@ -1,45 +1,38 @@
package generator

import (
"sort"
"strconv"
"strings"
"text/template"
"sort"

"github.com/Masterminds/sprig/v3"
"github.com/lucaslorentz/caddy-docker-proxy/v2/caddyfile"
)

type targetsProvider func() ([]string, error)

func labelsToCaddyfile(labels map[string]string, templateData interface{}, getTargets targetsProvider) (*caddyfile.Container, error) {
funcMap := template.FuncMap{
"upstreams": func(options ...interface{}) (string, error) {
targets, err := getTargets()
sort.Strings(targets)
transformed := []string{}
for _, target := range targets {
for _, param := range options {
if protocol, isProtocol := param.(string); isProtocol {
target = protocol + "://" + target
} else if port, isPort := param.(int); isPort {
target = target + ":" + strconv.Itoa(port)
}
funcMap := sprig.TxtFuncMap()
funcMap["upstreams"] = func(options ...interface{}) (string, error) {
targets, err := getTargets()
sort.Strings(targets)
transformed := []string{}
for _, target := range targets {
for _, param := range options {
if protocol, isProtocol := param.(string); isProtocol {
target = protocol + "://" + target
} else if port, isPort := param.(int); isPort {
target = target + ":" + strconv.Itoa(port)
}
transformed = append(transformed, target)
}
sort.Strings(transformed)
return strings.Join(transformed, " "), err
},
"http": func() string {
return "http"
},
"https": func() string {
return "https"
},
"h2c": func() string {
return "h2c"
},
transformed = append(transformed, target)
}
sort.Strings(transformed)
return strings.Join(transformed, " "), err
}
funcMap["http"] = func() string { return "http" }
funcMap["https"] = func() string { return "https" }
funcMap["h2c"] = func() string { return "h2c" }

return caddyfile.FromLabels(labels, templateData, funcMap)
}
Loading