Skip to content

Commit 872c290

Browse files
committed
fix(vmip): fix double create VirtualMachineIPLease
Signed-off-by: Dmitry Lopatin <dmitry.lopatin@flant.com>
1 parent ce873a2 commit 872c290

4 files changed

Lines changed: 43 additions & 15 deletions

File tree

images/virtualization-artifact/pkg/controller/vmip/internal/iplease_handler.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"errors"
2222
"fmt"
23+
"time"
2324

2425
corev1 "k8s.io/api/core/v1"
2526
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -183,7 +184,7 @@ func (h IPLeaseHandler) createNewLease(ctx context.Context, state state.VMIPStat
183184

184185
h.recorder.Event(vmip, corev1.EventTypeNormal, virtv2.ReasonBound, "VirtualMachineIPAddress is bound to a new VirtualMachineIPAddressLease.")
185186

186-
return reconcile.Result{}, nil
187+
return reconcile.Result{RequeueAfter: 2 * time.Second}, nil
187188
}
188189

189190
func (h IPLeaseHandler) Name() string {

images/virtualization-artifact/pkg/controller/vmip/vmip_reconciler.go

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ package vmip
1818

1919
import (
2020
"context"
21+
"errors"
2122
"fmt"
23+
"time"
2224

2325
"k8s.io/apimachinery/pkg/types"
2426
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -33,6 +35,7 @@ import (
3335
"github.com/deckhouse/virtualization-controller/pkg/controller/indexer"
3436
"github.com/deckhouse/virtualization-controller/pkg/controller/reconciler"
3537
"github.com/deckhouse/virtualization-controller/pkg/controller/vmip/internal/state"
38+
"github.com/deckhouse/virtualization-controller/pkg/logger"
3639
virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2"
3740
)
3841

@@ -151,19 +154,37 @@ func (r *Reconciler) Reconcile(ctx context.Context, req reconcile.Request) (reco
151154
return reconcile.Result{}, nil
152155
}
153156

157+
log := logger.FromContext(ctx)
158+
log.Debug("Start reconcile VMIP")
159+
154160
s := state.New(r.client, vmip.Changed())
161+
var handlerErrs []error
162+
163+
var result reconcile.Result
164+
for _, h := range r.handlers {
165+
log.Debug("Run handler", logger.SlogHandler(h.Name()))
166+
167+
var res reconcile.Result
168+
res, err = h.Handle(ctx, s)
169+
if err != nil {
170+
log.Error("Failed to handle VirtualMachineIP", logger.SlogErr(err), logger.SlogHandler(h.Name()))
171+
handlerErrs = append(handlerErrs, err)
172+
}
173+
174+
result = reconciler.MergeResults(result, res)
175+
}
155176

156-
rec := reconciler.NewBaseReconciler[Handler](r.handlers)
157-
rec.SetHandlerExecutor(func(ctx context.Context, h Handler) (reconcile.Result, error) {
158-
return h.Handle(ctx, s)
159-
})
160-
rec.SetResourceUpdater(func(ctx context.Context) error {
161-
vmip.Changed().Status.ObservedGeneration = vmip.Changed().Generation
177+
err = vmip.Update(ctx)
178+
if err != nil {
179+
return reconcile.Result{RequeueAfter: 2 * time.Second}, err
180+
}
162181

163-
return vmip.Update(ctx)
164-
})
182+
err = errors.Join(handlerErrs...)
183+
if err != nil {
184+
return reconcile.Result{}, err
185+
}
165186

166-
return rec.Reconcile(ctx)
187+
return result, nil
167188
}
168189

169190
func (r *Reconciler) factory() *virtv2.VirtualMachineIPAddress {

images/virtualization-artifact/pkg/controller/vmiplease/vmiplease_reconciler.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,6 @@ func (r *Reconciler) Reconcile(ctx context.Context, req reconcile.Request) (reco
109109
return h.Handle(ctx, s)
110110
})
111111
rec.SetResourceUpdater(func(ctx context.Context) error {
112-
if s.ShouldDeletion() {
113-
return r.client.Delete(ctx, lease.Changed())
114-
}
115-
116112
if !reflect.DeepEqual(lease.Current().Spec, lease.Changed().Spec) {
117113
leaseStatus := lease.Changed().Status.DeepCopy()
118114
err = r.client.Update(ctx, lease.Changed())
@@ -122,7 +118,16 @@ func (r *Reconciler) Reconcile(ctx context.Context, req reconcile.Request) (reco
122118
lease.Changed().Status = *leaseStatus
123119
}
124120

125-
return lease.Update(ctx)
121+
err = lease.Update(ctx)
122+
if err != nil {
123+
return err
124+
}
125+
126+
if s.ShouldDeletion() {
127+
return r.client.Delete(ctx, lease.Changed())
128+
}
129+
130+
return nil
126131
})
127132

128133
return rec.Reconcile(ctx)

tests/e2e/default_config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ logFilter:
5353
- "get storage class specified in spec: storage class not found" # Err.
5454
- "lastTransitionTime: Required value" # Err.
5555
- "virtualmachineipaddressleases.virtualization.deckhouse.io "
56+
- "Operation cannot be fulfilled on virtualmachineipaddresses.virtualization.deckhouse.io" # Err.
5657
regexpLogFilter:
5758
- "failed to detach: .* not found" # "err" "failed to detach: virtualmachine.kubevirt.io \"head-497d17b-vm-automatic-with-hotplug\" not found",
5859
- "error patching .* not found" # "err" "error patching *** virtualimages.virtualization.deckhouse.io \"head-497d17b-vi-pvc-oref-vi-oref-vd\" not found",

0 commit comments

Comments
 (0)