@@ -28,7 +28,6 @@ import (
2828 "github.com/gophercloud/gophercloud/v2/openstack/compute/v2/aggregates"
2929 "github.com/gophercloud/gophercloud/v2/openstack/compute/v2/services"
3030 "github.com/gophercloud/gophercloud/v2/openstack/placement/v1/resourceproviders"
31- corev1 "k8s.io/api/core/v1"
3231 "k8s.io/apimachinery/pkg/api/meta"
3332 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3433 "k8s.io/apimachinery/pkg/runtime"
@@ -44,7 +43,7 @@ import (
4443
4544const (
4645 decommissionFinalizerName = "cobaltcore.cloud.sap/decommission-hypervisor"
47- DecommissionControllerName = "nodeDecommission "
46+ DecommissionControllerName = "decommission "
4847)
4948
5049type NodeDecommissionReconciler struct {
@@ -57,20 +56,13 @@ type NodeDecommissionReconciler struct {
5756// The counter-side in gardener is here:
5857// https://github.com/gardener/machine-controller-manager/blob/rel-v0.56/pkg/util/provider/machinecontroller/machine.go#L646
5958
60- // +kubebuilder:rbac:groups="",resources=nodes,verbs=get;list;watch;patch;update
61- // +kubebuilder:rbac:groups="",resources=nodes/finalizers,verbs=update
6259// +kubebuilder:rbac:groups=kvm.cloud.sap,resources=hypervisors,verbs=get;list;watch
6360// +kubebuilder:rbac:groups=kvm.cloud.sap,resources=hypervisors/status,verbs=get;list;watch;update;patch
6461func (r * NodeDecommissionReconciler ) Reconcile (ctx context.Context , req ctrl.Request ) (ctrl.Result , error ) {
6562 hostname := req .Name
6663 log := logger .FromContext (ctx ).WithName (req .Name ).WithValues ("hostname" , hostname )
6764 ctx = logger .IntoContext (ctx , log )
6865
69- node := & corev1.Node {}
70- if err := r .Get (ctx , req .NamespacedName , node ); err != nil {
71- return ctrl.Result {}, k8sclient .IgnoreNotFound (err )
72- }
73-
7466 // Fetch HV to check if lifecycle management is enabled
7567 hv := & kvmv1.Hypervisor {}
7668 if err := r .Get (ctx , k8sclient.ObjectKey {Name : hostname }, hv ); err != nil {
@@ -79,14 +71,14 @@ func (r *NodeDecommissionReconciler) Reconcile(ctx context.Context, req ctrl.Req
7971 }
8072 if ! hv .Spec .LifecycleEnabled {
8173 // Get out of the way
82- return r .removeFinalizer (ctx , node )
74+ return r .removeFinalizer (ctx , hv )
8375 }
8476
85- if ! controllerutil .ContainsFinalizer (node , decommissionFinalizerName ) {
77+ if ! controllerutil .ContainsFinalizer (hv , decommissionFinalizerName ) {
8678 return ctrl.Result {}, retry .RetryOnConflict (retry .DefaultRetry , func () error {
87- patch := k8sclient .MergeFrom (node .DeepCopy ())
88- controllerutil .AddFinalizer (node , decommissionFinalizerName )
89- if err := r .Patch (ctx , node , patch ); err != nil {
79+ patch := k8sclient .MergeFrom (hv .DeepCopy ())
80+ controllerutil .AddFinalizer (hv , decommissionFinalizerName )
81+ if err := r .Patch (ctx , hv , patch ); err != nil {
9082 return fmt .Errorf ("failed to add finalizer due to %w" , err )
9183 }
9284 log .Info ("Added finalizer" )
@@ -95,16 +87,16 @@ func (r *NodeDecommissionReconciler) Reconcile(ctx context.Context, req ctrl.Req
9587 }
9688
9789 // Not yet deleting hv, nothing more to do
98- if node .DeletionTimestamp .IsZero () {
90+ if hv .DeletionTimestamp .IsZero () {
9991 return ctrl.Result {}, nil
10092 }
10193
10294 // Someone is just deleting the hv, without going through termination
10395 // See: https://github.com/gardener/machine-controller-manager/blob/rel-v0.56/pkg/util/provider/machinecontroller/machine.go#L658-L659
104- if ! IsNodeConditionTrue ( node .Status .Conditions , "Terminating" ) {
96+ if ! meta . IsStatusConditionTrue ( hv .Status .Conditions , "Terminating" ) {
10597 log .Info ("removing finalizer since not terminating" )
10698 // So we just get out of the way for now
107- return r .removeFinalizer (ctx , node )
99+ return r .removeFinalizer (ctx , hv )
108100 }
109101
110102 if meta .IsStatusConditionTrue (hv .Status .Conditions , kvmv1 .ConditionTypeReady ) {
@@ -116,7 +108,7 @@ func (r *NodeDecommissionReconciler) Reconcile(ctx context.Context, req ctrl.Req
116108 hypervisor , err := openstack .GetHypervisorByName (ctx , r .computeClient , hostname , true )
117109 if errors .Is (err , openstack .ErrNoHypervisor ) {
118110 // We are (hopefully) done
119- return r .removeFinalizer (ctx , node )
111+ return r .removeFinalizer (ctx , hv )
120112 }
121113
122114 // TODO: remove since RunningVMs is only available until micro-version 2.87, and also is updated asynchronously
@@ -141,7 +133,7 @@ func (r *NodeDecommissionReconciler) Reconcile(ctx context.Context, req ctrl.Req
141133 return r .setDecommissioningCondition (ctx , hv , fmt .Sprintf ("cannot list aggregates due to %v" , err ))
142134 }
143135
144- host := node .Name
136+ host := hv .Name
145137 for name , aggregate := range aggs {
146138 if slices .Contains (aggregate .Hosts , host ) {
147139 opts := aggregates.RemoveHostOpts {Host : host }
@@ -168,17 +160,17 @@ func (r *NodeDecommissionReconciler) Reconcile(ctx context.Context, req ctrl.Req
168160 return r .setDecommissioningCondition (ctx , hv , fmt .Sprintf ("cannot clean up resource provider: %v" , err ))
169161 }
170162
171- return r .removeFinalizer (ctx , node )
163+ return r .removeFinalizer (ctx , hv )
172164}
173165
174- func (r * NodeDecommissionReconciler ) removeFinalizer (ctx context.Context , node * corev1. Node ) (ctrl.Result , error ) {
175- if ! controllerutil .ContainsFinalizer (node , decommissionFinalizerName ) {
166+ func (r * NodeDecommissionReconciler ) removeFinalizer (ctx context.Context , hypervisor * kvmv1. Hypervisor ) (ctrl.Result , error ) {
167+ if ! controllerutil .ContainsFinalizer (hypervisor , decommissionFinalizerName ) {
176168 return ctrl.Result {}, nil
177169 }
178170
179- nodeBase := node .DeepCopy ()
180- controllerutil .RemoveFinalizer (node , decommissionFinalizerName )
181- err := r .Patch (ctx , node , k8sclient .MergeFromWithOptions (nodeBase ,
171+ base := hypervisor .DeepCopy ()
172+ controllerutil .RemoveFinalizer (hypervisor , decommissionFinalizerName )
173+ err := r .Patch (ctx , hypervisor , k8sclient .MergeFromWithOptions (base ,
182174 k8sclient.MergeFromWithOptimisticLock {}), k8sclient .FieldOwner (DecommissionControllerName ))
183175 return ctrl.Result {}, err
184176}
@@ -195,7 +187,7 @@ func (r *NodeDecommissionReconciler) setDecommissioningCondition(ctx context.Con
195187 k8sclient.MergeFromWithOptimisticLock {}), k8sclient .FieldOwner (DecommissionControllerName )); err != nil {
196188 return ctrl.Result {}, fmt .Errorf ("cannot update hypervisor status due to %w" , err )
197189 }
198- return ctrl.Result {RequeueAfter : shortRetryTime }, nil
190+ return ctrl.Result {}, nil
199191}
200192
201193// SetupWithManager sets up the controller with the Manager.
@@ -217,6 +209,6 @@ func (r *NodeDecommissionReconciler) SetupWithManager(mgr ctrl.Manager) error {
217209
218210 return ctrl .NewControllerManagedBy (mgr ).
219211 Named (DecommissionControllerName ).
220- For (& corev1. Node {}).
212+ For (& kvmv1. Hypervisor {}).
221213 Complete (r )
222214}
0 commit comments