Skip to content

Commit

Permalink
feat: client config file listen event file change
Browse files Browse the repository at this point in the history
  • Loading branch information
fgouteroux committed Jan 22, 2025
1 parent b0e813e commit d09f7be
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 deletions.
1 change: 1 addition & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func CheckAndDeployLocalCertificate(logger log.Logger, acmeClient *restclient.Cl
return
}

_ = level.Info(logger).Log("msg", "Checking local certificates with remote server")
certificates, err := acmeClient.GetAllCertificateMetadata()
if err != nil {
_ = level.Error(logger).Log("err", err)
Expand Down
50 changes: 45 additions & 5 deletions client/watcher.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,64 @@
package client

import (
"fmt"
"os"
"path/filepath"
"time"

"github.com/fsnotify/fsnotify"
"github.com/go-kit/log"
"github.com/go-kit/log/level"

"github.com/fgouteroux/acme_manager/restclient"
)

func WatchCertificate(logger log.Logger, interval time.Duration, configPath string, acmeClient *restclient.Client) {
func WatchLocalCertificate(logger log.Logger, interval time.Duration, acmeClient *restclient.Client) {
// create a new Ticker
tk := time.NewTicker(interval)

// start the ticker
for range tk.C {

// Compare and create/update certificate from config file to remote server
CheckCertificate(logger, configPath, acmeClient)

// check local certificate are up-to-date
CheckAndDeployLocalCertificate(logger, acmeClient)
}
}

func WatchCertificateUpdate(logger log.Logger, configPath string, acmeClient *restclient.Client) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
_ = level.Error(logger).Log("err", err)
os.Exit(1)
}
defer watcher.Close()

fileName := filepath.Base(configPath)
if !filepath.IsAbs(configPath) {
fileName = "./" + fileName
}

// watch the parent dir of the file to catch changes
err = watcher.Add(filepath.Dir(configPath))
if err != nil {
_ = level.Error(logger).Log("err", err)
os.Exit(1)
}

for {
select {
case event := <-watcher.Events:
// only work on WRITE events of the original filename
if event.Op&fsnotify.Write == fsnotify.Write && event.Name == fileName {
_ = level.Info(logger).Log("msg", fmt.Sprintf("modified file: %s", configPath))

// Compare and create/update certificate from config file to remote server
CheckCertificate(logger, configPath, acmeClient)

// check local certificate are up-to-date
CheckAndDeployLocalCertificate(logger, acmeClient)
}
case err := <-watcher.Errors:
_ = level.Error(logger).Log("err", err)
}
}
}
10 changes: 7 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,17 @@ func main() {
_ = level.Error(logger).Log("err", err)
os.Exit(1)
}
// Compare and create/update certificate from config file to remote server
// On startup compare and create/update certificate from config file to remote server
client.CheckCertificate(logger, *clientConfigPath, acmeClient)

// check local certificate are up-to-date
// on startup check local certificate are up-to-date
client.CheckAndDeployLocalCertificate(logger, acmeClient)

go client.WatchCertificate(logger, *clientCheckConfigInterval, *clientConfigPath, acmeClient)
// periodically check local certificate are up-to-date
go client.WatchLocalCertificate(logger, *clientCheckConfigInterval, acmeClient)

// listen for config file event change
go client.WatchCertificateUpdate(logger, *clientConfigPath, acmeClient)

http.Handle("/metrics", promhttp.Handler())

Expand Down

0 comments on commit d09f7be

Please sign in to comment.