Skip to content

Commit

Permalink
Ignore 'AlreadyExists" API error when creating IPAM IP (#103)
Browse files Browse the repository at this point in the history
* Ignore 'AlreadyExists" API error when creating IPAM IP

It was most probably to deferred deletion, the IP creation was queued
and will succeed after the existing IP is deleted

* Sleep for 5 seconds so the finalizer has a chance to finish

* Make flow more readable
  • Loading branch information
damyan authored Mar 21, 2024
1 parent 20ff97d commit 4f00a07
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM --platform=$BUILDPLATFORM golang:1.22-bullseye AS builder
FROM golang:1.22-bullseye AS builder

ARG GOARCH=''

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ run: all

.PHONY: docker-build
docker-build: ## Build docker image with the manager.
docker build -t ${IMG} $(GITHUB_PAT_MOUNT) .
DOCKER_BUILDKIT=1 docker build -t ${IMG} $(GITHUB_PAT_MOUNT) .

.PHONY: docker-push
docker-push: ## Push docker image with the manager.
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
pl_router "github.com/coredhcp/coredhcp/plugins/router"
pl_searchdomains "github.com/coredhcp/coredhcp/plugins/searchdomains"
pl_serverid "github.com/coredhcp/coredhcp/plugins/serverid"
pl_sleep "github.com/coredhcp/coredhcp/plugins/sleep"
pl_staticroute "github.com/coredhcp/coredhcp/plugins/staticroute"
pl_bluefield "github.com/ironcore-dev/fedhcp/plugins/bluefield"
pl_ipam "github.com/ironcore-dev/fedhcp/plugins/ipam"
Expand Down Expand Up @@ -76,6 +77,7 @@ var desiredPlugins = []*plugins.Plugin{
&pl_router.Plugin,
&pl_searchdomains.Plugin,
&pl_serverid.Plugin,
&pl_sleep.Plugin,
&pl_staticroute.Plugin,
&pl_bluefield.Plugin,
&pl_ipam.Plugin,
Expand Down
22 changes: 15 additions & 7 deletions plugins/ipam/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"os"
"reflect"
"strings"
"time"

ipamv1alpha1 "github.com/ironcore-dev/ipam/api/ipam/v1alpha1"
"github.com/pkg/errors"
Expand Down Expand Up @@ -152,23 +153,28 @@ func (k K8sClient) createIpamIP(ipaddr net.IP, mac net.HardwareAddr) error {
}

k.EventRecorder.Eventf(existingIpamIP, corev1.EventTypeNormal, "Deleted", "Deleted old IPAM IP")
log.Infof("Old IP deleted from subnet %s/%s", subnet.Namespace, subnet.Name)
log.Infof("Old IP deleted from subnet %s/%s, sleeping for 5 seconds, so the finalizer can run", subnet.Namespace, subnet.Name)
time.Sleep(5 * time.Second)
createIpamIP = true
}
}

if createIpamIP {
err = k.Client.Create(ctx, ipamIP)
if err != nil {
if err != nil && !apierrors.IsAlreadyExists(err) {
err = errors.Wrapf(err, "Failed to create IP %s/%s", ipamIP.Namespace, ipamIP.Name)
return err
}

log.Infof("New IP created in subnet %s/%s", subnet.Namespace, subnet.Name)
k.EventRecorder.Eventf(ipamIP, corev1.EventTypeNormal, "Created", "Created IPAM IP")
break
if apierrors.IsAlreadyExists(err) {
// do not create IP, because the deletion is not yet ready
noop()
} else {
log.Infof("New IP created in subnet %s/%s", subnet.Namespace, subnet.Name)
k.EventRecorder.Eventf(ipamIP, corev1.EventTypeNormal, "Created", "Created IPAM IP")
}
} else {
log.Infof("IP already exists in subnet %s/%s, nothing to do", subnet.Namespace, subnet.Name)
}
log.Infof("IP already exists in subnet %s/%s, nothing to do", subnet.Namespace, subnet.Name)
break
}

Expand Down Expand Up @@ -217,3 +223,5 @@ func checkIPv6InCIDR(ip net.IP, cidrStr string) bool {
// Check if the CIDR contains the IP
return cidrNet.Contains(ip)
}

func noop() {}

0 comments on commit 4f00a07

Please sign in to comment.