forked from joeholley/supergloo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
syncer.go
88 lines (69 loc) · 2.7 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package smi
import (
"context"
"fmt"
"github.com/solo-io/supergloo/pkg/config/utils"
"github.com/solo-io/supergloo/pkg/api/clientset"
"go.uber.org/zap"
"github.com/solo-io/supergloo/pkg/translator/smi"
"github.com/solo-io/go-utils/contextutils"
"github.com/solo-io/go-utils/errors"
"github.com/solo-io/solo-kit/pkg/api/v1/reporter"
v1 "github.com/solo-io/supergloo/pkg/api/v1"
)
type smiConfigSyncer struct {
translator smi.Translator
reconcilers Reconcilers
reporter reporter.Reporter
}
func NewSmiConfigSyncer(translator smi.Translator, reconcilers Reconcilers, reporter reporter.Reporter) v1.ConfigSyncer {
return &smiConfigSyncer{translator: translator, reconcilers: reconcilers, reporter: reporter}
}
// crds required for to sync Istio
var RequiredCrds = []string{
clientset.TrafficSplitCrdName,
clientset.HttpRouteGroupCrdName,
clientset.TrafficTargetCrdName,
}
func (s *smiConfigSyncer) ShouldSync(ctx context.Context, _, snap *v1.ConfigSnapshot) bool {
return utils.ShouldSync(ctx, "smi", RequiredCrds, snap.Customresourcedefinition)
}
func (s *smiConfigSyncer) Sync(ctx context.Context, snap *v1.ConfigSnapshot) error {
ctx = contextutils.WithLogger(ctx, fmt.Sprintf("smi-config-sync-%v", snap.Hash()))
logger := contextutils.LoggerFrom(ctx)
fields := []interface{}{
zap.Int("meshes", len(snap.Meshes)),
zap.Int("routing_rules", len(snap.Routingrules)),
}
logger.Infow("begin sync", fields...)
defer logger.Infow("end sync", fields...)
logger.Debugf("full snapshot: %v", snap)
meshConfigs, resourceErrs, err := s.translator.Translate(ctx, snap)
if err != nil {
return errors.Wrapf(err, "translation failed")
}
if err := resourceErrs.Validate(); err != nil {
logger.Errorf("invalid user config or internal error: %v", err)
}
// ensure that all resources that may have been created by this syncer are removed
if len(meshConfigs) == 0 {
return s.reconcilers.ReconcileAll(ctx, &smi.MeshConfig{})
}
// we don't need to return here; if the error was related to the mesh, it shouldn't have been
// added to the meshConfigs. all meshConfigs are considered to be valid
for mesh, config := range meshConfigs {
if mesh.DiscoveryMetadata == nil || !mesh.SmiEnabled {
return errors.Errorf("internal error: a non smi-mesh appeared in the mesh config snapshot")
}
logger.Infof("reconciling config for mesh %v: ", mesh.Metadata.Ref())
if err := s.reconcilers.ReconcileAll(ctx, config); err != nil {
return errors.Wrapf(err, "reconciling config for %v", mesh.Metadata.Ref())
}
}
// finally, write reports
if err := s.reporter.WriteReports(ctx, resourceErrs, nil); err != nil {
return errors.Wrapf(err, "writing reports")
}
logger.Infof("sync completed successfully!")
return nil
}