forked from solo-io/gloo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.go
79 lines (66 loc) · 2.56 KB
/
start.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
69
70
71
72
73
74
75
76
77
78
79
package upstreamdiscovery
import (
"context"
"log"
"time"
"github.com/pkg/errors"
"github.com/solo-io/gloo/pkg/plugins/cloudfoundry"
"github.com/solo-io/gloo/pkg/storage"
"github.com/solo-io/gloo/pkg/upstream-discovery/bootstrap"
"github.com/solo-io/gloo/pkg/upstream-discovery/consul"
"github.com/solo-io/gloo/pkg/upstream-discovery/copilot"
"github.com/solo-io/gloo/pkg/upstream-discovery/knative"
"github.com/solo-io/gloo/pkg/upstream-discovery/kube"
kubeutils "github.com/solo-io/gloo/pkg/utils/kube"
)
func Start(opts bootstrap.Options, store storage.Interface, stop <-chan struct{}) error {
if opts.UpstreamDiscoveryOptions.EnableDiscoveryForKubernetes {
cfg, err := kubeutils.GetConfig(opts.KubeOptions.MasterURL, opts.KubeOptions.KubeConfig)
if err != nil {
return errors.Wrap(err, "failed to create kube restclient config")
}
kubeController, err := kube.NewUpstreamController(cfg, store, opts.ConfigStorageOptions.SyncFrequency)
if err != nil {
return errors.Wrap(err, "failed to create kubernetes upstream discovery service")
}
go runController("kubernetes", kubeController, stop)
knativeController, err := knative.NewUpstreamController(cfg, store, opts.ConfigStorageOptions.SyncFrequency)
if err != nil {
return errors.Wrap(err, "failed to create knative upstream discovery service")
}
go runController("knative", knativeController, stop)
}
if opts.UpstreamDiscoveryOptions.EnableDiscoveryForCopilot {
istioclient, err := cloudfoundry.GetClientFromOptions(opts.CoPilotOptions)
if err != nil {
return errors.Wrap(err, "failed to create copilot client from config")
}
serviceCtl := copilot.NewUpstreamController(context.Background(), store, istioclient, 5*time.Second)
go runController("copilot", serviceCtl, stop)
}
if opts.UpstreamDiscoveryOptions.EnableDiscoveryForConsul {
cfg := opts.ConsulOptions.ToConsulConfig()
// TODO: expose this as a separate flag (interval for restarting a blocking query)
cfg.WaitTime = opts.ConfigStorageOptions.SyncFrequency
serviceCtl, err := consul.NewUpstreamController(cfg, store)
if err != nil {
return errors.Wrap(err, "starting consul upstream discovery")
}
go runController("consul", serviceCtl, stop)
}
return nil
}
func runController(name string, controller Controller, stop <-chan struct{}) {
go func(stop <-chan struct{}) {
for {
select {
case err := <-controller.Error():
log.Printf("%s service discovery encountered error: %v", name, err)
case <-stop:
return
}
}
}(stop)
log.Printf("starting %s service discovery", name)
controller.Run(stop)
}