-
Notifications
You must be signed in to change notification settings - Fork 24
/
master.go
402 lines (344 loc) · 11.9 KB
/
master.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
package components
import (
"context"
"fmt"
"strings"
"k8s.io/utils/ptr"
"go.ytsaurus.tech/yt/go/yson"
"go.ytsaurus.tech/yt/go/yt"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ytv1 "github.com/ytsaurus/ytsaurus-k8s-operator/api/v1"
"github.com/ytsaurus/ytsaurus-k8s-operator/pkg/apiproxy"
"github.com/ytsaurus/ytsaurus-k8s-operator/pkg/consts"
"github.com/ytsaurus/ytsaurus-k8s-operator/pkg/labeller"
"github.com/ytsaurus/ytsaurus-k8s-operator/pkg/resources"
"github.com/ytsaurus/ytsaurus-k8s-operator/pkg/ytconfig"
)
const (
defaultHostAddressLabel = "kubernetes.io/hostname"
)
type Master struct {
localServerComponent
cfgen *ytconfig.Generator
initJob *InitJob
exitReadOnlyJob *InitJob
adminCredentials corev1.Secret
}
func NewMaster(cfgen *ytconfig.Generator, ytsaurus *apiproxy.Ytsaurus) *Master {
resource := ytsaurus.GetResource()
l := labeller.Labeller{
ObjectMeta: &resource.ObjectMeta,
APIProxy: ytsaurus.APIProxy(),
ComponentType: consts.MasterType,
Annotations: resource.Spec.ExtraPodAnnotations,
}
if resource.Spec.PrimaryMasters.InstanceSpec.MonitoringPort == nil {
resource.Spec.PrimaryMasters.InstanceSpec.MonitoringPort = ptr.To(int32(consts.MasterMonitoringPort))
}
srv := newServer(
&l,
ytsaurus,
&resource.Spec.PrimaryMasters.InstanceSpec,
"/usr/bin/ytserver-master",
"ytserver-master.yson",
cfgen.GetMastersStatefulSetName(),
cfgen.GetMastersServiceName(),
func() ([]byte, error) { return cfgen.GetMasterConfig(&resource.Spec.PrimaryMasters) },
WithContainerPorts(corev1.ContainerPort{
Name: consts.YTRPCPortName,
ContainerPort: consts.MasterRPCPort,
Protocol: corev1.ProtocolTCP,
}),
)
initJob := NewInitJob(
&l,
ytsaurus.APIProxy(),
ytsaurus,
resource.Spec.ImagePullSecrets,
"default",
consts.ClientConfigFileName,
getImageWithDefault(resource.Spec.PrimaryMasters.InstanceSpec.Image, resource.Spec.CoreImage),
cfgen.GetNativeClientConfig,
getTolerationsWithDefault(resource.Spec.PrimaryMasters.Tolerations, resource.Spec.Tolerations),
getNodeSelectorWithDefault(resource.Spec.PrimaryMasters.NodeSelector, resource.Spec.NodeSelector),
)
exitReadOnlyJob := NewInitJob(
&l,
ytsaurus.APIProxy(),
ytsaurus,
resource.Spec.ImagePullSecrets,
"exit-read-only",
consts.ClientConfigFileName,
getImageWithDefault(resource.Spec.PrimaryMasters.InstanceSpec.Image, resource.Spec.CoreImage),
cfgen.GetNativeClientConfig,
getTolerationsWithDefault(resource.Spec.PrimaryMasters.Tolerations, resource.Spec.Tolerations),
getNodeSelectorWithDefault(resource.Spec.PrimaryMasters.NodeSelector, resource.Spec.NodeSelector),
)
return &Master{
localServerComponent: newLocalServerComponent(&l, ytsaurus, srv),
cfgen: cfgen,
initJob: initJob,
exitReadOnlyJob: exitReadOnlyJob,
}
}
func (m *Master) GetType() consts.ComponentType { return consts.MasterType }
func (m *Master) IsUpdatable() bool {
return true
}
func (m *Master) Fetch(ctx context.Context) error {
if m.ytsaurus.GetResource().Spec.AdminCredentials != nil {
err := m.ytsaurus.APIProxy().FetchObject(
ctx,
m.ytsaurus.GetResource().Spec.AdminCredentials.Name,
&m.adminCredentials)
if err != nil {
return err
}
}
return resources.Fetch(ctx,
m.server,
m.initJob,
m.exitReadOnlyJob,
)
}
func (m *Master) initAdminUser() string {
adminLogin, adminPassword := consts.DefaultAdminLogin, consts.DefaultAdminPassword
adminToken := consts.DefaultAdminPassword
if m.adminCredentials.Name != "" {
value, ok := m.adminCredentials.Data[consts.AdminLoginSecret]
if ok {
adminLogin = string(value)
}
value, ok = m.adminCredentials.Data[consts.AdminPasswordSecret]
if ok {
adminPassword = string(value)
}
value, ok = m.adminCredentials.Data[consts.AdminTokenSecret]
if ok {
adminToken = string(value)
}
}
commands := createUserCommand(adminLogin, adminPassword, adminToken, true)
return RunIfNonexistent(fmt.Sprintf("//sys/users/%s", adminLogin), commands...)
}
type Medium struct {
Name string `yson:"name"`
}
func (m *Master) getExtraMedia() []Medium {
mediaMap := make(map[string]Medium)
for _, d := range m.ytsaurus.GetResource().Spec.DataNodes {
for _, l := range d.Locations {
if l.Medium == consts.DefaultMedium {
continue
}
mediaMap[l.Medium] = Medium{
Name: l.Medium,
}
}
}
mediaSlice := make([]Medium, 0, len(mediaMap))
for _, v := range mediaMap {
mediaSlice = append(mediaSlice, v)
}
return mediaSlice
}
func (m *Master) initMedia() string {
var commands []string
for _, medium := range m.getExtraMedia() {
attr, err := yson.MarshalFormat(medium, yson.FormatText)
if err != nil {
panic(err)
}
// COMPAT(gritukan): Remove "medium" after some time.
commands = append(commands, fmt.Sprintf("/usr/bin/yt get //sys/media/%s/@name || /usr/bin/yt create domestic_medium --attr '%s' || /usr/bin/yt create medium --attr '%s'", medium.Name, string(attr), string(attr)))
}
return strings.Join(commands, "\n")
}
func (m *Master) initGroups() string {
commands := []string{
"/usr/bin/yt create group --attr '{name=admins}' --ignore-existing",
}
return strings.Join(commands, "\n")
}
func (m *Master) initSchemaACLs() string {
userReadACE := yt.ACE{
Action: "allow",
Subjects: []string{"users"},
Permissions: []yt.Permission{"read"},
}
userReadCreateACE := yt.ACE{
Action: "allow",
Subjects: []string{"users"},
Permissions: []yt.Permission{"read", "create"},
}
userReadWriteCreateACE := yt.ACE{
Action: "allow",
Subjects: []string{"users"},
Permissions: []yt.Permission{"read", "write", "create"},
}
adminACE := yt.ACE{
Action: "allow",
Subjects: []string{"admins"},
Permissions: []yt.Permission{"read", "write", "administer", "create", "remove"},
}
var commands []string
// Users should not be able to create or write objects of these types on their own.
for _, objectType := range []string{
"tablet_cell", "tablet_action", "tablet_cell_bundle",
"user", "group",
"rack", "data_center", "cluster_node",
"access_control_object_namespace", "access_control_object_namespace_map"} {
commands = append(commands, SetPathAcl(fmt.Sprintf("//sys/schemas/%s", objectType), []yt.ACE{
userReadACE,
adminACE,
}))
}
// COMPAT(achulkov2): Drop the first command after `medium` is obsolete in all major versions.
commands = append(commands, fmt.Sprintf("%s || %s",
SetPathAcl("//sys/schemas/medium", []yt.ACE{
userReadACE,
adminACE,
}),
SetPathAcl("//sys/schemas/domestic_medium", []yt.ACE{
userReadCreateACE,
adminACE,
})))
// Users can create pools, pool trees, accounts and access control objects given the right circumstances and permissions.
for _, objectType := range []string{"account", "scheduler_pool", "scheduler_pool_tree", "access_control_object"} {
commands = append(commands, SetPathAcl(fmt.Sprintf("//sys/schemas/%s", objectType), []yt.ACE{
userReadCreateACE,
adminACE,
}))
}
// Users can write account_resource_usage_lease objects.
commands = append(commands, SetPathAcl("//sys/schemas/account_resource_usage_lease", []yt.ACE{
userReadWriteCreateACE,
adminACE,
}))
return strings.Join(commands, "\n")
}
func (m *Master) createInitScript() string {
clusterConnection, err := m.cfgen.GetClusterConnection()
if err != nil {
panic(err)
}
script := []string{
initJobWithNativeDriverPrologue(),
m.initGroups(),
RunIfExists("//sys/@provision_lock", m.initSchemaACLs()),
"/usr/bin/yt create scheduler_pool_tree --attributes '{name=default; config={nodes_filter=\"\"}}' --ignore-existing",
SetWithIgnoreExisting("//sys/pool_trees/@default_tree", "default"),
RunIfNonexistent("//sys/pools", "/usr/bin/yt link //sys/pool_trees/default //sys/pools"),
"/usr/bin/yt create scheduler_pool --attributes '{name=research; pool_tree=default}' --ignore-existing",
"/usr/bin/yt create map_node //home --ignore-existing",
RunIfExists("//sys/@provision_lock", fmt.Sprintf("/usr/bin/yt set //sys/@cluster_connection '%s'", string(clusterConnection))),
SetWithIgnoreExisting("//sys/controller_agents/config/operation_options/spec_template", "'{enable_partitioned_data_balancing=%false}' -r"),
m.initAdminUser(),
m.initMedia(),
"/usr/bin/yt remove //sys/@provision_lock -f",
}
return strings.Join(script, "\n")
}
func (m *Master) createExitReadOnlyScript() string {
script := []string{
initJobWithNativeDriverPrologue(),
"export YT_LOG_LEVEL=DEBUG",
// COMPAT(l0kix2): remove || part when the compatibility with 23.1 and older is dropped.
`[[ "$YTSAURUS_VERSION" < "23.2" ]] && echo "master_exit_read_only is supported since 23.2, nothing to do" && exit 0`,
"/usr/bin/yt execute master_exit_read_only '{}'",
}
return strings.Join(script, "\n")
}
func (m *Master) doSync(ctx context.Context, dry bool) (ComponentStatus, error) {
var err error
if ytv1.IsReadyToUpdateClusterState(m.ytsaurus.GetClusterState()) && m.server.needUpdate() {
return SimpleStatus(SyncStatusNeedLocalUpdate), err
}
if m.ytsaurus.GetClusterState() == ytv1.ClusterStateUpdating {
if m.ytsaurus.GetUpdateState() == ytv1.UpdateStateWaitingForMasterExitReadOnly {
st, err := m.exitReadOnly(ctx, dry)
return *st, err
}
if status, err := handleUpdatingClusterState(ctx, m.ytsaurus, m, &m.localComponent, m.server, dry); status != nil {
return *status, err
}
}
if m.NeedSync() {
if !dry {
err = m.doServerSync(ctx)
}
return WaitingStatus(SyncStatusPending, "components"), err
}
if !m.server.arePodsReady(ctx) {
return WaitingStatus(SyncStatusBlocked, "pods"), err
}
if !dry {
m.initJob.SetInitScript(m.createInitScript())
}
return m.initJob.Sync(ctx, dry)
}
func (m *Master) Status(ctx context.Context) (ComponentStatus, error) {
return m.doSync(ctx, true)
}
func (m *Master) Sync(ctx context.Context) error {
_, err := m.doSync(ctx, false)
return err
}
func (m *Master) doServerSync(ctx context.Context) error {
statefulSet := m.server.buildStatefulSet()
podSpec := &statefulSet.Spec.Template.Spec
primaryMastersSpec := m.ytsaurus.GetResource().Spec.PrimaryMasters
if err := AddSidecarsToPodSpec(primaryMastersSpec.Sidecars, podSpec); err != nil {
return err
}
if len(primaryMastersSpec.HostAddresses) != 0 {
AddAffinity(statefulSet, m.getHostAddressLabel(), primaryMastersSpec.HostAddresses)
}
return m.server.Sync(ctx)
}
func (m *Master) getHostAddressLabel() string {
primaryMastersSpec := m.ytsaurus.GetResource().Spec.PrimaryMasters
if primaryMastersSpec.HostAddressLabel != "" {
return primaryMastersSpec.HostAddressLabel
}
return defaultHostAddressLabel
}
func (m *Master) exitReadOnly(ctx context.Context, dry bool) (*ComponentStatus, error) {
if !m.ytsaurus.IsUpdateStatusConditionTrue(consts.ConditionMasterExitReadOnlyPrepared) {
if !m.exitReadOnlyJob.isRestartPrepared() {
if err := m.exitReadOnlyJob.prepareRestart(ctx, dry); err != nil {
return ptr.To(SimpleStatus(SyncStatusUpdating)), err
}
}
if !dry {
m.setMasterReadOnlyExitPrepared(ctx, metav1.ConditionTrue)
}
return ptr.To(SimpleStatus(SyncStatusUpdating)), nil
}
if !m.exitReadOnlyJob.IsCompleted() {
if !dry {
m.exitReadOnlyJob.SetInitScript(m.createExitReadOnlyScript())
}
status, err := m.exitReadOnlyJob.Sync(ctx, dry)
return &status, err
}
if !dry {
m.ytsaurus.SetUpdateStatusCondition(ctx, metav1.Condition{
Type: consts.ConditionMasterExitedReadOnly,
Status: metav1.ConditionTrue,
Reason: "MasterExitedReadOnly",
Message: "Masters exited read-only state",
})
m.setMasterReadOnlyExitPrepared(ctx, metav1.ConditionFalse)
}
return ptr.To(SimpleStatus(SyncStatusUpdating)), nil
}
func (m *Master) setMasterReadOnlyExitPrepared(ctx context.Context, status metav1.ConditionStatus) {
m.ytsaurus.SetUpdateStatusCondition(ctx, metav1.Condition{
Type: consts.ConditionMasterExitReadOnlyPrepared,
Status: status,
Reason: "MasterExitReadOnlyPrepared",
Message: "Masters are ready to exit read-only state",
})
}