Skip to content

Commit 470c2dd

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: k8sgpt-ai#419 Signed-off-by: VaibhavMalik4187 <vaibhavmalik2018@gmail.com>
1 parent 0ff36f5 commit 470c2dd

File tree

4 files changed

+42
-9
lines changed

4 files changed

+42
-9
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: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"sigs.k8s.io/controller-runtime/pkg/metrics"
3535

3636
kclient "github.com/k8sgpt-ai/k8sgpt-operator/pkg/client"
37+
"github.com/k8sgpt-ai/k8sgpt-operator/pkg/common"
3738
"github.com/k8sgpt-ai/k8sgpt-operator/pkg/integrations"
3839
"github.com/k8sgpt-ai/k8sgpt-operator/pkg/resources"
3940
"github.com/k8sgpt-ai/k8sgpt-operator/pkg/sinks"
@@ -77,6 +78,8 @@ var (
7778
analysisRetryCount int
7879
// allowBackendAIRequest a circuit breaker that switching on/off backend AI calls
7980
allowBackendAIRequest = true
81+
calledOnce = false
82+
latestResponse = &common.K8sGPTReponse{}
8083
)
8184

8285
// K8sGPTReconciler reconciles a K8sGPT object
@@ -88,6 +91,22 @@ type K8sGPTReconciler struct {
8891
K8sGPTClient *kclient.Client
8992
}
9093

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+
91110
// +kubebuilder:rbac:groups=core.k8sgpt.ai,resources=k8sgpts,verbs=get;list;watch;create;update;patch;delete
92111
// +kubebuilder:rbac:groups=core.k8sgpt.ai,resources=k8sgpts/status,verbs=get;update;patch
93112
// +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
221240
return r.finishReconcile(err, false)
222241
}
223242

224-
defer k8sgptClient.Close()
225-
226243
// Configure the k8sgpt deployment if required
227244
if k8sgptConfig.Spec.RemoteCache != nil {
228245
err = k8sgptClient.AddConfig(k8sgptConfig)
@@ -243,7 +260,7 @@ func (r *K8sGPTReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
243260
}
244261
}
245262

246-
response, err := k8sgptClient.ProcessAnalysis(deployment, k8sgptConfig, allowBackendAIRequest)
263+
response, err := k8sgptClient.ProcessAnalysis(k8sgptConfig, allowBackendAIRequest)
247264
if err != nil {
248265
if k8sgptConfig.Spec.AI.Enabled {
249266
k8sgptNumberOfFailedBackendAICalls.With(prometheus.Labels{
@@ -270,6 +287,18 @@ func (r *K8sGPTReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
270287
// Reset analysisRetryCount
271288
analysisRetryCount = 0
272289

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+
273302
// Update metrics count
274303
if k8sgptConfig.Spec.AI.Enabled && len(response.Results) > 0 {
275304
k8sgptNumberOfBackendAICalls.With(prometheus.Labels{

pkg/client/analysis.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ import (
99
schemav1 "buf.build/gen/go/k8sgpt-ai/k8sgpt/protocolbuffers/go/schema/v1"
1010
"github.com/k8sgpt-ai/k8sgpt-operator/api/v1alpha1"
1111
"github.com/k8sgpt-ai/k8sgpt-operator/pkg/common"
12-
v1 "k8s.io/api/apps/v1"
1312
)
1413

15-
func (c *Client) ProcessAnalysis(deployment v1.Deployment, config *v1alpha1.K8sGPT, allowAIRequest bool) (*common.K8sGPTReponse, error) {
14+
func (c *Client) ProcessAnalysis(config *v1alpha1.K8sGPT, allowAIRequest bool) (*common.K8sGPTReponse, error) {
1615

1716
client := rpc.NewServerServiceClient(c.conn)
1817
req := &schemav1.AnalyzeRequest{

0 commit comments

Comments
 (0)