-
Notifications
You must be signed in to change notification settings - Fork 1
/
docker.go
105 lines (87 loc) · 2.69 KB
/
docker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package dns
import (
"context"
"fmt"
"log/slog"
"reflect"
"strconv"
"github.com/Tarow/dockdns/internal/config"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
)
const dockdnsNameLabel = "dockdns.name"
func (h handler) filterDockerLabels() ([]config.DomainRecord, error) {
containers, err := h.dockerCli.ContainerList(context.Background(), types.ContainerListOptions{
Filters: filters.NewArgs(filters.Arg("label", dockdnsNameLabel)),
})
if err != nil {
return nil, err
}
return parseContainerLabels(containers)
}
func parseContainerLabels(containers []types.Container) ([]config.DomainRecord, error) {
var labelRecords []config.DomainRecord
for _, container := range containers {
var record config.DomainRecord
err := parseLabels(container, &record)
if err != nil {
slog.Warn("error parsing label configuration, skipping container", "container", container.Names, "error", err)
continue
}
labelRecords = append(labelRecords, record)
}
return labelRecords, nil
}
func parseLabels(container types.Container, targetStruct *config.DomainRecord) error {
containerLabels := container.Labels
targetValue := reflect.ValueOf(targetStruct)
if targetValue.Kind() != reflect.Ptr || targetValue.Elem().Kind() != reflect.Struct {
return fmt.Errorf("targetStruct must be a pointer to a struct")
}
targetType := targetValue.Elem().Type()
for i := 0; i < targetType.NumField(); i++ {
field := targetType.Field(i)
label := field.Tag.Get("label")
if label != "" {
labelValue, exists := containerLabels[label]
if exists && labelValue != "" {
targetField := targetValue.Elem().Field(i)
if err := setFieldValue(targetField, labelValue); err != nil {
return fmt.Errorf("could not parse label value, label: %v, value: %v, error: %w", label, labelValue, err)
}
}
}
}
return nil
}
func setFieldValue(field reflect.Value, labelValue string) error {
if field.Kind() == reflect.Ptr {
// If the field is a pointer, create a new instance of the underlying type and set the value
if field.IsNil() {
field.Set(reflect.New(field.Type().Elem()))
}
field = field.Elem()
}
switch field.Kind() {
case reflect.String:
field.SetString(labelValue)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
intValue, err := strconv.ParseInt(labelValue, 10, 64)
if err != nil {
return err
}
field.SetInt(intValue)
case reflect.Bool:
boolValue, err := strconv.ParseBool(labelValue)
if err != nil {
return err
}
field.SetBool(boolValue)
case reflect.Uint8:
byteValue := []byte(labelValue)
field.SetBytes(byteValue)
default:
return fmt.Errorf("unsupported field type: %v", field.Kind())
}
return nil
}