Skip to content

Commit 566b4ad

Browse files
authored
🌱 Refactoring PR for: Remove usage of FailureReason and FailureMessage (baremetal) (#1735)
1 parent 47d1be0 commit 566b4ad

23 files changed

+272
-347
lines changed

.golangci.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,10 @@ issues:
169169
- staticcheck
170170
text: "SA1019: in.(.+) is deprecated"
171171
path: .*(api|types)\/.*\/conversion.*\.go$
172+
- linters:
173+
- staticcheck
174+
text: "SA1019: .*FailureReason is deprecated"
175+
path: pkg/services/baremetal/baremetal/baremetal.go
172176
- linters:
173177
- revive
174178
text: exported (method|function|type|const) (.+) should have comment or be unexported

api/v1beta1/hetznerbaremetalhost_types.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ const (
4040
DeprecatedBareMetalHostFinalizer = "hetznerbaremetalhost.infrastructure.cluster.x-k8s.io"
4141

4242
// HostAnnotation is the key for an annotation that should go on a HetznerBareMetalMachine to
43-
// reference what HetznerBareMetalHost it corresponds to.
43+
// reference what HetznerBareMetalHost it corresponds to. The annotation is a string in the
44+
// format "namespace/hbmh-name". Note: We should remove the namespace, as cross-namespace
45+
// references are not allowed.
4446
HostAnnotation = "infrastructure.cluster.x-k8s.io/HetznerBareMetalHost"
4547

4648
// WipeDiskAnnotation indicates which Disks (WWNs) to erase before provisioning
@@ -120,8 +122,10 @@ const (
120122
// RegistrationError is an error condition occurring when the
121123
// controller is unable to retrieve information on a specific server via robot.
122124
RegistrationError ErrorType = "registration error"
125+
123126
// PreparationError is an error condition occurring when something fails while preparing host reconciliation.
124127
PreparationError ErrorType = "preparation error"
128+
125129
// ProvisioningError is an error condition occurring when the controller
126130
// fails to provision or deprovision the Host.
127131
ProvisioningError ErrorType = "provisioning error"
@@ -230,8 +234,9 @@ type HetznerBareMetalHostSpec struct {
230234
// +optional
231235
ConsumerRef *corev1.ObjectReference `json:"consumerRef,omitempty"`
232236

233-
// MaintenanceMode indicates that a machine is supposed to be deprovisioned
234-
// and won't be selected by any Hetzner bare metal machine.
237+
// MaintenanceMode indicates that a machine is supposed to be deprovisioned. The CAPI Machine
238+
// will get the cluster.x-k8s.io/remediate-machine annotation, and CAPI will deprovision the
239+
// machine. Additionally, the host won't be selected by any Hetzner bare metal machine.
235240
MaintenanceMode *bool `json:"maintenanceMode,omitempty"`
236241

237242
// Description is a human-entered text used to help identify the host.

api/v1beta1/hetznerbaremetalmachine_types.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2626
"k8s.io/apimachinery/pkg/selection"
2727
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
28-
capierrors "sigs.k8s.io/cluster-api/errors" //nolint:staticcheck // we will handle that, when we update to capi v1.11
2928
)
3029

3130
const (
@@ -298,8 +297,11 @@ type HetznerBareMetalMachineStatus struct {
298297
LastUpdated *metav1.Time `json:"lastUpdated,omitempty"`
299298

300299
// FailureReason will be set in the event that there is a terminal problem.
300+
//
301+
// Deprecated: This field is deprecated and is going to be removed when support for v1beta1 will be dropped. Please see https://github.com/kubernetes-sigs/cluster-api/blob/main/docs/proposals/20240916-improve-status-in-CAPI-resources.md for more details.
302+
//
301303
// +optional
302-
FailureReason *capierrors.MachineStatusError `json:"failureReason,omitempty"`
304+
FailureReason *string `json:"failureReason,omitempty"`
303305

304306
// FailureMessage will be set in the event that there is a terminal problem.
305307
// +optional
@@ -358,7 +360,7 @@ func (hbmm *HetznerBareMetalMachine) SetConditions(conditions clusterv1.Conditio
358360
}
359361

360362
// SetFailure sets a failure reason and message.
361-
func (hbmm *HetznerBareMetalMachine) SetFailure(reason capierrors.MachineStatusError, message string) {
363+
func (hbmm *HetznerBareMetalMachine) SetFailure(reason string, message string) {
362364
hbmm.Status.FailureReason = &reason
363365
hbmm.Status.FailureMessage = &message
364366
}

api/v1beta1/hetznerbaremetalmachine_types_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
. "github.com/onsi/ginkgo/v2"
2424
. "github.com/onsi/gomega"
2525
"github.com/stretchr/testify/require"
26-
capierrors "sigs.k8s.io/cluster-api/errors" //nolint:staticcheck // we will handle that, when we update to capi v1.11
2726
)
2827

2928
var _ = Describe("Test Image.GetDetails", func() {
@@ -179,12 +178,12 @@ var _ = Describe("Test GetImageSuffix", func() {
179178

180179
var _ = Describe("Test SetFailure", func() {
181180
bmMachine := HetznerBareMetalMachine{}
182-
newFailureMessage := "bad error"
183-
newFailureReason := capierrors.CreateMachineError
181+
newFailureMessage := "bad failure message"
182+
newFailureReason := "bad failure reason"
184183

185184
It("sets new failure on the machine with existing failure", func() {
186185
failureMessage := "first message"
187-
failureReason := capierrors.MachineStatusError("first error")
186+
failureReason := "first error"
188187
bmMachine.Status.FailureMessage = &failureMessage
189188
bmMachine.Status.FailureReason = &failureReason
190189

api/v1beta1/zz_generated.deepcopy.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/infrastructure.cluster.x-k8s.io_hetznerbaremetalhosts.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,9 @@ spec:
133133
type: string
134134
maintenanceMode:
135135
description: |-
136-
MaintenanceMode indicates that a machine is supposed to be deprovisioned
137-
and won't be selected by any Hetzner bare metal machine.
136+
MaintenanceMode indicates that a machine is supposed to be deprovisioned. The CAPI Machine
137+
will get the cluster.x-k8s.io/remediate-machine annotation, and CAPI will deprovision the
138+
machine. Additionally, the host won't be selected by any Hetzner bare metal machine.
138139
type: boolean
139140
rootDeviceHints:
140141
description: |-

config/crd/bases/infrastructure.cluster.x-k8s.io_hetznerbaremetalmachines.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,8 +399,10 @@ spec:
399399
a terminal problem.
400400
type: string
401401
failureReason:
402-
description: FailureReason will be set in the event that there is
403-
a terminal problem.
402+
description: |-
403+
FailureReason will be set in the event that there is a terminal problem.
404+
405+
Deprecated: This field is deprecated and is going to be removed when support for v1beta1 will be dropped. Please see https://github.com/kubernetes-sigs/cluster-api/blob/main/docs/proposals/20240916-improve-status-in-CAPI-resources.md for more details.
404406
type: string
405407
lastUpdated:
406408
description: LastUpdated identifies when this status was last observed.

controllers/hcloudremediation_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func (r *HCloudRemediationReconciler) Reconcile(ctx context.Context, req reconci
9696

9797
key := client.ObjectKey{
9898
Name: machine.Spec.InfrastructureRef.Name,
99-
Namespace: machine.Spec.InfrastructureRef.Namespace,
99+
Namespace: hcloudRemediation.Namespace,
100100
}
101101

102102
if err := r.Get(ctx, key, hcloudMachine); err != nil {

controllers/hetznerbaremetalhost_controller.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,9 @@ func (r *HetznerBareMetalHostReconciler) Reconcile(ctx context.Context, req ctrl
199199
return res, nil
200200
}
201201

202+
// Case "Delete" was handled in reconcileSelectedStates. From now we know that the host has no
203+
// DeletionTimestamp set. But the hbmm could be in Deprovisioning.
204+
202205
hetznerCluster := &infrav1.HetznerCluster{}
203206

204207
hetznerClusterName := client.ObjectKey{
@@ -240,9 +243,13 @@ func (r *HetznerBareMetalHostReconciler) Reconcile(ctx context.Context, req ctrl
240243
}
241244

242245
log = log.WithValues("HetznerBareMetalMachine", klog.KObj(hetznerBareMetalMachine))
243-
244246
ctx = ctrl.LoggerInto(ctx, log)
245247

248+
// check whether rate limit has been reached and if so, then wait.
249+
if wait := reconcileRateLimit(bmHost, r.RateLimitWaitTime); wait {
250+
return ctrl.Result{RequeueAfter: 30 * time.Second}, nil
251+
}
252+
246253
// Get Hetzner robot api credentials
247254
secretManager := secretutil.NewSecretManager(log, r.Client, r.APIReader)
248255
robotCreds, err := getAndValidateRobotCredentials(ctx, req.Namespace, hetznerCluster, secretManager)
@@ -280,11 +287,6 @@ func (r *HetznerBareMetalHostReconciler) Reconcile(ctx context.Context, req ctrl
280287
return reconcile.Result{}, fmt.Errorf("failed to create scope: %w", err)
281288
}
282289

283-
// check whether rate limit has been reached and if so, then wait.
284-
if wait := reconcileRateLimit(bmHost, r.RateLimitWaitTime); wait {
285-
return ctrl.Result{RequeueAfter: 30 * time.Second}, nil
286-
}
287-
288290
return r.reconcile(ctx, hostScope)
289291
}
290292

@@ -321,7 +323,7 @@ func (r *HetznerBareMetalHostReconciler) reconcileSelectedStates(ctx context.Con
321323

322324
return ctrl.Result{RequeueAfter: 10 * time.Second}, nil
323325

324-
// Handle StateDeleting
326+
// Handle StateDeleting
325327
case infrav1.StateDeleting:
326328
if controllerutil.RemoveFinalizer(bmHost, infrav1.HetznerBareMetalHostFinalizer) ||
327329
controllerutil.RemoveFinalizer(bmHost, infrav1.DeprecatedBareMetalHostFinalizer) {

controllers/hetznerbaremetalhost_controller_test.go

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ import (
4444
)
4545

4646
const (
47-
bmMachineName = "bm-machine-host-testing"
48-
hostName = "test-host"
47+
hostName = "test-host"
4948
)
5049

5150
func verifyError(host *infrav1.HetznerBareMetalHost, errorType infrav1.ErrorType, errorMessage string) bool {
@@ -62,6 +61,7 @@ var _ = Describe("HetznerBareMetalHostReconciler", func() {
6261
var (
6362
host *infrav1.HetznerBareMetalHost
6463
bmMachine *infrav1.HetznerBareMetalMachine
64+
machineName string
6565
hetznerCluster *infrav1.HetznerCluster
6666

6767
capiCluster *clusterv1.Cluster
@@ -91,6 +91,8 @@ var _ = Describe("HetznerBareMetalHostReconciler", func() {
9191

9292
hetznerClusterName = utils.GenerateName(nil, "hetzner-cluster-test")
9393

94+
machineName = utils.GenerateName(nil, "machine")
95+
9496
capiCluster = &clusterv1.Cluster{
9597
ObjectMeta: metav1.ObjectMeta{
9698
GenerateName: "test1-",
@@ -169,15 +171,15 @@ var _ = Describe("HetznerBareMetalHostReconciler", func() {
169171
osSSHClientAfterInstallImage.On("CheckCloudInitLogsForSigTerm").Return(sshclient.Output{})
170172
osSSHClientAfterInstallImage.On("ResetKubeadm").Return(sshclient.Output{})
171173
osSSHClientAfterInstallImage.On("GetHostName").Return(sshclient.Output{
172-
StdOut: infrav1.BareMetalHostNamePrefix + bmMachineName,
174+
StdOut: infrav1.BareMetalHostNamePrefix + machineName,
173175
StdErr: "",
174176
Err: nil,
175177
})
176178
osSSHClientAfterInstallImage.On("GetCloudInitOutput").Return(sshclient.Output{StdOut: "dummy content of /var/log/cloud-init-output.log"})
177179

178180
osSSHClientAfterCloudInit.On("Reboot").Return(sshclient.Output{})
179181
osSSHClientAfterCloudInit.On("GetHostName").Return(sshclient.Output{
180-
StdOut: infrav1.BareMetalHostNamePrefix + bmMachineName,
182+
StdOut: infrav1.BareMetalHostNamePrefix + machineName,
181183
StdErr: "",
182184
Err: nil,
183185
})
@@ -251,9 +253,9 @@ var _ = Describe("HetznerBareMetalHostReconciler", func() {
251253
BeforeEach(func() {
252254
capiMachine = &clusterv1.Machine{
253255
ObjectMeta: metav1.ObjectMeta{
254-
GenerateName: "capi-machine-",
255-
Namespace: testNs.Name,
256-
Finalizers: []string{clusterv1.MachineFinalizer},
256+
Name: machineName,
257+
Namespace: testNs.Name,
258+
Finalizers: []string{clusterv1.MachineFinalizer},
257259
Labels: map[string]string{
258260
clusterv1.ClusterNameLabel: capiCluster.Name,
259261
},
@@ -263,7 +265,7 @@ var _ = Describe("HetznerBareMetalHostReconciler", func() {
263265
InfrastructureRef: corev1.ObjectReference{
264266
APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1",
265267
Kind: "HetznerBareMetalMachine",
266-
Name: bmMachineName,
268+
Name: machineName,
267269
},
268270
FailureDomain: &defaultFailureDomain,
269271
Bootstrap: clusterv1.Bootstrap{
@@ -275,7 +277,7 @@ var _ = Describe("HetznerBareMetalHostReconciler", func() {
275277

276278
bmMachine = &infrav1.HetznerBareMetalMachine{
277279
ObjectMeta: metav1.ObjectMeta{
278-
Name: bmMachineName,
280+
Name: machineName,
279281
Namespace: testNs.Name,
280282
Labels: map[string]string{
281283
clusterv1.ClusterNameLabel: capiCluster.Name,
@@ -425,9 +427,9 @@ var _ = Describe("HetznerBareMetalHostReconciler", func() {
425427
BeforeEach(func() {
426428
capiMachine = &clusterv1.Machine{
427429
ObjectMeta: metav1.ObjectMeta{
428-
GenerateName: "capi-machine-",
429-
Namespace: testNs.Name,
430-
Finalizers: []string{clusterv1.MachineFinalizer},
430+
Name: machineName,
431+
Namespace: testNs.Name,
432+
Finalizers: []string{clusterv1.MachineFinalizer},
431433
Labels: map[string]string{
432434
clusterv1.ClusterNameLabel: capiCluster.Name,
433435
},
@@ -437,7 +439,7 @@ var _ = Describe("HetznerBareMetalHostReconciler", func() {
437439
InfrastructureRef: corev1.ObjectReference{
438440
APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1",
439441
Kind: "HetznerBareMetalMachine",
440-
Name: bmMachineName,
442+
Name: machineName,
441443
},
442444
FailureDomain: &defaultFailureDomain,
443445
Bootstrap: clusterv1.Bootstrap{
@@ -449,7 +451,7 @@ var _ = Describe("HetznerBareMetalHostReconciler", func() {
449451

450452
bmMachine = &infrav1.HetznerBareMetalMachine{
451453
ObjectMeta: metav1.ObjectMeta{
452-
Name: bmMachineName,
454+
Name: machineName,
453455
Namespace: testNs.Name,
454456
Labels: map[string]string{
455457
clusterv1.ClusterNameLabel: capiCluster.Name,
@@ -509,9 +511,9 @@ var _ = Describe("HetznerBareMetalHostReconciler", func() {
509511
BeforeEach(func() {
510512
capiMachine = &clusterv1.Machine{
511513
ObjectMeta: metav1.ObjectMeta{
512-
GenerateName: "capi-machine-",
513-
Namespace: testNs.Name,
514-
Finalizers: []string{clusterv1.MachineFinalizer},
514+
Name: machineName,
515+
Namespace: testNs.Name,
516+
Finalizers: []string{clusterv1.MachineFinalizer},
515517
Labels: map[string]string{
516518
clusterv1.ClusterNameLabel: capiCluster.Name,
517519
},
@@ -521,7 +523,7 @@ var _ = Describe("HetznerBareMetalHostReconciler", func() {
521523
InfrastructureRef: corev1.ObjectReference{
522524
APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1",
523525
Kind: "HetznerBareMetalMachine",
524-
Name: bmMachineName,
526+
Name: machineName,
525527
},
526528
FailureDomain: &defaultFailureDomain,
527529
Bootstrap: clusterv1.Bootstrap{
@@ -533,7 +535,7 @@ var _ = Describe("HetznerBareMetalHostReconciler", func() {
533535

534536
bmMachine = &infrav1.HetznerBareMetalMachine{
535537
ObjectMeta: metav1.ObjectMeta{
536-
Name: bmMachineName,
538+
Name: machineName,
537539
Namespace: testNs.Name,
538540
Labels: map[string]string{
539541
clusterv1.ClusterNameLabel: capiCluster.Name,
@@ -594,6 +596,7 @@ var _ = Describe("HetznerBareMetalHostReconciler - missing secrets", func() {
594596
var (
595597
host *infrav1.HetznerBareMetalHost
596598
bmMachine *infrav1.HetznerBareMetalMachine
599+
machineName string
597600
hetznerCluster *infrav1.HetznerCluster
598601
capiCluster *clusterv1.Cluster
599602
capiMachine *clusterv1.Machine
@@ -619,6 +622,7 @@ var _ = Describe("HetznerBareMetalHostReconciler - missing secrets", func() {
619622
Expect(err).NotTo(HaveOccurred())
620623

621624
hetznerClusterName = utils.GenerateName(nil, "hetzner-cluster-test")
625+
machineName = utils.GenerateName(nil, "machine")
622626

623627
capiCluster = &clusterv1.Cluster{
624628
ObjectMeta: metav1.ObjectMeta{
@@ -657,9 +661,9 @@ var _ = Describe("HetznerBareMetalHostReconciler - missing secrets", func() {
657661

658662
capiMachine = &clusterv1.Machine{
659663
ObjectMeta: metav1.ObjectMeta{
660-
GenerateName: "capi-machine-",
661-
Namespace: testNs.Name,
662-
Finalizers: []string{clusterv1.MachineFinalizer},
664+
Name: machineName,
665+
Namespace: testNs.Name,
666+
Finalizers: []string{clusterv1.MachineFinalizer},
663667
Labels: map[string]string{
664668
clusterv1.ClusterNameLabel: capiCluster.Name,
665669
},
@@ -669,7 +673,7 @@ var _ = Describe("HetznerBareMetalHostReconciler - missing secrets", func() {
669673
InfrastructureRef: corev1.ObjectReference{
670674
APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1",
671675
Kind: "HetznerBareMetalMachine",
672-
Name: bmMachineName,
676+
Name: machineName,
673677
},
674678
FailureDomain: &defaultFailureDomain,
675679
Bootstrap: clusterv1.Bootstrap{
@@ -681,7 +685,7 @@ var _ = Describe("HetznerBareMetalHostReconciler - missing secrets", func() {
681685

682686
bmMachine = &infrav1.HetznerBareMetalMachine{
683687
ObjectMeta: metav1.ObjectMeta{
684-
Name: bmMachineName,
688+
Name: machineName,
685689
Namespace: testNs.Name,
686690
Labels: map[string]string{
687691
clusterv1.ClusterNameLabel: capiCluster.Name,

0 commit comments

Comments
 (0)