-
Notifications
You must be signed in to change notification settings - Fork 335
/
finalizer.go
161 lines (138 loc) · 4.87 KB
/
finalizer.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
153
154
155
156
157
158
159
160
161
package gc
import (
"context"
"time"
"github.com/pkg/errors"
kuma_interfaces "github.com/kumahq/kuma/api/helpers"
"github.com/kumahq/kuma/pkg/core"
"github.com/kumahq/kuma/pkg/core/resources/manager"
core_model "github.com/kumahq/kuma/pkg/core/resources/model"
"github.com/kumahq/kuma/pkg/core/resources/registry"
"github.com/kumahq/kuma/pkg/core/runtime/component"
)
var (
finalizerLog = core.Log.WithName("finalizer")
)
// Every Insight has a statusSink that periodically increments the 'generation' counter while Kuma CP <-> DPP
// connection is active. This updates happens every <KumaCP.Config>.Metrics.Dataplane.IdleTimeout / 2.
//
// subscriptionFinalizer is a component that allows to finalize subscriptions for Insights. The component iterates
// over all online Insights and checks that 'generation' counter was changed since the last check. This check
// happens every <KumaCP.Config>.Metrics.Dataplane.IdleTimeout. If 'generation' counter didn't change then component
// finalizes the subscription by setting DisconnectTime.
//
// This component allows to solve the corner case we had with Insights:
// 1. Kuma CP is down
// 2. DPP is down
// 3. Kuma CP is up
// 4. DPP status is Online whereas it should be Offline
type descriptor struct {
id string
generation uint32
}
type insightMap map[core_model.ResourceKey]*descriptor
type insightsByType map[core_model.ResourceType]insightMap
type subscriptionFinalizer struct {
rm manager.ResourceManager
newTicker func() *time.Ticker
types []core_model.ResourceType
insights insightsByType
}
func NewSubscriptionFinalizer(rm manager.ResourceManager, newTicker func() *time.Ticker, types ...core_model.ResourceType) (component.Component, error) {
insights := insightsByType{}
for _, typ := range types {
if err := validateType(typ); err != nil {
return nil, err
}
insights[typ] = map[core_model.ResourceKey]*descriptor{}
}
return &subscriptionFinalizer{
rm: rm,
types: types,
newTicker: newTicker,
insights: insights,
}, nil
}
func (f *subscriptionFinalizer) Start(stop <-chan struct{}) error {
ticker := f.newTicker()
defer ticker.Stop()
finalizerLog.Info("started")
for {
select {
case <-ticker.C:
for _, typ := range f.types {
if err := f.checkGeneration(typ); err != nil {
finalizerLog.Error(err, "unable to check subscription's generation", "type", typ)
}
}
case <-stop:
finalizerLog.Info("stopped")
return nil
}
}
}
func (f *subscriptionFinalizer) checkGeneration(typ core_model.ResourceType) error {
// get all the insights for provided type
ctx := context.Background()
insights, _ := registry.Global().NewList(typ)
if err := f.rm.List(ctx, insights); err != nil {
return err
}
// delete items from the map that don't exist in the upstream
f.upstreamSync(insights)
for _, item := range insights.GetItems() {
log := finalizerLog.WithValues("type", typ, "name", item.GetMeta().GetName(), "mesh", item.GetMeta().GetMesh())
key := core_model.MetaToResourceKey(item.GetMeta())
insight := item.GetSpec().(kuma_interfaces.Insight)
if !insight.IsOnline() {
delete(f.insights[typ], key)
continue
}
old, ok := f.insights[typ][key]
if !ok || old.id != insight.GetLastSubscription().GetId() || old.generation != insight.GetLastSubscription().GetGeneration() {
// something changed since the last check, either subscriptionId or generation were updated
// don't finalize the subscription, update map with fresh data
f.insights[typ][key] = &descriptor{
id: insight.GetLastSubscription().GetId(),
generation: insight.GetLastSubscription().GetGeneration(),
}
continue
}
log.V(1).Info("mark subscription as disconnected")
insight.GetLastSubscription().SetDisconnectTime(core.Now())
upsertInsight, _ := registry.Global().NewObject(typ)
err := manager.Upsert(f.rm, core_model.MetaToResourceKey(item.GetMeta()), upsertInsight, func(_ core_model.Resource) {
upsertInsight.GetSpec().(kuma_interfaces.Insight).UpdateSubscription(insight.GetLastSubscription())
})
if err != nil {
log.Error(err, "unable to finalize subscription")
}
delete(f.insights[typ], key)
}
return nil
}
func (f *subscriptionFinalizer) upstreamSync(insights core_model.ResourceList) {
byResourceKey := map[core_model.ResourceKey]bool{}
for _, item := range insights.GetItems() {
byResourceKey[core_model.MetaToResourceKey(item.GetMeta())] = true
}
for rk := range f.insights[insights.GetItemType()] {
if !byResourceKey[rk] {
delete(f.insights[insights.GetItemType()], rk)
}
}
}
func validateType(typ core_model.ResourceType) error {
obj, err := registry.Global().NewObject(typ)
if err != nil {
return err
}
_, ok := obj.GetSpec().(kuma_interfaces.Insight)
if !ok {
return errors.Errorf("type %v doesn't implement interfaces.Insight", typ)
}
return nil
}
func (f *subscriptionFinalizer) NeedLeaderElection() bool {
return true
}