Skip to content

Commit

Permalink
Merge pull request #266 from web-seven/265-bugcfg-configuration-serve…
Browse files Browse the repository at this point in the history
…-issue

switched configuration serve to rjeczalik notify library
  • Loading branch information
evghen1 authored Jan 22, 2025
2 parents d171e4e + ebeef2f commit 1d3618d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 22 deletions.
2 changes: 2 additions & 0 deletions cmd/overlock/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ func (c *cli) AfterApply(ctx *kong.Context) error { //nolint:unparam
kubeClient, _ := kube.Client(config)
ctx.Bind(dynamicClient)
ctx.Bind(kubeClient)
} else {
return fmt.Errorf("kubernetes context is not accessible")
}

cfg := zap.NewDevelopmentConfig()
Expand Down
35 changes: 13 additions & 22 deletions internal/configuration/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"context"
"fmt"
"os"
"path/filepath"

"go.uber.org/zap"

cmv1 "github.com/crossplane/crossplane/apis/pkg/meta/v1"
"github.com/fsnotify/fsnotify"
"github.com/rjeczalik/notify"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/rest"
Expand All @@ -20,38 +21,28 @@ func Serve(ctx context.Context, dc *dynamic.DynamicClient, config *rest.Config,

loadServed(ctx, dc, config, logger, path)

watcher, err := fsnotify.NewWatcher()
if err != nil {
logger.Error(err)
c := make(chan notify.EventInfo, 1)

if err := notify.Watch(fmt.Sprintf("%s/%s", path, "..."), c, notify.Create, notify.Write, notify.Rename, notify.Remove); err != nil {
return err
}
defer watcher.Close()
defer notify.Stop(c)

go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
if event.Has(fsnotify.Write) {

logger.Debugf("Changed file: %s", event)
case ev := <-c:
fileExt := filepath.Ext(ev.Path())
if fileExt == ".yaml" {
logger.Debugf("Changed file: %s", ev)
loadServed(ctx, dc, config, logger, path)
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
logger.Error(err)
case <-ctx.Done():
return
}
}
}()

err = watcher.Add(path)
if err != nil {
logger.Error(err)
}

<-make(chan struct{})
return nil
}
Expand Down

0 comments on commit 1d3618d

Please sign in to comment.