Skip to content

Commit 7f94358

Browse files
aledbfroboquat
authored andcommitted
[registry-facade] Refactor watch of configuration file
1 parent cc819bb commit 7f94358

File tree

1 file changed

+23
-61
lines changed
  • components/registry-facade/cmd

1 file changed

+23
-61
lines changed

components/registry-facade/cmd/run.go

Lines changed: 23 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55
package cmd
66

77
import (
8-
"crypto/sha256"
9-
"encoding/hex"
108
"fmt"
11-
"io"
129
"net"
1310
"net/http"
1411
"os"
@@ -32,6 +29,7 @@ import (
3229
"github.com/gitpod-io/gitpod/common-go/kubernetes"
3330
"github.com/gitpod-io/gitpod/common-go/log"
3431
"github.com/gitpod-io/gitpod/common-go/pprof"
32+
"github.com/gitpod-io/gitpod/common-go/watch"
3533
"github.com/gitpod-io/gitpod/registry-facade/api/config"
3634
"github.com/gitpod-io/gitpod/registry-facade/pkg/registry"
3735
)
@@ -150,7 +148,28 @@ var runCmd = &cobra.Command{
150148
log.WithError(err).Fatal("cannot create registry")
151149
}
152150

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+
154173
go func() {
155174
defer close(registryDoneChan)
156175
reg.MustServe()
@@ -199,60 +218,3 @@ func authorizerFromDockerConfig(cfg *configfile.ConfigFile) docker.Authorizer {
199218
return
200219
}))
201220
}
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

Comments
 (0)