Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
Signed-off-by: yaroslavborbat <[email protected]>
  • Loading branch information
yaroslavborbat committed Jul 9, 2024
1 parent e336c82 commit 136ffd2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 33 deletions.
32 changes: 14 additions & 18 deletions images/vmi-router/controllers/vmirouter_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"context"
"fmt"

virtv1alpha2 "github.com/deckhouse/virtualization/api/core/v1alpha2"
"github.com/go-logr/logr"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -34,6 +33,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"

virtv1alpha2 "github.com/deckhouse/virtualization/api/core/v1alpha2"
"vmi-router/netlinkmanager"
)

Expand Down Expand Up @@ -73,29 +74,24 @@ func (r *VMRouterReconciler) Reconcile(ctx context.Context, req reconcile.Reques
r.log.V(4).Info(fmt.Sprintf("Got reconcile request for %s", req.String()))

// Start with retrieving affected VMI.
var vm virtv1alpha2.VirtualMachine
var isAbsent bool
err := r.client.Get(ctx, req.NamespacedName, &vm)
if err != nil {
if k8serrors.IsNotFound(err) {
isAbsent = true
} else {
r.log.Error(err, fmt.Sprintf("fail to retrieve vm/%s", req.String()))
return reconcile.Result{}, err
}
vm := &virtv1alpha2.VirtualMachine{}
err := r.client.Get(ctx, req.NamespacedName, vm)
if err != nil && k8serrors.IsNotFound(err) {
r.log.Error(err, fmt.Sprintf("fail to retrieve vm/%s", req.String()))
return reconcile.Result{}, err
}

// Delete route on VM deletion.
if vm.GetDeletionTimestamp() != nil {
r.netlinkMgr.DeleteRoute(&vm)
return reconcile.Result{}, nil
var ipAddr string
if vm != nil {
ipAddr = vm.Status.IPAddress
}

if isAbsent {
r.netlinkMgr.DeleteRoute(nil)
// Delete route on VM deletion.
if vm == nil || vm.DeletionTimestamp != nil {
r.netlinkMgr.DeleteRoute(req.NamespacedName, ipAddr)
return reconcile.Result{}, nil
}

r.netlinkMgr.UpdateRoute(ctx, &vm)
r.netlinkMgr.UpdateRoute(ctx, vm)
return reconcile.Result{}, nil
}
23 changes: 8 additions & 15 deletions images/vmi-router/netlinkmanager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ import (

ciliumv2 "github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2"
"github.com/cilium/cilium/pkg/node/addressing"
virtv1alpha2 "github.com/deckhouse/virtualization/api/core/v1alpha2"
"github.com/go-logr/logr"
"github.com/vishvananda/netlink"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"

virtv1alpha2 "github.com/deckhouse/virtualization/api/core/v1alpha2"
"vmi-router/netlinkwrap"
"vmi-router/netutil"
)
Expand Down Expand Up @@ -275,31 +276,23 @@ func getCiliumInternalIPAddress(node *ciliumv2.CiliumNode) string {
return ""
}

func (m *Manager) DeleteRoute(vm *virtv1alpha2.VirtualMachine) {
// Check if IP is in cache. Do not delete routes for unknown IPs.
vmKey := fmt.Sprintf("%s/%s", vm.GetNamespace(), vm.GetName())

// Get IP either from Status, or from cache.
var vmIP string
if vm != nil {
vmIP = vm.Status.IPAddress
}
func (m *Manager) DeleteRoute(vmKey types.NamespacedName, vmIP string) {
if vmIP == "" {
// Try to recover IP from the cache.
m.vmIPsLock.RLock()
vmIP = m.vmIPs[vmKey]
vmIP = m.vmIPs[vmKey.String()]
m.vmIPsLock.RUnlock()
}
if vmIP == "" {
m.log.Info(fmt.Sprintf("Can't retrieve IP for VM %s/%s, it may lead to stale routes.", vm.GetNamespace(), vm.GetName()))
m.log.Info(fmt.Sprintf("Can't retrieve IP for VM %q, it may lead to stale routes.", vmKey.String()))
return
}

// Prepare ip with the mask to use as the route destination.
vmIPWithNetmask := netutil.AppendHostNetmask(vmIP)
_, vmRouteDst, err := net.ParseCIDR(netutil.AppendHostNetmask(vmIP))
if err != nil {
m.log.Error(err, fmt.Sprintf("failed to parse IP with netmask %s for VM %s/%s", vmIPWithNetmask, vm.GetNamespace(), vm.GetName()))
m.log.Error(err, fmt.Sprintf("failed to parse IP with netmask %s for VM %q", vmIPWithNetmask, vmKey.String()))
return
}

Expand All @@ -311,11 +304,11 @@ func (m *Manager) DeleteRoute(vm *virtv1alpha2.VirtualMachine) {
if err := m.nlWrapper.RouteDel(&route); err != nil && !os.IsNotExist(err) {
m.log.Error(err, "failed to delete route")
}
m.log.Info(fmt.Sprintf("route %s deleted for VM %s/%s", fmtRoute(route), vmKey))
m.log.Info(fmt.Sprintf("route %s deleted for VM %q", fmtRoute(route), vmKey))

// Delete IP from the cache.
m.vmIPsLock.Lock()
delete(m.vmIPs, vmKey)
delete(m.vmIPs, vmKey.String())
m.vmIPsLock.Unlock()
}

Expand Down

0 comments on commit 136ffd2

Please sign in to comment.