forked from joeholley/supergloo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
syncer.go
74 lines (64 loc) · 2.31 KB
/
syncer.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
package gloo
import (
"context"
"fmt"
"github.com/solo-io/go-utils/contextutils"
"github.com/solo-io/go-utils/errors"
"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"
"go.uber.org/zap"
)
type glooMtlsSyncer struct {
cs *clientset.Clientset
reporter reporter.Reporter
plugins GlooIngressPlugins
}
func NewGlooRegistrationSyncer(cs *clientset.Clientset, plugins ...GlooIngressPlugin) v1.RegistrationSyncer {
glooReporter := reporter.NewReporter("gloo-registration-reporter",
cs.Supergloo.Mesh.BaseClient(),
cs.Supergloo.MeshIngress.BaseClient(),
)
return &glooMtlsSyncer{reporter: glooReporter, cs: cs, plugins: plugins}
}
func (s *glooMtlsSyncer) Sync(ctx context.Context, snap *v1.RegistrationSnapshot) error {
ctx = contextutils.WithLogger(ctx, fmt.Sprintf("gloo-registration-sync-%v", snap.Hash()))
logger := contextutils.LoggerFrom(ctx)
fields := []interface{}{
zap.Int("meshes", len(snap.Meshes)),
zap.Int("mesh-ingresses", len(snap.Meshingresses)),
}
logger.Infow("begin sync", fields...)
defer logger.Infow("end sync", fields...)
var glooMeshIngresses v1.MeshIngressList
for _, meshIngress := range snap.Meshingresses {
if _, ok := meshIngress.MeshIngressType.(*v1.MeshIngress_Gloo); ok {
glooMeshIngresses = append(glooMeshIngresses, meshIngress)
}
}
errs := reporter.ResourceErrors{}
for _, glooIngress := range glooMeshIngresses {
if err := s.handleGlooMeshIngress(ctx, glooIngress, snap.Meshes); err != nil {
errs.AddError(glooIngress, err)
logger.Errorf("unable to update gloo ingress %v, %s", glooIngress.Metadata, err)
}
}
logger.Infof("sync completed successfully!")
return s.reporter.WriteReports(ctx, errs, nil)
}
func (s *glooMtlsSyncer) handleGlooMeshIngress(ctx context.Context, meshIngress *v1.MeshIngress, meshes v1.MeshList) error {
var targetMeshes v1.MeshList
for _, targetMesh := range meshIngress.Meshes {
mesh, err := meshes.Find(targetMesh.GetNamespace(), targetMesh.GetName())
if err != nil {
return errors.Wrapf(err, "could not find mesh %v", targetMesh.Key())
}
targetMeshes = append(targetMeshes, mesh)
}
for _, plugin := range s.plugins {
if err := plugin.HandleMeshes(ctx, meshIngress, targetMeshes); err != nil {
return err
}
}
return nil
}