@@ -12,15 +12,15 @@ import (
12
12
"os"
13
13
"reflect"
14
14
"strings"
15
+ "time"
16
+
17
+ "k8s.io/apimachinery/pkg/util/wait"
15
18
16
19
"github.com/ironcore-dev/fedhcp/internal/kubernetes"
17
20
ipamv1alpha1 "github.com/ironcore-dev/ipam/api/ipam/v1alpha1"
18
- ipam "github.com/ironcore-dev/ipam/clientgo/ipam"
19
- "github.com/pkg/errors"
20
21
corev1 "k8s.io/api/core/v1"
21
22
apierrors "k8s.io/apimachinery/pkg/api/errors"
22
23
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23
- "k8s.io/apimachinery/pkg/watch"
24
24
"k8s.io/client-go/kubernetes/scheme"
25
25
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
26
26
"k8s.io/client-go/tools/record"
@@ -33,7 +33,6 @@ const (
33
33
34
34
type K8sClient struct {
35
35
Client client.Client
36
- Clientset ipam.Clientset
37
36
Namespace string
38
37
SubnetNames []string
39
38
Ctx context.Context
@@ -44,11 +43,6 @@ func NewK8sClient(namespace string, subnetNames []string) (*K8sClient, error) {
44
43
cfg := kubernetes .GetConfig ()
45
44
cl := kubernetes .GetClient ()
46
45
47
- clientset , err := ipam .NewForConfig (cfg )
48
- if err != nil {
49
- return nil , fmt .Errorf ("failed to create IPAM clientset: %w" , err )
50
- }
51
-
52
46
corev1Client , err := corev1client .NewForConfig (cfg )
53
47
if err != nil {
54
48
return nil , fmt .Errorf ("failed to create core client: %w" , err )
@@ -66,7 +60,6 @@ func NewK8sClient(namespace string, subnetNames []string) (*K8sClient, error) {
66
60
67
61
k8sClient := K8sClient {
68
62
Client : cl ,
69
- Clientset : * clientset ,
70
63
Namespace : namespace ,
71
64
SubnetNames : subnetNames ,
72
65
Ctx : context .Background (),
@@ -189,7 +182,12 @@ func (k K8sClient) prepareCreateIpamIP(
189
182
existingIpamIP .Name , err )
190
183
}
191
184
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
+ })
193
191
if err != nil {
194
192
return nil , fmt .Errorf ("failed to delete IP %s/%s: %w" , existingIpamIP .Namespace ,
195
193
existingIpamIP .Name , err )
@@ -208,35 +206,6 @@ func (k K8sClient) prepareCreateIpamIP(
208
206
return ipamIP , nil
209
207
}
210
208
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
-
240
209
func (k K8sClient ) doCreateIpamIP (ipamIP * ipamv1alpha1.IP ) error {
241
210
err := k .Client .Create (k .Ctx , ipamIP )
242
211
if err != nil && ! apierrors .IsAlreadyExists (err ) {
0 commit comments