Skip to content

Commit

Permalink
feat: fix grpc client creation slightly (#73)
Browse files Browse the repository at this point in the history
* feat: fix grpc client creation slightly

Signed-off-by: Alex Jones <alexsimonjones@gmail.com>

* feat: fix grpc client creation slightly

Signed-off-by: Alex Jones <alexsimonjones@gmail.com>

---------

Signed-off-by: Alex Jones <alexsimonjones@gmail.com>
  • Loading branch information
AlexsJones authored May 9, 2023
1 parent feb6745 commit 98b39a9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
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

0 comments on commit 98b39a9

Please sign in to comment.