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

feat: fix grpc client creation slightly #73

Merged
merged 2 commits into from
May 9, 2023
Merged
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
30 changes: 26 additions & 4 deletions controllers/k8sgpt_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ package controllers
import (
"context"
"fmt"
"os"
"strings"
"time"

corev1alpha1 "github.com/k8sgpt-ai/k8sgpt-operator/api/v1alpha1"
k8sgptclient "github.com/k8sgpt-ai/k8sgpt-operator/pkg/client"

kclient "github.com/k8sgpt-ai/k8sgpt-operator/pkg/client"
"github.com/k8sgpt-ai/k8sgpt-operator/pkg/resources"
"github.com/k8sgpt-ai/k8sgpt-operator/pkg/utils"
"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -65,7 +67,9 @@ var (
type K8sGPTReconciler struct {
client.Client
Scheme *runtime.Scheme
K8sGPTClient *k8sgptclient.Client
K8sGPTClient *kclient.Client
// This is a map of clients for each deployment
k8sGPTClients map[string]*kclient.Client
}

// +kubebuilder:rbac:groups=core.k8sgpt.ai,resources=k8sgpts,verbs=get;list;watch;create;update;patch;delete
Expand Down Expand Up @@ -133,8 +137,25 @@ func (r *K8sGPTReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr

// If the deployment is active, we will query it directly for analysis data
if deployment.Status.ReadyReplicas > 0 {
// Get the K8sGPT client
response, err := r.K8sGPTClient.ProcessAnalysis(deployment, k8sgptConfig)
// Check if the client exists in the map
if _, ok := r.k8sGPTClients[k8sgptConfig.Name]; !ok {
// Create a new client
var address string
if os.Getenv("LOCAL_MODE") != "" {
address = "localhost:8080"
} else {
address = fmt.Sprintf("%s.%s:8080", "k8sgpt", deployment.Namespace)
}

k8sgptClient, err := kclient.NewClient(address)
if err != nil {
k8sgptReconcileErrorCount.Inc()
return r.finishReconcile(err, false)
}
r.k8sGPTClients[k8sgptConfig.Name] = k8sgptClient
}

response, err := r.k8sGPTClients[k8sgptConfig.Name].ProcessAnalysis(deployment, k8sgptConfig)
if err != nil {
k8sgptReconcileErrorCount.Inc()
return r.finishReconcile(err, false)
Expand Down Expand Up @@ -186,6 +207,7 @@ func (r *K8sGPTReconciler) SetupWithManager(mgr ctrl.Manager) error {
For(&corev1alpha1.K8sGPT{}).
Complete(r)

r.k8sGPTClients = make(map[string]*kclient.Client)
metrics.Registry.MustRegister(k8sgptReconcileErrorCount, k8sgptNumberOfResults, k8sgptNumberOfResultsByType)

return c
Expand Down
16 changes: 2 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (

corev1alpha1 "github.com/k8sgpt-ai/k8sgpt-operator/api/v1alpha1"
"github.com/k8sgpt-ai/k8sgpt-operator/controllers"
k8sgptClient "github.com/k8sgpt-ai/k8sgpt-operator/pkg/client"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
Expand Down Expand Up @@ -86,20 +85,9 @@ func main() {
os.Exit(1)
}

// New K8sGPT in cluster Client
var address string
if os.Getenv("LOCAL_MODE") != "" {
address = "localhost:8080"
} else {
address = os.Getenv("K8SGPT_API_HOST")
}

k8sgptClient, err := k8sgptClient.NewClient(address)

if err = (&controllers.K8sGPTReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
K8sGPTClient: k8sgptClient,
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "K8sGPT")
os.Exit(1)
Expand Down