5
5
package cmd
6
6
7
7
import (
8
- "crypto/sha256"
9
- "encoding/hex"
10
8
"fmt"
11
- "io"
12
9
"net"
13
10
"net/http"
14
11
"os"
@@ -32,6 +29,7 @@ import (
32
29
"github.com/gitpod-io/gitpod/common-go/kubernetes"
33
30
"github.com/gitpod-io/gitpod/common-go/log"
34
31
"github.com/gitpod-io/gitpod/common-go/pprof"
32
+ "github.com/gitpod-io/gitpod/common-go/watch"
35
33
"github.com/gitpod-io/gitpod/registry-facade/api/config"
36
34
"github.com/gitpod-io/gitpod/registry-facade/pkg/registry"
37
35
)
@@ -150,7 +148,28 @@ var runCmd = &cobra.Command{
150
148
log .WithError (err ).Fatal ("cannot create registry" )
151
149
}
152
150
153
- go watchConfig (configPath , reg )
151
+ ctx , cancel := context .WithCancel (context .Background ())
152
+ defer cancel ()
153
+
154
+ err = watch .File (ctx , configPath , func () {
155
+ ctx , cancel := context .WithTimeout (context .Background (), 60 * time .Second )
156
+ defer cancel ()
157
+
158
+ cfg , err := config .GetConfig (configPath )
159
+ if err != nil {
160
+ log .WithError (err ).Warn ("cannot reload configuration" )
161
+ return
162
+ }
163
+
164
+ err = reg .UpdateStaticLayer (ctx , cfg .Registry .StaticLayer )
165
+ if err != nil {
166
+ log .WithError (err ).Warn ("cannot reload configuration" )
167
+ }
168
+ })
169
+ if err != nil {
170
+ log .WithError (err ).Fatal ("cannot start watch of configuration file" )
171
+ }
172
+
154
173
go func () {
155
174
defer close (registryDoneChan )
156
175
reg .MustServe ()
@@ -199,60 +218,3 @@ func authorizerFromDockerConfig(cfg *configfile.ConfigFile) docker.Authorizer {
199
218
return
200
219
}))
201
220
}
202
-
203
- // watchConfig watches the configuration file and if changed reloads the static layer
204
- func watchConfig (fn string , reg * registry.Registry ) {
205
- hashConfig := func () (hash string , err error ) {
206
- f , err := os .Open (fn )
207
- if err != nil {
208
- return "" , err
209
- }
210
- defer f .Close ()
211
-
212
- h := sha256 .New ()
213
- _ , err = io .Copy (h , f )
214
- if err != nil {
215
- return "" , err
216
- }
217
-
218
- return hex .EncodeToString (h .Sum (nil )), nil
219
- }
220
- reloadConfig := func () error {
221
- cfg , err := config .GetConfig (fn )
222
- if err != nil {
223
- return err
224
- }
225
-
226
- ctx , cancel := context .WithTimeout (context .Background (), 60 * time .Second )
227
- defer cancel ()
228
-
229
- return reg .UpdateStaticLayer (ctx , cfg .Registry .StaticLayer )
230
- }
231
-
232
- var (
233
- tick = time .NewTicker (30 * time .Second )
234
- oldHash string
235
- )
236
- defer tick .Stop ()
237
- for range tick .C {
238
- currentHash , err := hashConfig ()
239
- if err != nil {
240
- log .WithError (err ).Warn ("cannot check if config has changed" )
241
- }
242
-
243
- if oldHash == "" {
244
- oldHash = currentHash
245
- }
246
- if currentHash == oldHash {
247
- continue
248
- }
249
- oldHash = currentHash
250
-
251
- err = reloadConfig ()
252
- if err == nil {
253
- log .Info ("configuration was updated - reloaded static layer config" )
254
- } else {
255
- log .WithError (err ).Error ("cannot reload config - config hot reloading did not work" )
256
- }
257
- }
258
- }
0 commit comments