-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
128 lines (115 loc) · 2.84 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
118
119
120
121
122
123
124
125
126
127
128
package greenlight
import (
"context"
"fmt"
"io"
"net/http"
"net/url"
"os"
"strings"
"time"
awsConfig "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/goccy/go-yaml"
)
const (
DefaultCheckInterval = 6 * time.Second
DefaultCheckTimeout = 5 * time.Second
DefaultListenAddr = ":8080"
)
type Config struct {
Responder *ResponderConfig `yaml:"responder"`
StartUp *PhaseConfig `yaml:"startup"`
Readiness *PhaseConfig `yaml:"readiness"`
}
type ResponderConfig struct {
Addr string `yaml:"addr"`
}
type PhaseConfig struct {
Checks []*CheckConfig `yaml:"checks"`
Interval time.Duration `yaml:"interval"`
GracePeriod time.Duration `yaml:"grace_period"`
}
type CheckConfig struct {
Name string `yaml:"name"`
Timeout time.Duration `yaml:"timeout"`
Command *CommandCheckConfig `yaml:"command"`
TCP *TCPCheckConfig `yaml:"tcp"`
HTTP *HTTPCheckConfig `yaml:"http"`
}
func LoadConfig(ctx context.Context, src string) (*Config, error) {
config := &Config{
StartUp: &PhaseConfig{
Interval: DefaultCheckInterval,
},
Readiness: &PhaseConfig{
Interval: DefaultCheckInterval,
},
Responder: &ResponderConfig{
Addr: DefaultListenAddr,
},
}
b, err := loadURL(ctx, src)
if err != nil {
return nil, err
}
if err = yaml.Unmarshal(b, config); err != nil {
return nil, err
}
for _, c := range config.StartUp.Checks {
if c.Timeout == 0 {
c.Timeout = DefaultCheckTimeout
}
}
for _, c := range config.Readiness.Checks {
if c.Timeout == 0 {
c.Timeout = DefaultCheckTimeout
}
}
return config, nil
}
func loadURL(ctx context.Context, s string) ([]byte, error) {
u, err := url.Parse(s)
if err != nil {
return nil, fmt.Errorf("invalid url %s: %w", s, err)
}
switch u.Scheme {
case "http", "https":
return loadHTTP(ctx, u)
case "file", "": // empty scheme is treated as file
return os.ReadFile(u.Path)
case "s3":
return loadS3(ctx, u)
default:
return nil, fmt.Errorf("invalid url %s: scheme must be http, https, file, or s3", s)
}
}
func loadHTTP(ctx context.Context, u *url.URL) ([]byte, error) {
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
return nil, fmt.Errorf("http get failed: %w", err)
}
resp, err := http.DefaultClient.Do(req.WithContext(ctx))
if err != nil {
return nil, fmt.Errorf("http get failed: %w", err)
}
defer resp.Body.Close()
return io.ReadAll(resp.Body)
}
func loadS3(ctx context.Context, u *url.URL) ([]byte, error) {
awscfg, err := awsConfig.LoadDefaultConfig(ctx)
if err != nil {
return nil, err
}
svc := s3.NewFromConfig(awscfg)
bucket, key := u.Host, strings.TrimPrefix(u.Path, "/")
out, err := svc.GetObject(ctx, &s3.GetObjectInput{
Bucket: &bucket,
Key: &key,
})
if err != nil {
return nil, err
}
defer out.Body.Close()
return io.ReadAll(out.Body)
}