From 1c5baf9215cd8ecd8c31473bcad39a002a1dcae8 Mon Sep 17 00:00:00 2001 From: VaibhavMalik4187 Date: Thu, 25 Apr 2024 09:41:02 +0530 Subject: [PATCH] feature: add interval on AI backend request This commit introduces the ability to send the requests to the backend AI server at set intervals, independent of the reconciler's requeue requests. To utilize this feature, the `Interval` field in the `AI` settings must be set in the k8sgpt configuration object. Also, the specified interval must be greater than or equal to the reconciler's requeue interval as the llm requests should be called after the reconciler executes k8sgpt analyze. Signed-off-by: VaibhavMalik4187 --- api/v1alpha1/k8sgpt_types.go | 10 ++++++---- config/crd/bases/core.k8sgpt.ai_k8sgpts.yaml | 3 +++ controllers/k8sgpt_controller.go | 13 +++++++++++++ 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/api/v1alpha1/k8sgpt_types.go b/api/v1alpha1/k8sgpt_types.go index 3fc6bd8c..86fb996d 100644 --- a/api/v1alpha1/k8sgpt_types.go +++ b/api/v1alpha1/k8sgpt_types.go @@ -88,10 +88,12 @@ type AISpec struct { BaseUrl string `json:"baseUrl,omitempty"` Region string `json:"region,omitempty"` // +kubebuilder:default:=gpt-3.5-turbo - Model string `json:"model,omitempty"` - Engine string `json:"engine,omitempty"` - Secret *SecretRef `json:"secret,omitempty"` - Enabled bool `json:"enabled,omitempty"` + Model string `json:"model,omitempty"` + Engine string `json:"engine,omitempty"` + // +kubebuilder:default:=0 + Interval int `json:"interval,omitempty"` + Secret *SecretRef `json:"secret,omitempty"` + Enabled bool `json:"enabled,omitempty"` // +kubebuilder:default:=true Anonymize *bool `json:"anonymized,omitempty"` // +kubebuilder:default:=english diff --git a/config/crd/bases/core.k8sgpt.ai_k8sgpts.yaml b/config/crd/bases/core.k8sgpt.ai_k8sgpts.yaml index 4a26a524..c8ab99e5 100644 --- a/config/crd/bases/core.k8sgpt.ai_k8sgpts.yaml +++ b/config/crd/bases/core.k8sgpt.ai_k8sgpts.yaml @@ -74,6 +74,9 @@ spec: type: boolean engine: type: string + interval: + default: 0 + type: integer language: default: english type: string diff --git a/controllers/k8sgpt_controller.go b/controllers/k8sgpt_controller.go index ae1eef2e..01a10bc4 100644 --- a/controllers/k8sgpt_controller.go +++ b/controllers/k8sgpt_controller.go @@ -77,6 +77,7 @@ var ( analysisRetryCount int // allowBackendAIRequest a circuit breaker that switching on/off backend AI calls allowBackendAIRequest = true + calledOnce = false ) // K8sGPTReconciler reconciles a K8sGPT object @@ -88,6 +89,13 @@ type K8sGPTReconciler struct { K8sGPTClient *kclient.Client } +func repeatBackendRequest(interval time.Duration) { + time.AfterFunc(interval, func() { + fmt.Println("Hello backend") + repeatBackendRequest(interval) + }) +} + // +kubebuilder:rbac:groups=core.k8sgpt.ai,resources=k8sgpts,verbs=get;list;watch;create;update;patch;delete // +kubebuilder:rbac:groups=core.k8sgpt.ai,resources=k8sgpts/status,verbs=get;update;patch // +kubebuilder:rbac:groups=core.k8sgpt.ai,resources=k8sgpts/finalizers,verbs=update @@ -270,6 +278,11 @@ func (r *K8sGPTReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr // Reset analysisRetryCount analysisRetryCount = 0 + if k8sgptConfig.Spec.AI.Interval >= int(ReconcileSuccessInterval/time.Second) && !calledOnce { + calledOnce = true + repeatBackendRequest(time.Duration(k8sgptConfig.Spec.AI.Interval) * time.Second) + } + // Update metrics count if k8sgptConfig.Spec.AI.Enabled && len(response.Results) > 0 { k8sgptNumberOfBackendAICalls.With(prometheus.Labels{