Skip to content

Commit d2caa51

Browse files
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. Fixes: #419 Signed-off-by: VaibhavMalik4187 <vaibhavmalik2018@gmail.com>
1 parent a72a4f9 commit d2caa51

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

api/v1alpha1/k8sgpt_types.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,12 @@ type AISpec struct {
8888
BaseUrl string `json:"baseUrl,omitempty"`
8989
Region string `json:"region,omitempty"`
9090
// +kubebuilder:default:=gpt-3.5-turbo
91-
Model string `json:"model,omitempty"`
92-
Engine string `json:"engine,omitempty"`
93-
Secret *SecretRef `json:"secret,omitempty"`
94-
Enabled bool `json:"enabled,omitempty"`
91+
Model string `json:"model,omitempty"`
92+
Engine string `json:"engine,omitempty"`
93+
// +kubebuilder:default:=0
94+
Interval int `json:"interval,omitempty"`
95+
Secret *SecretRef `json:"secret,omitempty"`
96+
Enabled bool `json:"enabled,omitempty"`
9597
// +kubebuilder:default:=true
9698
Anonymize *bool `json:"anonymized,omitempty"`
9799
// +kubebuilder:default:=english

config/crd/bases/core.k8sgpt.ai_k8sgpts.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ spec:
7474
type: boolean
7575
engine:
7676
type: string
77+
interval:
78+
default: 0
79+
type: integer
7780
language:
7881
default: english
7982
type: string

controllers/k8sgpt_controller.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ var (
7777
analysisRetryCount int
7878
// allowBackendAIRequest a circuit breaker that switching on/off backend AI calls
7979
allowBackendAIRequest = true
80+
calledOnce = false
8081
)
8182

8283
// K8sGPTReconciler reconciles a K8sGPT object
@@ -88,6 +89,13 @@ type K8sGPTReconciler struct {
8889
K8sGPTClient *kclient.Client
8990
}
9091

92+
func repeatBackendRequest(interval time.Duration) {
93+
time.AfterFunc(interval, func() {
94+
fmt.Println("Hello backend")
95+
repeatBackendRequest(interval)
96+
})
97+
}
98+
9199
// +kubebuilder:rbac:groups=core.k8sgpt.ai,resources=k8sgpts,verbs=get;list;watch;create;update;patch;delete
92100
// +kubebuilder:rbac:groups=core.k8sgpt.ai,resources=k8sgpts/status,verbs=get;update;patch
93101
// +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
270278
// Reset analysisRetryCount
271279
analysisRetryCount = 0
272280

281+
if k8sgptConfig.Spec.AI.Interval >= int(ReconcileSuccessInterval/time.Second) && !calledOnce {
282+
calledOnce = true
283+
repeatBackendRequest(time.Duration(k8sgptConfig.Spec.AI.Interval) * time.Second)
284+
}
285+
273286
// Update metrics count
274287
if k8sgptConfig.Spec.AI.Enabled && len(response.Results) > 0 {
275288
k8sgptNumberOfBackendAICalls.With(prometheus.Labels{

0 commit comments

Comments
 (0)