-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
probe.go
79 lines (71 loc) · 1.86 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
package health
import (
"fmt"
"net/url"
"strings"
)
type Probe struct {
Exec *ExecProbe `yaml:"exec,omitempty"`
HttpGet *HttpProbe `yaml:"http_get,omitempty"`
InitialDelay int `yaml:"initial_delay_seconds,omitempty"`
PeriodSeconds int `yaml:"period_seconds,omitempty"`
TimeoutSeconds int `yaml:"timeout_seconds,omitempty"`
SuccessThreshold int `yaml:"success_threshold,omitempty"`
FailureThreshold int `yaml:"failure_threshold,omitempty"`
}
type ExecProbe struct {
Command string `yaml:"command,omitempty"`
WorkingDir string `yaml:"working_dir,omitempty"`
}
type HttpProbe struct {
Host string `yaml:"host,omitempty"`
Path string `yaml:"path,omitempty"`
Scheme string `yaml:"scheme,omitempty"`
Port int `yaml:"port,omitempty"`
}
func (h HttpProbe) getUrl() (*url.URL, error) {
urlStr := ""
if h.Port != 0 {
urlStr = fmt.Sprintf("%s://%s:%d%s", h.Scheme, h.Host, h.Port, h.Path)
}
if h.Port == 0 {
urlStr = fmt.Sprintf("%s://%s%s", h.Scheme, h.Host, h.Path)
}
return url.Parse(urlStr)
}
func (p *Probe) validateAndSetDefaults() {
if p.InitialDelay < 0 {
p.InitialDelay = 0
}
if p.PeriodSeconds < 1 {
p.PeriodSeconds = 10
}
if p.TimeoutSeconds < 1 {
p.TimeoutSeconds = 1
}
if p.SuccessThreshold < 1 {
p.SuccessThreshold = 1
}
if p.FailureThreshold < 1 {
p.FailureThreshold = 3
}
p.validateAndSetHttpDefaults()
}
func (p *Probe) validateAndSetHttpDefaults() {
if p.HttpGet == nil {
return
}
if len(strings.TrimSpace(p.HttpGet.Host)) == 0 {
p.HttpGet.Host = "127.0.0.1"
}
if len(strings.TrimSpace(p.HttpGet.Scheme)) == 0 {
p.HttpGet.Scheme = "http"
}
if len(strings.TrimSpace(p.HttpGet.Path)) == 0 {
p.HttpGet.Path = "/"
}
if p.HttpGet.Port < 1 || p.HttpGet.Port > 65535 {
// if undefined or wrong value - will be treated as undefined
p.HttpGet.Port = 0
}
}