Skip to content

Commit bd87b33

Browse files
authored
🌱 Update names of finalizers. (#1455)
* 🌱 Update names of finalizers. old: foo.infrastructure.cluster.x-k8s.io new: infrastructure.cluster.x-k8s.io/foo Related: #1450
1 parent 472809c commit bd87b33

19 files changed

+73
-90
lines changed

api/v1beta1/hcloudmachine_types.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@ import (
2424
)
2525

2626
const (
27-
// MachineFinalizer allows ReconcileHCloudMachine to clean up HCloud
27+
// HCloudMachineFinalizer allows ReconcileHCloudMachine to clean up HCloud
2828
// resources associated with HCloudMachine before removing it from the
2929
// apiserver.
30-
MachineFinalizer = "hcloudmachine.infrastructure.cluster.x-k8s.io"
30+
HCloudMachineFinalizer = "infrastructure.cluster.x-k8s.io/hcloudmachine"
31+
// DeprecatedHCloudMachineFinalizer contains the old string.
32+
// The controller will automatically update to the new string.
33+
DeprecatedHCloudMachineFinalizer = "hcloudmachine.infrastructure.cluster.x-k8s.io"
3134
)
3235

3336
// HCloudMachineSpec defines the desired state of HCloudMachine.

api/v1beta1/hcloudremediation_types.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ import (
2222
)
2323

2424
const (
25-
// HCloudRemediationFinalizer allows HCloudRemediationReconciler to clean up resources associated with HCloudRemediation before
26-
// removing it from the apiserver.
27-
HCloudRemediationFinalizer = "hcloudremediation.infrastructure.cluster.x-k8s.io"
28-
2925
// HCloudRebootAnnotation indicates that a bare metal host object should be rebooted.
3026
HCloudRebootAnnotation = "reboot.hcloud.infrastructure.cluster.x-k8s.io"
3127
)

api/v1beta1/hetznerbaremetalhost_types.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,14 @@ import (
3131
)
3232

3333
const (
34-
// BareMetalHostFinalizer is the name of the finalizer added to
34+
// HetznerBareMetalHostFinalizer is the name of the finalizer added to
3535
// hosts to block delete operations until the physical host can be
3636
// deprovisioned.
37-
BareMetalHostFinalizer string = "hetznerbaremetalhost.infrastructure.cluster.x-k8s.io"
37+
HetznerBareMetalHostFinalizer = "infrastructure.cluster.x-k8s.io/hetznerbaremetalhost"
38+
39+
// DeprecatedBareMetalHostFinalizer contains the old string.
40+
// The controller will automatically update to the new string.
41+
DeprecatedBareMetalHostFinalizer = "hetznerbaremetalhost.infrastructure.cluster.x-k8s.io"
3842

3943
// HostAnnotation is the key for an annotation that should go on a HetznerBareMetalMachine to
4044
// reference what HetznerBareMetalHost it corresponds to.
@@ -132,17 +136,17 @@ const (
132136

133137
const (
134138
// ErrorMessageMissingRootDeviceHints specifies the error message when no root device hints are specified.
135-
ErrorMessageMissingRootDeviceHints string = "no root device hints specified"
139+
ErrorMessageMissingRootDeviceHints = "no root device hints specified"
136140
// ErrorMessageInvalidRootDeviceHints specifies the error message when invalid root device hints are specified.
137-
ErrorMessageInvalidRootDeviceHints string = "invalid root device hints specified"
141+
ErrorMessageInvalidRootDeviceHints = "invalid root device hints specified"
138142
// ErrorMessageMissingHetznerSecret specifies the error message when no Hetzner secret is found.
139-
ErrorMessageMissingHetznerSecret string = "could not find HetznerSecret"
143+
ErrorMessageMissingHetznerSecret = "could not find HetznerSecret"
140144
// ErrorMessageMissingRescueSSHSecret specifies the error message when no RescueSSH secret is found.
141-
ErrorMessageMissingRescueSSHSecret string = "could not find RescueSSHSecret"
145+
ErrorMessageMissingRescueSSHSecret = "could not find RescueSSHSecret"
142146
// ErrorMessageMissingOSSSHSecret specifies the error message when no OSSSH secret is found.
143-
ErrorMessageMissingOSSSHSecret string = "could not find OSSSHSecret"
147+
ErrorMessageMissingOSSSHSecret = "could not find OSSSHSecret"
144148
// ErrorMessageMissingOrInvalidSecretData specifies the error message when no data in secret is missing or invalid.
145-
ErrorMessageMissingOrInvalidSecretData string = "invalid or not specified information in secret"
149+
ErrorMessageMissingOrInvalidSecretData = "invalid or not specified information in secret"
146150
)
147151

148152
// ProvisioningState defines the states of provisioning of the host.

api/v1beta1/hetznerbaremetalmachine_types.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,13 @@ import (
2929
)
3030

3131
const (
32-
// BareMetalMachineFinalizer allows Reconcilehetznerbaremetalmachine to clean up resources associated with hetznerbaremetalmachine before
32+
// HetznerBareMetalMachineFinalizer allows Reconcilehetznerbaremetalmachine to clean up resources associated with hetznerbaremetalmachine before
3333
// removing it from the apiserver.
34-
BareMetalMachineFinalizer = "hetznerbaremetalmachine.infrastructure.cluster.x-k8s.io"
34+
HetznerBareMetalMachineFinalizer = "infrastructure.cluster.x-k8s.io/hetznerbaremetalmachine"
35+
36+
// DeprecatedBareMetalMachineFinalizer contains the old string.
37+
// The controller will automatically update to the new string.
38+
DeprecatedBareMetalMachineFinalizer = "hetznerbaremetalmachine.infrastructure.cluster.x-k8s.io"
3539

3640
// BareMetalHostNamePrefix is a prefix for all hostNames of bare metal servers.
3741
BareMetalHostNamePrefix = "bm-"

api/v1beta1/hetznerbaremetalremediation_types.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ import (
2121
)
2222

2323
const (
24-
// RemediationFinalizer allows HetznerBareMetalRemediationReconciler to clean up resources associated with HetznerBareMetalRemediation before
25-
// removing it from the apiserver.
26-
RemediationFinalizer = "hetznerbaremetalremediation.infrastructure.cluster.x-k8s.io"
27-
2824
// RebootAnnotation indicates that a bare metal host object should be rebooted.
2925
RebootAnnotation = "reboot.hetznerbaremetalhost.infrastructure.cluster.x-k8s.io"
3026

api/v1beta1/hetznercluster_types.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,15 @@ import (
2222
)
2323

2424
const (
25-
// ClusterFinalizer allows ReconcileHetznerCluster to clean up HCloud
25+
// HetznerClusterFinalizer allows ReconcileHetznerCluster to clean up HCloud
2626
// resources associated with HetznerCluster before removing it from the
2727
// apiserver.
28-
ClusterFinalizer = "hetznercluster.infrastructure.cluster.x-k8s.io"
28+
HetznerClusterFinalizer = "infrastructure.cluster.x-k8s.io/hetznercluster"
29+
30+
// DeprecatedHetznerClusterFinalizer contains the old string.
31+
// The controller will automatically update to the new string.
32+
DeprecatedHetznerClusterFinalizer = "hetznercluster.infrastructure.cluster.x-k8s.io"
33+
2934
// AllowEmptyControlPlaneAddressAnnotation allows HetznerCluster Webhook
3035
// to skip some validation steps for externally managed control planes.
3136
AllowEmptyControlPlaneAddressAnnotation = "capi.syself.com/allow-empty-control-plane-address"

controllers/hcloudmachine_controller.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ func (r *HCloudMachineReconciler) reconcileDelete(ctx context.Context, machineSc
187187
return result, nil
188188
}
189189
// Machine is deleted so remove the finalizer.
190-
controllerutil.RemoveFinalizer(machineScope.HCloudMachine, infrav1.MachineFinalizer)
190+
controllerutil.RemoveFinalizer(machineScope.HCloudMachine, infrav1.HCloudMachineFinalizer)
191+
controllerutil.RemoveFinalizer(machineScope.HCloudMachine, infrav1.DeprecatedHCloudMachineFinalizer)
191192

192193
return reconcile.Result{}, nil
193194
}
@@ -196,7 +197,8 @@ func (r *HCloudMachineReconciler) reconcileNormal(ctx context.Context, machineSc
196197
hcloudMachine := machineScope.HCloudMachine
197198

198199
// If the HCloudMachine doesn't have our finalizer, add it.
199-
controllerutil.AddFinalizer(machineScope.HCloudMachine, infrav1.MachineFinalizer)
200+
controllerutil.AddFinalizer(machineScope.HCloudMachine, infrav1.HCloudMachineFinalizer)
201+
controllerutil.RemoveFinalizer(machineScope.HCloudMachine, infrav1.DeprecatedHCloudMachineFinalizer)
200202

201203
// Register the finalizer immediately to avoid orphaning HCloud resources on delete.
202204
if err := machineScope.PatchObject(ctx); err != nil {

controllers/hcloudmachinetemplate_controller.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ func (r *HCloudMachineTemplateReconciler) Reconcile(ctx context.Context, req rec
7878
}()
7979

8080
// removing finalizer that was set in previous versions but is not needed
81-
controllerutil.RemoveFinalizer(machineTemplate, infrav1.MachineFinalizer)
81+
// We can remove that code in 2025.
82+
controllerutil.RemoveFinalizer(machineTemplate, infrav1.DeprecatedHCloudMachineFinalizer)
8283

8384
// Check whether owner is a ClusterClass. In that case there is nothing to do.
8485
if hasOwnerClusterClass(machineTemplate.ObjectMeta) {

controllers/hcloudremediation_controller_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ var _ = Describe("HCloudRemediationReconciler", func() {
258258
}, timeout).Should(BeTrue())
259259
})
260260

261-
It("should delete machine if retry limit reached and reboot timed out", func() {
261+
It("should delete machine if retry limit reached and reboot timed out (hcloud)", func() {
262262
By("creating hcloudRemediation")
263263
Expect(testEnv.Create(ctx, hcloudRemediation)).To(Succeed())
264264

controllers/hetznerbaremetalhost_controller.go

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
ctrl "sigs.k8s.io/controller-runtime"
3636
"sigs.k8s.io/controller-runtime/pkg/client"
3737
"sigs.k8s.io/controller-runtime/pkg/controller"
38+
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
3839
"sigs.k8s.io/controller-runtime/pkg/event"
3940
"sigs.k8s.io/controller-runtime/pkg/predicate"
4041
"sigs.k8s.io/controller-runtime/pkg/reconcile"
@@ -46,7 +47,6 @@ import (
4647
robotclient "github.com/syself/cluster-api-provider-hetzner/pkg/services/baremetal/client/robot"
4748
sshclient "github.com/syself/cluster-api-provider-hetzner/pkg/services/baremetal/client/ssh"
4849
"github.com/syself/cluster-api-provider-hetzner/pkg/services/baremetal/host"
49-
"github.com/syself/cluster-api-provider-hetzner/pkg/utils"
5050
)
5151

5252
// HetznerBareMetalHostReconciler reconciles a HetznerBareMetalHost object.
@@ -87,12 +87,12 @@ func (r *HetznerBareMetalHostReconciler) Reconcile(ctx context.Context, req ctrl
8787
}
8888

8989
// Add a finalizer to newly created objects.
90-
if bmHost.DeletionTimestamp.IsZero() && !hostHasFinalizer(bmHost) {
91-
bmHost.Finalizers = append(bmHost.Finalizers,
92-
infrav1.BareMetalHostFinalizer)
90+
if bmHost.DeletionTimestamp.IsZero() &&
91+
(controllerutil.AddFinalizer(bmHost, infrav1.HetznerBareMetalHostFinalizer) ||
92+
controllerutil.RemoveFinalizer(bmHost, infrav1.DeprecatedBareMetalHostFinalizer)) {
9393
err := r.Update(ctx, bmHost)
9494
if err != nil {
95-
return reconcile.Result{}, fmt.Errorf("failed to add finalizer: %w", err)
95+
return reconcile.Result{}, fmt.Errorf("failed to update finalizer: %w", err)
9696
}
9797
return ctrl.Result{Requeue: true}, nil
9898
}
@@ -230,21 +230,20 @@ func (r *HetznerBareMetalHostReconciler) reconcileSelectedStates(ctx context.Con
230230
if needsUpdate {
231231
err := r.Update(ctx, bmHost)
232232
if err != nil {
233-
return reconcile.Result{}, fmt.Errorf("failed to add finalizer: %w", err)
233+
return reconcile.Result{}, fmt.Errorf("Update() failed after setting ProvisioningState: %w", err)
234234
}
235235
}
236236

237237
return ctrl.Result{RequeueAfter: 10 * time.Second}, nil
238238

239239
// Handle StateDeleting
240240
case infrav1.StateDeleting:
241-
if !utils.StringInList(bmHost.Finalizers, infrav1.BareMetalHostFinalizer) {
242-
return reconcile.Result{Requeue: true}, nil
243-
}
244-
245-
bmHost.Finalizers = utils.FilterStringFromList(bmHost.Finalizers, infrav1.BareMetalHostFinalizer)
246-
if err := r.Update(ctx, bmHost); err != nil {
247-
return reconcile.Result{}, fmt.Errorf("failed to remove finalizer: %w", err)
241+
if controllerutil.RemoveFinalizer(bmHost, infrav1.HetznerBareMetalHostFinalizer) ||
242+
controllerutil.RemoveFinalizer(bmHost, infrav1.DeprecatedBareMetalHostFinalizer) {
243+
// at least one finalizer was removed.
244+
if err := r.Update(ctx, bmHost); err != nil {
245+
return reconcile.Result{}, fmt.Errorf("failed to remove finalizer: %w", err)
246+
}
248247
}
249248
return reconcile.Result{Requeue: true}, nil
250249
}
@@ -411,10 +410,6 @@ func hetznerSecretErrorResult(
411410
return reconcile.Result{}, fmt.Errorf("hetznerSecretErrorResult: an unhandled failure occurred: %T %w", err, err)
412411
}
413412

414-
func hostHasFinalizer(host *infrav1.HetznerBareMetalHost) bool {
415-
return utils.StringInList(host.Finalizers, infrav1.BareMetalHostFinalizer)
416-
}
417-
418413
// SetupWithManager sets up the controller with the Manager.
419414
func (r *HetznerBareMetalHostReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
420415
return ctrl.NewControllerManagedBy(mgr).

0 commit comments

Comments
 (0)