forked from joeholley/supergloo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.go
132 lines (112 loc) · 4.2 KB
/
setup.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package setup
import (
"context"
"flag"
"os"
"time"
"github.com/solo-io/go-utils/contextutils"
"github.com/solo-io/go-utils/errors"
"github.com/solo-io/solo-kit/pkg/api/v1/clients"
"github.com/solo-io/solo-kit/pkg/api/v1/reporter"
"github.com/solo-io/supergloo/pkg/api/clientset"
v1 "github.com/solo-io/supergloo/pkg/api/v1"
"github.com/solo-io/supergloo/pkg/constants"
"github.com/solo-io/supergloo/pkg/registration/appmesh"
"github.com/solo-io/supergloo/pkg/registration/gloo"
istio2 "github.com/solo-io/supergloo/pkg/registration/gloo/istio"
"github.com/solo-io/supergloo/pkg/registration/gloo/linkerd"
"github.com/solo-io/supergloo/pkg/registration/istio"
istiostats "github.com/solo-io/supergloo/pkg/stats/istio"
linkerdstats "github.com/solo-io/supergloo/pkg/stats/linkerd"
"k8s.io/helm/pkg/kube"
)
type Options struct {
SuperglooNamespace string
SidecarInjectorImageName string
SidecarInjectorImagePullPolicy string
DisablePrometheusBouncer bool
}
func RunRegistrationEventLoop(ctx context.Context, cs *clientset.Clientset, customErrHandler func(error)) error {
ctx = contextutils.WithLogger(ctx, "registration-event-loop")
logger := contextutils.LoggerFrom(ctx)
errHandler := func(err error) {
if err == nil {
return
}
logger.Errorf("registration error: %v", err)
if customErrHandler != nil {
customErrHandler(err)
}
}
opts, err := getOptions()
if err != nil {
return err
}
registrationSyncers := createRegistrationSyncers(cs, opts)
if err := runRegistrationEventLoop(ctx, errHandler, cs, registrationSyncers); err != nil {
return err
}
return nil
}
// Add registration syncers here
func createRegistrationSyncers(clientset *clientset.Clientset, opts Options) v1.RegistrationSyncer {
skipPrometheusBounce := opts.DisablePrometheusBouncer
return v1.RegistrationSyncers{
istio.NewIstioSecretDeleter(clientset.Kube),
istiostats.NewIstioPrometheusSyncer(clientset.Prometheus, clientset.Kube, skipPrometheusBounce),
linkerdstats.NewLinkerdPrometheusSyncer(clientset.Prometheus, clientset.Kube, skipPrometheusBounce),
gloo.NewGlooRegistrationSyncer(
clientset,
linkerd.NewGlooLinkerdMtlsPlugin(clientset),
istio2.NewGlooIstioMtlsPlugin(clientset),
),
appmesh.NewAppMeshRegistrationSyncer(
reporter.NewReporter("app-mesh-registration-reporter", clientset.Supergloo.Mesh.BaseClient()),
clientset.Kube,
clientset.Supergloo.Secret,
kube.New(nil),
opts.SuperglooNamespace,
opts.SidecarInjectorImageName,
opts.SidecarInjectorImagePullPolicy,
),
}
}
// start the registration event loop
func runRegistrationEventLoop(ctx context.Context, errHandler func(err error), clientset *clientset.Clientset, syncers v1.RegistrationSyncer) error {
registrationEmitter := v1.NewRegistrationEmitter(clientset.Supergloo.Mesh, clientset.Supergloo.MeshIngress)
registrationEventLoop := v1.NewRegistrationEventLoop(registrationEmitter, syncers)
watchOpts := clients.WatchOpts{
Ctx: ctx,
RefreshRate: time.Second * 1,
}
registrationEventLoopErrs, err := registrationEventLoop.Run(nil, watchOpts)
if err != nil {
return err
}
go func() {
for {
select {
case err := <-registrationEventLoopErrs:
errHandler(err)
case <-ctx.Done():
}
}
}()
return nil
}
func getOptions() (Options, error) {
var opts Options
flag.BoolVar(&opts.DisablePrometheusBouncer, "disable-prometheus-bouncer", false, "disable automatic "+
"bouncing of prometheus pods on config reload. enable this option if using the prometheus configmap reload")
flag.Parse()
if opts.SuperglooNamespace = os.Getenv(constants.PodNamespaceEnvName); opts.SuperglooNamespace == "" {
return Options{}, errors.Errorf("%s env must be set", constants.PodNamespaceEnvName)
}
if opts.SidecarInjectorImageName = os.Getenv(constants.SidecarInjectorImageNameEnvName); opts.SidecarInjectorImageName == "" {
return Options{}, errors.Errorf("%s env must be set", constants.SidecarInjectorImageNameEnvName)
}
if opts.SidecarInjectorImagePullPolicy = os.Getenv(constants.SidecarInjectorImagePullPolicyEnvName); opts.SidecarInjectorImagePullPolicy == "" {
return Options{}, errors.Errorf("%s env must be set", constants.SidecarInjectorImagePullPolicyEnvName)
}
return opts, nil
}