forked from joeholley/supergloo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.go
152 lines (127 loc) · 4.44 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package setup
import (
"context"
"time"
"github.com/solo-io/supergloo/pkg/config/appmesh"
appmeshtranslator "github.com/solo-io/supergloo/pkg/translator/appmesh"
"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"
policyv1alpha1 "github.com/solo-io/supergloo/pkg/api/external/istio/authorization/v1alpha1"
"github.com/solo-io/supergloo/pkg/api/external/istio/networking/v1alpha3"
rbacv1alpha1 "github.com/solo-io/supergloo/pkg/api/external/istio/rbac/v1alpha1"
v1 "github.com/solo-io/supergloo/pkg/api/v1"
"github.com/solo-io/supergloo/pkg/config/istio"
istiotranslator "github.com/solo-io/supergloo/pkg/translator/istio"
"github.com/solo-io/supergloo/pkg/translator/istio/plugins"
)
type EnabledConfigLoops struct {
Istio bool
Gloo bool
AppMesh bool
}
func RunConfigEventLoop(ctx context.Context, cs *clientset.Clientset, customErrHandler func(error), enabled EnabledConfigLoops) error {
ctx = contextutils.WithLogger(ctx, "config-event-loop")
logger := contextutils.LoggerFrom(ctx)
errHandler := func(err error) {
if err == nil {
return
}
logger.Errorf("config error: %v", err)
if customErrHandler != nil {
customErrHandler(err)
}
}
configSyncers, err := createConfigSyncers(ctx, cs, enabled)
if err != nil {
return err
}
if err := runConfigEventLoop(ctx, cs, errHandler, configSyncers); err != nil {
return err
}
return nil
}
// Add config syncers here
func createConfigSyncers(ctx context.Context, cs *clientset.Clientset, enabled EnabledConfigLoops) (v1.ConfigSyncer, error) {
var syncers v1.ConfigSyncers
if enabled.Istio {
istioSyncer, err := createIstioConfigSyncer(ctx, cs)
if err != nil {
return nil, err
}
syncers = append(syncers, istioSyncer)
}
if enabled.AppMesh {
appMeshSyncer, err := createAppmeshConfigSyncer(ctx, cs)
if err != nil {
return nil, err
}
syncers = append(syncers, appMeshSyncer)
}
return syncers, nil
}
func createAppmeshConfigSyncer(ctx context.Context, cs *clientset.Clientset) (v1.ConfigSyncer, error) {
translator := appmeshtranslator.NewAppMeshTranslator()
newReporter := reporter.NewReporter("appmesh-config-reporter",
cs.Input.Mesh.BaseClient(),
cs.Input.Upstream.BaseClient(),
cs.Input.RoutingRule.BaseClient(),
cs.Input.SecurityRule.BaseClient())
return appmesh.NewAppMeshConfigSyncer(translator, newReporter)
}
func createIstioConfigSyncer(ctx context.Context, cs *clientset.Clientset) (v1.ConfigSyncer, error) {
istioClients, err := clientset.IstioFromContext(ctx)
if err != nil {
return nil, errors.Wrapf(err, "initializing istio clients")
}
translator := istiotranslator.NewTranslator(plugins.Plugins(cs.Kube))
reconcilers := istio.NewIstioReconcilers(map[string]string{"owner_ref": "istio-config-syncer"},
rbacv1alpha1.NewRbacConfigReconciler(istioClients.RbacConfig),
rbacv1alpha1.NewServiceRoleReconciler(istioClients.ServiceRole),
rbacv1alpha1.NewServiceRoleBindingReconciler(istioClients.ServiceRoleBinding),
policyv1alpha1.NewMeshPolicyReconciler(istioClients.MeshPolicy),
v1alpha3.NewDestinationRuleReconciler(istioClients.DestinationRule),
v1alpha3.NewVirtualServiceReconciler(istioClients.VirtualService),
v1.NewTlsSecretReconciler(cs.Input.TlsSecret),
)
newReporter := reporter.NewReporter("istio-config-reporter",
cs.Input.Mesh.BaseClient(),
cs.Input.Upstream.BaseClient(),
cs.Input.RoutingRule.BaseClient(),
cs.Input.SecurityRule.BaseClient())
return istio.NewIstioConfigSyncer(translator, reconcilers, newReporter), nil
}
// start the istio config event loop
func runConfigEventLoop(ctx context.Context, clientset *clientset.Clientset, errHandler func(err error), syncers v1.ConfigSyncer) error {
configEmitter := v1.NewConfigEmitter(
clientset.Input.Mesh,
clientset.Input.MeshIngress,
clientset.Input.MeshGroup,
clientset.Input.RoutingRule,
clientset.Input.SecurityRule,
clientset.Input.TlsSecret,
clientset.Input.Upstream,
clientset.Discovery.Pod,
)
configEventLoop := v1.NewConfigEventLoop(configEmitter, syncers)
watchOpts := clients.WatchOpts{
Ctx: ctx,
RefreshRate: time.Second * 1,
}
configEventLoopErrs, err := configEventLoop.Run(nil, watchOpts)
if err != nil {
return err
}
go func() {
for {
select {
case err := <-configEventLoopErrs:
errHandler(err)
case <-ctx.Done():
}
}
}()
return nil
}