Skip to content

Commit 87d4da0

Browse files
committed
LabeledRoundRobin service fallback to default
1 parent 4650791 commit 87d4da0

File tree

6 files changed

+35
-22
lines changed

6 files changed

+35
-22
lines changed

pkg/middlewares/canary/canary.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,10 @@ func (c *Canary) processCanary(rw http.ResponseWriter, req *http.Request) {
153153
}
154154

155155
type userInfo struct {
156-
UID string `json:"uid"`
157-
Sub string `json:"sub"`
158-
ID string `json:"id"`
156+
UID0 string `json:"uid"`
157+
UID1 string `json:"_userId"`
158+
UID2 string `json:"sub"`
159+
UID3 string `json:"id"`
159160
}
160161

161162
func extractUserID(req *http.Request, uidCookies []string) string {
@@ -212,12 +213,14 @@ func extractUserIDFromBase64(s string) string {
212213
user := &userInfo{}
213214
if err = json.Unmarshal(b, user); err == nil {
214215
switch {
215-
case user.UID != "":
216-
return user.UID
217-
case user.Sub != "":
218-
return user.Sub
219-
case user.ID != "":
220-
return user.ID
216+
case user.UID0 != "":
217+
return user.UID0
218+
case user.UID1 != "":
219+
return user.UID1
220+
case user.UID2 != "":
221+
return user.UID2
222+
case user.UID3 != "":
223+
return user.UID3
221224
}
222225
}
223226
}

pkg/middlewares/canary/canary_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ func TestCanary(t *testing.T) {
172172
cfg := dynamic.Canary{MaxCacheSize: 3, Server: "localhost", Product: "Urbs", AddRequestID: true}
173173
c, err := New(context.Background(), next, cfg, "test")
174174
c.ls.mustFetchLabels = func(ctx context.Context, uid, requestID string) ([]Label, int64) {
175-
return []Label{Label{Label: uid}}, time.Now().Unix()
175+
return []Label{{Label: uid}}, time.Now().Unix()
176176
}
177177
a.Nil(err)
178178

pkg/provider/kubernetes/crd/kubernetes.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func (p *Provider) Provide(configurationChan chan<- dynamic.Message, pool *safe.
137137
case p.lastConfiguration.Get() == confHash:
138138
logger.Debugf("Skipping Kubernetes event kind %T", event)
139139
default:
140-
logger.Infof("Process Kubernetes event %T, hash %d, conf %+v", event, confHash, conf)
140+
logger.Infof("Process Kubernetes event %T, hash %d", event, confHash)
141141
p.lastConfiguration.Set(confHash)
142142
configurationChan <- dynamic.Message{
143143
ProviderName: providerName,

pkg/provider/kubernetes/crd/kubernetes_http.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -265,18 +265,23 @@ func (c configBuilder) buildLabeledLB(ctx context.Context, namespace string, tSe
265265
conf[fullNameMain] = k8sService
266266
}
267267

268-
labeledServices := make([]string, len(tService.Labeled.Services))
269-
270-
for i, service := range tService.Labeled.Services {
271-
fullName, k8sService, err := c.nameAndService(ctx, namespace, service.LoadBalancerSpec)
268+
logger := log.FromContext(ctx)
269+
labeledServices := make([]string, 0)
270+
for _, service := range tService.Labeled.Services {
271+
s := service.LoadBalancerSpec
272+
fullName, k8sService, err := c.nameAndService(ctx, namespace, s)
272273
if err != nil {
273-
return err
274+
logger.Errorf("buildLabeledLB (%s, %s, %s) failed: %s,", s.Name, s.Namespace, s.Kind, err.Error())
275+
continue // ignore invalid service
274276
}
275277

276-
if k8sService != nil {
277-
conf[fullName] = k8sService
278+
if k8sService == nil {
279+
logger.Errorf("buildLabeledLB %s failed: %s,", fullName, "no k8s service")
280+
continue // ignore invalid service
278281
}
279-
labeledServices[i] = fullName
282+
283+
conf[fullName] = k8sService
284+
labeledServices = append(labeledServices, fullName)
280285
}
281286

282287
conf[id] = &dynamic.Service{

pkg/server/service/proxy.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,14 @@ func buildProxy(passHostHeader *bool, responseForwarding *dynamic.ResponseForwar
8989
}
9090
}
9191

92-
log.Infof("'%d %s' caused by: %v", statusCode, statusText(statusCode), err)
92+
if statusCode > 500 {
93+
log.Warnf("Error proxying: %d, xRequestID: %s, host: %s, url: %s, caused by: %v",
94+
statusCode, request.Header.Get("X-Request-ID"), request.Host, request.URL.String(), err)
95+
}
9396
w.WriteHeader(statusCode)
9497
_, werr := w.Write([]byte(statusText(statusCode)))
9598
if werr != nil {
96-
log.Infof("Error while writing status code", werr)
99+
log.Warnf("Error while writing status code: %s", werr.Error())
97100
}
98101
},
99102
}

pkg/server/service/service.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,12 @@ func (m *Manager) getLRRServiceHandler(ctx context.Context, serviceName string,
182182
}
183183

184184
balancer := lrr.New(config.ServiceName, defaultHandler)
185+
logger := log.FromContext(ctx)
185186
for _, fullServiceName := range config.Services {
186187
serviceHandler, err := m.BuildHTTP(ctx, fullServiceName, responseModifier)
187188
if err != nil {
188-
return nil, err
189+
logger.Errorf("getLRRServiceHandler %s failed: %s,", fullServiceName, err.Error())
190+
continue // should fallback to defaultHandler
189191
}
190192

191193
balancer.AddService(fullServiceName, serviceHandler)

0 commit comments

Comments
 (0)