Skip to content

Commit ffbb8b9

Browse files
lucaslorentzclaude
andcommitted
Add sprig template functions to label templates
Add the sprig template function library, giving users access to 100+ utility functions (string manipulation, math, date, etc.) in their Docker label templates. This enables workarounds like trimPrefix for the leading slash in container names from the Docker API, e.g.: {{trimPrefix "/" (index .Names 0)}} Closes #648 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 25d6ba5 commit ffbb8b9

File tree

3 files changed

+21
-40
lines changed

3 files changed

+21
-40
lines changed

generator/containers.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package generator
22

33
import (
4-
"strings"
5-
64
"github.com/docker/docker/api/types"
75
"github.com/lucaslorentz/caddy-docker-proxy/v2/caddyfile"
86
"go.uber.org/zap"
@@ -11,21 +9,11 @@ import (
119
func (g *CaddyfileGenerator) getContainerCaddyfile(container *types.Container, logger *zap.Logger) (*caddyfile.Container, error) {
1210
caddyLabels := g.filterLabels(container.Labels)
1311

14-
trimContainerNames(container)
15-
1612
return labelsToCaddyfile(caddyLabels, container, func() ([]string, error) {
1713
return g.getContainerIPAddresses(container, logger, true)
1814
})
1915
}
2016

21-
// trimContainerNames removes the leading slash from container names.
22-
// The Docker API returns names prefixed with "/" (e.g. "/mycontainer").
23-
func trimContainerNames(container *types.Container) {
24-
for i, name := range container.Names {
25-
container.Names[i] = strings.TrimPrefix(name, "/")
26-
}
27-
}
28-
2917
func (g *CaddyfileGenerator) getContainerIPAddresses(container *types.Container, logger *zap.Logger, onlyIngressIps bool) ([]string, error) {
3018
ips := []string{}
3119

generator/containers_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func TestContainers_TemplateData(t *testing.T) {
2424
},
2525
},
2626
Labels: map[string]string{
27-
fmtLabel("%s"): "{{index .Names 0}}.testdomain.com",
27+
fmtLabel("%s"): "{{trimPrefix \"/\" (index .Names 0)}}.testdomain.com",
2828
fmtLabel("%s.reverse_proxy"): "{{(index .NetworkSettings.Networks \"caddy-network\").IPAddress}}:5000/api",
2929
},
3030
},

generator/labels.go

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,38 @@
11
package generator
22

33
import (
4+
"sort"
45
"strconv"
56
"strings"
6-
"text/template"
7-
"sort"
87

8+
"github.com/Masterminds/sprig/v3"
99
"github.com/lucaslorentz/caddy-docker-proxy/v2/caddyfile"
1010
)
1111

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

1414
func labelsToCaddyfile(labels map[string]string, templateData interface{}, getTargets targetsProvider) (*caddyfile.Container, error) {
15-
funcMap := template.FuncMap{
16-
"upstreams": func(options ...interface{}) (string, error) {
17-
targets, err := getTargets()
18-
sort.Strings(targets)
19-
transformed := []string{}
20-
for _, target := range targets {
21-
for _, param := range options {
22-
if protocol, isProtocol := param.(string); isProtocol {
23-
target = protocol + "://" + target
24-
} else if port, isPort := param.(int); isPort {
25-
target = target + ":" + strconv.Itoa(port)
26-
}
15+
funcMap := sprig.TxtFuncMap()
16+
funcMap["upstreams"] = func(options ...interface{}) (string, error) {
17+
targets, err := getTargets()
18+
sort.Strings(targets)
19+
transformed := []string{}
20+
for _, target := range targets {
21+
for _, param := range options {
22+
if protocol, isProtocol := param.(string); isProtocol {
23+
target = protocol + "://" + target
24+
} else if port, isPort := param.(int); isPort {
25+
target = target + ":" + strconv.Itoa(port)
2726
}
28-
transformed = append(transformed, target)
2927
}
30-
sort.Strings(transformed)
31-
return strings.Join(transformed, " "), err
32-
},
33-
"http": func() string {
34-
return "http"
35-
},
36-
"https": func() string {
37-
return "https"
38-
},
39-
"h2c": func() string {
40-
return "h2c"
41-
},
28+
transformed = append(transformed, target)
29+
}
30+
sort.Strings(transformed)
31+
return strings.Join(transformed, " "), err
4232
}
33+
funcMap["http"] = func() string { return "http" }
34+
funcMap["https"] = func() string { return "https" }
35+
funcMap["h2c"] = func() string { return "h2c" }
4336

4437
return caddyfile.FromLabels(labels, templateData, funcMap)
4538
}

0 commit comments

Comments
 (0)