@@ -12,11 +12,13 @@ import (
12
12
"github.com/flux-iac/tofu-controller/runner"
13
13
"github.com/fluxcd/pkg/runtime/logger"
14
14
"google.golang.org/grpc"
15
+ corev1 "k8s.io/api/core/v1"
15
16
v1 "k8s.io/api/core/v1"
16
17
"k8s.io/apimachinery/pkg/api/errors"
17
18
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
18
19
"k8s.io/apimachinery/pkg/types"
19
20
"k8s.io/apimachinery/pkg/util/wait"
21
+ "k8s.io/apimachinery/pkg/watch"
20
22
controllerruntime "sigs.k8s.io/controller-runtime"
21
23
ctrl "sigs.k8s.io/controller-runtime"
22
24
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -452,38 +454,58 @@ func (r *TerraformReconciler) reconcileRunnerPod(ctx context.Context, terraform
452
454
}
453
455
454
456
// wait for pod ip
457
+
458
+ watcher , err := r .Clientset .CoreV1 ().Pods (runnerPodKey .Namespace ).Watch (ctx , metav1 .SingleObject (metav1.ObjectMeta {
459
+ Name : runnerPodKey .Name ,
460
+ Namespace : runnerPodKey .Namespace ,
461
+ }))
462
+ if err != nil {
463
+ return "" , fmt .Errorf ("failed to create a watch on the pod: %w" , err )
464
+ }
465
+
466
+ defer watcher .Stop ()
467
+ // set a timeout for the watch
468
+ ctx , cancel := context .WithTimeout (ctx , timeout )
469
+ defer cancel ()
470
+
455
471
traceLog .Info ("Wait for pod to receive an IP and check for an error" )
456
- if err := wait . Poll ( interval , timeout , func () ( bool , error ) {
457
- traceLog . Info ( "Get pod and check for an error" )
458
- if err := r . Get ( ctx , runnerPodKey , & runnerPod ); err != nil {
459
- traceLog . Error ( err , "Hit an error" )
460
- return false , fmt .Errorf ("failed to get runner pod: %w" , err )
461
- }
472
+ for {
473
+ select {
474
+ case event , ok := <- watcher . ResultChan ():
475
+ if ! ok {
476
+ return "" , fmt .Errorf ("watch channel closed" )
477
+ }
462
478
463
- traceLog .Info ("Check if the pod has an IP" )
464
- if runnerPod .Status .PodIP != "" {
465
- traceLog .Info ("Success, pod has an IP" )
466
- return true , nil
467
- }
479
+ switch event .Type {
480
+ case watch .Added , watch .Modified :
481
+ runnerPod , ok := event .Object .(* corev1.Pod )
482
+ if ! ok {
483
+ return "" , fmt .Errorf ("failed to cast object to pod: %v" , event .Object )
484
+ }
468
485
469
- traceLog .Info ("Pod does not have an IP yet " )
470
- return false , nil
471
- }); err != nil {
472
- traceLog . Info ( "Failed to get the pod, force kill the pod" )
473
- traceLog . Error ( err , "Error getting the Pod" )
486
+ traceLog .Info ("Check if the pod has an IP" )
487
+ if runnerPod . Status . PodIP != "" {
488
+ traceLog . Info ( "Success, pod has an IP" )
489
+ return runnerPod . Status . PodIP , nil
490
+ }
474
491
475
- if err := r .Delete (ctx , & runnerPod ,
476
- client .GracePeriodSeconds (1 ), // force kill = 1 second
477
- client .PropagationPolicy (metav1 .DeletePropagationForeground ),
478
- ); err != nil {
479
- traceLog .Error (err , "Hit an error" )
480
- return "" , fmt .Errorf ("failed to obtain pod ip and delete runner pod: %w" , err )
481
- }
492
+ traceLog .Info ("Pod does not have an IP yet" )
493
+ }
494
+ case <- ctx .Done ():
495
+ traceLog .Info ("Failed to get the pod, force kill the pod" )
496
+ traceLog .Error (err , "Error getting the Pod" )
497
+
498
+ if err := r .Delete (ctx , & runnerPod ,
499
+ client .GracePeriodSeconds (1 ), // force kill = 1 second
500
+ client .PropagationPolicy (metav1 .DeletePropagationForeground ),
501
+ ); err != nil {
502
+ traceLog .Error (err , "Hit an error" )
503
+ return "" , fmt .Errorf ("failed to obtain pod ip and delete runner pod: %w" , err )
504
+ }
482
505
483
- return "" , fmt .Errorf ("failed to create and obtain pod ip" )
506
+ return "" , fmt .Errorf ("failed to create and obtain pod ip" )
507
+ }
484
508
}
485
-
486
- return runnerPod .Status .PodIP , nil
487
509
}
488
510
489
511
// reconcileRunnerSecret reconciles the runner secret used for mTLS
0 commit comments