Skip to content

Commit

Permalink
[WIP] API middleware handler to update network list
Browse files Browse the repository at this point in the history
API middleware handler to update memory list of networks if
Config.Network.NetworkConfigDir is newer.

Fixes containers#11828

Signed-off-by: Jhon Honce <[email protected]>
  • Loading branch information
jwhonce committed Oct 1, 2021
1 parent 1de96f2 commit 9f9ec93
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions pkg/api/server/handler_cache_networks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package server

import (
"net/http"
"os"
"sync"

"github.com/containers/podman/v3/libpod"
api "github.com/containers/podman/v3/pkg/api/types"
"github.com/gorilla/mux"
"github.com/sirupsen/logrus"
)

var (
previousNetworkUpdate os.FileInfo
networkUpdateSync sync.Once
)

// cacheNetworksHandler refreshes the in-memory network cache if the
// network configuration directory has been changed
func cacheNetworksHandler() mux.MiddlewareFunc {
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime)
cfg, err := runtime.GetConfig()
if err != nil {
logrus.Infof("Failed to retrieve podman configuration: %v", err)
} else {
netCfg := cfg.Network.NetworkConfigDir

// First pass of this handler is for initialization.
// 1+ passes will update memory network cache/configuration if the modification
// time of the network configuration directory changes.
networkUpdateSync.Do(func() {
var err error
previousNetworkUpdate, err = os.Stat(netCfg)
logrus.Infof("Failed to access %q: %v", netCfg, err)
})

if now, err := os.Stat(netCfg); err != nil {
logrus.Infof("Failed to access %q: %v", netCfg, err)
} else {
if now.ModTime().After(previousNetworkUpdate.ModTime()) {
//
// Reload network cache/configuration
//
previousNetworkUpdate = now
}
}
}

h.ServeHTTP(w, r)
})
}
}

0 comments on commit 9f9ec93

Please sign in to comment.