forked from containers/podman
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] API middleware handler to update network list
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
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |