Skip to content

Commit 596b93a

Browse files
authored
Merge pull request #1302 from flux-iac/speed-up-wait-for-pod-ip
Speedup wait for pod ip
2 parents 478a24d + 10c818e commit 596b93a

File tree

3 files changed

+58
-26
lines changed

3 files changed

+58
-26
lines changed

cmd/manager/main.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
flag "github.com/spf13/pflag"
3939
"k8s.io/apimachinery/pkg/runtime"
4040
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
41+
"k8s.io/client-go/kubernetes"
4142
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
4243
ctrlcache "sigs.k8s.io/controller-runtime/pkg/cache"
4344
"sigs.k8s.io/controller-runtime/pkg/healthz"
@@ -220,6 +221,12 @@ func main() {
220221
allowCrossNamespaceRefs = !aclOptions.NoCrossNamespaceRefs
221222
}
222223

224+
clientset, err := kubernetes.NewForConfig(mgr.GetConfig())
225+
if err != nil {
226+
setupLog.Error(err, "unable to set up cert rotation")
227+
os.Exit(1)
228+
}
229+
223230
reconciler := &controllers.TerraformReconciler{
224231
Client: mgr.GetClient(),
225232
Scheme: mgr.GetScheme(),
@@ -234,6 +241,7 @@ func main() {
234241
ClusterDomain: clusterDomain,
235242
NoCrossNamespaceRefs: !allowCrossNamespaceRefs,
236243
UsePodSubdomainResolution: usePodSubdomainResolution,
244+
Clientset: clientset,
237245
}
238246

239247
if err = reconciler.SetupWithManager(mgr, concurrent, httpRetry); err != nil {

controllers/tf_controller.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import (
5050
"k8s.io/apimachinery/pkg/types"
5151
kerrors "k8s.io/apimachinery/pkg/util/errors"
5252
"k8s.io/apimachinery/pkg/util/wait"
53+
"k8s.io/client-go/kubernetes"
5354
kuberecorder "k8s.io/client-go/tools/record"
5455
"sigs.k8s.io/cli-utils/pkg/kstatus/polling"
5556
ctrl "sigs.k8s.io/controller-runtime"
@@ -82,6 +83,7 @@ type TerraformReconciler struct {
8283
ClusterDomain string
8384
NoCrossNamespaceRefs bool
8485
UsePodSubdomainResolution bool
86+
Clientset *kubernetes.Clientset
8587
}
8688

8789
//+kubebuilder:rbac:groups=infra.contrib.fluxcd.io,resources=terraforms,verbs=get;list;watch;create;update;patch;delete

controllers/tf_controller_runner.go

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ import (
1212
"github.com/flux-iac/tofu-controller/runner"
1313
"github.com/fluxcd/pkg/runtime/logger"
1414
"google.golang.org/grpc"
15+
corev1 "k8s.io/api/core/v1"
1516
v1 "k8s.io/api/core/v1"
1617
"k8s.io/apimachinery/pkg/api/errors"
1718
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1819
"k8s.io/apimachinery/pkg/types"
1920
"k8s.io/apimachinery/pkg/util/wait"
21+
"k8s.io/apimachinery/pkg/watch"
2022
controllerruntime "sigs.k8s.io/controller-runtime"
2123
ctrl "sigs.k8s.io/controller-runtime"
2224
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -452,38 +454,58 @@ func (r *TerraformReconciler) reconcileRunnerPod(ctx context.Context, terraform
452454
}
453455

454456
// 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+
455471
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+
}
462478

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+
}
468485

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+
}
474491

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+
}
482505

483-
return "", fmt.Errorf("failed to create and obtain pod ip")
506+
return "", fmt.Errorf("failed to create and obtain pod ip")
507+
}
484508
}
485-
486-
return runnerPod.Status.PodIP, nil
487509
}
488510

489511
// reconcileRunnerSecret reconciles the runner secret used for mTLS

0 commit comments

Comments
 (0)