-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathapi.go
More file actions
102 lines (87 loc) · 2.73 KB
/
Copy pathapi.go
File metadata and controls
102 lines (87 loc) · 2.73 KB
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
package config
import (
"slices"
"time"
)
// API holds configuration for the Clair API services.
type API struct {
// V1 is the configuration for the HTTP v1 API.
V1 APIv1 `yaml:"v1,omitempty" json:"v1,omitempty"`
}
func (a *API) validate(_ Mode) ([]Warning, error) {
// TODO(hank) When there's an "UpdaterMode," don't bother with validating
// the API configurations.
enabled := slices.ContainsFunc([]*bool{}, func(e *bool) bool {
return e != nil && *e
})
// With multiple versions, the highest one should be the default, probably.
if !enabled {
a.V1.Enabled = &[]bool{true}[0] // TODO(go1.26) Use the "new(true)" syntax.
}
return nil, nil
}
// APIv1 holds configuration values for the HTTP v1 API.
type APIv1 struct {
// Enabled configures enabling the API server at all.
// The set of API endpoints served by any one process depends on the mode
// the process is started in.
//
// If unset, defaults to "true".
Enabled *bool `yaml:"enabled" json:"enabled"`
// Network configures the network type to be used for serving API requests.
//
// If unset, [DefaultAPIv1Network] will be used.
// See also: [net.Dial].
Network string `yaml:"network" json:"network"`
// Address configures the address to listen on for serving API requests.
// The format depends on the "network" member.
//
// If unset, [DefaultAPIv1Address] will be used.
// See also: [net.Dial].
Address string `yaml:"address" json:"address"`
// IdleTimeout configures whether the Clair process should exit after not
// handling any requests for a specified non-zero duration.
IdleTimeout Duration `yaml:"idle_timeout" json:"idle_timeout"`
// TLS configures HTTPS support.
//
// Note that any non-trivial deployment means the certificate provided here
// will need to be for the name the load balancer used to connect to a given
// Clair instance.
TLS *TLS `yaml:"tls,omitempty" json:"tls,omitempty"`
}
func (a *APIv1) validate(_ Mode) ([]Warning, error) {
if a.Enabled == nil || !*a.Enabled {
return nil, nil
}
if a.Network == "" {
a.Network = DefaultAPIv1Network
}
if a.Address == "" {
a.Address = DefaultAPIv1Address
}
return a.lint()
}
func (a *APIv1) lint() (ws []Warning, err error) {
if a.Network == "" {
ws = append(ws, Warning{
path: ".network",
msg: `listen network not provided, default will be used`,
})
}
if a.Address == "" {
ws = append(ws, Warning{
path: ".address",
msg: `listen address not provided, default will be used`,
})
}
switch dur := time.Duration(a.IdleTimeout); {
case dur == 0: // OK, disabled.
case dur < (2 * time.Minute):
ws = append(ws, Warning{
path: ".idle_timeout",
msg: `idle timeout seems short, may cause frequent startups`,
})
default: // OK, reasonably long.
}
return ws, nil
}