@@ -13,6 +13,8 @@ import (
13
13
"time"
14
14
15
15
"github.com/fsnotify/fsnotify"
16
+ ycsdk "github.com/yandex-cloud/go-sdk"
17
+ "github.com/yandex-cloud/go-sdk/iamkey"
16
18
17
19
"github.com/sputnik-systems/prom-dns-http-sd/pkg/storage"
18
20
"github.com/sputnik-systems/prom-dns-http-sd/pkg/storage/yandexcloud"
@@ -25,21 +27,33 @@ type SDConfig struct {
25
27
26
28
type SDConfigs []SDConfig
27
29
28
- var mu sync.Mutex
29
- var config * storage.Config
30
- var client storage.Client
31
- var responseData map [string ]SDConfigs
30
+ type params struct {
31
+ mu sync.Mutex
32
+ config * storage.Config
33
+ client storage.Client
34
+ responseData map [string ]SDConfigs
32
35
33
- var configFilePath = flag .String ("config-path" , "" , "application config file path" )
34
- var ycAuthJsonFilePath = flag .String ("yc-auth-json-file-path" , "" , "Yandex.Cloud iam.json file path" )
35
- var dataUpdateInterval = flag .String ("data-update-interval" , "5m" , "Interval between targets data updating" )
36
+ flags flags
37
+ }
38
+
39
+ type flags struct {
40
+ configFilePath , ycAuthJsonFilePath , dataUpdateInterval * string
41
+ }
42
+
43
+ var p = params {
44
+ flags : flags {
45
+ configFilePath : flag .String ("config-path" , "" , "application config file path" ),
46
+ ycAuthJsonFilePath : flag .String ("yc-auth-json-file-path" , "" , "Yandex.Cloud iam.json file path" ),
47
+ dataUpdateInterval : flag .String ("data-update-interval" , "1h" , "Interval between targets data updating" ),
48
+ },
49
+ }
36
50
37
51
func Run () error {
38
52
flag .Parse ()
39
53
40
54
ctx := context .Background ()
41
55
42
- interval , err := time .ParseDuration (* dataUpdateInterval )
56
+ interval , err := time .ParseDuration (* p . flags . dataUpdateInterval )
43
57
if err != nil {
44
58
return errors .New ("incorrect duration format" )
45
59
}
@@ -51,13 +65,17 @@ func Run() error {
51
65
go configFileUpdater (ctx )
52
66
}
53
67
68
+ http .HandleFunc ("/healthz" , healthCheck )
54
69
http .HandleFunc ("/" , giveResponse )
55
70
56
71
return http .ListenAndServe (":8080" , nil )
57
72
}
58
73
74
+ func healthCheck (w http.ResponseWriter , r * http.Request ) {
75
+ }
76
+
59
77
func giveResponse (w http.ResponseWriter , r * http.Request ) {
60
- if body , ok := responseData [r .URL .Path ]; ok {
78
+ if body , ok := p . responseData [r .URL .Path ]; ok {
61
79
resp , err := json .Marshal (body )
62
80
if err != nil {
63
81
w .WriteHeader (404 )
@@ -80,7 +98,7 @@ func configFileUpdater(ctx context.Context) {
80
98
}
81
99
defer watcher .Close ()
82
100
83
- err = watcher .Add (* configFilePath )
101
+ err = watcher .Add (* p . flags . configFilePath )
84
102
if err != nil {
85
103
log .Fatal (err )
86
104
}
@@ -95,15 +113,10 @@ func configFileUpdater(ctx context.Context) {
95
113
if event .Has (fsnotify .Write ) {
96
114
log .Println ("modified file:" , event .Name )
97
115
98
- config , err = storage .GetConfig (* configFilePath )
99
- if err != nil {
100
- log .Printf ("failed to get config: %s" , err )
116
+ if err := updateConfigAndClient (ctx ); err != nil {
117
+ log .Printf ("failed to update config and client: %s" , err )
101
118
}
102
119
103
- client , err = yandexcloud .NewClient (ctx , "./iam.json" , config )
104
- if err != nil {
105
- log .Printf ("failed to initialize client: %s" , err )
106
- }
107
120
}
108
121
case err , ok := <- watcher .Errors :
109
122
if ! ok {
@@ -116,13 +129,13 @@ func configFileUpdater(ctx context.Context) {
116
129
}
117
130
118
131
func responseDataUpdater (ctx context.Context ) {
119
- zones , err := client .ListZones (ctx , config .Zones ... )
132
+ zones , err := p . client .ListZones (ctx , p . config .Zones ... )
120
133
if err != nil {
121
134
log .Printf ("failed to list zones: %s" , err )
122
135
}
123
136
124
137
sds := make (map [string ]SDConfigs )
125
- for _ , rule := range config .Rules {
138
+ for _ , rule := range p . config .Rules {
126
139
if _ , ok := sds [rule .Path ]; ! ok {
127
140
sds [rule .Path ] = make (SDConfigs , 0 )
128
141
}
@@ -145,24 +158,17 @@ func responseDataUpdater(ctx context.Context) {
145
158
}
146
159
}
147
160
148
- mu .Lock ()
149
- responseData = sds
150
- mu .Unlock ()
161
+ p . mu .Lock ()
162
+ p . responseData = sds
163
+ p . mu .Unlock ()
151
164
}
152
165
153
166
func responseDataUpdateTicker (ctx context.Context , interval time.Duration ) {
154
167
ticker := time .NewTicker (interval )
155
168
defer ticker .Stop ()
156
169
157
- var err error
158
- config , err = storage .GetConfig (* configFilePath )
159
- if err != nil {
160
- log .Printf ("failed to get config: %s" , err )
161
- }
162
-
163
- client , err = yandexcloud .NewClient (ctx , * ycAuthJsonFilePath , config )
164
- if err != nil {
165
- log .Printf ("failed to initialize client: %s" , err )
170
+ if err := updateConfigAndClient (ctx ); err != nil {
171
+ log .Printf ("failed to update config and client: %s" , err )
166
172
}
167
173
168
174
responseDataUpdater (ctx )
@@ -174,3 +180,45 @@ func responseDataUpdateTicker(ctx context.Context, interval time.Duration) {
174
180
}
175
181
}
176
182
}
183
+
184
+ func updateConfigAndClient (ctx context.Context ) error {
185
+ var err error
186
+
187
+ p .mu .Lock ()
188
+
189
+ p .config , err = storage .GetConfig (* p .flags .configFilePath )
190
+ if err != nil {
191
+ return fmt .Errorf ("failed to get config: %w" , err )
192
+ }
193
+
194
+ var creds ycsdk.Credentials
195
+ switch {
196
+ case * p .flags .ycAuthJsonFilePath != "" :
197
+ key , err := iamkey .ReadFromJSONFile (* p .flags .ycAuthJsonFilePath )
198
+ if err != nil {
199
+ return err
200
+ }
201
+
202
+ if creds , err = ycsdk .ServiceAccountKey (key ); err != nil {
203
+ return err
204
+ }
205
+ default :
206
+ creds = ycsdk .InstanceServiceAccount ()
207
+ }
208
+
209
+ sdk , err := ycsdk .Build (ctx , ycsdk.Config {
210
+ Credentials : creds ,
211
+ })
212
+ if err != nil {
213
+ return fmt .Errorf ("failed to build yc sdk: %w" , err )
214
+ }
215
+
216
+ p .client , err = yandexcloud .NewClient (ctx , sdk , p .config )
217
+ if err != nil {
218
+ return fmt .Errorf ("failed to initialize client: %w" , err )
219
+ }
220
+
221
+ p .mu .Unlock ()
222
+
223
+ return nil
224
+ }
0 commit comments