Skip to content

Add option for node-cache to reload in-process CoreDNS via a signal. #689

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions cmd/node-cache/app/cache_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ limitations under the License.
package app

import (
"fmt"
"net"
"os"
"strings"
Expand Down Expand Up @@ -52,6 +53,7 @@ type ConfigParams struct {
HealthPort string // port for the healthcheck
SetupIptables bool
SkipTeardown bool // Indicates whether the iptables rules and interface should be torn down
ReloadWithSignal bool // Indicates config reload should be triggered with SIGUSR1, rather than expecting CoreDNS's reload plugin
}

type iptablesRule struct {
Expand All @@ -69,6 +71,7 @@ type CacheApp struct {
kubednsConfig *options.KubeDNSConfig
exitChan chan struct{} // Channel to terminate background goroutines
clusterDNSIP net.IP
selfProcess *os.Process
}

func isLockedErr(err error) bool {
Expand Down Expand Up @@ -277,6 +280,13 @@ func (c *CacheApp) RunApp() {
// NewCacheApp returns a new instance of CacheApp by applying the specified config params.
func NewCacheApp(params *ConfigParams) (*CacheApp, error) {
c := &CacheApp{params: params, kubednsConfig: options.NewKubeDNSConfig()}
if params.ReloadWithSignal {
var err error
c.selfProcess, err = os.FindProcess(os.Getpid())
if err != nil {
return nil, fmt.Errorf("failed to get process handle on self: %w", err)
}
}
c.clusterDNSIP = net.ParseIP(os.ExpandEnv(toSvcEnv(params.UpstreamSvcName)))
if c.clusterDNSIP == nil {
clog.Warningf("Unable to lookup IP address of Upstream service %s, env %s `%s`", params.UpstreamSvcName, toSvcEnv(params.UpstreamSvcName), os.ExpandEnv(toSvcEnv(params.UpstreamSvcName)))
Expand Down
9 changes: 9 additions & 0 deletions cmd/node-cache/app/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"os"
"path"
"strings"
"syscall"
"text/template"
"time"

Expand Down Expand Up @@ -132,6 +133,14 @@ func (c *CacheApp) updateCorefile(dnsConfig *config.Config) {
}
clog.Infof("Updated Corefile with %d custom stubdomains and upstream servers %s", len(dnsConfig.StubDomains), upstreamServers)
clog.Infof("Using config file:\n%s", newConfig.String())

// Trigger reload of in-process CoreDNS
if c.selfProcess != nil {
clog.Infof("Sending reload signal to PID %v", c.selfProcess.Pid)
if err := c.selfProcess.Signal(syscall.SIGUSR1); err != nil {
clog.Errorf("Failed to trigger CoreDNS reload: %v", err)
}
}
}

// syncInfo contains all parameters needed to watch a configmap directory for updates
Expand Down
1 change: 1 addition & 0 deletions cmd/node-cache/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func parseAndValidateFlags() (*app.ConfigParams, error) {
flag.StringVar(&params.UpstreamSvcName, "upstreamsvc", "kube-dns", "Service name whose cluster IP is upstream for node-cache")
flag.StringVar(&params.HealthPort, "health-port", "8080", "port used by health plugin")
flag.BoolVar(&params.SkipTeardown, "skipteardown", false, "indicates whether iptables rules should be torn down on exit")
flag.BoolVar(&params.ReloadWithSignal, "reloadwithsignal", false, "use SIGUSR1 on self to reload CoreDNS")
flag.Parse()

for _, ipstr := range strings.Split(params.LocalIPStr, ",") {
Expand Down