-
Notifications
You must be signed in to change notification settings - Fork 175
/
Copy pathinstall.go
559 lines (483 loc) · 18.7 KB
/
install.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
package cluster
// Copyright (c) Microsoft Corporation.
// Licensed under the Apache License 2.0.
import (
"context"
"fmt"
"time"
"github.com/coreos/go-semver/semver"
configclient "github.com/openshift/client-go/config/clientset/versioned"
imageregistryclient "github.com/openshift/client-go/imageregistry/clientset/versioned"
machineclient "github.com/openshift/client-go/machine/clientset/versioned"
operatorclient "github.com/openshift/client-go/operator/clientset/versioned"
samplesclient "github.com/openshift/client-go/samples/clientset/versioned"
securityclient "github.com/openshift/client-go/security/clientset/versioned"
mcoclient "github.com/openshift/machine-config-operator/pkg/generated/clientset/versioned"
extensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
"github.com/Azure/ARO-RP/pkg/api"
"github.com/Azure/ARO-RP/pkg/containerinstall"
"github.com/Azure/ARO-RP/pkg/database"
aroclient "github.com/Azure/ARO-RP/pkg/operator/clientset/versioned"
"github.com/Azure/ARO-RP/pkg/operator/deploy"
utilgenerics "github.com/Azure/ARO-RP/pkg/util/generics"
"github.com/Azure/ARO-RP/pkg/util/restconfig"
"github.com/Azure/ARO-RP/pkg/util/steps"
"github.com/Azure/ARO-RP/pkg/util/version"
)
const (
operatorCutOffVersion = "4.7.0" // OCP versions older than this will not receive ARO operator updates
)
// AdminUpdate performs an admin update of an ARO cluster
func (m *manager) AdminUpdate(ctx context.Context) error {
toRun := m.adminUpdate()
return m.runSteps(ctx, toRun, "adminUpdate")
}
func (m *manager) adminUpdate() []steps.Step {
task := m.doc.OpenShiftCluster.Properties.MaintenanceTask
isEverything := task == api.MaintenanceTaskEverything || task == ""
isOperator := task == api.MaintenanceTaskOperator
isRenewCerts := task == api.MaintenanceTaskRenewCerts
isSyncClusterObject := task == api.MaintenanceTaskSyncClusterObject
stepsToRun := m.getZerothSteps()
if isEverything {
stepsToRun = utilgenerics.ConcatMultipleSlices(
stepsToRun, m.getGeneralFixesSteps(), m.getCertificateRenewalSteps(),
)
if m.shouldUpdateOperator() {
stepsToRun = append(stepsToRun, m.getOperatorUpdateSteps()...)
}
if m.adoptViaHive && !m.clusterWasCreatedByHive() {
stepsToRun = append(stepsToRun, m.getHiveAdoptionAndReconciliationSteps()...)
}
} else if isOperator {
if m.shouldUpdateOperator() {
stepsToRun = append(stepsToRun, m.getOperatorUpdateSteps()...)
}
} else if isRenewCerts {
stepsToRun = append(stepsToRun, m.getCertificateRenewalSteps()...)
} else if isSyncClusterObject {
stepsToRun = append(stepsToRun, m.getSyncClusterObjectSteps()...)
}
// We don't run this on an operator-only deploy as PUCM scripts then cannot
// determine if the cluster has been fully admin-updated
// Run this last so we capture the resource provider only once the upgrade has been fully performed
if isEverything {
stepsToRun = append(stepsToRun, steps.Action(m.updateProvisionedBy))
}
return stepsToRun
}
func (m *manager) getZerothSteps() []steps.Step {
bootstrap := []steps.Step{
steps.Action(m.initializeKubernetesClients), // must be first
steps.Action(m.ensureBillingRecord), // belt and braces
steps.Action(m.ensureDefaults),
steps.Action(m.fixupClusterSPObjectID),
}
// Generic fix-up actions that are fairly safe to always take, and don't require a running cluster
step0 := []steps.Step{
steps.Action(m.fixInfraID), // Old clusters lacks infraID in the database. Which makes code prone to errors.
}
return append(bootstrap, step0...)
}
func (m *manager) getEnsureAPIServerReadySteps() []steps.Step {
return []steps.Step{
steps.Action(m.startVMs),
steps.Condition(m.apiServersReady, 30*time.Minute, true),
}
}
func (m *manager) getGeneralFixesSteps() []steps.Step {
stepsThatDontNeedAPIServer := []steps.Step{
steps.Action(m.ensureResourceGroup), // re-create RP RBAC if needed after tenant migration
steps.Action(m.createOrUpdateDenyAssignment),
steps.Action(m.ensureServiceEndpoints),
steps.Action(m.populateRegistryStorageAccountName), // must go before migrateStorageAccounts
steps.Action(m.migrateStorageAccounts),
steps.Action(m.fixSSH),
// steps.Action(m.removePrivateDNSZone), // TODO(mj): re-enable once we communicate this out
}
stepsThatNeedAPIServer := []steps.Step{
steps.Action(m.fixSREKubeconfig),
steps.Action(m.fixUserAdminKubeconfig),
steps.Action(m.createOrUpdateRouterIPFromCluster),
steps.Action(m.ensureGatewayUpgrade),
steps.Action(m.rotateACRTokenPassword),
steps.Action(m.populateRegistryStorageAccountName),
steps.Action(m.ensureMTUSize),
}
return utilgenerics.ConcatMultipleSlices(
stepsThatDontNeedAPIServer,
m.getEnsureAPIServerReadySteps(),
stepsThatNeedAPIServer,
)
}
func (m *manager) getCertificateRenewalSteps() []steps.Step {
steps := []steps.Step{
steps.Action(m.populateDatabaseIntIP),
steps.Action(m.fixMCSCert),
steps.Action(m.fixMCSUserData),
steps.Action(m.configureAPIServerCertificate),
steps.Action(m.configureIngressCertificate),
steps.Action(m.initializeOperatorDeployer),
steps.Action(m.renewMDSDCertificate), // Dependent on initializeOperatorDeployer.
}
return utilgenerics.ConcatMultipleSlices(m.getEnsureAPIServerReadySteps(), steps)
}
func (m *manager) getOperatorUpdateSteps() []steps.Step {
steps := []steps.Step{
steps.Action(m.initializeOperatorDeployer),
steps.Action(m.ensureAROOperator),
// The following are dependent on initializeOperatorDeployer.
steps.Condition(m.aroDeploymentReady, 20*time.Minute, true),
steps.Condition(m.ensureAROOperatorRunningDesiredVersion, 5*time.Minute, true),
// Once the ARO Operator is updated, synchronize the Cluster object. This is
// done after the ARO Operator is potentially updated so that any flag
// changes that happen in the same request only apply on the new Operator.
// Otherwise, it is possible for a flag change to occur on the old Operator
// version, then require reconciling to a new version a second time (e.g.
// DNSMasq changes) with the associated node cyclings for the resource
// updates.
steps.Action(m.syncClusterObject),
}
return utilgenerics.ConcatMultipleSlices(m.getEnsureAPIServerReadySteps(), steps)
}
func (m *manager) getSyncClusterObjectSteps() []steps.Step {
steps := []steps.Step{
steps.Action(m.initializeOperatorDeployer),
steps.Action(m.syncClusterObject),
}
return utilgenerics.ConcatMultipleSlices(m.getEnsureAPIServerReadySteps(), steps)
}
func (m *manager) getHiveAdoptionAndReconciliationSteps() []steps.Step {
return []steps.Step{
steps.Action(m.hiveCreateNamespace),
steps.Action(m.hiveEnsureResources),
steps.Condition(m.hiveClusterDeploymentReady, 5*time.Minute, false),
steps.Action(m.hiveResetCorrelationData),
}
}
func (m *manager) shouldUpdateOperator() bool {
runningVersion, err := semver.NewVersion(m.doc.OpenShiftCluster.Properties.ClusterProfile.Version)
if err != nil {
return false
}
cutOffVersion := semver.New(operatorCutOffVersion)
return cutOffVersion.Compare(*runningVersion) <= 0
}
func (m *manager) clusterWasCreatedByHive() bool {
if m.doc.OpenShiftCluster.Properties.HiveProfile.Namespace == "" {
return false
}
return m.doc.OpenShiftCluster.Properties.HiveProfile.CreatedByHive
}
func (m *manager) Update(ctx context.Context) error {
s := []steps.Step{
steps.AuthorizationRetryingAction(m.fpAuthorizer, m.validateResources),
steps.Action(m.initializeKubernetesClients), // All init steps are first
steps.Action(m.initializeOperatorDeployer), // depends on kube clients
// Since ServicePrincipalProfile is now a pointer and our converters re-build the struct,
// our update path needs to enrich the doc with SPObjectID since it was overwritten by our API on put/patch.
steps.AuthorizationRetryingAction(m.fpAuthorizer, m.fixupClusterSPObjectID),
// credentials rotation flow steps
steps.Action(m.createOrUpdateClusterServicePrincipalRBAC),
steps.Action(m.createOrUpdateDenyAssignment),
steps.Action(m.startVMs),
steps.Condition(m.apiServersReady, 30*time.Minute, true),
steps.Action(m.rotateACRTokenPassword),
steps.Action(m.configureAPIServerCertificate),
steps.Action(m.configureIngressCertificate),
steps.Action(m.renewMDSDCertificate),
steps.Action(m.ensureCredentialsRequest),
steps.Action(m.updateOpenShiftSecret),
steps.Condition(m.aroCredentialsRequestReconciled, 3*time.Minute, true),
steps.Action(m.updateAROSecret),
steps.Action(m.restartAROOperatorMaster), // depends on m.updateOpenShiftSecret; the point of restarting is to pick up any changes made to the secret
steps.Condition(m.aroDeploymentReady, 5*time.Minute, true),
steps.Action(m.ensureUpgradeAnnotation),
steps.Action(m.reconcileLoadBalancerProfile),
}
if m.adoptViaHive {
s = append(s,
// Hive reconciliation: we mostly need it to make sure that
// hive has the latest credentials after rotation.
steps.Action(m.hiveCreateNamespace),
steps.Action(m.hiveEnsureResources),
steps.Condition(m.hiveClusterDeploymentReady, 5*time.Minute, true),
steps.Action(m.hiveResetCorrelationData),
)
}
return m.runSteps(ctx, s, "update")
}
func (m *manager) runPodmanInstaller(ctx context.Context) error {
version, err := m.openShiftVersionFromVersion(ctx)
if err != nil {
return err
}
i, err := containerinstall.New(ctx, m.log, m.env, m.doc.ID)
if err != nil {
return err
}
return i.Install(ctx, m.subscriptionDoc, m.doc, version)
}
func (m *manager) runHiveInstaller(ctx context.Context) error {
var err error
m.doc, err = m.db.PatchWithLease(ctx, m.doc.Key, setFieldCreatedByHive(true))
if err != nil {
return err
}
version, err := m.openShiftVersionFromVersion(ctx)
if err != nil {
return err
}
// Run installer. For M5/M6 we will persist the graph inside the installer
// code since it's easier, but in the future, this data should be collected
// from Hive's outputs where needed.
return m.hiveClusterManager.Install(ctx, m.subscriptionDoc, m.doc, version)
}
func setFieldCreatedByHive(createdByHive bool) database.OpenShiftClusterDocumentMutator {
return func(doc *api.OpenShiftClusterDocument) error {
doc.OpenShiftCluster.Properties.HiveProfile.CreatedByHive = createdByHive
return nil
}
}
func (m *manager) bootstrap() []steps.Step {
s := []steps.Step{
steps.AuthorizationRetryingAction(m.fpAuthorizer, m.validateResources),
steps.Action(m.ensurePreconfiguredNSG),
steps.Action(m.ensureACRToken),
steps.Action(m.ensureInfraID),
steps.Action(m.ensureSSHKey),
steps.Action(m.ensureStorageSuffix),
steps.Action(m.populateMTUSize),
steps.Action(m.createDNS),
steps.Action(m.createOIDC),
steps.Action(m.initializeClusterSPClients), // must run before clusterSPObjectID
// TODO: this relies on an authorizer that isn't exposed in the manager
// struct, so we'll rebuild the fpAuthorizer and use the error catching
// to advance
steps.AuthorizationRetryingAction(m.fpAuthorizer, m.clusterSPObjectID),
steps.Action(m.ensureResourceGroup),
steps.Action(m.ensureServiceEndpoints),
steps.Action(m.setMasterSubnetPolicies),
steps.AuthorizationRetryingAction(m.fpAuthorizer, m.deployBaseResourceTemplate),
steps.AuthorizationRetryingAction(m.fpAuthorizer, m.attachNSGs),
steps.Action(m.updateAPIIPEarly),
steps.Action(m.createOrUpdateRouterIPEarly),
steps.Action(m.ensureGatewayCreate),
steps.Action(m.createAPIServerPrivateEndpoint),
steps.Action(m.createCertificates),
}
if m.adoptViaHive || m.installViaHive {
// We will always need a Hive namespace, whether we are installing
// via Hive or adopting
s = append(s, steps.Action(m.hiveCreateNamespace))
}
if m.installViaHive {
s = append(s,
steps.Action(m.runHiveInstaller),
// Give Hive 60 minutes to install the cluster, since this includes
// all of bootstrapping being complete
steps.Condition(m.hiveClusterInstallationComplete, 60*time.Minute, true),
steps.Condition(m.hiveClusterDeploymentReady, 5*time.Minute, true),
steps.Action(m.generateKubeconfigs),
)
} else {
s = append(s,
steps.Action(m.runPodmanInstaller),
steps.Action(m.generateKubeconfigs),
)
if m.adoptViaHive {
s = append(s,
steps.Action(m.hiveEnsureResources),
steps.Condition(m.hiveClusterDeploymentReady, 5*time.Minute, true),
)
}
}
if m.adoptViaHive || m.installViaHive {
s = append(s,
// Reset correlation data whether adopting or installing via Hive
steps.Action(m.hiveResetCorrelationData),
)
}
s = append(s,
steps.Action(m.ensureBillingRecord),
steps.Action(m.initializeKubernetesClients),
steps.Action(m.initializeOperatorDeployer), // depends on kube clients
steps.Condition(m.apiServersReady, 30*time.Minute, true),
steps.Action(m.installAROOperator),
steps.Action(m.incrInstallPhase),
)
return s
}
// Install installs an ARO cluster
func (m *manager) Install(ctx context.Context) error {
steps := map[api.InstallPhase][]steps.Step{
api.InstallPhaseBootstrap: m.bootstrap(),
api.InstallPhaseRemoveBootstrap: {
steps.Action(m.initializeKubernetesClients),
steps.Action(m.initializeOperatorDeployer), // depends on kube clients
steps.Action(m.removeBootstrap),
steps.Action(m.removeBootstrapIgnition),
// Occasionally, the apiserver experiences disruptions, causing the certificate configuration step to fail.
// This issue is currently under investigation.
steps.Condition(m.apiServersReady, 30*time.Minute, true),
steps.Action(m.configureAPIServerCertificate),
steps.Condition(m.apiServersReady, 30*time.Minute, true),
steps.Condition(m.minimumWorkerNodesReady, 30*time.Minute, true),
steps.Condition(m.operatorConsoleExists, 30*time.Minute, true),
steps.Action(m.updateConsoleBranding),
steps.Condition(m.operatorConsoleReady, 20*time.Minute, true),
steps.Action(m.disableSamples),
steps.Action(m.disableOperatorHubSources),
steps.Action(m.disableUpdates),
steps.Condition(m.clusterVersionReady, 30*time.Minute, true),
steps.Condition(m.aroDeploymentReady, 20*time.Minute, true),
steps.Action(m.updateClusterData),
steps.Action(m.configureIngressCertificate),
steps.Condition(m.ingressControllerReady, 30*time.Minute, true),
steps.Action(m.configureDefaultStorageClass),
steps.Action(m.finishInstallation),
},
}
err := m.startInstallation(ctx)
if err != nil {
return err
}
if steps[m.doc.OpenShiftCluster.Properties.Install.Phase] == nil {
return fmt.Errorf("unrecognised phase %s", m.doc.OpenShiftCluster.Properties.Install.Phase)
}
m.log.Printf("starting phase %s", m.doc.OpenShiftCluster.Properties.Install.Phase)
return m.runSteps(ctx, steps[m.doc.OpenShiftCluster.Properties.Install.Phase], "install")
}
func (m *manager) runSteps(ctx context.Context, s []steps.Step, metricsTopic string) error {
var err error
if metricsTopic != "" {
var stepsTimeRun map[string]int64
stepsTimeRun, err = steps.Run(ctx, m.log, 10*time.Second, s, m.now)
if err == nil {
var totalInstallTime int64
for stepName, duration := range stepsTimeRun {
metricName := fmt.Sprintf("backend.openshiftcluster.%s.%s.duration.seconds", metricsTopic, stepName)
m.metricsEmitter.EmitGauge(metricName, duration, nil)
totalInstallTime += duration
}
metricName := fmt.Sprintf("backend.openshiftcluster.%s.duration.total.seconds", metricsTopic)
m.metricsEmitter.EmitGauge(metricName, totalInstallTime, nil)
}
} else {
_, err = steps.Run(ctx, m.log, 10*time.Second, s, nil)
}
if err != nil {
m.gatherFailureLogs(ctx)
}
return err
}
func (m *manager) startInstallation(ctx context.Context) error {
var err error
m.doc, err = m.db.PatchWithLease(ctx, m.doc.Key, func(doc *api.OpenShiftClusterDocument) error {
if doc.OpenShiftCluster.Properties.Install == nil {
// set the install time which is used for the SAS token with which
// the bootstrap node retrieves its ignition payload
doc.OpenShiftCluster.Properties.Install = &api.Install{
Now: time.Now().UTC(),
}
}
return nil
})
return err
}
func (m *manager) incrInstallPhase(ctx context.Context) error {
var err error
m.doc, err = m.db.PatchWithLease(ctx, m.doc.Key, func(doc *api.OpenShiftClusterDocument) error {
doc.OpenShiftCluster.Properties.Install.Phase++
return nil
})
return err
}
func (m *manager) finishInstallation(ctx context.Context) error {
var err error
m.doc, err = m.db.PatchWithLease(ctx, m.doc.Key, func(doc *api.OpenShiftClusterDocument) error {
doc.OpenShiftCluster.Properties.Install = nil
return nil
})
return err
}
// initializeKubernetesClients initializes clients which are used
// once the cluster is up later on in the install process.
func (m *manager) initializeKubernetesClients(ctx context.Context) error {
restConfig, err := restconfig.RestConfig(m.env, m.doc.OpenShiftCluster)
if err != nil {
return err
}
m.kubernetescli, err = kubernetes.NewForConfig(restConfig)
if err != nil {
return err
}
m.dynamiccli, err = dynamic.NewForConfig(restConfig)
if err != nil {
return err
}
m.extensionscli, err = extensionsclient.NewForConfig(restConfig)
if err != nil {
return err
}
m.maocli, err = machineclient.NewForConfig(restConfig)
if err != nil {
return err
}
m.mcocli, err = mcoclient.NewForConfig(restConfig)
if err != nil {
return err
}
m.operatorcli, err = operatorclient.NewForConfig(restConfig)
if err != nil {
return err
}
m.securitycli, err = securityclient.NewForConfig(restConfig)
if err != nil {
return err
}
m.samplescli, err = samplesclient.NewForConfig(restConfig)
if err != nil {
return err
}
m.arocli, err = aroclient.NewForConfig(restConfig)
if err != nil {
return err
}
m.configcli, err = configclient.NewForConfig(restConfig)
if err != nil {
return err
}
m.imageregistrycli, err = imageregistryclient.NewForConfig(restConfig)
if err != nil {
return err
}
mapper, err := apiutil.NewDynamicRESTMapper(restConfig, apiutil.WithLazyDiscovery)
if err != nil {
return err
}
m.client, err = client.New(restConfig, client.Options{
Mapper: mapper,
})
return err
}
// initializeKubernetesClients initializes clients which are used
// once the cluster is up later on in the install process.
func (m *manager) initializeOperatorDeployer(ctx context.Context) (err error) {
m.aroOperatorDeployer, err = deploy.New(m.log, m.env, m.doc.OpenShiftCluster, m.arocli, m.client, m.extensionscli, m.kubernetescli, m.operatorcli, m.subscriptionDoc)
return
}
// updateProvisionedBy sets the deploying resource provider version in
// the cluster document for deployment-tracking purposes.
func (m *manager) updateProvisionedBy(ctx context.Context) error {
var err error
m.doc, err = m.db.PatchWithLease(ctx, m.doc.Key, func(doc *api.OpenShiftClusterDocument) error {
doc.OpenShiftCluster.Properties.ProvisionedBy = version.GitCommit
return nil
})
return err
}