/
host.go
1414 lines (1253 loc) · 56.6 KB
/
host.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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package host
import (
"context"
"encoding/json"
"fmt"
"math"
"net/http"
reflect "reflect"
"strconv"
"strings"
"time"
"github.com/filanov/stateswitch"
"github.com/go-openapi/strfmt"
"github.com/go-openapi/swag"
"github.com/openshift/assisted-service/internal/common"
eventgen "github.com/openshift/assisted-service/internal/common/events"
eventsapi "github.com/openshift/assisted-service/internal/events/api"
"github.com/openshift/assisted-service/internal/hardware"
"github.com/openshift/assisted-service/internal/host/hostcommands"
"github.com/openshift/assisted-service/internal/host/hostutil"
"github.com/openshift/assisted-service/internal/metrics"
"github.com/openshift/assisted-service/internal/operators"
"github.com/openshift/assisted-service/internal/provider/registry"
"github.com/openshift/assisted-service/models"
"github.com/openshift/assisted-service/pkg/leader"
logutil "github.com/openshift/assisted-service/pkg/log"
"github.com/openshift/assisted-service/pkg/s3wrapper"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/thoas/go-funk"
"gorm.io/gorm"
"k8s.io/apimachinery/pkg/types"
)
var InstallationProgressTimeout = map[models.HostStage]time.Duration{
models.HostStageStartingInstallation: 30 * time.Minute,
models.HostStageWaitingForControlPlane: 60 * time.Minute,
models.HostStageWaitingForController: 60 * time.Minute,
models.HostStageWaitingForBootkube: 60 * time.Minute,
models.HostStageInstalling: 60 * time.Minute,
models.HostStageJoined: 60 * time.Minute,
models.HostStageWritingImageToDisk: 30 * time.Minute,
models.HostStageRebooting: 40 * time.Minute,
models.HostStageConfiguring: 60 * time.Minute,
models.HostStageWaitingForIgnition: 24 * time.Hour,
"DEFAULT": 60 * time.Minute,
}
const singleNodeRebootTimeout = 80 * time.Minute
var WrongBootOrderIgnoreTimeoutStages = []models.HostStage{
models.HostStageWaitingForControlPlane,
models.HostStageWaitingForController,
models.HostStageWaitingForBootkube,
models.HostStageRebooting,
}
var InstallationTimeout = 20 * time.Minute
var ReclaimTimeout = 60 * time.Minute
// Weights for sorting hosts in the monitor
const (
HostWeightMinimumCpuCores float64 = 4
HostWeightMinimumMemGib float64 = 16
HostWeightMinimumDiskCapacityGib float64 = 100
HostWeightMemWeight float64 = 0.1
HostWeightDiskWeight float64 = 0.004
)
type LogTimeoutConfig struct {
LogCollectionTimeout time.Duration `envconfig:"HOST_LOG_COLLECTION_TIMEOUT" default:"10m"`
LogPendingTimeout time.Duration `envconfig:"HOST_LOG_PENDING_TIMEOUT" default:"2m"`
}
type Config struct {
LogTimeoutConfig
EnableAutoReset bool `envconfig:"ENABLE_AUTO_RESET" default:"false"`
EnableAutoAssign bool `envconfig:"ENABLE_AUTO_ASSIGN" default:"true"`
ResetTimeout time.Duration `envconfig:"RESET_CLUSTER_TIMEOUT" default:"3m"`
MonitorBatchSize int `envconfig:"HOST_MONITOR_BATCH_SIZE" default:"100"`
DisabledHostvalidations DisabledHostValidations `envconfig:"DISABLED_HOST_VALIDATIONS" default:""` // Which host validations to disable (should not run in preprocess)
BootstrapHostMAC string `envconfig:"BOOTSTRAP_HOST_MAC" default:""` // For ephemeral installer to ensure the bootstrap for the (single) cluster lands on the same host as assisted-service
MaxHostDisconnectionTime time.Duration `envconfig:"HOST_MAX_DISCONNECTION_TIME" default:"3m"`
EnableVirtualInterfaces bool `envconfig:"ENABLE_VIRTUAL_INTERFACES" default:"false"`
}
//go:generate mockgen --build_flags=--mod=mod -package=host -aux_files=github.com/openshift/assisted-service/internal/host/hostcommands=instruction_manager.go -destination=mock_host_api.go . API
type API interface {
hostcommands.InstructionApi
// Register a new host
RegisterHost(ctx context.Context, h *models.Host, db *gorm.DB) error
UnRegisterHost(ctx context.Context, hostID, infraEnvID string) error
HandleInstallationFailure(ctx context.Context, h *models.Host) error
HandleMediaDisconnected(ctx context.Context, h *models.Host) error
HandleReclaimBootArtifactDownload(ctx context.Context, h *models.Host) error
HandleReclaimFailure(ctx context.Context, h *models.Host) error
UpdateInstallProgress(ctx context.Context, h *models.Host, progress *models.HostProgress) error
RefreshStatus(ctx context.Context, h *models.Host, db *gorm.DB) error
SetBootstrap(ctx context.Context, h *models.Host, isbootstrap bool, db *gorm.DB) error
UpdateConnectivityReport(ctx context.Context, h *models.Host, connectivityReport string) error
UpdateApiVipConnectivityReport(ctx context.Context, h *models.Host, connectivityReport string) error
UpdateTangConnectivityReport(ctx context.Context, h *models.Host, connectivityReport string) error
HostMonitoring()
CancelInstallation(ctx context.Context, h *models.Host, reason string, db *gorm.DB) *common.ApiErrorResponse
IsRequireUserActionReset(h *models.Host) bool
ResetHost(ctx context.Context, h *models.Host, reason string, db *gorm.DB) *common.ApiErrorResponse
ResetPendingUserAction(ctx context.Context, h *models.Host, db *gorm.DB) error
// Install host - db is optional, for transactions
Install(ctx context.Context, h *models.Host, db *gorm.DB) error
GetStagesByRole(h *models.Host, isSNO bool) []models.HostStage
IndexOfStage(element models.HostStage, data []models.HostStage) int
IsInstallable(h *models.Host) bool
// auto assign host role
AutoAssignRole(ctx context.Context, h *models.Host, db *gorm.DB) (bool, error)
RefreshRole(ctx context.Context, h *models.Host, db *gorm.DB) error
IsValidMasterCandidate(h *models.Host, c *common.Cluster, db *gorm.DB, log logrus.FieldLogger) (bool, error)
SetUploadLogsAt(ctx context.Context, h *models.Host, db *gorm.DB) error
UpdateLogsProgress(ctx context.Context, h *models.Host, progress string) error
PermanentHostsDeletion(olderThan strfmt.DateTime) error
ReportValidationFailedMetrics(ctx context.Context, h *models.Host, ocpVersion, emailDomain string) error
UpdateRole(ctx context.Context, h *models.Host, role models.HostRole, db *gorm.DB) error
UpdateHostname(ctx context.Context, h *models.Host, hostname string, db *gorm.DB) error
UpdateInventory(ctx context.Context, h *models.Host, inventory string) error
UpdateMediaConnected(ctx context.Context, h *models.Host) error
RefreshInventory(ctx context.Context, cluster *common.Cluster, h *models.Host, db *gorm.DB) error
UpdateNTP(ctx context.Context, h *models.Host, ntpSources []*models.NtpSource, db *gorm.DB) error
UpdateMachineConfigPoolName(ctx context.Context, db *gorm.DB, h *models.Host, machineConfigPoolName string) error
UpdateIgnitionEndpointToken(ctx context.Context, db *gorm.DB, h *models.Host, token string) error
UpdateNodeLabels(ctx context.Context, h *models.Host, nodeLabelsStr string, db *gorm.DB) error
UpdateNodeSkipDiskFormatting(ctx context.Context, h *models.Host, skipDiskFormatting string, db *gorm.DB) error
UpdateInstallationDisk(ctx context.Context, db *gorm.DB, h *models.Host, installationDiskId string) error
UpdateKubeKeyNS(ctx context.Context, hostID, namespace string) error
GetHostValidDisks(role *models.Host) ([]*models.Disk, error)
UpdateImageStatus(ctx context.Context, h *models.Host, imageStatus *models.ContainerImageAvailability, db *gorm.DB) error
SetDiskSpeed(ctx context.Context, h *models.Host, path string, speedMs int64, exitCode int64, db *gorm.DB) error
ResetHostValidation(ctx context.Context, hostID, infraEnvID strfmt.UUID, validationID string, db *gorm.DB) error
GetHostByKubeKey(key types.NamespacedName) (*common.Host, error)
UpdateDomainNameResolution(ctx context.Context, h *models.Host, domainResolutionResponse models.DomainResolutionResponse, db *gorm.DB) error
BindHost(ctx context.Context, h *models.Host, clusterID strfmt.UUID, db *gorm.DB) error
UnbindHost(ctx context.Context, h *models.Host, db *gorm.DB, reclaim bool) error
GetKnownHostApprovedCounts(clusterID strfmt.UUID) (registered, approved int, err error)
HostWithCollectedLogsExists(clusterId strfmt.UUID) (bool, error)
GetKnownApprovedHosts(clusterId strfmt.UUID) ([]*common.Host, error)
}
type Manager struct {
log logrus.FieldLogger
db *gorm.DB
instructionApi hostcommands.InstructionApi
hwValidator hardware.Validator
eventsHandler eventsapi.Handler
sm stateswitch.StateMachine
rp *refreshPreprocessor
metricApi metrics.API
Config Config
leaderElector leader.Leader
monitorClusterQueryGenerator *common.MonitorClusterQueryGenerator
monitorInfraEnvQueryGenerator *common.MonitorInfraEnvQueryGenerator
kubeApiEnabled bool
objectHandler s3wrapper.API
}
func NewManager(log logrus.FieldLogger, db *gorm.DB, eventsHandler eventsapi.Handler, hwValidator hardware.Validator, instructionApi hostcommands.InstructionApi,
hwValidatorCfg *hardware.ValidatorCfg, metricApi metrics.API, config *Config, leaderElector leader.ElectorInterface, operatorsApi operators.API, providerRegistry registry.ProviderRegistry, kubeApiEnabled bool, objectHandler s3wrapper.API) *Manager {
th := &transitionHandler{
db: db,
log: log,
config: config,
eventsHandler: eventsHandler,
}
sm := NewHostStateMachine(stateswitch.NewStateMachine(), th)
sm = NewPoolHostStateMachine(sm, th)
return &Manager{
log: log,
db: db,
instructionApi: instructionApi,
hwValidator: hwValidator,
eventsHandler: eventsHandler,
sm: sm,
rp: newRefreshPreprocessor(log, hwValidatorCfg, hwValidator, operatorsApi, config.DisabledHostvalidations, providerRegistry),
metricApi: metricApi,
Config: *config,
leaderElector: leaderElector,
kubeApiEnabled: kubeApiEnabled,
objectHandler: objectHandler,
}
}
func (m *Manager) RegisterHost(ctx context.Context, h *models.Host, db *gorm.DB) error {
dbHost, err := common.GetHostFromDB(db, h.InfraEnvID.String(), h.ID.String())
var host *models.Host
if err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) {
return err
}
// Delete any previews record of the host if it was soft deleted from the cluster,
// no error will be returned if the host was not existed.
if err := db.Unscoped().Delete(&common.Host{}, "id = ? and infra_env_id = ?", *h.ID, h.InfraEnvID).Error; err != nil {
return errors.Wrapf(
err,
"error while trying to delete previews record from db (if exists) of host %s in infra env %s",
h.ID.String(), h.InfraEnvID.String())
}
host = h
} else {
host = &dbHost.Host
if h != nil {
host.Kind = h.Kind
}
// If the host already exists in the DB, we update its registration timestamp to track
// the subsequent registration events. This can happen if the host rebooted or the agent has
// been restarted.
if err := db.Model(&common.Host{}).Where("id = ? and infra_env_id = ?", host.ID, host.InfraEnvID).Update("registered_at", strfmt.DateTime(time.Now())).Error; err != nil {
return errors.Wrapf(
err,
"error while updating registration timestamp of host %s in infra env %s",
host.ID.String(), host.InfraEnvID.String())
}
}
return m.sm.Run(TransitionTypeRegisterHost, newStateHost(host), &TransitionArgsRegisterHost{
ctx: ctx,
discoveryAgentVersion: h.DiscoveryAgentVersion,
db: db,
})
}
func (m *Manager) HandleInstallationFailure(ctx context.Context, h *models.Host) error {
lastStatusUpdateTime := h.StatusUpdatedAt
err := m.sm.Run(TransitionTypeHostInstallationFailed, newStateHost(h), &TransitionArgsHostInstallationFailed{
ctx: ctx,
reason: "installation command failed",
})
if err == nil {
m.reportInstallationMetrics(ctx, h, &models.HostProgressInfo{CurrentStage: "installation command failed",
StageStartedAt: lastStatusUpdateTime}, models.HostStageFailed)
}
return err
}
func (m *Manager) HandleMediaDisconnected(ctx context.Context, h *models.Host) error {
return m.sm.Run(TransitionTypeMediaDisconnect, newStateHost(h), &TransitionArgsMediaDisconnected{ctx: ctx, db: m.db})
}
// populateDisksEligibility updates a given inventory object by updating the
// eligibility struct of each disk in the inventory with service-side checks
// for disk eligibility, in addition to agent-side checks that have already
// been performed. The reason that some checks are performed by the agent (and
// not the service) is because the agent has data that is not available in the
// service.
func (m *Manager) populateDisksEligibility(ctx context.Context, inventory *models.Inventory, infraEnv *common.InfraEnv, cluster *common.Cluster, host *models.Host) error {
for _, disk := range inventory.Disks {
if !hardware.DiskEligibilityInitialized(disk) {
// for backwards compatibility, pretend that the agent has decided that this disk is eligible
disk.InstallationEligibility.Eligible = true
disk.InstallationEligibility.NotEligibleReasons = make([]string, 0)
}
// Append to the existing reasons already filled in by the agent
reasons, err := m.hwValidator.DiskIsEligible(ctx, disk, infraEnv, cluster, host, inventory.Disks)
if err != nil {
return err
}
disk.InstallationEligibility.NotEligibleReasons = reasons
disk.InstallationEligibility.Eligible = len(disk.InstallationEligibility.NotEligibleReasons) == 0
}
return nil
}
// populateDisksId ensures that every disk has an id.
// The id used to identify the disk and mark a disk as selected
// This value should be equal to the host.installationDiskId
func (m *Manager) populateDisksId(inventory *models.Inventory) {
for _, disk := range inventory.Disks {
if disk.ID == "" {
disk.ID = common.GetDeviceIdentifier(disk)
}
}
}
func (m *Manager) HandlePrepareInstallationFailure(ctx context.Context, h *models.Host, reason string) error {
lastStatusUpdateTime := h.StatusUpdatedAt
err := m.sm.Run(TransitionTypeHostInstallationFailed, newStateHost(h), &TransitionArgsHostInstallationFailed{
ctx: ctx,
reason: reason,
})
if err == nil {
m.reportInstallationMetrics(ctx, h, &models.HostProgressInfo{CurrentStage: "installation command failed",
StageStartedAt: lastStatusUpdateTime}, models.HostStageFailed)
}
return err
}
func (m *Manager) RefreshInventory(ctx context.Context, cluster *common.Cluster, h *models.Host, db *gorm.DB) error {
return m.updateInventory(ctx, cluster, h, h.Inventory, db)
}
func (m *Manager) UpdateInventory(ctx context.Context, h *models.Host, inventoryStr string) error {
return m.updateInventory(ctx, nil, h, inventoryStr, m.db)
}
func (m *Manager) updateInventory(ctx context.Context, cluster *common.Cluster, h *models.Host, inventoryStr string, db *gorm.DB) error {
log := logutil.FromContext(ctx, m.log)
hostStatus := swag.StringValue(h.Status)
allowedStatuses := append(hostStatusesBeforeInstallation[:], models.HostStatusInstallingInProgress)
allowedStatuses = append(allowedStatuses, hostStatusesInInfraEnv[:]...)
if !funk.ContainsString(allowedStatuses, hostStatus) {
return common.NewApiError(http.StatusConflict,
errors.Errorf("Host is in %s state, host can be updated only in one of %s states",
hostStatus, allowedStatuses))
}
inventory, err := common.UnmarshalInventory(inventoryStr)
if err != nil {
return err
}
infraEnv, err := common.GetInfraEnvFromDB(db, h.InfraEnvID)
if err != nil {
log.WithError(err).Errorf("not updating inventory - failed to find infra env %s", h.InfraEnvID.String())
return common.NewApiError(http.StatusNotFound, err)
}
if h.ClusterID != nil && h.ClusterID.String() != "" {
cluster, err = common.GetClusterFromDB(db, *h.ClusterID, common.SkipEagerLoading)
if err != nil {
log.WithError(err).Errorf("not updating inventory - failed to find cluster %s", h.ClusterID.String())
return common.NewApiError(http.StatusNotFound, err)
}
if m.Config.BootstrapHostMAC != "" && !h.Bootstrap {
for _, iface := range inventory.Interfaces {
if iface.MacAddress == m.Config.BootstrapHostMAC {
log.Infof("selected local bootstrap host %s for cluster %s", h.ID, cluster.ID)
err = updateRole(log, h, models.HostRoleMaster, models.HostRoleMaster, db, string(h.Role))
if err != nil {
log.WithError(err).Errorf("failed to set master role on bootstrap host for cluster %s", cluster.ID)
return errors.Wrapf(err, "Failed to set master role on bootstrap host for cluster %s", cluster.ID)
}
err = m.SetBootstrap(ctx, h, true, db)
if err != nil {
log.WithError(err).Errorf("failed to update bootstrap host for cluster %s", cluster.ID)
return errors.Wrapf(err, "Failed to update bootstrap host for cluster %s", cluster.ID)
}
break
}
}
}
}
err = m.populateDisksEligibility(ctx, inventory, infraEnv, cluster, h)
if err != nil {
log.WithError(err).Errorf("not updating inventory - failed to check disks eligibility for host %s", h.ID)
return common.NewApiError(http.StatusInternalServerError, err)
}
existingHostInventory, err := common.UnmarshalInventory(h.Inventory)
if err != nil {
log.WithError(err).Debugf("failed to unmarshal host existing inventory %s", h.Inventory)
}
var physicalInterfaces []*models.Interface = make([]*models.Interface, 0)
var virtualInterfaces []string = make([]string, 0)
for _, intf := range inventory.Interfaces {
if intf.Type == "" || intf.Type == "physical" || intf.Type == "vlan" || intf.Type == "bond" {
// Empty interface type indicates an older agent, which only passes physical interfaces
physicalInterfaces = append(physicalInterfaces, intf)
} else {
virtualInterfaces = append(virtualInterfaces, intf.Type)
}
}
// Only record metrics if it's new/changed inventory and the host is associated with a cluster
if existingHostInventory == nil || (existingHostInventory != nil && len(physicalInterfaces) != len(existingHostInventory.Interfaces)) {
if h.ClusterID != nil {
m.eventsHandler.AddMetricsEvent(ctx, *h.ClusterID, h.ID, models.EventSeverityInfo, "nic.virtual_interfaces", time.Now(), "count", len(virtualInterfaces), "types", virtualInterfaces)
m.eventsHandler.AddMetricsEvent(ctx, *h.ClusterID, h.ID, models.EventSeverityInfo, "nic.physical_interfaces", time.Now(), "count", len(physicalInterfaces))
}
}
// Don't store virtual interfaces if the feature is not enabled
if !m.Config.EnableVirtualInterfaces {
inventory.Interfaces = physicalInterfaces
}
m.populateDisksId(inventory)
inventoryStr, err = common.MarshalInventory(inventory)
if err != nil {
return err
}
validDisks := m.hwValidator.ListEligibleDisks(inventory)
installationDisk := hostutil.DetermineInstallationDisk(validDisks, hostutil.GetHostInstallationPath(h))
var (
installationDiskPath string
installationDiskID string
)
if installationDisk == nil {
installationDiskPath = ""
installationDiskID = ""
} else {
installationDiskPath = common.GetDeviceFullName(installationDisk)
installationDiskID = common.GetDeviceIdentifier(installationDisk)
}
disksToBeFormatted := strings.Join(common.GetDisksIdentifiersToBeFormatted(inventory), ",")
// If there is substantial change in the inventory that might cause the state machine to move to a new status
// or one of the validations to change, then the updated_at field has to be modified. Otherwise, we just
// perform update with touching the updated_at field
return db.Model(h).Updates(map[string]interface{}{
"inventory": inventoryStr,
"installation_disk_path": installationDiskPath,
"installation_disk_id": installationDiskID,
"disks_to_be_formatted": disksToBeFormatted,
}).Error
}
func (m *Manager) UpdateMediaConnected(_ context.Context, h *models.Host) error {
return m.db.Model(h).Updates(map[string]interface{}{
"media_status": models.HostMediaStatusConnected,
}).Error
}
func (m *Manager) refreshRoleInternal(ctx context.Context, h *models.Host, db *gorm.DB, forceRefresh bool) error {
//update suggested role, if not yet set
var suggestedRole models.HostRole
var err error
if m.Config.EnableAutoAssign || forceRefresh {
//because of possible hw changes, or new host being registered
//suggested role should be calculated periodically even if the
//suggested role is already set
if h.Role == models.HostRoleAutoAssign &&
funk.ContainsString(hostStatusesBeforeInstallation[:], *h.Status) {
host := *h //must have a defensive copy becuase selectRole changes the host object
if suggestedRole, err = m.selectRole(ctx, &host, db); err == nil {
m.log.Debugf("calculated role for host %s is %s (original suggested = %s)", hostutil.GetHostnameForMsg(h), suggestedRole, h.SuggestedRole)
if h.SuggestedRole != suggestedRole {
if err = updateRole(m.log, h, h.Role, suggestedRole, db, string(h.Role)); err == nil {
h.SuggestedRole = suggestedRole
m.log.Infof("suggested role for host %s is %s", *h.ID, suggestedRole)
eventgen.SendHostRoleUpdatedEvent(ctx, m.eventsHandler, *h.ID, h.InfraEnvID, hostutil.GetHostnameForMsg(h), string(suggestedRole))
}
}
}
}
}
return err
}
func (m *Manager) refreshStatusInternal(ctx context.Context, h *models.Host, c *common.Cluster, i *common.InfraEnv,
inventoryCache InventoryCache, db *gorm.DB) error {
log := logutil.FromContext(ctx, m.log)
if db == nil {
db = m.db
}
var (
vc *validationContext
err error
conditions map[string]bool
newValidationRes ValidationsStatus
)
vc, err = newValidationContext(ctx, h, c, i, db, inventoryCache, m.hwValidator, m.kubeApiEnabled, m.objectHandler)
if err != nil {
return err
}
conditions, newValidationRes, err = m.rp.preprocess(ctx, vc)
if err != nil {
return err
}
currentValidationRes, err := GetValidations(h)
if err != nil {
return err
}
log.Debugf("Host %s: validation details: %+v", hostutil.GetHostnameForMsg(h), currentValidationRes)
if m.didValidationChanged(ctx, newValidationRes, currentValidationRes) {
// Validation status changes are detected when new validations are different from the
// current validations in the DB.
// For changes to be detected and reported correctly, the comparison needs to be
// performed before the new validations are updated to the DB.
m.reportValidationStatusChanged(ctx, vc, h, newValidationRes, currentValidationRes)
_, err = m.updateValidationsInDB(ctx, db, h, newValidationRes)
if err != nil {
return err
}
}
err = m.sm.Run(TransitionTypeRefresh, newStateHost(h), &TransitionArgsRefreshHost{
ctx: ctx,
db: db,
eventHandler: m.eventsHandler,
conditions: conditions,
validationResults: newValidationRes,
})
if err != nil {
return common.NewApiError(http.StatusConflict, err)
}
return nil
}
func (m *Manager) RefreshRole(ctx context.Context, h *models.Host, db *gorm.DB) error {
if db == nil {
db = m.db
}
return m.refreshRoleInternal(ctx, h, db, true)
}
func (m *Manager) RefreshStatus(ctx context.Context, h *models.Host, db *gorm.DB) error {
if db == nil {
db = m.db
}
return m.refreshStatusInternal(ctx, h, nil, nil, make(InventoryCache), db)
}
func (m *Manager) Install(ctx context.Context, h *models.Host, db *gorm.DB) error {
cdb := m.db
if db != nil {
cdb = db
}
return m.sm.Run(TransitionTypeInstallHost, newStateHost(h), &TransitionArgsInstallHost{
ctx: ctx,
db: cdb,
})
}
func (m *Manager) BindHost(ctx context.Context, h *models.Host, clusterID strfmt.UUID, db *gorm.DB) error {
return m.sm.Run(TransitionTypeBindHost, newStateHost(h), &TransitionArgsBindHost{
ctx: ctx,
db: db,
clusterID: clusterID,
})
}
func (m *Manager) UnbindHost(ctx context.Context, h *models.Host, db *gorm.DB, reclaim bool) error {
transition := TransitionTypeUnbindHost
if reclaim {
transition = TransitionTypeReclaimHost
}
return m.sm.Run(stateswitch.TransitionType(transition), newStateHost(h), &TransitionArgsUnbindHost{
ctx: ctx,
db: db,
})
}
func (m *Manager) GetNextSteps(ctx context.Context, host *models.Host) (models.Steps, error) {
return m.instructionApi.GetNextSteps(ctx, host)
}
func (m *Manager) IndexOfStage(element models.HostStage, data []models.HostStage) int {
return IndexOfStage(element, data)
}
func (m *Manager) UpdateInstallProgress(ctx context.Context, h *models.Host, progress *models.HostProgress) error {
previousProgress := h.Progress
if previousProgress != nil &&
previousProgress.CurrentStage == progress.CurrentStage {
if previousProgress.ProgressInfo == progress.ProgressInfo {
return nil
}
updates := map[string]interface{}{
"progress_progress_info": progress.ProgressInfo,
"progress_stage_updated_at": strfmt.DateTime(time.Now()),
}
return m.db.Model(h).UpdateColumns(updates).Error
}
validStatuses := []string{
models.HostStatusInstalling, models.HostStatusInstallingInProgress, models.HostStatusInstallingPendingUserAction,
}
if !funk.ContainsString(validStatuses, swag.StringValue(h.Status)) {
return errors.Errorf("Can't set progress <%s> to host in status <%s>", progress.CurrentStage, swag.StringValue(h.Status))
}
var extra []interface{}
if progress.CurrentStage != models.HostStageFailed {
isSno := hostutil.IsSingleNode(m.log, m.db, h)
stages := m.GetStagesByRole(h, isSno)
if previousProgress != nil && previousProgress.CurrentStage != "" {
// Verify the new stage is higher or equal to the current host stage according to its role stages array
currentIndex := m.IndexOfStage(progress.CurrentStage, stages)
if currentIndex == -1 {
return errors.Errorf("Stages %s isn't available for host role %s bootstrap %s",
progress.CurrentStage, h.Role, strconv.FormatBool(h.Bootstrap))
}
if currentIndex < m.IndexOfStage(previousProgress.CurrentStage, stages) {
return errors.Errorf("Can't assign lower stage \"%s\" after host has been in stage \"%s\"",
progress.CurrentStage, previousProgress.CurrentStage)
}
}
currentIndex := m.IndexOfStage(progress.CurrentStage, stages)
installationPercentage := (float64(currentIndex+1) / float64(len(stages))) * 100
extra = append(extra, "progress_installation_percentage", installationPercentage)
}
statusInfo := string(progress.CurrentStage)
var err error
switch progress.CurrentStage {
case models.HostStageDone:
_, err = hostutil.UpdateHostProgress(ctx, logutil.FromContext(ctx, m.log), m.db, m.eventsHandler, h.InfraEnvID, *h.ID,
swag.StringValue(h.Status), models.HostStatusInstalled, statusInfo,
previousProgress.CurrentStage, progress.CurrentStage, progress.ProgressInfo, extra...)
case models.HostStageFailed:
// Keeps the last progress
if progress.ProgressInfo != "" {
statusInfo += fmt.Sprintf(" - %s", progress.ProgressInfo)
}
_, err = hostutil.UpdateHostStatus(ctx, logutil.FromContext(ctx, m.log), m.db, m.eventsHandler, h.InfraEnvID, *h.ID,
swag.StringValue(h.Status), models.HostStatusError, statusInfo)
case models.HostStageRebooting:
if swag.StringValue(h.Kind) == models.HostKindAddToExistingClusterHost {
infoMessage := statusInfo
stage := models.HostStageDone
if !m.kubeApiEnabled {
// in case kubeApiEnabled the agent controller will keep updating the host stage until the installation is complete
infoMessage = statusInfoRebootingDay2
stage = models.HostStageRebooting
}
_, err = hostutil.UpdateHostProgress(ctx, logutil.FromContext(ctx, m.log), m.db, m.eventsHandler, h.InfraEnvID, *h.ID,
swag.StringValue(h.Status), models.HostStatusAddedToExistingCluster, infoMessage,
h.Progress.CurrentStage, stage, progress.ProgressInfo, extra...)
break
}
fallthrough
default:
_, err = hostutil.UpdateHostProgress(ctx, logutil.FromContext(ctx, m.log), m.db, m.eventsHandler, h.InfraEnvID, *h.ID,
swag.StringValue(h.Status), models.HostStatusInstallingInProgress, statusInfo,
previousProgress.CurrentStage, progress.CurrentStage, progress.ProgressInfo, extra...)
}
m.reportInstallationMetrics(ctx, h, previousProgress, progress.CurrentStage)
return err
}
func (m *Manager) SetBootstrap(ctx context.Context, h *models.Host, isbootstrap bool, db *gorm.DB) error {
if h.Bootstrap != isbootstrap {
err := db.Model(h).Update("bootstrap", isbootstrap).Error
if err != nil {
return errors.Wrapf(err, "failed to set bootstrap to host %s", h.ID.String())
}
eventgen.SendHostBootstrapSetEvent(ctx, m.eventsHandler, *h.ID, h.InfraEnvID, h.ClusterID, hostutil.GetHostnameForMsg(h))
}
return nil
}
func (m *Manager) UpdateLogsProgress(ctx context.Context, h *models.Host, progress string) error {
_, err := hostutil.UpdateLogsProgress(ctx, logutil.FromContext(ctx, m.log), m.db, m.eventsHandler, h.InfraEnvID, *h.ID,
swag.StringValue(h.Status), progress)
return err
}
func (m *Manager) SetUploadLogsAt(ctx context.Context, h *models.Host, db *gorm.DB) error {
err := db.Model(h).Update("logs_collected_at", strfmt.DateTime(time.Now())).Error
if err != nil {
return errors.Wrapf(err, "failed to set logs_collected_at to host %s", h.ID.String())
}
return nil
}
func (m *Manager) UpdateConnectivityReport(ctx context.Context, h *models.Host, connectivityReport string) error {
if h.Connectivity != connectivityReport {
// Only if the connectivity between the hosts changed change the updated_at field
if err := m.db.Model(h).Update("connectivity", connectivityReport).Error; err != nil {
return errors.Wrapf(err, "failed to set connectivity to host %s", h.ID.String())
}
}
return nil
}
func (m *Manager) UpdateApiVipConnectivityReport(ctx context.Context, h *models.Host, apiVipConnectivityReport string) error {
if h.APIVipConnectivity != apiVipConnectivityReport {
if err := m.db.Model(h).Update("api_vip_connectivity", apiVipConnectivityReport).Error; err != nil {
return errors.Wrapf(err, "failed to set api_vip_connectivity to host %s", h.ID.String())
}
}
return nil
}
func (m *Manager) UpdateTangConnectivityReport(ctx context.Context, h *models.Host, tangConnectivityReport string) error {
if h.TangConnectivity != tangConnectivityReport {
if err := m.db.Model(h).Update("tang_connectivity", tangConnectivityReport).Error; err != nil {
return errors.Wrapf(err, "failed to set tang_connectivity to host %s", h.ID.String())
}
}
return nil
}
func (m *Manager) UpdateRole(ctx context.Context, h *models.Host, role models.HostRole, db *gorm.DB) error {
cdb := m.db
if db != nil {
cdb = db
}
//UpdateRole is always invoked from an API and therefore
//the roles are user-selected. In this case suggested roles
//takes the user selection
return updateRole(m.log, h, role, role, cdb, string(h.Role))
}
func (m *Manager) UpdateMachineConfigPoolName(ctx context.Context, db *gorm.DB, h *models.Host, machineConfigPoolName string) error {
hostStatus := swag.StringValue(h.Status)
if !funk.ContainsString(hostStatusesBeforeInstallationOrUnbound[:], hostStatus) {
return common.NewApiError(http.StatusBadRequest,
errors.Errorf("Host is in %s state, host machine config pool can be set only in one of %s states",
hostStatus, hostStatusesBeforeInstallation[:]))
}
cdb := m.db
if db != nil {
cdb = db
}
return cdb.Model(common.Host{Host: *h}).Updates(map[string]interface{}{"machine_config_pool_name": machineConfigPoolName, "trigger_monitor_timestamp": time.Now()}).Error
}
func (m *Manager) UpdateIgnitionEndpointToken(ctx context.Context, db *gorm.DB, h *models.Host, token string) error {
hostStatus := swag.StringValue(h.Status)
if token != "" && !funk.ContainsString(hostStatusesBeforeInstallationOrUnbound[:], hostStatus) {
return common.NewApiError(http.StatusBadRequest,
errors.Errorf("Host is in %s state, host ignition endpoint token can be set only in one of %s states",
hostStatus, hostStatusesBeforeInstallation[:]))
}
cdb := m.db
if db != nil {
cdb = db
}
tokenSet := true
if token == "" {
tokenSet = false
}
return cdb.Model(common.Host{Host: *h}).Updates(map[string]interface{}{
"ignition_endpoint_token": token,
"ignition_endpoint_token_set": tokenSet,
"trigger_monitor_timestamp": time.Now()}).Error
}
func (m *Manager) UpdateNodeLabels(ctx context.Context, h *models.Host, nodeLabelsStr string, db *gorm.DB) error {
hostStatus := swag.StringValue(h.Status)
if !funk.ContainsString(hostStatusesBeforeInstallationOrUnbound[:], hostStatus) {
return common.NewApiError(http.StatusBadRequest,
errors.Errorf("Host is in %s state, labels can be set only in one of %s states",
hostStatus, hostStatusesBeforeInstallation[:]))
}
h.NodeLabels = nodeLabelsStr
cdb := m.db
if db != nil {
cdb = db
}
return cdb.Model(common.Host{Host: *h}).Updates(map[string]interface{}{"node_labels": nodeLabelsStr, "trigger_monitor_timestamp": time.Now()}).Error
}
func (m *Manager) UpdateNodeSkipDiskFormatting(ctx context.Context, h *models.Host, skipDiskFormatting string, db *gorm.DB) error {
hostStatus := swag.StringValue(h.Status)
if !funk.ContainsString(hostStatusesBeforeInstallationOrUnbound[:], hostStatus) {
return common.NewApiError(http.StatusBadRequest,
errors.Errorf("Host is in %s state, skip disk formatting can be set only in one of %s states",
hostStatus, hostStatusesBeforeInstallation[:]))
}
h.SkipFormattingDisks = skipDiskFormatting
cdb := m.db
if db != nil {
cdb = db
}
return cdb.Model(common.Host{Host: *h}).Updates(map[string]interface{}{"skip_formatting_disks": h.SkipFormattingDisks, "trigger_monitor_timestamp": time.Now()}).Error
}
func (m *Manager) UpdateNTP(ctx context.Context, h *models.Host, ntpSources []*models.NtpSource, db *gorm.DB) error {
bytes, err := json.Marshal(ntpSources)
if err != nil {
return errors.Wrapf(err, "Failed to marshal NTP sources for host %s", h.ID.String())
}
m.log.Infof("Updating ntp source of host %s to %s", h.ID, string(bytes))
return db.Model(h).Update("ntp_sources", string(bytes)).Error
}
func (m *Manager) UpdateDomainNameResolution(ctx context.Context, h *models.Host, domainResolutionResponse models.DomainResolutionResponse, db *gorm.DB) error {
response, err := json.Marshal(domainResolutionResponse)
if err != nil {
return errors.Wrapf(err, "Failed to marshal domain name resolution for host %s", h.ID.String())
}
if db == nil {
db = m.db
}
if string(response) != h.DomainNameResolutions {
updates := map[string]interface{}{
"domain_name_resolutions": string(response),
"trigger_monitor_timestamp": time.Now(),
}
if err := db.Model(h).Updates(updates).Error; err != nil {
return errors.Wrapf(err, "failed to update api_domain_name_resolution to host %s", h.ID.String())
}
}
return nil
}
func (m *Manager) UpdateImageStatus(ctx context.Context, h *models.Host, newImageStatus *models.ContainerImageAvailability, db *gorm.DB) error {
hostImageStatuses, err := common.UnmarshalImageStatuses(h.ImagesStatus)
if err != nil {
return errors.Wrapf(err, "Unmarshal image status for host %s", h.ID.String())
}
oldImageStatus, alreadyExists := common.GetImageStatus(hostImageStatuses, newImageStatus.Name)
if alreadyExists {
m.log.Infof("Updating image status for %s with status %s to host %s", newImageStatus.Name, newImageStatus.Result, h.ID.String())
oldImageStatus.Result = newImageStatus.Result
common.SetImageStatus(hostImageStatuses, oldImageStatus)
} else {
common.SetImageStatus(hostImageStatuses, newImageStatus)
m.log.Infof("Adding new image status for %s with status %s to host %s", newImageStatus.Name, newImageStatus.Result, h.ID.String())
m.metricApi.ImagePullStatus(newImageStatus.Name, string(newImageStatus.Result), newImageStatus.DownloadRate)
var eventInfo string
if newImageStatus.SizeBytes > 0 {
eventInfo += fmt.Sprintf("time: %.2f seconds; size: %.2f Megabytes; download rate: %.2f MBps",
newImageStatus.Time, newImageStatus.SizeBytes/math.Pow(1024, 2), newImageStatus.DownloadRate)
}
eventgen.SendImageStatusUpdatedEvent(ctx, m.eventsHandler, *h.ID, h.InfraEnvID, h.ClusterID,
hostutil.GetHostnameForMsg(h), newImageStatus.Name, string(newImageStatus.Result), eventInfo)
}
marshalledStatuses, err := common.MarshalImageStatuses(hostImageStatuses)
if err != nil {
return errors.Wrapf(err, "Failed to marshal image statuses for host %s", h.ID.String())
}
return db.Model(h).Update("images_status", marshalledStatuses).Error
}
func (m *Manager) UpdateHostname(ctx context.Context, h *models.Host, hostname string, db *gorm.DB) error {
hostStatus := swag.StringValue(h.Status)
if !funk.ContainsString(hostStatusesBeforeInstallationOrUnbound[:], hostStatus) {
return common.NewApiError(http.StatusBadRequest,
errors.Errorf("Host is in %s state, host name can be set only in one of %s states",
hostStatus, hostStatusesBeforeInstallation[:]))
}
h.RequestedHostname = hostname
cdb := m.db
if db != nil {
cdb = db
}
return cdb.Model(common.Host{Host: *h}).Updates(map[string]interface{}{"requested_hostname": hostname, "trigger_monitor_timestamp": time.Now()}).Error
}
func (m *Manager) UpdateInstallationDisk(ctx context.Context, db *gorm.DB, h *models.Host, installationDiskPath string) error {
hostStatus := swag.StringValue(h.Status)
if !funk.ContainsString(hostStatusesBeforeInstallationOrUnbound[:], hostStatus) {
return common.NewApiError(http.StatusBadRequest,
errors.Errorf("Host is in %s state, installation disk path can be set only in one of %s states",
hostStatus, hostStatusesBeforeInstallation[:]))
}
validDisks, err := m.hwValidator.GetHostValidDisks(h)
if err != nil {
return err
}
matchedInstallationDisk := hostutil.GetDiskByInstallationPath(validDisks, installationDiskPath)
if matchedInstallationDisk == nil {
return common.NewApiError(http.StatusBadRequest,
errors.Errorf("Requested installation disk is not part of the host's valid disks"))
}
h.InstallationDiskPath = common.GetDeviceFullName(matchedInstallationDisk)
h.InstallationDiskID = common.GetDeviceIdentifier(matchedInstallationDisk)
cdb := m.db
if db != nil {
cdb = db
}
return cdb.Model(common.Host{Host: *h}).Updates(map[string]interface{}{
"installation_disk_path": h.InstallationDiskPath,
"installation_disk_id": h.InstallationDiskID,
"trigger_monitor_timestamp": time.Now(),
}).Error
}
func (m *Manager) UpdateKubeKeyNS(ctx context.Context, hostID, namespace string) error {
return m.db.Model(&common.Host{}).Where("id = ?", hostID).Update("kube_key_namespace", namespace).Error
}
func (m *Manager) CancelInstallation(ctx context.Context, h *models.Host, reason string, db *gorm.DB) *common.ApiErrorResponse {
shouldAddEvent := true
isFailed := false
var err error
defer func() {
if shouldAddEvent {
if isFailed {
eventgen.SendHostCancelInstallationFailedEvent(ctx, m.eventsHandler, *h.ID, h.InfraEnvID, h.ClusterID,
hostutil.GetHostnameForMsg(h), err.Error())
} else {
eventgen.SendHostInstallationCancelledEvent(ctx, m.eventsHandler, *h.ID, h.InfraEnvID, h.ClusterID,
hostutil.GetHostnameForMsg(h))
}
}
}()
err = m.sm.Run(TransitionTypeCancelInstallation, newStateHost(h), &TransitionArgsCancelInstallation{
ctx: ctx,
reason: reason,
db: db,
})
if err != nil {
isFailed = true
return common.NewApiError(http.StatusConflict, err)
}
return nil
}
func (m *Manager) IsRequireUserActionReset(h *models.Host) bool {
if swag.StringValue(h.Status) != models.HostStatusResetting {
return false
}
if time.Since(time.Time(h.StatusUpdatedAt)) >= m.Config.ResetTimeout {
m.log.Infof("Cluster: %s Host %s is hanged in resetting status. Agent seems to be stuck. "+
"Exceeded reset timeout: %s", h.ClusterID.String(), h.ID.String(), m.Config.ResetTimeout.String())
return true
}
hostStage := h.Progress.CurrentStage
if funk.Contains(manualRebootStages, hostStage) {
m.log.Infof("Cluster %s Host %s is in stage %s and must be restarted by user to the live image "+
"in order to reset the installation.", h.ClusterID.String(), h.ID.String(), hostStage)
return true
}
return false
}
func (m *Manager) ResetHost(ctx context.Context, h *models.Host, reason string, db *gorm.DB) *common.ApiErrorResponse {
shouldAddEvent := true
isFailed := false
var err error
defer func() {
if shouldAddEvent {
if isFailed {
eventgen.SendHostInstallationResetFailedEvent(ctx, m.eventsHandler, *h.ID, h.InfraEnvID, h.ClusterID,
hostutil.GetHostnameForMsg(h), err.Error())
} else {
eventgen.SendHostInstallationResetEvent(ctx, m.eventsHandler, *h.ID, h.InfraEnvID, h.ClusterID,
hostutil.GetHostnameForMsg(h))
}
}
}()
var transitionType stateswitch.TransitionType
var transitionArgs stateswitch.TransitionArgs
if m.Config.EnableAutoReset {
transitionType = TransitionTypeResetHost
transitionArgs = &TransitionArgsResetHost{
ctx: ctx,
reason: reason,
db: db,
}
} else {
transitionType = TransitionTypeResettingPendingUserAction
transitionArgs = &TransitionResettingPendingUserAction{
ctx: ctx,
db: db,
}
}
if err = m.sm.Run(transitionType, newStateHost(h), transitionArgs); err != nil {
isFailed = true
return common.NewApiError(http.StatusConflict, err)
}
return nil
}
func (m *Manager) ResetPendingUserAction(ctx context.Context, h *models.Host, db *gorm.DB) error {
shouldAddEvent := true
isFailed := false
var err error
defer func() {
if shouldAddEvent {
if isFailed {
eventgen.SendHostSetStatusFailedEvent(ctx, m.eventsHandler, *h.ID, h.InfraEnvID, h.ClusterID,
hostutil.GetHostnameForMsg(h), err.Error())
} else {