Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add interval on AI backend request #426

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions api/v1alpha1/k8sgpt_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions config/crd/bases/core.k8sgpt.ai_k8sgpts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ spec:
type: boolean
engine:
type: string
interval:
default: 0
type: integer
language:
default: english
type: string
Expand Down
35 changes: 32 additions & 3 deletions controllers/k8sgpt_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/metrics"

kclient "github.com/k8sgpt-ai/k8sgpt-operator/pkg/client"
"github.com/k8sgpt-ai/k8sgpt-operator/pkg/common"
"github.com/k8sgpt-ai/k8sgpt-operator/pkg/integrations"
"github.com/k8sgpt-ai/k8sgpt-operator/pkg/resources"
"github.com/k8sgpt-ai/k8sgpt-operator/pkg/sinks"
Expand Down Expand Up @@ -77,6 +78,8 @@ var (
analysisRetryCount int
// allowBackendAIRequest a circuit breaker that switching on/off backend AI calls
allowBackendAIRequest = true
calledOnce = false
latestResponse = &common.K8sGPTReponse{}
)

// K8sGPTReconciler reconciles a K8sGPT object
Expand All @@ -88,6 +91,22 @@ type K8sGPTReconciler struct {
K8sGPTClient *kclient.Client
}

func repeatBackendRequest(interval time.Duration, k8sgptClient *kclient.Client, k8sgptConfig *corev1alpha1.K8sGPT) {
time.AfterFunc(interval, func() {
fmt.Println("Hello backend")
response, err := k8sgptClient.ProcessAnalysis(k8sgptConfig, allowBackendAIRequest)
if err != nil {
fmt.Printf("error: %s\n", err)
k8sgptClient.Close()
return
}

latestResponse = response
fmt.Println("Number of results", len(latestResponse.Results))
repeatBackendRequest(interval, k8sgptClient, k8sgptConfig)
})
}

// +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
Expand Down Expand Up @@ -221,8 +240,6 @@ func (r *K8sGPTReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
return r.finishReconcile(err, false)
}

defer k8sgptClient.Close()

// Configure the k8sgpt deployment if required
if k8sgptConfig.Spec.RemoteCache != nil {
err = k8sgptClient.AddConfig(k8sgptConfig)
Expand All @@ -243,7 +260,7 @@ func (r *K8sGPTReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
}
}

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

// interval := time.Duration(10) * time.Second
// if interval >= time.Duration(1)*time.Second && !calledOnce {
interval := time.Duration(k8sgptConfig.Spec.AI.Interval) * time.Second
if interval >= ReconcileSuccessInterval && !calledOnce {
calledOnce = true
repeatBackendRequest(interval, k8sgptClient, k8sgptConfig)
} else {
// If backend request interval is not set, close the client as soon
// as the reconciler call ends.
defer k8sgptClient.Close()
}

// Update metrics count
if k8sgptConfig.Spec.AI.Enabled && len(response.Results) > 0 {
k8sgptNumberOfBackendAICalls.With(prometheus.Labels{
Expand Down
3 changes: 1 addition & 2 deletions pkg/client/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ import (
schemav1 "buf.build/gen/go/k8sgpt-ai/k8sgpt/protocolbuffers/go/schema/v1"
"github.com/k8sgpt-ai/k8sgpt-operator/api/v1alpha1"
"github.com/k8sgpt-ai/k8sgpt-operator/pkg/common"
v1 "k8s.io/api/apps/v1"
)

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

client := rpc.NewServerServiceClient(c.conn)
req := &schemav1.AnalyzeRequest{
Expand Down