-
Notifications
You must be signed in to change notification settings - Fork 5
/
health_check_unmarshal.go
69 lines (66 loc) · 1.79 KB
/
health_check_unmarshal.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
package gtm
import (
"crypto/tls"
"encoding/json"
"fmt"
"net/http"
"time"
)
//UnmarshalJSON Record parsing the data
func (r *Record) UnmarshalJSON(data []byte) error {
var v struct {
Name string `json:"name"`
IPs []IP `json:"ips"`
TTL int `json:"ttl"`
HealthCheckConfig HealthCheckConfig `json:"health_check"`
}
if err := json.Unmarshal(data, &v); err != nil {
return err
}
r.Name = v.Name
r.IPs = v.IPs
r.TTL = v.TTL
r.HealthCheckConfig = v.HealthCheckConfig
var hk HealthCheckMethod
if r.HealthCheckConfig.Type == "layer4" {
frequency, err := time.ParseDuration(r.HealthCheckConfig.Fequency)
if err != nil {
return fmt.Errorf("format of frequency is not valid: %v", err)
}
hk = Layer4HealthCheck(r.HealthCheckConfig.PORT)
r.HealthCheck = &DefaultHealthCheck{
EndPoints: r.IPs,
Frequency: frequency,
CheckHealth: hk,
SleepFunc: sleepDuration(frequency),
}
} else if r.HealthCheckConfig.Type == "layer7" {
frequency, err := time.ParseDuration(r.HealthCheckConfig.Fequency)
if err != nil {
return fmt.Errorf("format of frequency is not valid: %v", err)
}
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: r.HealthCheckConfig.SkipSSL},
}
client := &http.Client{Transport: tr}
var schema string
if r.HealthCheckConfig.HTTPS {
schema = "https"
} else {
schema = "http"
}
hk = Layer7HealthCheck(client, schema, r.HealthCheckConfig.PATH, r.HealthCheckConfig.HTTPStatusCode)
r.HealthCheck = &DefaultHealthCheck{
EndPoints: r.IPs,
Frequency: frequency,
CheckHealth: hk,
SleepFunc: sleepDuration(frequency),
}
} else {
r.HealthCheck = &DoNothingHealthCheck{
ips: r.IPs,
}
}
r.HealthCheck.Start()
return nil
}