Skip to content

Commit 7c36dd0

Browse files
authored
Merge pull request #689 from Michcioperz/push-pslmrqtzxzks
Add option for node-cache to reload in-process CoreDNS via a signal.
2 parents 87e2164 + 9c1282a commit 7c36dd0

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

cmd/node-cache/app/cache_app.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ limitations under the License.
1414
package app
1515

1616
import (
17+
"fmt"
1718
"net"
1819
"os"
1920
"strings"
@@ -52,6 +53,7 @@ type ConfigParams struct {
5253
HealthPort string // port for the healthcheck
5354
SetupIptables bool
5455
SkipTeardown bool // Indicates whether the iptables rules and interface should be torn down
56+
ReloadWithSignal bool // Indicates config reload should be triggered with SIGUSR1, rather than expecting CoreDNS's reload plugin
5557
}
5658

5759
type iptablesRule struct {
@@ -69,6 +71,7 @@ type CacheApp struct {
6971
kubednsConfig *options.KubeDNSConfig
7072
exitChan chan struct{} // Channel to terminate background goroutines
7173
clusterDNSIP net.IP
74+
selfProcess *os.Process
7275
}
7376

7477
func isLockedErr(err error) bool {
@@ -277,6 +280,13 @@ func (c *CacheApp) RunApp() {
277280
// NewCacheApp returns a new instance of CacheApp by applying the specified config params.
278281
func NewCacheApp(params *ConfigParams) (*CacheApp, error) {
279282
c := &CacheApp{params: params, kubednsConfig: options.NewKubeDNSConfig()}
283+
if params.ReloadWithSignal {
284+
var err error
285+
c.selfProcess, err = os.FindProcess(os.Getpid())
286+
if err != nil {
287+
return nil, fmt.Errorf("failed to get process handle on self: %w", err)
288+
}
289+
}
280290
c.clusterDNSIP = net.ParseIP(os.ExpandEnv(toSvcEnv(params.UpstreamSvcName)))
281291
if c.clusterDNSIP == nil {
282292
clog.Warningf("Unable to lookup IP address of Upstream service %s, env %s `%s`", params.UpstreamSvcName, toSvcEnv(params.UpstreamSvcName), os.ExpandEnv(toSvcEnv(params.UpstreamSvcName)))

cmd/node-cache/app/configmap.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"os"
1919
"path"
2020
"strings"
21+
"syscall"
2122
"text/template"
2223
"time"
2324

@@ -132,6 +133,14 @@ func (c *CacheApp) updateCorefile(dnsConfig *config.Config) {
132133
}
133134
clog.Infof("Updated Corefile with %d custom stubdomains and upstream servers %s", len(dnsConfig.StubDomains), upstreamServers)
134135
clog.Infof("Using config file:\n%s", newConfig.String())
136+
137+
// Trigger reload of in-process CoreDNS
138+
if c.selfProcess != nil {
139+
clog.Infof("Sending reload signal to PID %v", c.selfProcess.Pid)
140+
if err := c.selfProcess.Signal(syscall.SIGUSR1); err != nil {
141+
clog.Errorf("Failed to trigger CoreDNS reload: %v", err)
142+
}
143+
}
135144
}
136145

137146
// syncInfo contains all parameters needed to watch a configmap directory for updates

cmd/node-cache/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ func parseAndValidateFlags() (*app.ConfigParams, error) {
9090
flag.StringVar(&params.UpstreamSvcName, "upstreamsvc", "kube-dns", "Service name whose cluster IP is upstream for node-cache")
9191
flag.StringVar(&params.HealthPort, "health-port", "8080", "port used by health plugin")
9292
flag.BoolVar(&params.SkipTeardown, "skipteardown", false, "indicates whether iptables rules should be torn down on exit")
93+
flag.BoolVar(&params.ReloadWithSignal, "reloadwithsignal", false, "use SIGUSR1 on self to reload CoreDNS")
9394
flag.Parse()
9495

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

0 commit comments

Comments
 (0)