-
Notifications
You must be signed in to change notification settings - Fork 10
/
probe.go
89 lines (79 loc) · 2.31 KB
/
probe.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
package config
import (
"errors"
"net/url"
"github.com/mackerelio/mackerel-container-agent/cmdutil"
)
// Probe configuration.
type Probe struct {
Exec *ProbeExec `yaml:"exec"`
HTTP *ProbeHTTP `yaml:"http"`
TCP *ProbeTCP `yaml:"tcp"`
InitialDelaySeconds int `yaml:"initialDelaySeconds"`
PeriodSeconds int `yaml:"periodSeconds"`
TimeoutSeconds int `yaml:"timeoutSeconds"`
}
func (p *Probe) validate() error {
if p.Exec != nil && p.HTTP != nil || p.HTTP != nil && p.TCP != nil || p.TCP != nil && p.Exec != nil {
return errors.New("either one of exec, http or tcp can be configured for probe")
}
if p.Exec == nil && p.HTTP == nil && p.TCP == nil {
return errors.New("configure exec, http or tcp for probe")
}
if p.Exec != nil && p.Exec.Command.IsEmpty() {
return errors.New("specify command of exec probe")
}
if p.HTTP != nil && p.HTTP.Path == "" {
return errors.New("specify path of http probe")
}
if p.TCP != nil && p.TCP.Port == "" {
return errors.New("specify port of tcp probe")
}
if p.InitialDelaySeconds < 0 {
return errors.New("initialDelaySeconds should be positive")
}
if p.PeriodSeconds < 0 {
return errors.New("periodSeconds should be positive")
}
if p.TimeoutSeconds < 0 {
return errors.New("timeoutSeconds should be positive")
}
return nil
}
// ProbeExec is a probe with command.
type ProbeExec struct {
Command cmdutil.Command `yaml:"command"`
User string `yaml:"user"`
Env Env `yaml:"env"`
}
// ProbeHTTP is a probe with http.
type ProbeHTTP struct {
Scheme string `yaml:"scheme"`
Method string `yaml:"method"`
Host string `yaml:"host"`
Port string `yaml:"port"`
Path string `yaml:"path"`
Headers []Header `yaml:"headers"`
UserAgent string
Proxy URLWrapper `yaml:"proxy"`
}
// Header is a request header for http probe.
type Header struct {
Name string `yaml:"name"`
Value string `yaml:"value"`
}
// URLWrapper wraps url.URL
type URLWrapper struct {
*url.URL
}
// UnmarshalText decodes host status string
func (u *URLWrapper) UnmarshalText(text []byte) error {
var err error
u.URL, err = url.Parse(string(text))
return err
}
// ProbeTCP is a probe with tcp.
type ProbeTCP struct {
Host string `yaml:"host"`
Port string `yaml:"port"`
}