Skip to content

Commit

Permalink
Add fsnotify to monitor the configmap path
Browse files Browse the repository at this point in the history
Signed-off-by: lubronzhan <lubronzhan@gmail.com>
  • Loading branch information
lubronzhan committed Feb 17, 2024
1 parent d03b6d7 commit 50eac9d
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
50 changes: 50 additions & 0 deletions cmd/contour/filewatcher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright Project Contour Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"github.com/fsnotify/fsnotify"
"github.com/sirupsen/logrus"
)

const defaultConfigPath = "/config"

// set up a filesystem watcher for the mounted files
// right now just the configMap
// reboot the app whenever there is an update via the returned stopCh.
func initializeWatch(path string, log logrus.FieldLogger) (*fsnotify.Watcher, error) {
watch, err := fsnotify.NewWatcher()
if err != nil {
log.Fatalln("fail to setup config watcher")
}
go func() {
for {
select {
case err := <-watch.Errors:
log.Warningf("watcher receives err: %v\n", err)
case event := <-watch.Events:
if event.Op != fsnotify.Chmod {
log.Fatalf("restarting contour because received event %v on file %s\n", event.Op, defaultConfigPath)
} else {
log.Printf("watcher receives %s on the mounted file %s\n", event.Op.String(), event.Name)
}
}
}
}()

if err := watch.Add(path); err != nil {
log.Fatalf("fail to watch contour config file %s\n", path)
}
return watch, err
}
12 changes: 12 additions & 0 deletions cmd/contour/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/alecthomas/kingpin/v2"
envoy_cache_v3 "github.com/envoyproxy/go-control-plane/pkg/cache/v3"
envoy_server_v3 "github.com/envoyproxy/go-control-plane/pkg/server/v3"
"github.com/fsnotify/fsnotify"
"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
core_v1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -750,6 +751,17 @@ func (s *Server) doServe() error {
return err
}

// Start a process to monitor the path of mounted configMap `/config`
// so that it can restart contour if the configMap gets updated.

watch, err := initializeWatch(defaultConfigPath, s.log.WithField("context", "fsnotify-watcher"))
if err != nil {
s.log.WithField("context", "fsnotify-watcher").Fatalf("fail to initialize watch on configMap path '/config': %v\n", err)
}
defer func(watch *fsnotify.Watcher) {
_ = watch.Close() // ignore explicitly when the watch closes
}(watch)

// GO!
return s.mgr.Start(signals.SetupSignalHandler())
}
Expand Down

0 comments on commit 50eac9d

Please sign in to comment.