@@ -34,6 +34,7 @@ import (
34
34
"sigs.k8s.io/controller-runtime/pkg/metrics"
35
35
36
36
kclient "github.com/k8sgpt-ai/k8sgpt-operator/pkg/client"
37
+ "github.com/k8sgpt-ai/k8sgpt-operator/pkg/common"
37
38
"github.com/k8sgpt-ai/k8sgpt-operator/pkg/integrations"
38
39
"github.com/k8sgpt-ai/k8sgpt-operator/pkg/resources"
39
40
"github.com/k8sgpt-ai/k8sgpt-operator/pkg/sinks"
77
78
analysisRetryCount int
78
79
// allowBackendAIRequest a circuit breaker that switching on/off backend AI calls
79
80
allowBackendAIRequest = true
81
+ calledOnce = false
82
+ latestResponse = & common.K8sGPTReponse {}
80
83
)
81
84
82
85
// K8sGPTReconciler reconciles a K8sGPT object
@@ -88,6 +91,22 @@ type K8sGPTReconciler struct {
88
91
K8sGPTClient * kclient.Client
89
92
}
90
93
94
+ func repeatBackendRequest (interval time.Duration , k8sgptClient * kclient.Client , k8sgptConfig * corev1alpha1.K8sGPT ) {
95
+ time .AfterFunc (interval , func () {
96
+ fmt .Println ("Hello backend" )
97
+ response , err := k8sgptClient .ProcessAnalysis (k8sgptConfig , allowBackendAIRequest )
98
+ if err != nil {
99
+ fmt .Printf ("error: %s\n " , err )
100
+ k8sgptClient .Close ()
101
+ return
102
+ }
103
+
104
+ latestResponse = response
105
+ fmt .Println ("Number of results" , len (latestResponse .Results ))
106
+ repeatBackendRequest (interval , k8sgptClient , k8sgptConfig )
107
+ })
108
+ }
109
+
91
110
// +kubebuilder:rbac:groups=core.k8sgpt.ai,resources=k8sgpts,verbs=get;list;watch;create;update;patch;delete
92
111
// +kubebuilder:rbac:groups=core.k8sgpt.ai,resources=k8sgpts/status,verbs=get;update;patch
93
112
// +kubebuilder:rbac:groups=core.k8sgpt.ai,resources=k8sgpts/finalizers,verbs=update
@@ -221,8 +240,6 @@ func (r *K8sGPTReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
221
240
return r .finishReconcile (err , false )
222
241
}
223
242
224
- defer k8sgptClient .Close ()
225
-
226
243
// Configure the k8sgpt deployment if required
227
244
if k8sgptConfig .Spec .RemoteCache != nil {
228
245
err = k8sgptClient .AddConfig (k8sgptConfig )
@@ -243,7 +260,7 @@ func (r *K8sGPTReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
243
260
}
244
261
}
245
262
246
- response , err := k8sgptClient .ProcessAnalysis (deployment , k8sgptConfig , allowBackendAIRequest )
263
+ response , err := k8sgptClient .ProcessAnalysis (k8sgptConfig , allowBackendAIRequest )
247
264
if err != nil {
248
265
if k8sgptConfig .Spec .AI .Enabled {
249
266
k8sgptNumberOfFailedBackendAICalls .With (prometheus.Labels {
@@ -270,6 +287,18 @@ func (r *K8sGPTReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
270
287
// Reset analysisRetryCount
271
288
analysisRetryCount = 0
272
289
290
+ // interval := time.Duration(10) * time.Second
291
+ // if interval >= time.Duration(1)*time.Second && !calledOnce {
292
+ interval := time .Duration (k8sgptConfig .Spec .AI .Interval ) * time .Second
293
+ if interval >= ReconcileSuccessInterval && ! calledOnce {
294
+ calledOnce = true
295
+ repeatBackendRequest (interval , k8sgptClient , k8sgptConfig )
296
+ } else {
297
+ // If backend request interval is not set, close the client as soon
298
+ // as the reconciler call ends.
299
+ defer k8sgptClient .Close ()
300
+ }
301
+
273
302
// Update metrics count
274
303
if k8sgptConfig .Spec .AI .Enabled && len (response .Results ) > 0 {
275
304
k8sgptNumberOfBackendAICalls .With (prometheus.Labels {
0 commit comments