1
0
Fork 0

feat: custom label shorthands

This commit is contained in:
Arthur K. 2025-06-11 15:04:44 +03:00
parent c826cc97e6
commit b4ef1baf65
Signed by: wzray
GPG key ID: B97F30FDC4636357
8 changed files with 144 additions and 16 deletions

View file

@ -1,6 +1,7 @@
package docker
import (
"bytes"
"context"
"errors"
"fmt"
@ -28,15 +29,50 @@ func NewDynConfBuilder(configuration Shared, apiClient client.APIClient, swarm b
return &DynConfBuilder{Shared: configuration, apiClient: apiClient, swarm: swarm}
}
func (p *DynConfBuilder) applyLabels(container *dockerData, model interface{}) {
for _, item := range p.LabelMap {
value, ok := container.Labels[item.From]
if !ok { // label doesn't exist
continue
}
writer := &bytes.Buffer{}
if err := item.toTpl.Execute(writer, model); err != nil {
continue // should never happen?
}
to := writer.String()
if item.Value != nil {
container.Labels[to] = *item.Value
} else {
container.Labels[to] = value
}
}
}
func (p *DynConfBuilder) build(ctx context.Context, containersInspected []dockerData) *dynamic.Configuration {
configurations := make(map[string]*dynamic.Configuration)
for _, container := range containersInspected {
serviceName := getServiceName(container)
model := struct {
Name string
ContainerName string
Labels *map[string]string
}{
Name: serviceName,
ContainerName: strings.TrimPrefix(container.Name, "/"),
Labels: &container.Labels,
}
containerName := getServiceName(container) + "-" + container.ID
logger := log.Ctx(ctx).With().Str("container", containerName).Logger()
ctxContainer := logger.WithContext(ctx)
p.applyLabels(&container, model)
if !p.keepContainer(ctxContainer, container) {
continue
}
@ -83,18 +119,6 @@ func (p *DynConfBuilder) build(ctx context.Context, containersInspected []docker
continue
}
serviceName := getServiceName(container)
model := struct {
Name string
ContainerName string
Labels map[string]string
}{
Name: serviceName,
ContainerName: strings.TrimPrefix(container.Name, "/"),
Labels: container.Labels,
}
provider.BuildRouterConfiguration(ctx, confFromLabel.HTTP, serviceName, p.defaultRuleTpl, model)
configurations[containerName] = confFromLabel

View file

@ -49,8 +49,15 @@ func (p *Provider) Init() error {
if err != nil {
return fmt.Errorf("error while parsing default rule: %w", err)
}
p.defaultRuleTpl = defaultRuleTpl
for _, item := range p.LabelMap {
toTpl, err := provider.MakeAnyTemplate(item.From, item.To, nil)
if err != nil {
return fmt.Errorf("error while parsing label %v: %w", item.To, err)
}
item.toTpl = toTpl
}
return nil
}

View file

@ -33,9 +33,19 @@ type Shared struct {
Watch bool `description:"Watch Docker events." json:"watch,omitempty" toml:"watch,omitempty" yaml:"watch,omitempty" export:"true"`
DefaultRule string `description:"Default rule." json:"defaultRule,omitempty" toml:"defaultRule,omitempty" yaml:"defaultRule,omitempty"`
LabelMap []*LabelMapItem `description:"Label shorthands." json:"labelMap,omitempty" toml:"labelMap,omitempty" yaml:"labelMap,omitempty"`
defaultRuleTpl *template.Template
}
type LabelMapItem struct {
From string `description:"Shorthand label." json:"from,omitempty" toml:"from,omitempty" yaml:"from,omitempty"`
To string `description:"Full label with templates." json:"to,omitempty" toml:"to,omitempty" yaml:"to,omitempty"`
Value *string `description:"Optional override; used instead of user input if set." json:"value,omitempty" toml:"value,omitempty" yaml:"value,omitempty"`
toTpl *template.Template
}
func inspectContainers(ctx context.Context, dockerClient client.ContainerAPIClient, containerID string) dockerData {
containerInspected, err := dockerClient.ContainerInspect(ctx, containerID)
if err != nil {