in cmd/node-cache/app/configmap.go [84:130]
func (c *CacheApp) updateCorefile(dnsConfig *config.Config) {
// construct part of the Corefile
baseConfig, err := ioutil.ReadFile(c.params.BaseCoreFile)
if err != nil {
clog.Errorf("Failed to read node-cache coreFile %s - %v", c.params.BaseCoreFile, err)
setupErrCount.WithLabelValues("configmap").Inc()
return
}
stubDomainStr := getStubDomainStr(dnsConfig.StubDomains, &stubDomainInfo{Port: c.params.LocalPort, CacheTTL: defaultTTL,
LocalIP: strings.Replace(c.params.LocalIPStr, ",", " ", -1)})
upstreamServers := strings.Join(dnsConfig.UpstreamNameservers, " ")
if upstreamServers == "" {
// forward plugin supports both nameservers as well as resolv.conf
// use resolv.conf by default and use TCP for upstream.
upstreamServers = "/etc/resolv.conf"
baseConfig = bytes.Replace(baseConfig, []byte(UpstreamServerVar), []byte(upstreamServers), -1)
} else {
// Use UDP to connect to custom upstream DNS servers.
upstreamUDP := bytes.Replace([]byte(upstreamUDPBlock), []byte(UpstreamServerVar), []byte(upstreamServers), -1)
// In case upstream was configured for TCP in the existing config, change to UDP since we now have custom upstream
baseConfig = bytes.Replace(baseConfig, []byte(upstreamTCPBlock), upstreamUDP, -1)
// Just in case previous replace failed due to different indentation in config file or existing config was
// already using UDP, this step will put in the correct upstream servers.
if bytes.Contains(baseConfig, []byte(UpstreamServerVar)) {
clog.Warningf("Did not find TCP upstream block to replace, assuming upstreams already use UDP.")
baseConfig = bytes.Replace(baseConfig, []byte(UpstreamServerVar), []byte(upstreamServers), -1)
}
}
baseConfig = bytes.Replace(baseConfig, []byte(UpstreamClusterDNSVar), []byte(c.clusterDNSIP.String()), -1)
baseConfig = bytes.Replace(baseConfig, []byte(LocalListenIPsVar), []byte(strings.Replace(c.params.LocalIPStr, ",", " ", -1)), -1)
// All Listen IP Substitutions should have happened with replacing "LocalListenIPsVar". This is to ensure that no
// variables are left unsubstituted.
if bytes.Contains(baseConfig, []byte(LocalDNSServerVar)) {
baseConfig = bytes.Replace(baseConfig, []byte(LocalDNSServerVar), []byte(""), -1)
}
newConfig := bytes.Buffer{}
newConfig.WriteString(string(baseConfig))
newConfig.WriteString(stubDomainStr)
if err := ioutil.WriteFile(c.params.CoreFile, newConfig.Bytes(), 0666); err != nil {
clog.Errorf("Failed to write config file %s - err %v", c.params.CoreFile, err)
setupErrCount.WithLabelValues("configmap").Inc()
return
}
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())
}