55 "fmt"
66 "time"
77
8+ "k8s.io/apimachinery/pkg/util/wait"
9+ "k8s.io/client-go/util/retry"
810 ctrl "sigs.k8s.io/controller-runtime"
911 "sigs.k8s.io/controller-runtime/pkg/client"
1012 "sigs.k8s.io/controller-runtime/pkg/log"
@@ -17,8 +19,14 @@ import (
1719 "github.com/netobserv/network-observability-operator/internal/pkg/manager/status"
1820)
1921
20- const (
21- initReconcileAttempts = 5
22+ var (
23+ retryBackoff = wait.Backoff {
24+ Steps : 6 ,
25+ Duration : 2 * time .Second ,
26+ Factor : 2 ,
27+ Jitter : 0.1 ,
28+ }
29+ clog = log .Log .WithName ("static-controller" )
2230)
2331
2432type Reconciler struct {
@@ -27,62 +35,66 @@ type Reconciler struct {
2735 status status.Instance
2836}
2937
30- func Start (ctx context.Context , mgr * manager.Manager ) error {
38+ func Start (ctx context.Context , mgr * manager.Manager ) (manager. PostCreateHook , error ) {
3139 log := log .FromContext (ctx )
3240 log .Info ("Starting Static controller" )
3341 r := Reconciler {
3442 Client : mgr .Client ,
3543 mgr : mgr ,
36- status : mgr .Status .ForComponent (status .StaticPlugin ),
44+ status : mgr .Status .ForComponent (status .StaticController ),
3745 }
3846
39- // force reconcile at startup
40- go r .InitReconcile (ctx )
41-
42- return ctrl .NewControllerManagedBy (mgr ).
47+ // Return initReconcile as a post-create hook
48+ return r .initReconcile , ctrl .NewControllerManagedBy (mgr ).
4349 For (& flowslatest.FlowCollector {}, reconcilers .IgnoreStatusChange ).
4450 Named ("staticPlugin" ).
4551 Complete (& r )
4652}
4753
48- func (r * Reconciler ) InitReconcile (ctx context.Context ) {
49- log := log .FromContext (ctx )
50- log .Info ("Initializing resources..." )
51-
52- for attempt := range initReconcileAttempts {
53- // delay the reconcile calls to let some time to the cache to load
54- time .Sleep (5 * time .Second )
55- _ , err := r .Reconcile (ctx , ctrl.Request {})
56- if err != nil {
57- log .Error (err , "Error while doing initial reconcile" , "attempt" , attempt )
58- } else {
59- return
54+ func (r * Reconciler ) initReconcile (ctx context.Context ) error {
55+ attempt := 0
56+ err := retry .OnError (retryBackoff , func (error ) bool { return true }, func () error {
57+ attempt ++
58+ if _ , err := r .Reconcile (ctx , ctrl.Request {}); err != nil {
59+ clog .WithValues ("attempt" , attempt , "error" , err ).Info ("Initial reconcile: attempt failed" )
60+ return err
6061 }
62+ return nil
63+ })
64+ if err != nil {
65+ return fmt .Errorf ("failed initial reconcile, all attempts failed: %w" , err )
6166 }
67+ return nil
6268}
6369
6470// Reconcile is the controller entry point for reconciling current state with desired state.
6571// It manages the controller status at a high level. Business logic is delegated into `reconcile`.
6672func (r * Reconciler ) Reconcile (ctx context.Context , _ ctrl.Request ) (ctrl.Result , error ) {
67- l := log .Log .WithName ("staticPlugin" ) // clear context (too noisy)
68- ctx = log .IntoContext (ctx , l )
73+ ctx = log .IntoContext (ctx , clog )
6974
7075 r .status .SetUnknown ()
7176 defer r .status .Commit (ctx , r .Client )
7277
73- // always reconcile static console plugin
74- scp , err := helper .NewControllerClientHelper (ctx , r .mgr .Config .Namespace , r .Client )
75- if err != nil {
76- return ctrl.Result {}, fmt .Errorf ("failed to get controller deployment: %w" , err )
77- }
78- staticPluginReconciler := consoleplugin .NewStaticReconciler (r .newDefaultReconcilerInstance (scp ))
79- if err := staticPluginReconciler .ReconcileStaticPlugin (ctx , true ); err != nil {
80- l .Error (err , "Static plugin reconcile failure" )
81- // Set status failure unless it was already set
82- if ! r .status .HasFailure () {
83- r .status .SetFailure ("StaticPluginError" , err .Error ())
78+ if r .mgr .ClusterInfo .HasConsolePlugin () {
79+ if supported , _ , err := r .mgr .ClusterInfo .IsOpenShiftVersionAtLeast ("4.15.0" ); err != nil {
80+ return ctrl.Result {}, err
81+ } else if ! supported {
82+ clog .Info ("Skipping static plugin reconciler (no console detected)" )
83+ } else {
84+ scp , err := helper .NewControllerClientHelper (ctx , r .mgr .Config .Namespace , r .Client )
85+ if err != nil {
86+ return ctrl.Result {}, fmt .Errorf ("failed to get controller deployment: %w" , err )
87+ }
88+ staticPluginReconciler := consoleplugin .NewStaticReconciler (r .newDefaultReconcilerInstance (scp ))
89+ if err := staticPluginReconciler .ReconcileStaticPlugin (ctx , true ); err != nil {
90+ clog .Error (err , "Static plugin reconcile failure" )
91+ // Set status failure unless it was already set
92+ if ! r .status .HasFailure () {
93+ r .status .SetFailure ("StaticPluginError" , err .Error ())
94+ }
95+ return ctrl.Result {}, err
96+ }
8497 }
85- return ctrl.Result {}, err
8698 }
8799
88100 r .status .SetReady ()
0 commit comments