@@ -12,15 +12,15 @@ import (
1212 "os"
1313 "reflect"
1414 "strings"
15+ "time"
16+
17+ "k8s.io/apimachinery/pkg/util/wait"
1518
1619 "github.com/ironcore-dev/fedhcp/internal/kubernetes"
1720 ipamv1alpha1 "github.com/ironcore-dev/ipam/api/ipam/v1alpha1"
18- ipam "github.com/ironcore-dev/ipam/clientgo/ipam"
19- "github.com/pkg/errors"
2021 corev1 "k8s.io/api/core/v1"
2122 apierrors "k8s.io/apimachinery/pkg/api/errors"
2223 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23- "k8s.io/apimachinery/pkg/watch"
2424 "k8s.io/client-go/kubernetes/scheme"
2525 corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
2626 "k8s.io/client-go/tools/record"
@@ -33,7 +33,6 @@ const (
3333
3434type K8sClient struct {
3535 Client client.Client
36- Clientset ipam.Clientset
3736 Namespace string
3837 SubnetNames []string
3938 Ctx context.Context
@@ -44,11 +43,6 @@ func NewK8sClient(namespace string, subnetNames []string) (*K8sClient, error) {
4443 cfg := kubernetes .GetConfig ()
4544 cl := kubernetes .GetClient ()
4645
47- clientset , err := ipam .NewForConfig (cfg )
48- if err != nil {
49- return nil , fmt .Errorf ("failed to create IPAM clientset: %w" , err )
50- }
51-
5246 corev1Client , err := corev1client .NewForConfig (cfg )
5347 if err != nil {
5448 return nil , fmt .Errorf ("failed to create core client: %w" , err )
@@ -66,7 +60,6 @@ func NewK8sClient(namespace string, subnetNames []string) (*K8sClient, error) {
6660
6761 k8sClient := K8sClient {
6862 Client : cl ,
69- Clientset : * clientset ,
7063 Namespace : namespace ,
7164 SubnetNames : subnetNames ,
7265 Ctx : context .Background (),
@@ -189,7 +182,12 @@ func (k K8sClient) prepareCreateIpamIP(
189182 existingIpamIP .Name , err )
190183 }
191184
192- err = k .waitForDeletion (existingIpamIP )
185+ err = wait .PollUntilContextTimeout (k .Ctx , 1 * time .Second , 10 * time .Second , true , func (ctx context.Context ) (bool , error ) {
186+ if err := k .Client .Get (ctx , client .ObjectKeyFromObject (existingIpamIP ), existingIpamIP ); ! apierrors .IsNotFound (err ) {
187+ return false , err
188+ }
189+ return true , nil
190+ })
193191 if err != nil {
194192 return nil , fmt .Errorf ("failed to delete IP %s/%s: %w" , existingIpamIP .Namespace ,
195193 existingIpamIP .Name , err )
@@ -208,35 +206,6 @@ func (k K8sClient) prepareCreateIpamIP(
208206 return ipamIP , nil
209207}
210208
211- func (k K8sClient ) waitForDeletion (ipamIP * ipamv1alpha1.IP ) error {
212- // Define the namespace and resource name (if you want to watch a specific resource)
213- namespace := ipamIP .Namespace
214- resourceName := ipamIP .Name
215- fieldSelector := "metadata.name=" + resourceName + ",metadata.namespace=" + namespace
216- timeout := int64 (5 )
217-
218- // watch for deletion finished event
219- watcher , err := k .Clientset .IpamV1alpha1 ().IPs (namespace ).Watch (context .TODO (), metav1.ListOptions {
220- FieldSelector : fieldSelector ,
221- TimeoutSeconds : & timeout ,
222- })
223- if err != nil {
224- log .Errorf ("Error watching for IP: %v" , err )
225- }
226-
227- log .Debugf ("Watching for changes to IP %s/%s..." , namespace , resourceName )
228-
229- for event := range watcher .ResultChan () {
230- log .Debugf ("Type: %s, Object: %v\n " , event .Type , event .Object )
231- foundIpamIP := event .Object .(* ipamv1alpha1.IP )
232- if event .Type == watch .Deleted && reflect .DeepEqual (ipamIP .Spec , foundIpamIP .Spec ) {
233- log .Infof ("IP %s/%s deleted" , foundIpamIP .Namespace , foundIpamIP .Name )
234- return nil
235- }
236- }
237- return errors .New ("timeout reached, IP not deleted" )
238- }
239-
240209func (k K8sClient ) doCreateIpamIP (ipamIP * ipamv1alpha1.IP ) error {
241210 err := k .Client .Create (k .Ctx , ipamIP )
242211 if err != nil && ! apierrors .IsAlreadyExists (err ) {
0 commit comments