@@ -24,14 +24,12 @@ import (
2424 "context"
2525 "fmt"
2626
27- k8serrors "k8s.io/apimachinery/pkg/api/errors"
2827 "k8s.io/apimachinery/pkg/api/meta"
2928 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3029 "k8s.io/apimachinery/pkg/runtime"
3130 k8sacmetav1 "k8s.io/client-go/applyconfigurations/meta/v1"
3231 ctrl "sigs.k8s.io/controller-runtime"
3332 k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
34- "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
3533 logger "sigs.k8s.io/controller-runtime/pkg/log"
3634
3735 "github.com/gophercloud/gophercloud/v2"
@@ -238,25 +236,39 @@ func (hec *HypervisorMaintenanceController) reconcileEviction(
238236
239237func (hec * HypervisorMaintenanceController ) ensureEviction (ctx context.Context , eviction * kvmv1.Eviction , hypervisor * kvmv1.Hypervisor ) (metav1.ConditionStatus , error ) {
240238 log := logger .FromContext (ctx )
241- if err := hec .Get (ctx , k8sclient .ObjectKeyFromObject (eviction ), eviction ); err != nil {
242- if ! k8serrors .IsNotFound (err ) {
243- return metav1 .ConditionUnknown , fmt .Errorf ("failed to get eviction due to %w" , err )
244- }
245- if err := controllerutil .SetControllerReference (hypervisor , eviction , hec .Scheme ); err != nil {
246- return metav1 .ConditionUnknown , err
247- }
248- log .Info ("Creating new eviction" , "name" , eviction .Name )
249- eviction .Spec = kvmv1.EvictionSpec {
250- Hypervisor : hypervisor .Name ,
251- Reason : "openstack-hypervisor-operator maintenance" ,
239+
240+ // Build labels to transport from hypervisor (e.g. label-selector, if set)
241+ evictionLabels := make (map [string ]string )
242+ for _ , label := range transferLabels {
243+ if v , ok := hypervisor .Labels [label ]; ok {
244+ evictionLabels [label ] = v
252245 }
246+ }
253247
254- // This also transports the label-selector, if set
255- transportLabels (& hypervisor .ObjectMeta , & eviction .ObjectMeta )
248+ ownerRef := k8sacmetav1 .OwnerReference ().
249+ WithAPIVersion (kvmv1 .GroupVersion .String ()).
250+ WithKind ("Hypervisor" ).
251+ WithName (hypervisor .Name ).
252+ WithUID (hypervisor .UID ).
253+ WithController (true ).
254+ WithBlockOwnerDeletion (true )
255+
256+ evictionApplyCfg := apiv1 .Eviction (eviction .Name , eviction .Namespace ).
257+ WithLabels (evictionLabels ).
258+ WithOwnerReferences (ownerRef ).
259+ WithSpec (apiv1 .EvictionSpec ().
260+ WithHypervisor (hypervisor .Name ).
261+ WithReason ("openstack-hypervisor-operator maintenance" ))
262+
263+ log .Info ("Applying eviction" , "name" , eviction .Name )
264+ if err := hec .Apply (ctx , evictionApplyCfg ,
265+ k8sclient .ForceOwnership , k8sclient .FieldOwner (HypervisorMaintenanceControllerName )); err != nil {
266+ return metav1 .ConditionUnknown , fmt .Errorf ("failed to apply eviction due to %w" , err )
267+ }
256268
257- if err = hec . Create ( ctx , eviction ); err != nil {
258- return metav1 . ConditionUnknown , fmt . Errorf ( "failed to create eviction due to %w" , err )
259- }
269+ // Re-fetch to read current eviction status
270+ if err := hec . Get ( ctx , k8sclient . ObjectKeyFromObject ( eviction ), eviction ); err != nil {
271+ return metav1 . ConditionUnknown , fmt . Errorf ( "failed to get eviction status due to %w" , err )
260272 }
261273
262274 // check if we are still evicting (defaulting to yes)
0 commit comments