Repository navigation
Expand file tree
/
Copy pathhetznerbaremetalhost_types.go
More file actions
774 lines (660 loc) · 30.9 KB
/
Copy pathhetznerbaremetalhost_types.go
File metadata and controls
774 lines (660 loc) · 30.9 KB
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
/*
Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package v1beta2
import (
"bytes"
"crypto/sha256"
"encoding/json"
"fmt"
"time"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2"
conditions "sigs.k8s.io/cluster-api/util/conditions"
deprecatedv1beta1conditions "sigs.k8s.io/cluster-api/util/conditions/deprecated/v1beta1"
)
const (
// HetznerBareMetalHostFinalizer is the name of the finalizer added to
// hosts to block delete operations until the physical host can be
// deprovisioned.
HetznerBareMetalHostFinalizer = "infrastructure.cluster.x-k8s.io/hetznerbaremetalhost"
// HostAnnotation is the key for an annotation that should go on a HetznerBareMetalMachine to
// reference what HetznerBareMetalHost it corresponds to. The annotation is a string in the
// format "namespace/hbmh-name". Note: We should remove the namespace, as cross-namespace
// references are not allowed.
HostAnnotation = "infrastructure.cluster.x-k8s.io/HetznerBareMetalHost"
// WipeDiskAnnotation indicates which Disks (WWNs) to erase before provisioning
// The value is a list of WWNS or "all".
WipeDiskAnnotation = "capi.syself.com/wipe-disk"
// IgnoreCheckDiskAnnotation indicates that the machine should get provisioned, even if CheckDisk fails.
IgnoreCheckDiskAnnotation = "capi.syself.com/ignore-check-disk"
)
// RootDeviceHints holds the hints for specifying the storage location
// for the root filesystem for the image. Need to specify either WWN or raid
// to provision the host machine successfully. It is important to find the correct root device.
// If none are specified, the host will stop provisioning in between to wait for
// the details to be specified. HardwareDetails in the host's status can be used to find the correct device.
// Currently, you can specify one disk or a raid setup.
type RootDeviceHints struct {
// wwn is a unique storage identifier used for non-raid setups. The hint
// must match the actual value exactly.
// +optional
WWN string `json:"wwn,omitempty"`
// raid is used to specify multiple storage devices. It provides the controller with information
// on which disks a raid can be established.
// +optional
Raid Raid `json:"raid,omitempty"`
}
// IsValid checks whether rootDeviceHint is valid.
func (rdh *RootDeviceHints) IsValid() bool {
return rdh.IsValidWithMessage() == ""
}
// IsValidWithMessage checks whether rootDeviceHint is valid.
// If valid, an empty string gets returned.
func (rdh *RootDeviceHints) IsValidWithMessage() string {
if rdh.WWN == "" && len(rdh.Raid.WWN) == 0 {
return "rootDeviceHint.wwn and rootDeviceHint.raid.wwn are empty. Please specify one or the other."
}
if rdh.WWN == "" && len(rdh.Raid.WWN) == 1 {
return "rootDeviceHint.raid.wwn contains only one entry. At least two entries are needed."
}
if rdh.WWN != "" && len(rdh.Raid.WWN) > 0 {
return "WWN specified twice (rootDeviceHint.wwn and rootDeviceHint.raid.wwn). Please specify only one or the other."
}
return ""
}
// ListOfWWN gives the list of WWNs - no matter if it's in WWN or Raid.
func (rdh *RootDeviceHints) ListOfWWN() []string {
if rdh.WWN == "" {
return rdh.Raid.WWN
}
return []string{rdh.WWN}
}
// Raid can be used instead of WWN to point to multiple storage devices.
type Raid struct {
// wwn defines a list of unique storage identifiers used for raid setups.
WWN []string `json:"wwn,omitempty"`
}
// ErrorType is an error that the controllers act on.
type ErrorType string
const (
// ErrorTypeFatal leads to the CAPI Machine being replaced. Deprovisioning clears it.
ErrorTypeFatal ErrorType = "fatal error"
// ErrorTypePermanent is like ErrorTypeFatal, but deprovisioning keeps it on the
// HetznerBareMetalHost. It stays until someone removes the permanent error annotation.
ErrorTypePermanent ErrorType = "permanent error"
)
// OngoingReboot represents a reboot request we sent and are waiting to complete.
type OngoingReboot struct {
// type is the kind of reboot we sent.
// +required
// +kubebuilder:validation:Enum=ssh;sw;hw
Type RebootType `json:"type,omitempty"`
// triggeredAt is when we sent the reboot.
// +required
TriggeredAt metav1.Time `json:"triggeredAt,omitzero"`
}
const (
// ErrorMessageMissingRootDeviceHints specifies the error message when no root device hints are specified.
ErrorMessageMissingRootDeviceHints = "no root device hints specified"
// ErrorMessageMissingHetznerSecret specifies the error message when no Hetzner secret is found.
ErrorMessageMissingHetznerSecret = "could not find HetznerSecret"
// ErrorMessageMissingRescueSSHSecret specifies the error message when no RescueSSH secret is found.
ErrorMessageMissingRescueSSHSecret = "could not find RescueSSHSecret"
// ErrorMessageMissingOSSSHSecret specifies the error message when no OSSSH secret is found.
ErrorMessageMissingOSSSHSecret = "could not find OSSSHSecret"
// ErrorMessageMissingOrInvalidSecretData specifies the error message when no data in secret is missing or invalid.
ErrorMessageMissingOrInvalidSecretData = "invalid or not specified information in secret"
)
// ProvisioningState defines the states of provisioning of the host.
type ProvisioningState string
const (
// StateNone means the state is unknown.
StateNone ProvisioningState = ""
// StatePreparing means we are checking if the server exists and preparing it.
StatePreparing ProvisioningState = "preparing"
// StateRegistering means we are getting hardware details.
StateRegistering ProvisioningState = "registering"
// StatePreProvisioning means we run the pre-provisioning-command (if given).
StatePreProvisioning ProvisioningState = "pre-provisioning"
// StateImageInstalling means we install a new image.
StateImageInstalling ProvisioningState = "image-installing"
// StateEnsureProvisioned means we are ensuring the reboot worked and cloud-init was executed successfully.
StateEnsureProvisioned ProvisioningState = "ensure-provisioned"
// StateProvisioned means we have sent userData to the host and booted the machine.
StateProvisioned ProvisioningState = "provisioned"
// StateDeprovisioning means we are removing all machine-specific information from the host.
StateDeprovisioning ProvisioningState = "deprovisioning"
// StateDeleting means we are deleting the host.
StateDeleting ProvisioningState = "deleting"
)
// RebootType defines the reboot type of servers via Hetzner robot API.
type RebootType string
const (
// RebootTypePower defines the power reboot. "Press power button of server".
RebootTypePower RebootType = "power"
// RebootTypeSoftware defines the software reboot. "Send CTRL+ALT+DEL to the server".
RebootTypeSoftware RebootType = "sw"
// RebootTypeHardware defines the hardware reboot. "Execute an automatic hardware reset".
// The RebootTypeHardware is supported by all servers.
RebootTypeHardware RebootType = "hw"
// RebootTypeManual defines the manual reboot. "Order a manual power cycle".
RebootTypeManual RebootType = "man"
// RebootTypeSSH defines the ssh reboot. This is done via caph, not via the robot-API.
RebootTypeSSH RebootType = "ssh"
)
// VerboseRebootType returns the verbose name of a reboot Type.
// The string is CamelCase.
func VerboseRebootType(rebootType RebootType) string {
return map[RebootType]string{
RebootTypePower: "Power",
RebootTypeSoftware: "Software",
RebootTypeHardware: "Hardware",
RebootTypeManual: "Manual",
RebootTypeSSH: "SSH",
}[rebootType]
}
// RebootAnnotationArguments defines the arguments of the RebootAnnotation type.
type RebootAnnotationArguments struct {
Type RebootType `json:"type"`
}
// HetznerBareMetalHostConsumerReference is a reference to the HetznerBareMetalMachine
// that is using a HetznerBareMetalHost.
type HetznerBareMetalHostConsumerReference struct {
// kind is the kind of the resource being referenced.
// +required
// +kubebuilder:validation:MinLength=1
// +kubebuilder:validation:MaxLength=63
// +kubebuilder:validation:Pattern=`^[a-zA-Z]([-a-zA-Z0-9]*[a-zA-Z0-9])?$`
Kind string `json:"kind,omitempty"`
// name is the name of the resource being referenced.
// +required
// +kubebuilder:validation:MinLength=1
// +kubebuilder:validation:MaxLength=253
// +kubebuilder:validation:Pattern=`^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$`
Name string `json:"name,omitempty"`
// apiGroup is the group of the resource being referenced.
// +required
// +kubebuilder:validation:MinLength=1
// +kubebuilder:validation:MaxLength=253
// +kubebuilder:validation:Pattern=`^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$`
APIGroup string `json:"apiGroup,omitempty"`
}
// HetznerBareMetalHostSpec defines the desired state of HetznerBareMetalHost.
type HetznerBareMetalHostSpec struct {
// serverID defines the ID of the server provided by Hetzner.
// Find it on your Hetzner robot dashboard.
ServerID int `json:"serverID"`
// rootDeviceHints provides guidance about how to choose the device for the image
// being provisioned. They need to be specified to provision the host.
// +optional
RootDeviceHints *RootDeviceHints `json:"rootDeviceHints,omitempty"`
// consumerRef is a reference to the HetznerBareMetalMachine
// that is using this host. When it is not empty, the host is considered "in use".
// +optional
ConsumerRef *HetznerBareMetalHostConsumerReference `json:"consumerRef,omitempty"`
// maintenanceMode indicates that a machine is supposed to be deprovisioned. The CAPI Machine
// will get the cluster.x-k8s.io/remediate-machine annotation, and CAPI will deprovision the
// machine. Additionally, the host won't be selected by any Hetzner bare metal machine.
MaintenanceMode *bool `json:"maintenanceMode,omitempty"`
// description is a human-entered text used to help identify the host.
// It can be used to store some valuable information about the host.
// +optional
Description string `json:"description,omitempty"`
}
// HetznerBareMetalHostStatus defines the observed state of HetznerBareMetalHost.
type HetznerBareMetalHostStatus struct {
// hardwareDetails are automatically gathered and should not be modified by the user.
// +optional
HardwareDetails *HardwareDetails `json:"hardwareDetails,omitempty"`
// ipv4 address of the server.
// +optional
IPv4 string `json:"ipv4,omitempty"`
// ipv6 address of the server.
// +optional
IPv6 string `json:"ipv6,omitempty"`
// rebootTypes is a list of all available reboot types for API reboots.
// +optional
// +listType=atomic
RebootTypes []RebootType `json:"rebootTypes,omitempty"`
// sshStatus contains the name and fingerprint of the SSH keys that have been used.
// +optional
SSHStatus SSHStatus `json:"sshStatus,omitempty"`
// errorType is the fatal or permanent error of the host. It is empty when the host has neither.
// +kubebuilder:validation:Enum="fatal error";"permanent error"
// +optional
ErrorType ErrorType `json:"errorType,omitempty"`
// provisioningState is the current state tracked by the provisioner.
// +optional
ProvisioningState ProvisioningState `json:"provisioningState,omitempty"`
// ongoingReboot is the reboot request we sent and are waiting to complete. It is empty when we
// are not waiting for a reboot.
// +optional
OngoingReboot *OngoingReboot `json:"ongoingReboot,omitempty"`
// rebooted shows whether the server is currently being rebooted.
// +optional
Rebooted bool `json:"rebooted,omitempty"`
// nodeBootID reflects the BootID of the corresponding Node resource in the workload-cluster.
// +optional
NodeBootID string `json:"nodeBootID,omitempty"`
// conditions represents the observations of a HetznerBareMetalHost's current state.
// Known condition types are Ready, SSHKeysAvailable, RobotCredentialsAvailable, RootDeviceHintsValidated, ProvisionSucceeded, NodeBootIDRetrieved, RebootSucceeded, Deleting, RobotRateLimitExceeded, ActionCompleted.
// +optional
// +listType=map
// +listMapKey=type
// +kubebuilder:validation:MaxItems=32
Conditions []metav1.Condition `json:"conditions,omitempty"`
// deprecated groups all the status fields that are deprecated and will be removed when support for v1beta1 will be dropped.
// +optional
Deprecated *HetznerBareMetalHostDeprecatedStatus `json:"deprecated,omitempty"`
}
// HetznerBareMetalHostDeprecatedStatus groups all the status fields that are deprecated and will be removed when support for v1beta1 will be dropped.
// See https://github.com/kubernetes-sigs/cluster-api/blob/main/docs/proposals/20240916-improve-status-in-CAPI-resources.md for more context.
type HetznerBareMetalHostDeprecatedStatus struct {
// v1beta1 groups all the status fields that are deprecated and will be removed when support for v1beta1 will be dropped.
// +optional
V1Beta1 *HetznerBareMetalHostV1Beta1DeprecatedStatus `json:"v1beta1,omitempty"`
}
// HetznerBareMetalHostV1Beta1DeprecatedStatus groups all the status fields that are deprecated and will be removed when support for v1beta1 will be dropped.
// See https://github.com/kubernetes-sigs/cluster-api/blob/main/docs/proposals/20240916-improve-status-in-CAPI-resources.md for more context.
type HetznerBareMetalHostV1Beta1DeprecatedStatus struct {
// conditions define the current service state of the HetznerBareMetalHost.
//
// +optional
// +listType=map
// +listMapKey=type
//
// Deprecated: This field is deprecated and is going to be removed when support for v1beta1 is dropped.
Conditions []clusterv1.Condition `json:"conditions,omitempty"`
}
// GetIPAddress returns the IPv4 if set, otherwise the IPv6.
func (sts HetznerBareMetalHostStatus) GetIPAddress() string {
if sts.IPv4 == "" {
return sts.IPv6
}
return sts.IPv4
}
// HasFatalError returns true, if the corresponding capi machine should get deleted.
func (sts HetznerBareMetalHostStatus) HasFatalError() bool {
return sts.ErrorType == ErrorTypeFatal || sts.ErrorType == ErrorTypePermanent
}
// GetConditions returns the conditions for the HetznerBareMetalHost object.
func (host *HetznerBareMetalHost) GetConditions() []metav1.Condition {
return host.Status.Conditions
}
// SetConditions sets the conditions on the HetznerBareMetalHost object.
func (host *HetznerBareMetalHost) SetConditions(conditions []metav1.Condition) {
host.Status.Conditions = conditions
}
// GetV1Beta1Conditions returns the deprecated v1beta1 conditions of the HetznerBareMetalHost object.
func (host *HetznerBareMetalHost) GetV1Beta1Conditions() clusterv1.Conditions {
if host.Status.Deprecated == nil || host.Status.Deprecated.V1Beta1 == nil {
return nil
}
return host.Status.Deprecated.V1Beta1.Conditions
}
// SetV1Beta1Conditions sets the deprecated v1beta1 conditions on the HetznerBareMetalHost object.
func (host *HetznerBareMetalHost) SetV1Beta1Conditions(conditions clusterv1.Conditions) {
if host.Status.Deprecated == nil {
host.Status.Deprecated = &HetznerBareMetalHostDeprecatedStatus{}
}
if host.Status.Deprecated.V1Beta1 == nil {
host.Status.Deprecated.V1Beta1 = &HetznerBareMetalHostV1Beta1DeprecatedStatus{}
}
host.Status.Deprecated.V1Beta1.Conditions = conditions
}
// HetznerBareMetalHostSummaryOpts returns the summary options for a HetznerBareMetalHost.
//
// The order of conditions in ForConditionTypes defines the priority for the Ready summary:
// when multiple conditions are unhealthy, the summary lists all of them in priority
// order (highest-priority first). Credentials and provisioning problems must outrank
// Deleting, since deletion may itself need credentials to succeed.
// 1. RobotCredentialsAvailable - invalid Robot credentials block every Robot API call.
// 2. RobotRateLimitExceeded - a rate limit blocks every Robot API call (negative polarity).
// 3. SSHKeysAvailable - missing/invalid SSH keys block (de)provisioning.
// 4. RootDeviceHintsValidated - device hints must validate before provisioning.
// 5. ActionCompleted - the action the host is running did not complete.
// 6. ProvisionSucceeded - provisioning state (rescue -> image -> OS).
// 7. RebootSucceeded - post-provision reboot via annotation.
// 8. NodeBootIDRetrieved - workload-cluster Node check after provisioning.
// 9. Deleting - deletion state (negative polarity).
func HetznerBareMetalHostSummaryOpts() []conditions.SummaryOption {
return []conditions.SummaryOption{
// ForConditionTypes lists every condition that contributes to Ready, in
// priority order. When multiple conditions are unhealthy the summary
// surfaces them in this order, so the most important issue is listed first.
conditions.ForConditionTypes{
HetznerBareMetalHostRobotCredentialsAvailableCondition,
HetznerBareMetalHostRobotRateLimitExceededCondition,
HetznerBareMetalHostSSHKeysAvailableCondition,
HetznerBareMetalHostRootDeviceHintsValidatedCondition,
HetznerBareMetalHostActionCompletedCondition,
HetznerBareMetalHostProvisionSucceededCondition,
HetznerBareMetalHostRebootSucceededCondition,
HetznerBareMetalHostNodeBootIDRetrievedCondition,
HetznerBareMetalHostDeletingCondition,
},
// IgnoreTypesIfMissing tells the summary not to treat the absence of a
// listed condition as Unknown. Several reconcile paths exit before every
// condition has been set (for example, before Robot credentials are
// checked or before the host has been provisioned), and we don't want
// those early exits to flip Ready to Unknown.
conditions.IgnoreTypesIfMissing{
HetznerBareMetalHostRobotRateLimitExceededCondition,
HetznerBareMetalHostSSHKeysAvailableCondition,
HetznerBareMetalHostRootDeviceHintsValidatedCondition,
HetznerBareMetalHostActionCompletedCondition,
HetznerBareMetalHostProvisionSucceededCondition,
HetznerBareMetalHostRebootSucceededCondition,
HetznerBareMetalHostNodeBootIDRetrievedCondition,
HetznerBareMetalHostDeletingCondition,
},
// CustomMergeStrategy is used only to override the merge reasons, so
// the Ready summary uses CAPI's standard Ready reasons (Ready /
// NotReady / ReadyUnknown) instead of the generic merge defaults
// (IssuesReported / UnknownReported / InfoReported).
//
// Negative polarity is passed directly into GetDefaultMergePriorityFunc
// here. When a CustomMergeStrategy is provided, NewSummaryCondition
// skips the path that wires up the NegativePolarityConditionTypes
// SummaryOption into the default strategy, so the negative-polarity
// types must be specified explicitly inside the strategy.
conditions.CustomMergeStrategy{
MergeStrategy: conditions.DefaultMergeStrategy(
conditions.GetPriorityFunc(conditions.GetDefaultMergePriorityFunc(
// conditions with negative polarity
HetznerBareMetalHostRobotRateLimitExceededCondition,
HetznerBareMetalHostDeletingCondition,
)),
conditions.ComputeReasonFunc(conditions.GetDefaultComputeMergeReasonFunc(
clusterv1.NotReadyReason,
clusterv1.ReadyUnknownReason,
clusterv1.ReadyReason,
)),
),
},
}
}
// SSHStatus contains all status information about SSHStatus.
type SSHStatus struct {
// currentRescue gives information about the secret where the rescue ssh key is stored.
CurrentRescue *SecretStatus `json:"currentRescue,omitempty"`
// currentOS gives information about the secret where the os ssh key is stored.
CurrentOS *SecretStatus `json:"currentOS,omitempty"`
// osKey contains name and fingerprint of the in HetznerBareMetalMachine spec specified SSH key.
OSKey *SSHKey `json:"osKey,omitempty"`
// rescueKey contains name and fingerprint of the in HetznerCluster spec specified SSH key.
RescueKey *SSHKey `json:"rescueKey,omitempty"`
}
// SecretStatus contains the reference and the data hash of the last secret that was used.
type SecretStatus struct {
Reference *corev1.SecretReference `json:"credentials,omitempty"`
DataHash []byte `json:"credentialsDataHash,omitempty"`
}
// Match compares the saved status information with the name and
// content of a secret object. Returns false if an error occurred.
func (cs SecretStatus) Match(secret corev1.Secret) bool {
switch {
case cs.Reference == nil:
return false
case cs.Reference.Name != secret.Name:
return false
case cs.Reference.Namespace != secret.Namespace:
return false
}
hash, err := HashOfSecretData(secret.Data)
if err != nil {
return false
}
return bytes.Equal(cs.DataHash, hash)
}
// Capacity is a disk size in Bytes.
type Capacity int64
// ClockSpeed is a clock speed in MHz
// +kubebuilder:validation:Format=double
type ClockSpeed string
// CPU describes one processor on the host.
type CPU struct {
Arch string `json:"arch,omitempty"`
Model string `json:"model,omitempty"`
ClockGigahertz ClockSpeed `json:"clockGigahertz,omitempty"`
Threads int `json:"threads,omitempty"`
Cores int `json:"cores,omitempty"`
}
// Storage describes one storage device (disk, SSD, etc.) on the host.
type Storage struct {
// name is the Linux device name of the disk, e.g. "/dev/sda". Note that this
// may not be stable across reboots.
Name string `json:"name,omitempty"`
// sizeBytes is the size of the disk in Bytes.
SizeBytes Capacity `json:"sizeBytes,omitempty"`
// sizeGB is the size of the disk in GB.
SizeGB Capacity `json:"sizeGB,omitempty"`
// vendor is the name of the vendor of the device.
Vendor string `json:"vendor,omitempty"`
// model represents the Hardware model.
Model string `json:"model,omitempty"`
// serialNumber denotes the serial number of the device.
SerialNumber string `json:"serialNumber,omitempty"`
// wwn defines the WWN of the device.
WWN string `json:"wwn,omitempty"`
// hctl defines the SCSI location of the device.
HCTL string `json:"hctl,omitempty"`
// rota defines if it's an HDD device or not.
Rota bool `json:"rota,omitempty"`
}
// NIC describes one network interface on the host.
type NIC struct {
// name is the name of the network interface, e.g. "en0"
Name string `json:"name,omitempty"`
// model is the vendor and product IDs of the NIC, e.g. "0x8086 0x1572"
Model string `json:"model,omitempty"`
// mac is the device MAC address
// +kubebuilder:validation:Pattern=`[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){5}`
MAC string `json:"mac,omitempty"`
// ip is the IP address of the interface. This will be an IPv4 or IPv6 address
// if one is present. If both IPv4 and IPv6 addresses are present in a
// dual-stack environment, two nics will be output, one with each IP.
IP string `json:"ip,omitempty"`
// speedMbps is the speed of the device in Megabits per second
SpeedMbps int `json:"speedMbps,omitempty"`
}
// HardwareDetails collects all of the information about hardware
// discovered on the host.
type HardwareDetails struct {
RAMGB int `json:"ramGB,omitempty"`
// +optional
// +listType=atomic
NIC []NIC `json:"nics,omitempty"`
// +optional
// +listType=atomic
Storage []Storage `json:"storage,omitempty"`
CPU CPU `json:"cpu,omitempty"`
}
// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:resource:shortName=hbmh
// +kubebuilder:printcolumn:name="Phase",type="string",JSONPath=".status.provisioningState",description="Phase of provisioning"
// +kubebuilder:printcolumn:name="HetznerBareMetalMachine",type="string",JSONPath=".spec.consumerRef.name",description="HetznerBareMetalMachine using this host"
// +kubebuilder:printcolumn:name="Maintenance",type="boolean",JSONPath=".spec.maintenanceMode",description="Maintenance Mode"
// +kubebuilder:printcolumn:name="Ready",type="string",JSONPath=".status.conditions[?(@.type=='Ready')].status",description="HetznerBareMetalHost is ready"
// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp",description="Time duration since creation of BaremetalHost"
// +kubebuilder:printcolumn:name="IPv4",type="string",JSONPath=".status.ipv4",description="IPv4 of the host",priority=1
// +kubebuilder:printcolumn:name="IPv6",type="string",JSONPath=".status.ipv6",description="IPv6 of the host",priority=1
// +kubebuilder:printcolumn:name="CPU",type=integer,JSONPath=".status.hardwareDetails.cpu.threads",description="CPU threads",priority=1
// +kubebuilder:printcolumn:name="RAM",type=integer,JSONPath=".status.hardwareDetails.ramGB",description="RAM in GB",priority=1
// +kubebuilder:printcolumn:name="Server ID",type="integer",JSONPath=".spec.serverID",description="Server ID of the host",priority=1
// +kubebuilder:printcolumn:name="Reason",type="string",JSONPath=".status.conditions[?(@.type=='Ready')].reason",priority=1
// +kubebuilder:printcolumn:name="Message",type="string",JSONPath=".status.conditions[?(@.type=='Ready')].message",priority=1
// +k8s:defaulter-gen=true
// HetznerBareMetalHost is the Schema for the hetznerbaremetalhosts API.
type HetznerBareMetalHost struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec HetznerBareMetalHostSpec `json:"spec,omitempty"`
Status HetznerBareMetalHostStatus `json:"status,omitempty"`
}
// UpdateRescueSSHStatus modifies the ssh status with the last chosen ssh secret.
func (host *HetznerBareMetalHost) UpdateRescueSSHStatus(secret corev1.Secret) error {
status, err := statusFromSecret(secret)
if err != nil {
return fmt.Errorf("failed get status from secret: %w", err)
}
host.Status.SSHStatus.CurrentRescue = status
return nil
}
// UpdateOSSSHStatus modifies the ssh status with the last chosen ssh secret.
func (host *HetznerBareMetalHost) UpdateOSSSHStatus(secret corev1.Secret) error {
status, err := statusFromSecret(secret)
if err != nil {
return fmt.Errorf("failed get status from secret: %w", err)
}
host.Status.SSHStatus.CurrentOS = status
return nil
}
// statusFromSecret modifies the ssh status with information from ssh secret.
func statusFromSecret(secret corev1.Secret) (*SecretStatus, error) {
hash, err := HashOfSecretData(secret.Data)
if err != nil {
return nil, fmt.Errorf("failed to calculate hash of data")
}
return &SecretStatus{
Reference: &corev1.SecretReference{
Name: secret.Name,
Namespace: secret.Namespace,
},
DataHash: hash,
}, nil
}
// HashOfSecretData returns the sha256 of secret data.
func HashOfSecretData(data map[string][]byte) ([]byte, error) {
b, err := json.Marshal(data)
if err != nil {
return nil, err
}
hash := sha256.New()
if _, err := hash.Write(b); err != nil {
return nil, err
}
return hash.Sum(nil), nil
}
// HasSoftwareReboot returns a boolean indicating whether software reboot exists for server.
func (host *HetznerBareMetalHost) HasSoftwareReboot() bool {
for _, rt := range host.Status.RebootTypes {
if rt == RebootTypeSoftware {
return true
}
}
return false
}
// HasHardwareReboot returns a boolean indicating whether hardware reboot exists for the server.
func (host *HetznerBareMetalHost) HasHardwareReboot() bool {
for _, rt := range host.Status.RebootTypes {
if rt == RebootTypeHardware {
return true
}
}
return false
}
// SetError sets the error type on the status and puts errorMessage on the ActionCompleted condition.
// It also clears the ongoing reboot, because the controllers stop waiting for a reboot when the host
// has a fatal or permanent error. For a permanent error the message also names the annotation that
// an operator has to remove.
//
// When errorType is ErrorTypePermanent, it returns permanentErrorSet as true along with the message
// that callers holding an EventRecorder should emit as a "PermanentErrorSet" warning event.
func (host *HetznerBareMetalHost) SetError(errorType ErrorType, errorMessage string) (permanentErrorSet bool, message string) {
host.Status.ErrorType = errorType
host.Status.OngoingReboot = nil
message = errorMessage
if errorType == ErrorTypePermanent {
// A permanent error stays on the host until someone removes the annotation. The condition has
// to name the annotation.
message = fmt.Sprintf("%s. Remove annotation %q, if you want the controller to use the hbmh again.",
errorMessage, PermanentErrorAnnotation)
}
var reason, v1beta1Reason string
switch errorType {
case ErrorTypeFatal:
reason = HetznerBareMetalHostActionCompletedFatalErrorReason
v1beta1Reason = ActionCompletedFatalErrorV1Beta1Reason
case ErrorTypePermanent:
reason = HetznerBareMetalHostActionCompletedPermanentErrorReason
v1beta1Reason = ActionCompletedPermanentErrorV1Beta1Reason
default:
reason = HetznerBareMetalHostActionCompletedUnknownErrorReason
v1beta1Reason = ActionCompletedUnknownErrorV1Beta1Reason
}
conditions.Set(host, metav1.Condition{
Type: HetznerBareMetalHostActionCompletedCondition,
Status: metav1.ConditionFalse,
Reason: reason,
Message: message,
})
// Clients that read the host through the v1beta1 API see the same condition in status.conditions.
deprecatedv1beta1conditions.MarkFalse(host, ActionCompletedV1Beta1Condition,
v1beta1Reason, clusterv1.ConditionSeverityError, "%s", message)
if errorType != ErrorTypePermanent {
return false, ""
}
if host.Annotations == nil {
host.Annotations = make(map[string]string, 1)
}
host.Annotations[PermanentErrorAnnotation] = time.Now().Format(time.RFC3339)
return true, message
}
// ClearError clears the error type and removes the ActionCompleted condition from both surfaces.
func (host *HetznerBareMetalHost) ClearError() {
host.Status.ErrorType = ""
conditions.Delete(host, HetznerBareMetalHostActionCompletedCondition)
deprecatedv1beta1conditions.Delete(host, ActionCompletedV1Beta1Condition)
}
// ErrorMessage returns the message of the ActionCompleted condition. It returns an empty string
// when the host does not have an ActionCompleted condition.
func (host *HetznerBareMetalHost) ErrorMessage() string {
actionCompleted := conditions.Get(host, HetznerBareMetalHostActionCompletedCondition)
if actionCompleted == nil {
return ""
}
return actionCompleted.Message
}
// HasRebootAnnotation checks for the existence of reboot annotations and returns true if at least one exists.
func (host *HetznerBareMetalHost) HasRebootAnnotation() bool {
for annotation := range host.GetAnnotations() {
if isRebootAnnotation(annotation) {
return true
}
}
return false
}
// ClearRebootAnnotations deletes all reboot annotations that exist on the host.
func (host *HetznerBareMetalHost) ClearRebootAnnotations() {
for annotation := range host.Annotations {
if isRebootAnnotation(annotation) {
delete(host.Annotations, annotation)
}
}
}
func isRebootAnnotation(annotation string) bool {
return annotation == RebootAnnotation
}
//+kubebuilder:object:root=true
// HetznerBareMetalHostList contains a list of HetznerBareMetalHost.
type HetznerBareMetalHostList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []HetznerBareMetalHost `json:"items"`
}
func init() {
objectTypes = append(objectTypes, &HetznerBareMetalHost{}, &HetznerBareMetalHostList{})
}