Skip to content

Commit

Permalink
Merge pull request #7311 from QiWang19/service-reload
Browse files Browse the repository at this point in the history
Support sighup reload configuration files
  • Loading branch information
openshift-merge-robot authored Aug 19, 2020
2 parents 9d096c1 + 5b02b69 commit 7e2a1b3
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 0 deletions.
26 changes: 26 additions & 0 deletions cmd/podman/system/service_abi.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ package system
import (
"context"
"net"
"os"
"os/signal"
"strings"

"github.com/containers/podman/v2/cmd/podman/utils"
"github.com/containers/podman/v2/libpod"
api "github.com/containers/podman/v2/pkg/api/server"
"github.com/containers/podman/v2/pkg/domain/entities"
"github.com/containers/podman/v2/pkg/domain/infra"
Expand Down Expand Up @@ -39,6 +43,7 @@ func restService(opts entities.ServiceOptions, flags *pflag.FlagSet, cfg *entiti
return err
}

startWatcher(rt)
server, err := api.NewServerWithSettings(rt, opts.Timeout, listener)
if err != nil {
return err
Expand All @@ -55,3 +60,24 @@ func restService(opts entities.ServiceOptions, flags *pflag.FlagSet, cfg *entiti
}
return err
}

// startWatcher starts a new SIGHUP go routine for the current config.
func startWatcher(rt *libpod.Runtime) {
// Setup the signal notifier
ch := make(chan os.Signal, 1)
signal.Notify(ch, utils.SIGHUP)

go func() {
for {
// Block until the signal is received
logrus.Debugf("waiting for SIGHUP to reload configuration")
<-ch
if err := rt.Reload(); err != nil {
logrus.Errorf("unable to reload configuration: %v", err)
continue
}
}
}()

logrus.Debugf("registered SIGHUP watcher for config")
}
14 changes: 14 additions & 0 deletions cmd/podman/utils/signals_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// +build !windows

package utils

import (
"os"

"golang.org/x/sys/unix"
)

// Platform specific signal synonyms
var (
SIGHUP os.Signal = unix.SIGHUP
)
14 changes: 14 additions & 0 deletions cmd/podman/utils/signals_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// +build windows

package utils

import (
"os"

"golang.org/x/sys/windows"
)

// Platform specific signal synonyms
var (
SIGHUP os.Signal = windows.SIGHUP
)
49 changes: 49 additions & 0 deletions libpod/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ import (
"syscall"

"github.com/containers/common/pkg/config"
"github.com/containers/image/v5/pkg/sysregistriesv2"
is "github.com/containers/image/v5/storage"
"github.com/containers/image/v5/types"
"github.com/containers/podman/v2/libpod/define"
"github.com/containers/podman/v2/libpod/events"
"github.com/containers/podman/v2/libpod/image"
"github.com/containers/podman/v2/libpod/lock"
"github.com/containers/podman/v2/pkg/cgroups"
"github.com/containers/podman/v2/pkg/registries"
"github.com/containers/podman/v2/pkg/rootless"
"github.com/containers/podman/v2/pkg/util"
"github.com/containers/storage"
Expand Down Expand Up @@ -816,3 +818,50 @@ func (r *Runtime) mergeDBConfig(dbConfig *DBConfig) {
func (r *Runtime) EnableLabeling() bool {
return r.config.Containers.EnableLabeling
}

// Reload reloads the configurations files
func (r *Runtime) Reload() error {
if err := r.reloadContainersConf(); err != nil {
return err
}
if err := r.reloadStorageConf(); err != nil {
return err
}
if err := reloadRegistriesConf(); err != nil {
return err
}
return nil
}

// reloadContainersConf reloads the containers.conf
func (r *Runtime) reloadContainersConf() error {
config, err := config.Reload()
if err != nil {
return err
}
r.config = config
logrus.Infof("applied new containers configuration: %v", config)
return nil
}

// reloadRegistries reloads the registries.conf
func reloadRegistriesConf() error {
sysregistriesv2.InvalidateCache()
registries, err := sysregistriesv2.GetRegistries(&types.SystemContext{SystemRegistriesConfPath: registries.SystemRegistriesConfPath()})
if err != nil {
return err
}
logrus.Infof("applied new registry configuration: %+v", registries)
return nil
}

// reloadStorageConf reloads the storage.conf
func (r *Runtime) reloadStorageConf() error {
configFile, err := storage.DefaultConfigFile(rootless.IsRootless())
if err != nil {
return err
}
storage.ReloadConfigurationFile(configFile, &r.storageConfig)
logrus.Infof("applied new storage configuration: %v", r.storageConfig)
return nil
}

0 comments on commit 7e2a1b3

Please sign in to comment.