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 fc3f552
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
12 changes: 12 additions & 0 deletions cmd/contour/contour.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

"github.com/alecthomas/kingpin/v2"
resource_v3 "github.com/envoyproxy/go-control-plane/pkg/resource/v3"
"github.com/fsnotify/fsnotify"
"github.com/sirupsen/logrus"
"go.uber.org/automaxprocs/maxprocs"

Expand Down Expand Up @@ -178,9 +179,20 @@ func main() {
log.WithError(err).Fatal("unable to initialize Server dependencies required to start Contour")
}

// 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, log)
if err != nil {
log.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)

if err := serve.doServe(); err != nil {
log.WithError(err).Fatal("Contour server failed")
}
return
case version.FullCommand():
println(build.PrintBuildInfo())
default:
Expand Down
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.Logger) (*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
}

0 comments on commit fc3f552

Please sign in to comment.