Skip to content

Commit 22696db

Browse files
authored
Merge pull request #29 from cortexproject/27-create-proxies-only-specificed-in-the-configuration-file
Create proxies only specified in the configuration file
2 parents 2bfcbc9 + 76b2dd3 commit 22696db

File tree

2 files changed

+65
-33
lines changed

2 files changed

+65
-33
lines changed

gateway/gateway.go

Lines changed: 52 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import (
44
"net/http"
55

66
"github.com/cortexproject/auth-gateway/server"
7+
"github.com/sirupsen/logrus"
78
)
89

910
type Gateway struct {
1011
distributorProxy *Proxy
1112
queryFrontendProxy *Proxy
1213
alertmanagerProxy *Proxy
13-
ruler *Proxy
14+
rulerProxy *Proxy
1415
srv *server.Server
1516
}
1617

@@ -58,44 +59,70 @@ var defaultRulerAPIs = []string{
5859
}
5960

6061
func New(config *Config, srv *server.Server) (*Gateway, error) {
61-
distributor, err := NewProxy(config.Distributor.URL, config.Distributor, DISTRIBUTOR)
62-
if err != nil {
63-
return nil, err
62+
gateway := &Gateway{
63+
srv: srv,
6464
}
6565

66-
frontend, err := NewProxy(config.QueryFrontend.URL, config.QueryFrontend, FRONTEND)
67-
if err != nil {
68-
return nil, err
66+
components := []string{DISTRIBUTOR, FRONTEND, ALERTMANAGER, RULER}
67+
for _, componentName := range components {
68+
upstreamConfig := config.getUpstreamConfig(componentName)
69+
proxy, err := setupProxy(upstreamConfig, componentName, componentName)
70+
if err != nil {
71+
return nil, err
72+
}
73+
switch componentName {
74+
case DISTRIBUTOR:
75+
gateway.distributorProxy = proxy
76+
case FRONTEND:
77+
gateway.queryFrontendProxy = proxy
78+
case ALERTMANAGER:
79+
gateway.alertmanagerProxy = proxy
80+
case RULER:
81+
gateway.rulerProxy = proxy
82+
}
6983
}
7084

71-
alertmanager, err := NewProxy(config.Alertmanager.URL, config.Alertmanager, ALERTMANAGER)
72-
if err != nil {
73-
return nil, err
74-
}
75-
76-
ruler, err := NewProxy(config.Ruler.URL, config.Ruler, RULER)
77-
if err != nil {
78-
return nil, err
79-
}
80-
81-
return &Gateway{
82-
distributorProxy: distributor,
83-
queryFrontendProxy: frontend,
84-
alertmanagerProxy: alertmanager,
85-
ruler: ruler,
86-
srv: srv,
87-
}, nil
85+
return gateway, nil
8886
}
8987

9088
func (g *Gateway) Start(config *Config) {
9189
g.registerRoutes(config)
9290
}
9391

92+
func (c *Config) getUpstreamConfig(componentName string) Upstream {
93+
switch componentName {
94+
case DISTRIBUTOR:
95+
return c.Distributor
96+
case FRONTEND:
97+
return c.QueryFrontend
98+
case ALERTMANAGER:
99+
return c.Alertmanager
100+
case RULER:
101+
return c.Ruler
102+
default:
103+
return Upstream{}
104+
}
105+
}
106+
107+
func setupProxy(upstreamConfig Upstream, proxyType string, description string) (*Proxy, error) {
108+
if upstreamConfig.URL != "" {
109+
proxy, err := NewProxy(upstreamConfig.URL, upstreamConfig, proxyType)
110+
if err != nil {
111+
return nil, err
112+
}
113+
return proxy, nil
114+
} else {
115+
logrus.Infof("%s URL configuration not provided. %s will not be set up.", description, description)
116+
}
117+
118+
return nil, nil
119+
}
120+
94121
func (g *Gateway) registerRoutes(config *Config) {
95122
g.registerProxyRoutes(config.Distributor.Paths, defaultDistributorAPIs, http.HandlerFunc(g.distributorProxy.Handler))
96123
g.registerProxyRoutes(config.QueryFrontend.Paths, defaultQueryFrontendAPIs, http.HandlerFunc(g.queryFrontendProxy.Handler))
97124
g.registerProxyRoutes(config.Alertmanager.Paths, defaultAlertmanagerAPIs, http.HandlerFunc(g.alertmanagerProxy.Handler))
98-
g.registerProxyRoutes(config.Ruler.Paths, defaultRulerAPIs, http.HandlerFunc(g.ruler.Handler))
125+
g.registerProxyRoutes(config.Ruler.Paths, defaultRulerAPIs, http.HandlerFunc(g.rulerProxy.Handler))
99126
g.srv.RegisterTo("/", http.HandlerFunc(g.notFoundHandler), server.UNAUTH)
100127
}
101128

gateway/gateway_test.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -261,15 +261,20 @@ func TestStartGateway(t *testing.T) {
261261
expectedStatus: http.StatusNotFound,
262262
},
263263
{
264-
name: "invalid distributor proxy",
265-
config: &Config{},
264+
name: "invalid distributor proxy",
265+
config: &Config{
266+
Distributor: Upstream{
267+
URL: "localhost",
268+
Paths: []string{},
269+
},
270+
},
266271
expectedErr: errors.New("invalid URL scheme"),
267272
},
268273
{
269274
name: "invalid frontend proxy",
270275
config: &Config{
271276
Distributor: Upstream{
272-
URL: distributorServer.URL,
277+
URL: "localhost",
273278
Paths: []string{},
274279
},
275280
},
@@ -279,11 +284,11 @@ func TestStartGateway(t *testing.T) {
279284
name: "invalid alertmanager proxy",
280285
config: &Config{
281286
Distributor: Upstream{
282-
URL: distributorServer.URL,
287+
URL: "localhost",
283288
Paths: []string{},
284289
},
285290
QueryFrontend: Upstream{
286-
URL: frontendServer.URL,
291+
URL: "localhost",
287292
Paths: []string{},
288293
},
289294
},
@@ -293,15 +298,15 @@ func TestStartGateway(t *testing.T) {
293298
name: "invalid ruler proxy",
294299
config: &Config{
295300
Distributor: Upstream{
296-
URL: distributorServer.URL,
301+
URL: "localhost",
297302
Paths: []string{},
298303
},
299304
QueryFrontend: Upstream{
300-
URL: frontendServer.URL,
305+
URL: "localhost",
301306
Paths: []string{},
302307
},
303308
Alertmanager: Upstream{
304-
URL: alertmanagerServer.URL,
309+
URL: "localhost",
305310
Paths: []string{},
306311
},
307312
},

0 commit comments

Comments
 (0)