Enhance REST provider

This commit is contained in:
Daniel Tomcej 2019-07-12 17:24:03 -06:00 committed by Traefiker Bot
parent 48d98dcf45
commit 51486b18fa
5 changed files with 94 additions and 40 deletions

View file

@ -32,41 +32,81 @@ func (s *RestSuite) TestSimpleConfiguration(c *check.C) {
err = try.GetRequest("http://127.0.0.1:8000/", 1000*time.Millisecond, try.StatusCodeIs(http.StatusNotFound)) err = try.GetRequest("http://127.0.0.1:8000/", 1000*time.Millisecond, try.StatusCodeIs(http.StatusNotFound))
c.Assert(err, checker.IsNil) c.Assert(err, checker.IsNil)
config := dynamic.HTTPConfiguration{ testCase := []struct {
Routers: map[string]*dynamic.Router{ desc string
"router1": { config *dynamic.Configuration
EntryPoints: []string{"web"}, ruleMatch string
Middlewares: []string{}, }{
Service: "service1", {
Rule: "PathPrefix(`/`)", desc: "deploy http configuration",
}, config: &dynamic.Configuration{
}, HTTP: &dynamic.HTTPConfiguration{
Services: map[string]*dynamic.Service{ Routers: map[string]*dynamic.Router{
"service1": { "router1": {
LoadBalancer: &dynamic.LoadBalancerService{ EntryPoints: []string{"web"},
Servers: []dynamic.Server{ Middlewares: []string{},
{ Service: "service1",
URL: "http://" + s.composeProject.Container(c, "whoami1").NetworkSettings.IPAddress + ":80", Rule: "PathPrefix(`/`)",
},
},
Services: map[string]*dynamic.Service{
"service1": {
LoadBalancer: &dynamic.LoadBalancerService{
Servers: []dynamic.Server{
{
URL: "http://" + s.composeProject.Container(c, "whoami1").NetworkSettings.IPAddress + ":80",
},
},
},
}, },
}, },
}, },
}, },
ruleMatch: "PathPrefix(`/`)",
},
{
desc: "deploy tcp configuration",
config: &dynamic.Configuration{
TCP: &dynamic.TCPConfiguration{
Routers: map[string]*dynamic.TCPRouter{
"router1": {
EntryPoints: []string{"web"},
Service: "service1",
Rule: "HostSNI(`*`)",
},
},
Services: map[string]*dynamic.TCPService{
"service1": {
LoadBalancer: &dynamic.TCPLoadBalancerService{
Servers: []dynamic.TCPServer{
{
Address: s.composeProject.Container(c, "whoami1").NetworkSettings.IPAddress + ":80",
},
},
},
},
},
},
},
ruleMatch: "HostSNI(`*`)",
}, },
} }
json, err := json.Marshal(config) for _, test := range testCase {
c.Assert(err, checker.IsNil) json, err := json.Marshal(test.config)
c.Assert(err, checker.IsNil)
request, err := http.NewRequest(http.MethodPut, "http://127.0.0.1:8080/api/providers/rest", bytes.NewReader(json)) request, err := http.NewRequest(http.MethodPut, "http://127.0.0.1:8080/api/providers/rest", bytes.NewReader(json))
c.Assert(err, checker.IsNil) c.Assert(err, checker.IsNil)
response, err := http.DefaultClient.Do(request) response, err := http.DefaultClient.Do(request)
c.Assert(err, checker.IsNil) c.Assert(err, checker.IsNil)
c.Assert(response.StatusCode, checker.Equals, http.StatusOK) c.Assert(response.StatusCode, checker.Equals, http.StatusOK)
err = try.GetRequest("http://127.0.0.1:8080/api/rawdata", 1000*time.Millisecond, try.BodyContains("PathPrefix(`/`)")) err = try.GetRequest("http://127.0.0.1:8080/api/rawdata", 1000*time.Millisecond, try.BodyContains(test.ruleMatch))
c.Assert(err, checker.IsNil) c.Assert(err, checker.IsNil)
err = try.GetRequest("http://127.0.0.1:8000/", 1000*time.Millisecond, try.StatusCodeIs(http.StatusOK)) err = try.GetRequest("http://127.0.0.1:8000/", 1000*time.Millisecond, try.StatusCodeIs(http.StatusOK))
c.Assert(err, checker.IsNil) c.Assert(err, checker.IsNil)
}
} }

View file

@ -440,13 +440,15 @@ func (s *SimpleSuite) TestMultiprovider(c *check.C) {
err = try.GetRequest("http://127.0.0.1:8080/api/rawdata", 1000*time.Millisecond, try.BodyContains("service")) err = try.GetRequest("http://127.0.0.1:8080/api/rawdata", 1000*time.Millisecond, try.BodyContains("service"))
c.Assert(err, checker.IsNil) c.Assert(err, checker.IsNil)
config := dynamic.HTTPConfiguration{ config := dynamic.Configuration{
Routers: map[string]*dynamic.Router{ HTTP: &dynamic.HTTPConfiguration{
"router1": { Routers: map[string]*dynamic.Router{
EntryPoints: []string{"web"}, "router1": {
Middlewares: []string{"customheader@file"}, EntryPoints: []string{"web"},
Service: "service@file", Middlewares: []string{"customheader@file"},
Rule: "PathPrefix(`/`)", Service: "service@file",
Rule: "PathPrefix(`/`)",
},
}, },
}, },
} }

View file

@ -30,7 +30,7 @@ type Configuration struct {
// TLSConfiguration contains all the configuration parameters of a TLS connection. // TLSConfiguration contains all the configuration parameters of a TLS connection.
type TLSConfiguration struct { type TLSConfiguration struct {
Certificates []*tls.CertAndStores `json:"-" toml:"certificates,omitempty" yaml:"certificates,omitempty" label:"-"` Certificates []*tls.CertAndStores `json:"certificates,omitempty" toml:"certificates,omitempty" yaml:"certificates,omitempty" label:"-"`
Options map[string]tls.Options `json:"options,omitempty" toml:"options,omitempty" yaml:"options,omitempty"` Options map[string]tls.Options `json:"options,omitempty" toml:"options,omitempty" yaml:"options,omitempty"`
Stores map[string]tls.Store `json:"stores,omitempty" toml:"stores,omitempty" yaml:"stores,omitempty"` Stores map[string]tls.Store `json:"stores,omitempty" toml:"stores,omitempty" yaml:"stores,omitempty"`
} }

View file

@ -48,7 +48,7 @@ func (p *Provider) Append(systemRouter *mux.Router) {
return return
} }
configuration := new(dynamic.HTTPConfiguration) configuration := new(dynamic.Configuration)
body, _ := ioutil.ReadAll(request.Body) body, _ := ioutil.ReadAll(request.Body)
if err := json.Unmarshal(body, configuration); err != nil { if err := json.Unmarshal(body, configuration); err != nil {
@ -57,9 +57,7 @@ func (p *Provider) Append(systemRouter *mux.Router) {
return return
} }
p.configurationChan <- dynamic.Message{ProviderName: "rest", Configuration: &dynamic.Configuration{ p.configurationChan <- dynamic.Message{ProviderName: "rest", Configuration: configuration}
HTTP: configuration,
}}
if err := templatesRenderer.JSON(response, http.StatusOK, configuration); err != nil { if err := templatesRenderer.JSON(response, http.StatusOK, configuration); err != nil {
log.WithoutContext().Error(err) log.WithoutContext().Error(err)
} }

View file

@ -175,8 +175,22 @@ func (s *Server) preLoadConfiguration(configMsg dynamic.Message) {
logger := log.WithoutContext().WithField(log.ProviderName, configMsg.ProviderName) logger := log.WithoutContext().WithField(log.ProviderName, configMsg.ProviderName)
if log.GetLevel() == logrus.DebugLevel { if log.GetLevel() == logrus.DebugLevel {
jsonConf, _ := json.Marshal(configMsg.Configuration) copyConf := configMsg.Configuration.DeepCopy()
logger.Debugf("Configuration received from provider %s: %s", configMsg.ProviderName, string(jsonConf)) if copyConf.TLS != nil {
copyConf.TLS.Certificates = nil
for _, v := range copyConf.TLS.Stores {
v.DefaultCertificate = nil
}
}
jsonConf, err := json.Marshal(copyConf)
if err != nil {
logger.Errorf("Could not marshal dynamic configuration: %v", err)
logger.Debugf("Configuration received from provider %s: [struct] %#v", configMsg.ProviderName, copyConf)
} else {
logger.Debugf("Configuration received from provider %s: %s", configMsg.ProviderName, string(jsonConf))
}
} }
if isEmptyConfiguration(configMsg.Configuration) { if isEmptyConfiguration(configMsg.Configuration) {