Merge pull request #588 from aledbf/avoid-multiple-reads

Read resolv.conf file just once
This commit is contained in:
Manuel Alejandro de Brito Fontes 2017-04-11 15:35:38 -03:00 committed by GitHub
commit 9ff3b86315
3 changed files with 11 additions and 17 deletions

View file

@ -41,6 +41,7 @@ import (
"k8s.io/ingress/controllers/nginx/pkg/version" "k8s.io/ingress/controllers/nginx/pkg/version"
"k8s.io/ingress/core/pkg/ingress" "k8s.io/ingress/core/pkg/ingress"
"k8s.io/ingress/core/pkg/ingress/defaults" "k8s.io/ingress/core/pkg/ingress/defaults"
"k8s.io/ingress/core/pkg/net/dns"
"k8s.io/ingress/core/pkg/net/ssl" "k8s.io/ingress/core/pkg/net/ssl"
) )
@ -69,10 +70,17 @@ func newNGINXController() ingress.Controller {
if ngx == "" { if ngx == "" {
ngx = binary ngx = binary
} }
h, err := dns.GetSystemNameServers()
if err != nil {
glog.Warningf("unexpected error reading system nameservers: %v", err)
}
n := &NGINXController{ n := &NGINXController{
binary: ngx, binary: ngx,
configmap: &api_v1.ConfigMap{}, configmap: &api_v1.ConfigMap{},
isIPV6Enabled: isIPv6Enabled(), isIPV6Enabled: isIPv6Enabled(),
resolver: h,
} }
var onChange func() var onChange func()
@ -125,6 +133,8 @@ type NGINXController struct {
// returns true if IPV6 is enabled in the pod // returns true if IPV6 is enabled in the pod
isIPV6Enabled bool isIPV6Enabled bool
resolver []net.IP
} }
// Start start a new NGINX master process running in foreground. // Start start a new NGINX master process running in foreground.
@ -340,6 +350,7 @@ func (n *NGINXController) OnUpdate(ingressCfg ingress.Configuration) ([]byte, er
} }
cfg := ngx_template.ReadConfig(n.configmap.Data) cfg := ngx_template.ReadConfig(n.configmap.Data)
cfg.Resolver = n.resolver
// we need to check if the status module configuration changed // we need to check if the status module configuration changed
if cfg.EnableVtsStatus { if cfg.EnableVtsStatus {

View file

@ -24,7 +24,6 @@ import (
"github.com/mitchellh/mapstructure" "github.com/mitchellh/mapstructure"
"k8s.io/ingress/controllers/nginx/pkg/config" "k8s.io/ingress/controllers/nginx/pkg/config"
"k8s.io/ingress/core/pkg/net/dns"
) )
const ( const (
@ -72,13 +71,6 @@ func ReadConfig(src map[string]string) config.Configuration {
to.SkipAccessLogURLs = skipUrls to.SkipAccessLogURLs = skipUrls
to.WhitelistSourceRange = whitelist to.WhitelistSourceRange = whitelist
h, err := dns.GetSystemNameServers()
if err != nil {
glog.Warningf("unexpected error reading system nameservers: %v", err)
} else {
to.Resolver = h
}
config := &mapstructure.DecoderConfig{ config := &mapstructure.DecoderConfig{
Metadata: nil, Metadata: nil,
WeaklyTypedInput: true, WeaklyTypedInput: true,

View file

@ -22,7 +22,6 @@ import (
"github.com/kylelemons/godebug/pretty" "github.com/kylelemons/godebug/pretty"
"k8s.io/ingress/controllers/nginx/pkg/config" "k8s.io/ingress/controllers/nginx/pkg/config"
"k8s.io/ingress/core/pkg/net/dns"
) )
func TestFilterErrors(t *testing.T) { func TestFilterErrors(t *testing.T) {
@ -54,26 +53,18 @@ func TestMergeConfigMapToStruct(t *testing.T) {
def.UseProxyProtocol = true def.UseProxyProtocol = true
def.GzipTypes = "text/html" def.GzipTypes = "text/html"
h, err := dns.GetSystemNameServers()
if err != nil {
t.Errorf("unexpected error: %v", err)
}
def.Resolver = h
to := ReadConfig(conf) to := ReadConfig(conf)
if diff := pretty.Compare(to, def); diff != "" { if diff := pretty.Compare(to, def); diff != "" {
t.Errorf("unexpected diff: (-got +want)\n%s", diff) t.Errorf("unexpected diff: (-got +want)\n%s", diff)
} }
def = config.NewDefault() def = config.NewDefault()
def.Resolver = h
to = ReadConfig(map[string]string{}) to = ReadConfig(map[string]string{})
if diff := pretty.Compare(to, def); diff != "" { if diff := pretty.Compare(to, def); diff != "" {
t.Errorf("unexpected diff: (-got +want)\n%s", diff) t.Errorf("unexpected diff: (-got +want)\n%s", diff)
} }
def = config.NewDefault() def = config.NewDefault()
def.Resolver = h
def.WhitelistSourceRange = []string{"1.1.1.1/32"} def.WhitelistSourceRange = []string{"1.1.1.1/32"}
to = ReadConfig(map[string]string{ to = ReadConfig(map[string]string{
"whitelist-source-range": "1.1.1.1/32", "whitelist-source-range": "1.1.1.1/32",