mirror of
https://github.com/umputun/reproxy.git
synced 2025-02-16 18:34:30 +02:00
comments and more logging
This commit is contained in:
parent
d45415080a
commit
2c757b4c37
@ -6,6 +6,7 @@ package discovery
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"log"
|
||||||
"regexp"
|
"regexp"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
@ -63,10 +64,14 @@ func (s *Service) Run(ctx context.Context) error {
|
|||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return ctx.Err()
|
return ctx.Err()
|
||||||
case <-ch:
|
case <-ch:
|
||||||
m := s.mergeLists()
|
log.Printf("[DEBUG] new update event received")
|
||||||
|
lst := s.mergeLists()
|
||||||
|
for _, m := range lst {
|
||||||
|
log.Printf("[INFO] match for %s: %s %s %s", m.ProviderID, m.Server, m.SrcMatch.String(), m.Dst)
|
||||||
|
}
|
||||||
s.lock.Lock()
|
s.lock.Lock()
|
||||||
s.mappers = make([]UrlMapper, len(m))
|
s.mappers = make([]UrlMapper, len(lst))
|
||||||
copy(s.mappers, m)
|
copy(s.mappers, lst)
|
||||||
s.lock.Unlock()
|
s.lock.Unlock()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,8 +20,8 @@ func TestService_Do(t *testing.T) {
|
|||||||
},
|
},
|
||||||
ListFunc: func() ([]UrlMapper, error) {
|
ListFunc: func() ([]UrlMapper, error) {
|
||||||
return []UrlMapper{
|
return []UrlMapper{
|
||||||
{SrcMatch: regexp.MustCompile("^/api/svc1/(.*)"), Dst: "http://127.0.0.1:8080/blah1/$1"},
|
{Server: "*", SrcMatch: regexp.MustCompile("^/api/svc1/(.*)"), Dst: "http://127.0.0.1:8080/blah1/$1"},
|
||||||
{SrcMatch: regexp.MustCompile("^/api/svc2/(.*)"), Dst: "http://127.0.0.2:8080/blah2/$1/abc"},
|
{Server: "*", SrcMatch: regexp.MustCompile("^/api/svc2/(.*)"), Dst: "http://127.0.0.2:8080/blah2/$1/abc"},
|
||||||
}, nil
|
}, nil
|
||||||
},
|
},
|
||||||
IDFunc: func() ProviderID {
|
IDFunc: func() ProviderID {
|
||||||
@ -34,7 +34,7 @@ func TestService_Do(t *testing.T) {
|
|||||||
},
|
},
|
||||||
ListFunc: func() ([]UrlMapper, error) {
|
ListFunc: func() ([]UrlMapper, error) {
|
||||||
return []UrlMapper{
|
return []UrlMapper{
|
||||||
{SrcMatch: regexp.MustCompile("/api/svc3/xyz"), Dst: "http://127.0.0.3:8080/blah3/xyz"},
|
{Server: "localhost", SrcMatch: regexp.MustCompile("/api/svc3/xyz"), Dst: "http://127.0.0.3:8080/blah3/xyz"},
|
||||||
}, nil
|
}, nil
|
||||||
},
|
},
|
||||||
IDFunc: func() ProviderID {
|
IDFunc: func() ProviderID {
|
||||||
@ -51,6 +51,7 @@ func TestService_Do(t *testing.T) {
|
|||||||
assert.Equal(t, context.DeadlineExceeded, err)
|
assert.Equal(t, context.DeadlineExceeded, err)
|
||||||
assert.Equal(t, 3, len(svc.mappers))
|
assert.Equal(t, 3, len(svc.mappers))
|
||||||
assert.Equal(t, PIFile, svc.mappers[0].ProviderID)
|
assert.Equal(t, PIFile, svc.mappers[0].ProviderID)
|
||||||
|
assert.Equal(t, "*", svc.mappers[0].Server)
|
||||||
assert.Equal(t, "^/api/svc1/(.*)", svc.mappers[0].SrcMatch.String())
|
assert.Equal(t, "^/api/svc1/(.*)", svc.mappers[0].SrcMatch.String())
|
||||||
assert.Equal(t, "http://127.0.0.1:8080/blah1/$1", svc.mappers[0].Dst)
|
assert.Equal(t, "http://127.0.0.1:8080/blah1/$1", svc.mappers[0].Dst)
|
||||||
|
|
||||||
|
@ -7,7 +7,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
dclient "github.com/fsouza/go-dockerclient"
|
dc "github.com/fsouza/go-dockerclient"
|
||||||
log "github.com/go-pkgz/lgr"
|
log "github.com/go-pkgz/lgr"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
|
||||||
@ -16,7 +16,11 @@ import (
|
|||||||
|
|
||||||
//go:generate moq -out docker_client_mock.go -skip-ensure -fmt goimports . DockerClient
|
//go:generate moq -out docker_client_mock.go -skip-ensure -fmt goimports . DockerClient
|
||||||
|
|
||||||
// Docker emits all changes from all containers states
|
// Docker provide watch compatible changes from containers
|
||||||
|
// and maps by default from ^/api/%s/(.*) to http://%s:%d/$1, i.e. http://example.com/api/my_container/something
|
||||||
|
// will be mapped to http://172.17.42.1:8080/something. Ip will be the internal ip of the container and port - exposed the one
|
||||||
|
// Alternatively labels can alter this. dpx.route sets source route, and dpx.dest sets the destination. Optional dpx.server enforces
|
||||||
|
// match by server name (hostname).
|
||||||
type Docker struct {
|
type Docker struct {
|
||||||
DockerClient DockerClient
|
DockerClient DockerClient
|
||||||
Excludes []string
|
Excludes []string
|
||||||
@ -25,8 +29,8 @@ type Docker struct {
|
|||||||
|
|
||||||
// DockerClient defines interface listing containers and subscribing to events
|
// DockerClient defines interface listing containers and subscribing to events
|
||||||
type DockerClient interface {
|
type DockerClient interface {
|
||||||
ListContainers(opts dclient.ListContainersOptions) ([]dclient.APIContainers, error)
|
ListContainers(opts dc.ListContainersOptions) ([]dc.APIContainers, error)
|
||||||
AddEventListener(listener chan<- *dclient.APIEvents) error
|
AddEventListener(listener chan<- *dc.APIEvents) error
|
||||||
}
|
}
|
||||||
|
|
||||||
// containerInfo is simplified docker.APIEvents for containers only
|
// containerInfo is simplified docker.APIEvents for containers only
|
||||||
@ -98,7 +102,7 @@ func (d *Docker) ID() discovery.ProviderID { return discovery.PIDocker }
|
|||||||
// activate starts blocking listener for all docker events
|
// activate starts blocking listener for all docker events
|
||||||
// filters everything except "container" type, detects stop/start events and publishes signals to eventsCh
|
// filters everything except "container" type, detects stop/start events and publishes signals to eventsCh
|
||||||
func (d *Docker) events(ctx context.Context, client DockerClient, eventsCh chan struct{}) error {
|
func (d *Docker) events(ctx context.Context, client DockerClient, eventsCh chan struct{}) error {
|
||||||
dockerEventsCh := make(chan *dclient.APIEvents)
|
dockerEventsCh := make(chan *dc.APIEvents)
|
||||||
if err := client.AddEventListener(dockerEventsCh); err != nil {
|
if err := client.AddEventListener(dockerEventsCh); err != nil {
|
||||||
return errors.Wrap(err, "can't add even listener")
|
return errors.Wrap(err, "can't add even listener")
|
||||||
}
|
}
|
||||||
@ -132,14 +136,14 @@ func (d *Docker) events(ctx context.Context, client DockerClient, eventsCh chan
|
|||||||
|
|
||||||
func (d *Docker) listContainers() (res []containerInfo, err error) {
|
func (d *Docker) listContainers() (res []containerInfo, err error) {
|
||||||
|
|
||||||
portExposed := func(c dclient.APIContainers) (int, bool) {
|
portExposed := func(c dc.APIContainers) (int, bool) {
|
||||||
if len(c.Ports) == 0 {
|
if len(c.Ports) == 0 {
|
||||||
return 0, false
|
return 0, false
|
||||||
}
|
}
|
||||||
return int(c.Ports[0].PrivatePort), true
|
return int(c.Ports[0].PrivatePort), true
|
||||||
}
|
}
|
||||||
|
|
||||||
containers, err := d.DockerClient.ListContainers(dclient.ListContainersOptions{All: false})
|
containers, err := d.DockerClient.ListContainers(dc.ListContainersOptions{All: false})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "can't list containers")
|
return nil, errors.Wrap(err, "can't list containers")
|
||||||
}
|
}
|
||||||
|
@ -25,7 +25,7 @@ type File struct {
|
|||||||
func (d *File) Events(ctx context.Context) <-chan struct{} {
|
func (d *File) Events(ctx context.Context) <-chan struct{} {
|
||||||
res := make(chan struct{})
|
res := make(chan struct{})
|
||||||
|
|
||||||
// no need to queue multiple events or wait
|
// no need to queue multiple events
|
||||||
trySubmit := func(ch chan struct{}) {
|
trySubmit := func(ch chan struct{}) {
|
||||||
select {
|
select {
|
||||||
case ch <- struct{}{}:
|
case ch <- struct{}{}:
|
||||||
|
@ -12,7 +12,7 @@ import (
|
|||||||
|
|
||||||
// Static provider, rules are server,from,to
|
// Static provider, rules are server,from,to
|
||||||
type Static struct {
|
type Static struct {
|
||||||
Rules []string
|
Rules []string // each rule is 2 or 3 elements comma separated. [server,]source url,destination
|
||||||
}
|
}
|
||||||
|
|
||||||
// Events returns channel updating once
|
// Events returns channel updating once
|
||||||
|
Loading…
x
Reference in New Issue
Block a user