Repository: containous/traefik
Pull request #5265 (2019-08-28 15:59:34)

Add errors about unknown entryPoint in runtime api
### What does this PR do?
This PR adds error in runtime api when a router uses a wrong entryPoint.

### Motivation
See this error in API.

### More

- [x] Added/updated tests



Patch:
diff --git a/integration/fixtures/router_errors.toml b/integration/fixtures/router_errors.toml
index 335339e2b..3e9a75f29 100644
--- a/integration/fixtures/router_errors.toml
+++ b/integration/fixtures/router_errors.toml
@@ -17,6 +17,11 @@
 ## dynamic configuration ##
 
 [http.routers]
+  [http.routers.router3]
+    entrypoints=["unknown-entrypoint"]
+    service = "service1"
+    rule = "Host(`mydomain.com`)"
+
   [http.routers.router4]
     service = "service1"
     rule = "Host(`snitest.net`)"
diff --git a/pkg/config/runtime/runtime_http.go b/pkg/config/runtime/runtime_http.go
index 1c6b9e4aa..e604ac32c 100644
--- a/pkg/config/runtime/runtime_http.go
+++ b/pkg/config/runtime/runtime_http.go
@@ -2,6 +2,7 @@ package runtime
 
 import (
 	"context"
+	"fmt"
 	"sync"
 
 	"github.com/containous/traefik/v2/pkg/config/dynamic"
@@ -17,23 +18,32 @@ func (c *Configuration) GetRoutersByEntryPoints(ctx context.Context, entryPoints
 			continue
 		}
 
+		logger := log.FromContext(log.With(ctx, log.Str(log.RouterName, rtName)))
 		eps := rt.EntryPoints
 		if len(eps) == 0 {
+			logger.Debugf("No entrypoint defined for this router, using the default one(s) instead: %+v", entryPoints)
 			eps = entryPoints
 		}
+		entryPointsCount := 0
 		for _, entryPointName := range eps {
 			if !contains(entryPoints, entryPointName) {
-				log.FromContext(log.With(ctx, log.Str(log.EntryPointName, entryPointName))).
+				rt.AddError(fmt.Errorf("entryPoint %q doesn't exist", entryPointName), false)
+				logger.WithField(log.EntryPointName, entryPointName).
 					Errorf("entryPoint %q doesn't exist", entryPointName)
 				continue
 			}
 
+			entryPointsCount++
 			if _, ok := entryPointsRouters[entryPointName]; !ok {
 				entryPointsRouters[entryPointName] = make(map[string]*RouterInfo)
 			}
 
 			entryPointsRouters[entryPointName][rtName] = rt
 		}
+		if entryPointsCount == 0 {
+			rt.AddError(fmt.Errorf("no valid entryPoint for this router"), true)
+			logger.Error("no valid entryPoint for this router")
+		}
 	}
 
 	return entryPointsRouters


Test patch:
diff --git a/integration/simple_test.go b/integration/simple_test.go
index 9b40b8b7c..b95151295 100644
--- a/integration/simple_test.go
+++ b/integration/simple_test.go
@@ -546,6 +546,10 @@ func (s *SimpleSuite) TestRouterConfigErrors(c *check.C) {
 	err = try.GetRequest("http://127.0.0.1:8080/api/http/routers", 1000*time.Millisecond, try.BodyContains(`["middleware \"unknown@file\" does not exist","found different TLS options for routers on the same host snitest.net, so using the default TLS options instead"]`))
 	c.Assert(err, checker.IsNil)
 
+	// router3 has an error because it uses an unknown entrypoint
+	err = try.GetRequest("http://127.0.0.1:8080/api/http/routers/router3@file", 1000*time.Millisecond, try.BodyContains(`entryPoint \"unknown-entrypoint\" doesn't exist`, "no valid entryPoint for this router"))
+	c.Assert(err, checker.IsNil)
+
 	// router4 is enabled, but in warning state because its tls options conf was messed up
 	err = try.GetRequest("http://127.0.0.1:8080/api/http/routers/router4@file", 1000*time.Millisecond, try.BodyContains(`"status":"warning"`))
 	c.Assert(err, checker.IsNil)
diff --git a/pkg/config/runtime/runtime_http_test.go b/pkg/config/runtime/runtime_http_test.go
index 38102c61d..d95c6e780 100644
--- a/pkg/config/runtime/runtime_http_test.go
+++ b/pkg/config/runtime/runtime_http_test.go
@@ -111,7 +111,8 @@ func TestGetRoutersByEntryPoints(t *testing.T) {
 							Service:     "foobar-service@myprovider",
 							Rule:        "Host(`bar.foobar`)",
 						},
-						Status: "enabled",
+						Status: "warning",
+						Err:    []string{`entryPoint "webs" doesn't exist`},
 					},
 				},
 			},