/
component_manager.go
269 lines (222 loc) · 8.3 KB
/
component_manager.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package controllers
import (
"context"
"fmt"
"time"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log"
apiProxy "github.com/ytsaurus/yt-k8s-operator/pkg/apiproxy"
"github.com/ytsaurus/yt-k8s-operator/pkg/components"
"github.com/ytsaurus/yt-k8s-operator/pkg/labeller"
"github.com/ytsaurus/yt-k8s-operator/pkg/ytconfig"
)
type ComponentManager struct {
ytsaurus *apiProxy.Ytsaurus
allComponents []components.Component
queryTrackerComponent components.Component
schedulerComponent components.Component
status ComponentManagerStatus
}
type ComponentManagerStatus struct {
needSync bool
needInit bool
needUpdate []components.Component
allReadyOrUpdating bool
}
func NewComponentManager(
ctx context.Context,
ytsaurus *apiProxy.Ytsaurus,
) (*ComponentManager, error) {
logger := log.FromContext(ctx)
resource := ytsaurus.GetResource()
clusterDomain := getClusterDomain(ytsaurus.APIProxy().Client())
cfgen := ytconfig.NewGenerator(resource, clusterDomain)
d := components.NewDiscovery(cfgen, ytsaurus)
m := components.NewMaster(cfgen, ytsaurus)
var hps []components.Component
for _, hpSpec := range ytsaurus.GetResource().Spec.HTTPProxies {
hps = append(hps, components.NewHTTPProxy(cfgen, ytsaurus, m, hpSpec))
}
yc := components.NewYtsaurusClient(cfgen, ytsaurus, hps[0])
var dnds []components.Component
nodeCfgGen := ytconfig.NewLocalNodeGenerator(ytsaurus.GetResource(), clusterDomain)
if resource.Spec.DataNodes != nil && len(resource.Spec.DataNodes) > 0 {
for _, dndSpec := range ytsaurus.GetResource().Spec.DataNodes {
dnds = append(dnds, components.NewDataNode(nodeCfgGen, ytsaurus, m, dndSpec))
}
}
var s components.Component
allComponents := []components.Component{
d, m, yc,
}
allComponents = append(allComponents, dnds...)
allComponents = append(allComponents, hps...)
if resource.Spec.UI != nil {
ui := components.NewUI(cfgen, ytsaurus, m)
allComponents = append(allComponents, ui)
}
if resource.Spec.RPCProxies != nil && len(resource.Spec.RPCProxies) > 0 {
var rps []components.Component
for _, rpSpec := range ytsaurus.GetResource().Spec.RPCProxies {
rps = append(rps, components.NewRPCProxy(cfgen, ytsaurus, m, rpSpec))
}
allComponents = append(allComponents, rps...)
}
if resource.Spec.TCPProxies != nil && len(resource.Spec.TCPProxies) > 0 {
var tps []components.Component
for _, tpSpec := range ytsaurus.GetResource().Spec.TCPProxies {
tps = append(tps, components.NewTCPProxy(cfgen, ytsaurus, m, tpSpec))
}
allComponents = append(allComponents, tps...)
}
var ends []components.Component
if resource.Spec.ExecNodes != nil && len(resource.Spec.ExecNodes) > 0 {
for _, endSpec := range ytsaurus.GetResource().Spec.ExecNodes {
ends = append(ends, components.NewExecNode(nodeCfgGen, ytsaurus, m, endSpec))
}
}
allComponents = append(allComponents, ends...)
var tnds []components.Component
if resource.Spec.TabletNodes != nil && len(resource.Spec.TabletNodes) > 0 {
for idx, tndSpec := range ytsaurus.GetResource().Spec.TabletNodes {
tnds = append(tnds, components.NewTabletNode(nodeCfgGen, ytsaurus, yc, tndSpec, idx == 0))
}
}
allComponents = append(allComponents, tnds...)
if resource.Spec.Schedulers != nil {
s = components.NewScheduler(cfgen, ytsaurus, m, ends, tnds)
allComponents = append(allComponents, s)
}
if resource.Spec.ControllerAgents != nil {
ca := components.NewControllerAgent(cfgen, ytsaurus, m)
allComponents = append(allComponents, ca)
}
var q components.Component
if resource.Spec.QueryTrackers != nil && resource.Spec.Schedulers != nil && resource.Spec.TabletNodes != nil && len(resource.Spec.TabletNodes) > 0 {
q = components.NewQueryTracker(cfgen, ytsaurus, yc, tnds)
allComponents = append(allComponents, q)
}
if resource.Spec.QueueAgents != nil && resource.Spec.TabletNodes != nil && len(resource.Spec.TabletNodes) > 0 {
qa := components.NewQueueAgent(cfgen, ytsaurus, yc, m, tnds)
allComponents = append(allComponents, qa)
}
if resource.Spec.YQLAgents != nil {
yqla := components.NewYQLAgent(cfgen, ytsaurus, m)
allComponents = append(allComponents, yqla)
}
if (resource.Spec.DeprecatedChytController != nil || resource.Spec.StrawberryController != nil) && resource.Spec.Schedulers != nil {
strawberry := components.NewStrawberryController(cfgen, ytsaurus, m, s, dnds)
allComponents = append(allComponents, strawberry)
}
if resource.Spec.MasterCaches != nil {
mc := components.NewMasterCache(cfgen, ytsaurus)
allComponents = append(allComponents, mc)
}
// Fetch component status.
var readyComponents []string
var notReadyComponents []string
status := ComponentManagerStatus{
needInit: false,
needSync: false,
needUpdate: nil,
allReadyOrUpdating: true,
}
for _, c := range allComponents {
err := c.Fetch(ctx)
if err != nil {
logger.Error(err, "failed to fetch status for controller", "component", c.GetName())
return nil, err
}
componentStatus, err := c.Status(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get component %s status: %w", c.GetName(), err)
}
c.SetReadyCondition(componentStatus)
syncStatus := componentStatus.SyncStatus
if syncStatus == components.SyncStatusNeedLocalUpdate {
status.needUpdate = append(status.needUpdate, c)
}
if !components.IsRunningStatus(syncStatus) {
status.needInit = true
}
if syncStatus != components.SyncStatusReady && syncStatus != components.SyncStatusUpdating {
status.allReadyOrUpdating = false
}
if syncStatus != components.SyncStatusReady {
logger.Info("component is not ready", "component", c.GetName(), "syncStatus", syncStatus)
notReadyComponents = append(notReadyComponents, c.GetName())
status.needSync = true
} else {
readyComponents = append(readyComponents, c.GetName())
}
}
logger.Info("Ytsaurus sync status",
"notReadyComponents", notReadyComponents,
"readyComponents", readyComponents,
"updateState", resource.Status.UpdateStatus.State,
"clusterState", resource.Status.State)
return &ComponentManager{
ytsaurus: ytsaurus,
allComponents: allComponents,
queryTrackerComponent: q,
schedulerComponent: s,
status: status,
}, nil
}
func (cm *ComponentManager) Sync(ctx context.Context) (ctrl.Result, error) {
logger := log.FromContext(ctx)
hasPending := false
for _, c := range cm.allComponents {
status, err := c.Status(ctx)
if err != nil {
return ctrl.Result{Requeue: true}, fmt.Errorf("failed to get status for %s: %w", c.GetName(), err)
}
if status.SyncStatus == components.SyncStatusPending ||
status.SyncStatus == components.SyncStatusUpdating {
hasPending = true
logger.Info("component sync", "component", c.GetName())
if err := c.Sync(ctx); err != nil {
logger.Error(err, "component sync failed", "component", c.GetName())
return ctrl.Result{Requeue: true}, err
}
}
}
if err := cm.ytsaurus.APIProxy().UpdateStatus(ctx); err != nil {
logger.Error(err, "update Ytsaurus status failed")
return ctrl.Result{Requeue: true}, err
}
if !hasPending {
// All components are blocked.
return ctrl.Result{RequeueAfter: 5 * time.Second}, nil
}
return ctrl.Result{RequeueAfter: time.Second}, nil
}
func (cm *ComponentManager) needSync() bool {
return cm.status.needSync
}
func (cm *ComponentManager) needInit() bool {
return cm.status.needInit
}
func (cm *ComponentManager) needUpdate() []components.Component {
return cm.status.needUpdate
}
func (cm *ComponentManager) allReadyOrUpdating() bool {
return cm.status.allReadyOrUpdating
}
func (cm *ComponentManager) needQueryTrackerUpdate() bool {
return cm.queryTrackerComponent != nil && components.IsUpdatingComponent(cm.ytsaurus, cm.queryTrackerComponent)
}
func (cm *ComponentManager) needSchedulerUpdate() bool {
return cm.schedulerComponent != nil && components.IsUpdatingComponent(cm.ytsaurus, cm.schedulerComponent)
}
func (cm *ComponentManager) arePodsRemoved() bool {
for _, cmp := range cm.allComponents {
if components.IsUpdatingComponent(cm.ytsaurus, cmp) && !cm.areComponentPodsRemoved(cmp) {
return false
}
}
return true
}
func (cm *ComponentManager) areComponentPodsRemoved(component components.Component) bool {
return cm.ytsaurus.IsUpdateStatusConditionTrue(labeller.GetPodsRemovedCondition(component.GetName()))
}