-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkolumbus.go
68 lines (54 loc) · 1.77 KB
/
kolumbus.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"sync"
"github.com/docker/docker/api/types"
"github.com/pkg/errors"
)
// Kolumbus provides the methods
type Kolumbus struct {
ContainerInfo *types.Container
Services map[string][]Endpoint
sync.RWMutex
}
// Endpoint contains information for an endpoint instance of a given service
type Endpoint struct {
Host string // host that the service is available on
Port string // port of the service
}
// FindABraveNewWorld will initialize a new kolumbus dns server
func FindABraveNewWorld() *Kolumbus {
dns := Kolumbus{}
dns.Services = make(map[string][]Endpoint)
return &dns
}
// StartEnvoyDataPlaneServer will start a http server to provide service
// information for envoy proxies
func (dns *Kolumbus) StartEnvoyDataPlaneServer(config Config, errs chan<- error) {
mux := http.NewServeMux()
mux.HandleFunc("/v2/discovery:clusters", HandleEnvoyClusterRequest(dns, config, errs))
mux.HandleFunc("/v2/discovery:routes", HandleEnvoyRouteRequest(dns, config, errs))
mux.HandleFunc("/v1/certs/list/approved", HandleEnvoyCertificateRequest(dns, config, errs))
mux.HandleFunc("/", HandleAnyRequest(dns, errs))
go func() {
err := http.ListenAndServe(fmt.Sprintf(":%d", config.DataPlanePort), mux)
if err != nil {
errs <- errors.Wrap(err, "could not start discovery service")
}
}()
}
// HandleAnyRequest will handle all request to paths that were not specified before
func HandleAnyRequest(dns *Kolumbus, errs chan<- error) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
log.Printf("got other request: %s\n", r.URL.String())
body, err := ioutil.ReadAll(r.Body)
if err != nil {
errs <- errors.Wrap(err, "could not read body")
}
_ = r.Body.Close()
log.Printf("body: %s\n", body)
}
}