Skip to content

Commit

Permalink
fix wildcard alias and some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yusing committed Feb 10, 2025
1 parent 4363ca8 commit c9b5516
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 37 deletions.
14 changes: 10 additions & 4 deletions internal/route/provider/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,11 @@ func (p *DockerProvider) routesFromContainerLabels(container *docker.Container)

// init entries map for all aliases
for _, a := range container.Aliases {
routes[a] = &route.Route{}
routes[a].Metadata.Container = container
routes[a] = &route.Route{
Metadata: route.Metadata{
Container: container,
},
}
}

errs := E.NewBuilder("label errors")
Expand Down Expand Up @@ -157,8 +160,11 @@ func (p *DockerProvider) routesFromContainerLabels(container *docker.Container)
// init entry if not exist
r, ok := routes[alias]
if !ok {
r = &route.Route{}
r.Metadata.Container = container
r = &route.Route{
Metadata: route.Metadata{
Container: container,
},
}
routes[alias] = r
}

Expand Down
52 changes: 26 additions & 26 deletions internal/route/provider/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func makeRoutes(cont *types.Container, dockerHostIP ...string) route.Routes {
} else {
host = client.DefaultDockerHost
}
cont.ID = "test"
p.name = "test"
routes := Must(p.routesFromContainerLabels(D.FromDocker(cont, host)))
for _, r := range routes {
Expand All @@ -55,37 +56,35 @@ func TestApplyLabel(t *testing.T) {
"GET /static",
}
middlewaresExpect := map[string]map[string]any{
"middleware1": {
"prop1": "value1",
"prop2": "value2",
},
"middleware2": {
"prop3": "value3",
"prop4": "value4",
"request": {
"set_headers": map[string]any{
"X-Header": "value1",
},
"add_headers": map[string]any{
"X-Header2": "value2",
},
},
}
entries := makeRoutes(&types.Container{
Names: dummyNames,
Labels: map[string]string{
D.LabelAliases: "a,b",
D.LabelIdleTimeout: "",
D.LabelStopMethod: common.StopMethodDefault,
D.LabelStopSignal: "SIGTERM",
D.LabelStopTimeout: common.StopTimeoutDefault,
D.LabelWakeTimeout: common.WakeTimeoutDefault,
"proxy.*.no_tls_verify": "true",
"proxy.*.scheme": "https",
"proxy.*.host": "app",
"proxy.*.port": "4567",
"proxy.a.path_patterns": pathPatterns,
"proxy.a.middlewares.middleware1.prop1": "value1",
"proxy.a.middlewares.middleware1.prop2": "value2",
"proxy.a.middlewares.middleware2.prop3": "value3",
"proxy.a.middlewares.middleware2.prop4": "value4",
"proxy.a.homepage.show": "true",
"proxy.a.homepage.icon": "png/adguard-home.png",
"proxy.a.healthcheck.path": "/ping",
"proxy.a.healthcheck.interval": "10s",
D.LabelAliases: "a,b",
D.LabelIdleTimeout: "",
D.LabelStopMethod: common.StopMethodDefault,
D.LabelStopSignal: "SIGTERM",
D.LabelStopTimeout: common.StopTimeoutDefault,
D.LabelWakeTimeout: common.WakeTimeoutDefault,
"proxy.*.no_tls_verify": "true",
"proxy.*.scheme": "https",
"proxy.*.host": "app",
"proxy.*.port": "4567",
"proxy.a.path_patterns": pathPatterns,
"proxy.a.middlewares.request.set_headers.X-Header": "value1",
"proxy.a.middlewares.request.add_headers.X-Header2": "value2",
"proxy.a.homepage.show": "true",
"proxy.a.homepage.icon": "png/adguard-home.png",
"proxy.a.healthcheck.path": "/ping",
"proxy.a.healthcheck.interval": "10s",
},
})

Expand Down Expand Up @@ -198,6 +197,7 @@ func TestApplyLabelWithRefIndexError(t *testing.T) {
Labels: map[string]string{
D.LabelAliases: "a,b",
"proxy.#1.host": "localhost",
"proxy.*.port": "4444",
"proxy.#4.scheme": "https",
},
}, "")
Expand Down
3 changes: 0 additions & 3 deletions internal/route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,6 @@ func (r *Route) Validate() (err E.Error) {
r.LisURL = E.Collect(errs, net.ParseURL, fmt.Sprintf("%s://%s:%d", r.Scheme, r.Host, r.Port.Listening))
fallthrough
default:
if r.Port.Proxy == 0 && !r.IsDocker() {
errs.Adds("missing proxy port")
}
if r.LoadBalance != nil && r.LoadBalance.Link == "" {
r.LoadBalance = nil
}
Expand Down
8 changes: 4 additions & 4 deletions internal/route/rules/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ var (
ErrInvalidCommandSequence = E.New("invalid command sequence")
ErrInvalidSetTarget = E.New("invalid `rule.set` target")

ErrExpectNoArg = E.New("expect no arg")
ErrExpectOneArg = E.New("expect 1 arg")
ErrExpectTwoArgs = E.New("expect 2 args")
ErrExpectKVOptionalV = E.New("expect 'key' or 'key value'")
ErrExpectNoArg = E.Wrap(ErrInvalidArguments, "expect no arg")
ErrExpectOneArg = E.Wrap(ErrInvalidArguments, "expect 1 arg")
ErrExpectTwoArgs = E.Wrap(ErrInvalidArguments, "expect 2 args")
ErrExpectKVOptionalV = E.Wrap(ErrInvalidArguments, "expect 'key' or 'key value'")
)

0 comments on commit c9b5516

Please sign in to comment.