-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
config.go
117 lines (98 loc) · 3.12 KB
/
config.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
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package docker // import "github.com/open-telemetry/opentelemetry-collector-contrib/internal/docker"
import (
"errors"
"fmt"
"strconv"
"strings"
"time"
"github.com/docker/docker/api/types/versions"
)
type Config struct {
// The URL of the docker server. Default is "unix:///var/run/docker.sock"
Endpoint string `mapstructure:"endpoint"`
// The maximum amount of time to wait for docker API responses. Default is 5s
Timeout time.Duration `mapstructure:"timeout"`
// A list of filters whose matching images are to be excluded. Supports literals, globs, and regex.
ExcludedImages []string `mapstructure:"excluded_images"`
// Docker client API version.
DockerAPIVersion string `mapstructure:"api_version"`
}
// NewConfig creates a new config to be used when creating
// a docker client
func NewConfig(endpoint string, timeout time.Duration, excludedImages []string, apiVersion string) (*Config, error) {
cfg := &Config{
Endpoint: endpoint,
Timeout: timeout,
ExcludedImages: excludedImages,
DockerAPIVersion: apiVersion,
}
return cfg, cfg.validate()
}
// NewDefaultConfig creates a new config with default values
// to be used when creating a docker client
func NewDefaultConfig() *Config {
cfg := &Config{
Endpoint: "unix:///var/run/docker.sock",
Timeout: 5 * time.Second,
DockerAPIVersion: minimumRequiredDockerAPIVersion,
}
return cfg
}
// validate asserts that an endpoint field is set
// on the config struct
func (config Config) validate() error {
if config.Endpoint == "" {
return errors.New("config.Endpoint must be specified")
}
if err := VersionIsValidAndGTE(config.DockerAPIVersion, minimumRequiredDockerAPIVersion); err != nil {
return err
}
return nil
}
type apiVersion struct {
major int
minor int
}
func NewAPIVersion(version string) (string, error) {
s := strings.TrimSpace(version)
split := strings.Split(s, ".")
invalidVersion := "invalid version %q"
nParts := len(split)
if s == "" || nParts < 1 || nParts > 2 {
return "", fmt.Errorf(invalidVersion, s)
}
apiVer := new(apiVersion)
var err error
target := map[int]*int{0: &apiVer.major, 1: &apiVer.minor}
for i, part := range split {
part = strings.TrimSpace(part)
if part != "" {
if *target[i], err = strconv.Atoi(part); err != nil {
return "", fmt.Errorf(invalidVersion+": %w", s, err)
}
}
}
return fmt.Sprintf("%d.%d", apiVer.major, apiVer.minor), nil
}
// MustNewAPIVersion evaluates version as a client api version and panics if invalid.
func MustNewAPIVersion(version string) string {
v, err := NewAPIVersion(version)
if err != nil {
panic(err)
}
return v
}
// VersionIsValidAndGTE evalutes version as a client api version and returns an error if invalid or less than gte.
// gte is assumed to be valid (easiest if result of MustNewAPIVersion on initialization)
func VersionIsValidAndGTE(version, gte string) error {
v, err := NewAPIVersion(version)
if err != nil {
return err
}
if versions.LessThan(v, gte) {
return fmt.Errorf(`"api_version" %s must be at least %s`, version, gte)
}
return nil
}